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
32package ptypes
33
34import (
35	"testing"
36
37	"github.com/golang/protobuf/proto"
38	pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
39	"github.com/golang/protobuf/ptypes/any"
40)
41
42func TestMarshalUnmarshal(t *testing.T) {
43	orig := &any.Any{Value: []byte("test")}
44
45	packed, err := MarshalAny(orig)
46	if err != nil {
47		t.Errorf("MarshalAny(%+v): got: _, %v exp: _, nil", orig, err)
48	}
49
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)
54	}
55}
56
57func TestIs(t *testing.T) {
58	a, err := MarshalAny(&pb.FileDescriptorProto{})
59	if err != nil {
60		t.Fatal(err)
61	}
62	if Is(a, &pb.DescriptorProto{}) {
63		// No spurious match for message names of different length.
64		t.Error("FileDescriptorProto is not a DescriptorProto, but Is says it is")
65	}
66	if Is(a, &pb.EnumDescriptorProto{}) {
67		// No spurious match for message names of equal length.
68		t.Error("FileDescriptorProto is not an EnumDescriptorProto, but Is says it is")
69	}
70	if !Is(a, &pb.FileDescriptorProto{}) {
71		t.Error("FileDescriptorProto is indeed a FileDescriptorProto, but Is says it is not")
72	}
73}
74
75func TestIsDifferentUrlPrefixes(t *testing.T) {
76	m := &pb.FileDescriptorProto{}
77	a := &any.Any{TypeUrl: "foo/bar/" + proto.MessageName(m)}
78	if !Is(a, m) {
79		t.Errorf("message with type url %q didn't satisfy Is for type %q", a.TypeUrl, proto.MessageName(m))
80	}
81}
82
83func TestIsCornerCases(t *testing.T) {
84	m := &pb.FileDescriptorProto{}
85	if Is(nil, m) {
86		t.Errorf("message with nil type url incorrectly claimed to be %q", proto.MessageName(m))
87	}
88	noPrefix := &any.Any{TypeUrl: proto.MessageName(m)}
89	if Is(noPrefix, m) {
90		t.Errorf("message with type url %q incorrectly claimed to be %q", noPrefix.TypeUrl, proto.MessageName(m))
91	}
92	shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(m)}
93	if !Is(shortPrefix, m) {
94		t.Errorf("message with type url %q didn't satisfy Is for type %q", shortPrefix.TypeUrl, proto.MessageName(m))
95	}
96}
97
98func TestUnmarshalDynamic(t *testing.T) {
99	want := &pb.FileDescriptorProto{Name: proto.String("foo")}
100	a, err := MarshalAny(want)
101	if err != nil {
102		t.Fatal(err)
103	}
104	var got DynamicAny
105	if err := UnmarshalAny(a, &got); err != nil {
106		t.Fatal(err)
107	}
108	if !proto.Equal(got.Message, want) {
109		t.Errorf("invalid result from UnmarshalAny, got %q want %q", got.Message, want)
110	}
111}
112
113func TestEmpty(t *testing.T) {
114	want := &pb.FileDescriptorProto{}
115	a, err := MarshalAny(want)
116	if err != nil {
117		t.Fatal(err)
118	}
119	got, err := Empty(a)
120	if err != nil {
121		t.Fatal(err)
122	}
123	if !proto.Equal(got, want) {
124		t.Errorf("unequal empty message, got %q, want %q", got, want)
125	}
126
127	// that's a valid type_url for a message which shouldn't be linked into this
128	// test binary. We want an error.
129	a.TypeUrl = "type.googleapis.com/google.protobuf.FieldMask"
130	if _, err := Empty(a); err == nil {
131		t.Errorf("got no error for an attempt to create a message of type %q, which shouldn't be linked in", a.TypeUrl)
132	}
133}
134
135func TestEmptyCornerCases(t *testing.T) {
136	_, err := Empty(nil)
137	if err == nil {
138		t.Error("expected Empty for nil to fail")
139	}
140	want := &pb.FileDescriptorProto{}
141	noPrefix := &any.Any{TypeUrl: proto.MessageName(want)}
142	_, err = Empty(noPrefix)
143	if err == nil {
144		t.Errorf("expected Empty for any type %q to fail", noPrefix.TypeUrl)
145	}
146	shortPrefix := &any.Any{TypeUrl: "/" + proto.MessageName(want)}
147	got, err := Empty(shortPrefix)
148	if err != nil {
149		t.Errorf("Empty for any type %q failed: %s", shortPrefix.TypeUrl, err)
150	}
151	if !proto.Equal(got, want) {
152		t.Errorf("Empty for any type %q differs, got %q, want %q", shortPrefix.TypeUrl, got, want)
153	}
154}
155