...

Source file src/google.golang.org/protobuf/types/dynamicpb/types_test.go

Documentation: google.golang.org/protobuf/types/dynamicpb

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dynamicpb_test
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"google.golang.org/protobuf/reflect/protoreflect"
    12  	"google.golang.org/protobuf/reflect/protoregistry"
    13  	"google.golang.org/protobuf/types/descriptorpb"
    14  	"google.golang.org/protobuf/types/dynamicpb"
    15  
    16  	registrypb "google.golang.org/protobuf/internal/testprotos/registry"
    17  )
    18  
    19  var _ protoregistry.ExtensionTypeResolver = &dynamicpb.Types{}
    20  var _ protoregistry.MessageTypeResolver = &dynamicpb.Types{}
    21  
    22  func newTestTypes() *dynamicpb.Types {
    23  	files := &protoregistry.Files{}
    24  	files.RegisterFile(registrypb.File_internal_testprotos_registry_test_proto)
    25  	return dynamicpb.NewTypes(files)
    26  }
    27  
    28  func TestDynamicTypesTypeMismatch(t *testing.T) {
    29  	types := newTestTypes()
    30  	const messageName = "testprotos.Message1"
    31  	const enumName = "testprotos.Enum1"
    32  
    33  	_, err := types.FindEnumByName(messageName)
    34  	want := "found wrong type: got message, want enum"
    35  	if err == nil || !strings.Contains(err.Error(), want) {
    36  		t.Errorf("types.FindEnumByName(%q) = _, %q, want %q", messageName, err, want)
    37  	}
    38  
    39  	_, err = types.FindMessageByName(enumName)
    40  	want = "found wrong type: got enum, want message"
    41  	if err == nil || !strings.Contains(err.Error(), want) {
    42  		t.Errorf("types.FindMessageByName(%q) = _, %q, want %q", messageName, err, want)
    43  	}
    44  
    45  	_, err = types.FindExtensionByName(enumName)
    46  	want = "found wrong type: got enum, want extension"
    47  	if err == nil || !strings.Contains(err.Error(), want) {
    48  		t.Errorf("types.FindExtensionByName(%q) = _, %q, want %q", messageName, err, want)
    49  	}
    50  }
    51  
    52  func TestDynamicTypesEnumNotFound(t *testing.T) {
    53  	types := newTestTypes()
    54  	for _, name := range []protoreflect.FullName{
    55  		"Enum1",
    56  		"testprotos.DoesNotExist",
    57  	} {
    58  		_, err := types.FindEnumByName(name)
    59  		if err != protoregistry.NotFound {
    60  			t.Errorf("types.FindEnumByName(%q) = _, %v; want protoregistry.NotFound", name, err)
    61  		}
    62  	}
    63  }
    64  
    65  func TestDynamicTypesFindEnumByName(t *testing.T) {
    66  	types := newTestTypes()
    67  	name := protoreflect.FullName("testprotos.Enum1")
    68  	et, err := types.FindEnumByName(name)
    69  	if err != nil {
    70  		t.Fatalf("types.FindEnumByName(%q) = %v", name, err)
    71  	}
    72  	if got, want := et.Descriptor().FullName(), name; got != want {
    73  		t.Fatalf("types.FindEnumByName(%q).Descriptor().FullName() = %q, want %q", name, got, want)
    74  	}
    75  }
    76  
    77  func TestDynamicTypesMessageNotFound(t *testing.T) {
    78  	types := newTestTypes()
    79  	for _, name := range []protoreflect.FullName{
    80  		"Message1",
    81  		"testprotos.DoesNotExist",
    82  	} {
    83  		_, err := types.FindMessageByName(name)
    84  		if err != protoregistry.NotFound {
    85  			t.Errorf("types.FindMessageByName(%q) = _, %v; want protoregistry.NotFound", name, err)
    86  		}
    87  	}
    88  }
    89  
    90  func TestDynamicTypesFindMessageByName(t *testing.T) {
    91  	types := newTestTypes()
    92  	name := protoreflect.FullName("testprotos.Message1")
    93  	mt, err := types.FindMessageByName(name)
    94  	if err != nil {
    95  		t.Fatalf("types.FindMessageByName(%q) = %v", name, err)
    96  	}
    97  	if got, want := mt.Descriptor().FullName(), name; got != want {
    98  		t.Fatalf("types.FindMessageByName(%q).Descriptor().FullName() = %q, want %q", name, got, want)
    99  	}
   100  }
   101  
   102  func TestDynamicTypesExtensionNotFound(t *testing.T) {
   103  	types := newTestTypes()
   104  	for _, name := range []protoreflect.FullName{
   105  		"string_field",
   106  		"testprotos.DoesNotExist",
   107  	} {
   108  		_, err := types.FindExtensionByName(name)
   109  		if err != protoregistry.NotFound {
   110  			t.Errorf("types.FindExtensionByName(%q) = _, %v; want protoregistry.NotFound", name, err)
   111  		}
   112  	}
   113  	messageName := protoreflect.FullName("testprotos.Message1")
   114  	if _, err := types.FindExtensionByNumber(messageName, 100); err != protoregistry.NotFound {
   115  		t.Errorf("types.FindExtensionByNumber(%q, 100) = _, %v; want protoregistry.NotFound", messageName, 100)
   116  	}
   117  }
   118  
   119  func TestDynamicTypesFindExtensionByNameOrNumber(t *testing.T) {
   120  	types := newTestTypes()
   121  	messageName := protoreflect.FullName("testprotos.Message1")
   122  	mt, err := types.FindMessageByName(messageName)
   123  	if err != nil {
   124  		t.Fatalf("types.FindMessageByName(%q) = %v", messageName, err)
   125  	}
   126  	for _, extensionName := range []protoreflect.FullName{
   127  		"testprotos.string_field",
   128  		"testprotos.Message4.message_field",
   129  	} {
   130  		xt, err := types.FindExtensionByName(extensionName)
   131  		if err != nil {
   132  			t.Fatalf("types.FindExtensionByName(%q) = %v", extensionName, err)
   133  		}
   134  		if got, want := xt.TypeDescriptor().FullName(), extensionName; got != want {
   135  			t.Fatalf("types.FindExtensionByName(%q).TypeDescriptor().FullName() = %q, want %q", extensionName, got, want)
   136  		}
   137  		if got, want := xt.TypeDescriptor().ContainingMessage(), mt.Descriptor(); got != want {
   138  			t.Fatalf("xt.TypeDescriptor().ContainingMessage() = %q, want %q", got.FullName(), want.FullName())
   139  		}
   140  		number := xt.TypeDescriptor().Number()
   141  		xt2, err := types.FindExtensionByNumber(messageName, number)
   142  		if err != nil {
   143  			t.Fatalf("types.FindExtensionByNumber(%q, %v) = %v", messageName, number, err)
   144  		}
   145  		if xt != xt2 {
   146  			t.Fatalf("FindExtensionByName returned a differet extension than FindExtensionByNumber")
   147  		}
   148  	}
   149  }
   150  
   151  func TestDynamicTypesFilesChangeAfterCreation(t *testing.T) {
   152  	files := &protoregistry.Files{}
   153  	files.RegisterFile(descriptorpb.File_google_protobuf_descriptor_proto)
   154  	types := dynamicpb.NewTypes(files)
   155  
   156  	// Not found: Files registry does not contain this file.
   157  	const message = "testprotos.Message1"
   158  	const number = 11
   159  	if _, err := types.FindMessageByName(message); err != protoregistry.NotFound {
   160  		t.Errorf("types.FindMessageByName(%q) = %v, want protoregistry.NotFound", message, err)
   161  	}
   162  	if _, err := types.FindExtensionByNumber(message, number); err != protoregistry.NotFound {
   163  		t.Errorf("types.FindExtensionByNumber(%q, %v) = %v, want protoregistry.NotFound", message, number, err)
   164  	}
   165  
   166  	// Found: Add the file to the registry and recheck.
   167  	files.RegisterFile(registrypb.File_internal_testprotos_registry_test_proto)
   168  	if _, err := types.FindMessageByName(message); err != nil {
   169  		t.Errorf("types.FindMessageByName(%q) = %v, want nil", message, err)
   170  	}
   171  	if _, err := types.FindExtensionByNumber(message, number); err != nil {
   172  		t.Errorf("types.FindExtensionByNumber(%q, %v) = %v, want nil", message, number, err)
   173  	}
   174  }
   175  

View as plain text