fileinit_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package fileinit_test
  2. import (
  3. "bytes"
  4. "compress/gzip"
  5. "io/ioutil"
  6. "testing"
  7. proto "github.com/golang/protobuf/proto"
  8. testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
  9. "github.com/golang/protobuf/v2/reflect/protodesc"
  10. "github.com/golang/protobuf/v2/reflect/protoreflect"
  11. descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
  12. )
  13. func TestInit(t *testing.T) {
  14. // Compare the FileDescriptorProto for the same test file from two different sources:
  15. //
  16. // 1. The result of passing the fileinit-produced FileDescriptor through protodesc.
  17. // 2. The protoc-generated wire-encoded message.
  18. //
  19. // This serves as a test of both fileinit and protodesc.
  20. got := protodesc.ToFileDescriptorProto(testpb.File_test_test_proto)
  21. want := &descriptorpb.FileDescriptorProto{}
  22. zb, _ := (&testpb.TestAllTypes{}).Descriptor()
  23. r, _ := gzip.NewReader(bytes.NewBuffer(zb))
  24. b, _ := ioutil.ReadAll(r)
  25. if err := proto.Unmarshal(b, want); err != nil {
  26. t.Fatal(err)
  27. }
  28. if !proto.Equal(got, want) {
  29. t.Errorf("protodesc.ToFileDescriptorProto(testpb.Test_protoFile) is not equal to the protoc-generated FileDescriptorProto for internal/testprotos/test/test.proto")
  30. }
  31. // Verify that the test proto file provides exhaustive coverage of all descriptor fields.
  32. seen := make(map[protoreflect.FullName]bool)
  33. visitFields(want.ProtoReflect(), func(field protoreflect.FieldDescriptor) {
  34. seen[field.FullName()] = true
  35. })
  36. ignore := map[protoreflect.FullName]bool{
  37. // The protoreflect descriptors don't include source info.
  38. "google.protobuf.FileDescriptorProto.source_code_info": true,
  39. "google.protobuf.FileDescriptorProto.syntax": true,
  40. // TODO: Test oneof and extension options. Testing these requires extending the
  41. // options messages (because they contain no user-settable fields), but importing
  42. // decriptor.proto from test.proto currently causes an import cycle. Add test
  43. // cases when that import cycle has been fixed.
  44. "google.protobuf.OneofDescriptorProto.options": true,
  45. }
  46. for _, messageName := range []protoreflect.Name{
  47. "FileDescriptorProto",
  48. "DescriptorProto",
  49. "FieldDescriptorProto",
  50. "OneofDescriptorProto",
  51. "EnumDescriptorProto",
  52. "EnumValueDescriptorProto",
  53. "ServiceDescriptorProto",
  54. "MethodDescriptorProto",
  55. } {
  56. message := descriptorpb.File_google_protobuf_descriptor_proto.Messages().ByName(messageName)
  57. for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
  58. if name := fields.Get(i).FullName(); !seen[name] && !ignore[name] {
  59. t.Errorf("No test for descriptor field: %v", name)
  60. }
  61. }
  62. }
  63. // Verify that message descriptors for map entries have no Go type info.
  64. mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry")
  65. d := testpb.File_test_test_proto.Messages().ByName("TestAllTypes").Fields().ByName("map_int32_int32").Message()
  66. if gotName, wantName := d.FullName(), mapEntryName; gotName != wantName {
  67. t.Fatalf("looked up wrong descriptor: got %v, want %v", gotName, wantName)
  68. }
  69. if _, ok := d.(protoreflect.MessageType); ok {
  70. t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName)
  71. }
  72. }
  73. // visitFields calls f for every field set in m and its children.
  74. func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) {
  75. typ := m.Type()
  76. k := m.KnownFields()
  77. k.Range(func(num protoreflect.FieldNumber, value protoreflect.Value) bool {
  78. field := typ.Fields().ByNumber(num)
  79. f(field)
  80. switch field.Kind() {
  81. case protoreflect.MessageKind, protoreflect.GroupKind:
  82. if field.Cardinality() == protoreflect.Repeated {
  83. for i, list := 0, value.List(); i < list.Len(); i++ {
  84. visitFields(list.Get(i).Message(), f)
  85. }
  86. } else {
  87. visitFields(value.Message(), f)
  88. }
  89. }
  90. return true
  91. })
  92. }
  93. func TestWeakInit(t *testing.T) {
  94. file := testpb.File_test_test_proto
  95. fd := file.Messages().ByName("TestWeak").Fields().ByName("weak_message")
  96. if want, got := fd.IsWeak(), true; got != want {
  97. t.Errorf("field %v: IsWeak() = %v, want %v", fd.FullName(), want, got)
  98. }
  99. if want, got := fd.Message().IsPlaceholder(), false; got != want {
  100. t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd.FullName(), want, got)
  101. }
  102. if fd.Message().Fields().Len() == 0 {
  103. t.Errorf("field %v: Message().Fields().Len() == 0, want >0", fd.FullName())
  104. }
  105. }