github.com/gogo/protobuf@v1.3.2/types/any_test.go (about) 1 // Go support for Protocol Buffers - Google's data interchange format 2 // 3 // Copyright 2016 The Go Authors. All rights reserved. 4 // https://github.com/golang/protobuf 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 package types 33 34 import ( 35 "testing" 36 37 "github.com/gogo/protobuf/proto" 38 pb "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" 39 ) 40 41 func TestMarshalUnmarshal(t *testing.T) { 42 orig := &Any{Value: []byte("test")} 43 44 packed, err := MarshalAny(orig) 45 if err != nil { 46 t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err) 47 } 48 49 unpacked := &Any{} 50 err = UnmarshalAny(packed, unpacked) 51 if err != nil || !proto.Equal(unpacked, orig) { 52 t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig) 53 } 54 } 55 56 func TestIs(t *testing.T) { 57 a, err := MarshalAny(&pb.FileDescriptorProto{}) 58 if err != nil { 59 t.Fatal(err) 60 } 61 if Is(a, &pb.DescriptorProto{}) { 62 // No spurious match for message names of different length. 63 t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is") 64 } 65 if Is(a, &pb.EnumDescriptorProto{}) { 66 // No spurious match for message names of equal length. 67 t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is") 68 } 69 if !Is(a, &pb.FileDescriptorProto{}) { 70 t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not") 71 } 72 } 73 74 func TestIsDifferentUrlPrefixes(t *testing.T) { 75 m := &pb.FileDescriptorProto{} 76 a := &Any{TypeUrl: "foo/bar/" + proto.MessageName(m)} 77 if !Is(a, m) { 78 t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m)) 79 } 80 } 81 82 func TestIsCornerCases(t *testing.T) { 83 m := &pb.FileDescriptorProto{} 84 if Is(nil, m) { 85 t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m)) 86 } 87 noPrefix := &Any{TypeUrl: proto.MessageName(m)} 88 if Is(noPrefix, m) { 89 t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m)) 90 } 91 shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(m)} 92 if !Is(shortPrefix, m) { 93 t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m)) 94 } 95 } 96 97 func TestUnmarshalDynamic(t *testing.T) { 98 want := &pb.FileDescriptorProto{Name: proto.String("foo")} 99 a, err := MarshalAny(want) 100 if err != nil { 101 t.Fatal(err) 102 } 103 var got DynamicAny 104 if err := UnmarshalAny(a, &got); err != nil { 105 t.Fatal(err) 106 } 107 if !proto.Equal(got.Message, want) { 108 t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want) 109 } 110 } 111 112 func TestEmpty(t *testing.T) { 113 want := &pb.FileDescriptorProto{} 114 a, err := MarshalAny(want) 115 if err != nil { 116 t.Fatal(err) 117 } 118 got, err := EmptyAny(a) 119 if err != nil { 120 t.Fatal(err) 121 } 122 if !proto.Equal(got, want) { 123 t.Errorf("unequal empty message, got %q, want %q", got, want) 124 } 125 126 // that's a valid type_url for a message which shouldn't be linked into this 127 // test binary. We want an error. 128 a.TypeUrl = "type.googleapis.com/google.protobuf.TestAny" 129 if _, err := EmptyAny(a); err == nil { 130 t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl) 131 } 132 } 133 134 func TestEmptyCornerCases(t *testing.T) { 135 _, err := EmptyAny(nil) 136 if err == nil { 137 t.Error("expected Empty for nil to fail") 138 } 139 want := &pb.FileDescriptorProto{} 140 noPrefix := &Any{TypeUrl: proto.MessageName(want)} 141 _, err = EmptyAny(noPrefix) 142 if err == nil { 143 t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl) 144 } 145 shortPrefix := &Any{TypeUrl: "/" + proto.MessageName(want)} 146 got, err := EmptyAny(shortPrefix) 147 if err != nil { 148 t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err) 149 } 150 if !proto.Equal(got, want) { 151 t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want) 152 } 153 }