1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
37 "github.com/golang/protobuf/proto"
38 pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
39 "github.com/golang/protobuf/ptypes/any"
42 func TestMarshalUnmarshal(t *testing.T) {
43 orig := &any.Any{Value: []byte("test")}
45 packed, err := MarshalAny(orig)
47 t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err)
50 unpacked := &any.Any{}
51 err = UnmarshalAny(packed, unpacked)
52 if err != nil || !proto.Equal(unpacked, orig) {
53 t.Errorf("got: %v, %+v; want nil, %+v", err, unpacked, orig)
57 func TestIs(t *testing.T) {
58 a, err := MarshalAny(&pb.FileDescriptorProto{})
62 if Is(a, &pb.DescriptorProto{}) {
63 t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
65 if !Is(a, &pb.FileDescriptorProto{}) {
66 t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
70 func TestIsDifferentUrlPrefixes(t *testing.T) {
71 m := &pb.FileDescriptorProto{}
72 a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
74 t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
78 func TestUnmarshalDynamic(t *testing.T) {
79 want := &pb.FileDescriptorProto{Name: proto.String("foo")}
80 a, err := MarshalAny(want)
85 if err := UnmarshalAny(a, &got); err != nil {
88 if !proto.Equal(got.Message, want) {
89 t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
93 func TestEmpty(t *testing.T) {
94 want := &pb.FileDescriptorProto{}
95 a, err := MarshalAny(want)
103 if !proto.Equal(got, want) {
104 t.Errorf("unequal empty message, got %q, want %q", got, want)
107 // that's a valid type_url for a message which shouldn't be linked into this
108 // test binary. We want an error.
109 a.TypeUrl = "type.googleapis.com/google.protobuf.FieldMask"
110 if _, err := Empty(a); err == nil {
111 t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)