blob: 3170ee262b0df7a6604e40d6442bcf964380b21a [file] [log] [blame]
Joe Tsaid8881392019-06-06 13:01:53 -07001// 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
5package filedesc_test
Damien Neile475eaa2019-01-26 14:24:59 -08006
7import (
8 "bytes"
9 "compress/gzip"
10 "io/ioutil"
11 "testing"
12
Joe Tsai8d30bbe2019-05-16 15:53:25 -070013 "google.golang.org/protobuf/proto"
Damien Neile89e6242019-05-13 23:55:40 -070014 "google.golang.org/protobuf/reflect/protodesc"
15 "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaia95b29f2019-05-16 12:47:20 -070016
17 testpb "google.golang.org/protobuf/internal/testprotos/test"
Joe Tsai3d8e3692019-04-08 13:52:14 -070018 _ "google.golang.org/protobuf/internal/testprotos/test/weak1"
Joe Tsaia95b29f2019-05-16 12:47:20 -070019 "google.golang.org/protobuf/types/descriptorpb"
Damien Neile475eaa2019-01-26 14:24:59 -080020)
21
22func TestInit(t *testing.T) {
23 // Compare the FileDescriptorProto for the same test file from two different sources:
24 //
Joe Tsaid8881392019-06-06 13:01:53 -070025 // 1. The result of passing the filedesc-produced FileDescriptor through protodesc.
Damien Neile475eaa2019-01-26 14:24:59 -080026 // 2. The protoc-generated wire-encoded message.
27 //
Joe Tsaid8881392019-06-06 13:01:53 -070028 // This serves as a test of both filedesc and protodesc.
Joe Tsai19058432019-02-27 21:46:29 -080029 got := protodesc.ToFileDescriptorProto(testpb.File_test_test_proto)
Damien Neile475eaa2019-01-26 14:24:59 -080030
31 want := &descriptorpb.FileDescriptorProto{}
32 zb, _ := (&testpb.TestAllTypes{}).Descriptor()
33 r, _ := gzip.NewReader(bytes.NewBuffer(zb))
34 b, _ := ioutil.ReadAll(r)
35 if err := proto.Unmarshal(b, want); err != nil {
36 t.Fatal(err)
37 }
38
39 if !proto.Equal(got, want) {
40 t.Errorf("protodesc.ToFileDescriptorProto(testpb.Test_protoFile) is not equal to the protoc-generated FileDescriptorProto for internal/testprotos/test/test.proto")
41 }
42
43 // Verify that the test proto file provides exhaustive coverage of all descriptor fields.
44 seen := make(map[protoreflect.FullName]bool)
45 visitFields(want.ProtoReflect(), func(field protoreflect.FieldDescriptor) {
46 seen[field.FullName()] = true
47 })
48 ignore := map[protoreflect.FullName]bool{
49 // The protoreflect descriptors don't include source info.
50 "google.protobuf.FileDescriptorProto.source_code_info": true,
51 "google.protobuf.FileDescriptorProto.syntax": true,
52
53 // TODO: Test oneof and extension options. Testing these requires extending the
54 // options messages (because they contain no user-settable fields), but importing
55 // decriptor.proto from test.proto currently causes an import cycle. Add test
56 // cases when that import cycle has been fixed.
57 "google.protobuf.OneofDescriptorProto.options": true,
58 }
59 for _, messageName := range []protoreflect.Name{
60 "FileDescriptorProto",
61 "DescriptorProto",
62 "FieldDescriptorProto",
63 "OneofDescriptorProto",
64 "EnumDescriptorProto",
65 "EnumValueDescriptorProto",
66 "ServiceDescriptorProto",
67 "MethodDescriptorProto",
68 } {
Joe Tsai40692112019-02-27 20:25:51 -080069 message := descriptorpb.File_google_protobuf_descriptor_proto.Messages().ByName(messageName)
Damien Neile475eaa2019-01-26 14:24:59 -080070 for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
71 if name := fields.Get(i).FullName(); !seen[name] && !ignore[name] {
72 t.Errorf("No test for descriptor field: %v", name)
73 }
74 }
75 }
76
Joe Tsai4532dd72019-03-19 17:04:06 -070077 // Verify that message descriptors for map entries have no Go type info.
78 mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry")
Joe Tsaid24bc722019-04-15 23:39:09 -070079 d := testpb.File_test_test_proto.Messages().ByName("TestAllTypes").Fields().ByName("map_int32_int32").Message()
Damien Neil2300c182019-04-15 13:05:13 -070080 if gotName, wantName := d.FullName(), mapEntryName; gotName != wantName {
81 t.Fatalf("looked up wrong descriptor: got %v, want %v", gotName, wantName)
Joe Tsai4532dd72019-03-19 17:04:06 -070082 }
83 if _, ok := d.(protoreflect.MessageType); ok {
84 t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName)
85 }
Damien Neile475eaa2019-01-26 14:24:59 -080086}
87
88// visitFields calls f for every field set in m and its children.
89func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) {
Joe Tsai378c1322019-04-25 23:48:08 -070090 m.Range(func(fd protoreflect.FieldDescriptor, value protoreflect.Value) bool {
91 f(fd)
92 switch fd.Kind() {
Damien Neile475eaa2019-01-26 14:24:59 -080093 case protoreflect.MessageKind, protoreflect.GroupKind:
Joe Tsai378c1322019-04-25 23:48:08 -070094 if fd.IsList() {
Damien Neile475eaa2019-01-26 14:24:59 -080095 for i, list := 0, value.List(); i < list.Len(); i++ {
96 visitFields(list.Get(i).Message(), f)
97 }
98 } else {
99 visitFields(value.Message(), f)
100 }
101 }
102 return true
103 })
104}
Damien Neil82a03062019-05-08 07:52:49 -0700105
106func TestWeakInit(t *testing.T) {
107 file := testpb.File_test_test_proto
Joe Tsai3d8e3692019-04-08 13:52:14 -0700108
109 // We do not expect to get a placeholder since weak1 is imported.
110 fd1 := file.Messages().ByName("TestWeak").Fields().ByName("weak_message1")
111 if got, want := fd1.IsWeak(), true; got != want {
112 t.Errorf("field %v: IsWeak() = %v, want %v", fd1.FullName(), got, want)
Damien Neil82a03062019-05-08 07:52:49 -0700113 }
Joe Tsai3d8e3692019-04-08 13:52:14 -0700114 if got, want := fd1.Message().IsPlaceholder(), false; got != want {
115 t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd1.FullName(), got, want)
Damien Neil82a03062019-05-08 07:52:49 -0700116 }
Joe Tsai3d8e3692019-04-08 13:52:14 -0700117 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.
122 fd2 := file.Messages().ByName("TestWeak").Fields().ByName("weak_message2")
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 Neil82a03062019-05-08 07:52:49 -0700131 }
132}