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