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