...

Source file src/google.golang.org/protobuf/internal/filedesc/build_test.go

Documentation: google.golang.org/protobuf/internal/filedesc

     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
     6  
     7  import (
     8  	"bytes"
     9  	"compress/gzip"
    10  	"io/ioutil"
    11  	"testing"
    12  
    13  	"google.golang.org/protobuf/proto"
    14  	"google.golang.org/protobuf/reflect/protodesc"
    15  	"google.golang.org/protobuf/reflect/protoreflect"
    16  
    17  	testpb "google.golang.org/protobuf/internal/testprotos/test"
    18  	_ "google.golang.org/protobuf/internal/testprotos/test/weak1"
    19  	"google.golang.org/protobuf/types/descriptorpb"
    20  )
    21  
    22  var testFile = new(testpb.TestAllTypes).ProtoReflect().Descriptor().ParentFile()
    23  
    24  func TestInit(t *testing.T) {
    25  	// Compare the FileDescriptorProto for the same test file from two different sources:
    26  	//
    27  	// 1. The result of passing the filedesc-produced FileDescriptor through protodesc.
    28  	// 2. The protoc-generated wire-encoded message.
    29  	//
    30  	// This serves as a test of both filedesc and protodesc.
    31  	got := protodesc.ToFileDescriptorProto(testFile)
    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  	})
    50  	descFile := new(descriptorpb.DescriptorProto).ProtoReflect().Descriptor().ParentFile()
    51  	descPkg := descFile.Package()
    52  	ignore := map[protoreflect.FullName]bool{
    53  		// The protoreflect descriptors don't include source info.
    54  		descPkg.Append("FileDescriptorProto.source_code_info"): true,
    55  		descPkg.Append("FileDescriptorProto.syntax"):           true,
    56  		// Nothing is using edition yet.
    57  		descPkg.Append("FileDescriptorProto.edition"): true,
    58  
    59  		// Impossible to test proto3 optional in a proto2 file.
    60  		descPkg.Append("FieldDescriptorProto.proto3_optional"): true,
    61  
    62  		// TODO: Test oneof and extension options. Testing these requires extending the
    63  		// options messages (because they contain no user-settable fields), but importing
    64  		// descriptor.proto from test.proto currently causes an import cycle. Add test
    65  		// cases when that import cycle has been fixed.
    66  		descPkg.Append("OneofDescriptorProto.options"): true,
    67  	}
    68  	for _, messageName := range []protoreflect.Name{
    69  		"FileDescriptorProto",
    70  		"DescriptorProto",
    71  		"FieldDescriptorProto",
    72  		"OneofDescriptorProto",
    73  		"EnumDescriptorProto",
    74  		"EnumValueDescriptorProto",
    75  	} {
    76  		message := descFile.Messages().ByName(messageName)
    77  		for i, fields := 0, message.Fields(); i < fields.Len(); i++ {
    78  			if name := fields.Get(i).FullName(); !seen[name] && !ignore[name] {
    79  				t.Errorf("No test for descriptor field: %v", name)
    80  			}
    81  		}
    82  	}
    83  
    84  	// Verify that message descriptors for map entries have no Go type info.
    85  	mapEntryName := protoreflect.FullName("goproto.proto.test.TestAllTypes.MapInt32Int32Entry")
    86  	d := testFile.Messages().ByName("TestAllTypes").Fields().ByName("map_int32_int32").Message()
    87  	if gotName, wantName := d.FullName(), mapEntryName; gotName != wantName {
    88  		t.Fatalf("looked up wrong descriptor: got %v, want %v", gotName, wantName)
    89  	}
    90  	if _, ok := d.(protoreflect.MessageType); ok {
    91  		t.Errorf("message descriptor for %v must not implement protoreflect.MessageType", mapEntryName)
    92  	}
    93  }
    94  
    95  // visitFields calls f for every field set in m and its children.
    96  func visitFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor)) {
    97  	m.Range(func(fd protoreflect.FieldDescriptor, value protoreflect.Value) bool {
    98  		f(fd)
    99  		switch fd.Kind() {
   100  		case protoreflect.MessageKind, protoreflect.GroupKind:
   101  			if fd.IsList() {
   102  				for i, list := 0, value.List(); i < list.Len(); i++ {
   103  					visitFields(list.Get(i).Message(), f)
   104  				}
   105  			} else {
   106  				visitFields(value.Message(), f)
   107  			}
   108  		}
   109  		return true
   110  	})
   111  }
   112  
   113  func TestWeakInit(t *testing.T) {
   114  	// We do not expect to get a placeholder since weak1 is imported.
   115  	fd1 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message1")
   116  	if got, want := fd1.IsWeak(), true; got != want {
   117  		t.Errorf("field %v: IsWeak() = %v, want %v", fd1.FullName(), got, want)
   118  	}
   119  	if got, want := fd1.Message().IsPlaceholder(), false; got != want {
   120  		t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd1.FullName(), got, want)
   121  	}
   122  	if got, want := fd1.Message().Fields().Len(), 1; got != want {
   123  		t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd1.FullName(), got, want)
   124  	}
   125  
   126  	// We do expect to get a placeholder since weak2 is not imported.
   127  	fd2 := testFile.Messages().ByName("TestWeak").Fields().ByName("weak_message2")
   128  	if got, want := fd2.IsWeak(), true; got != want {
   129  		t.Errorf("field %v: IsWeak() = %v, want %v", fd2.FullName(), got, want)
   130  	}
   131  	if got, want := fd2.Message().IsPlaceholder(), true; got != want {
   132  		t.Errorf("field %v: Message.IsPlaceholder() = %v, want %v", fd2.FullName(), got, want)
   133  	}
   134  	if got, want := fd2.Message().Fields().Len(), 0; got != want {
   135  		t.Errorf("field %v: Message().Fields().Len() == %d, want %d", fd2.FullName(), got, want)
   136  	}
   137  }
   138  

View as plain text