fileinit_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  64. // visitFields calls f for every field set in m and its children.
  65. func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) {
  66. typ := m.Type()
  67. k := m.KnownFields()
  68. k.Range(func(num protoreflect.FieldNumber, value protoreflect.Value) bool {
  69. field := typ.Fields().ByNumber(num)
  70. f(field)
  71. switch field.Kind() {
  72. case protoreflect.MessageKind, protoreflect.GroupKind:
  73. if field.Cardinality() == protoreflect.Repeated {
  74. for i, list := 0, value.List(); i < list.Len(); i++ {
  75. visitFields(list.Get(i).Message(), f)
  76. }
  77. } else {
  78. visitFields(value.Message(), f)
  79. }
  80. }
  81. return true
  82. })
  83. }