1// Protocol Buffers for Go with Gadgets
2//
3// Copyright (c) 2013, The GoGo Authors. All rights reserved.
4// http://github.com/gogo/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//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29package test
30
31import (
32	"math"
33	math_rand "math/rand"
34	"testing"
35	"time"
36
37	"github.com/gogo/protobuf/proto"
38)
39
40//func SetRawExtension(base extendableProto, id int32, b []byte) {
41//func HasExtension(pb extendableProto, extension *ExtensionDesc) bool {
42//func ClearExtension(pb extendableProto, extension *ExtensionDesc) {
43//func GetExtension(pb extendableProto, extension *ExtensionDesc) (interface{}, error) {
44//func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
45//func SetExtension(pb extendableProto, extension *ExtensionDesc, value interface{}) error {
46
47type extendable interface {
48	proto.Message
49	ExtensionRangeArray() []proto.ExtensionRange
50}
51
52func check(t *testing.T, m extendable, fieldA float64, ext *proto.ExtensionDesc) {
53	if !proto.HasExtension(m, ext) {
54		t.Fatalf("expected extension to be set")
55	}
56	fieldA2Interface, err := proto.GetExtension(m, ext)
57	if err != nil {
58		t.Fatal(err)
59	}
60	fieldA2 := fieldA2Interface.(*float64)
61	if fieldA != *fieldA2 {
62		t.Fatalf("Expected %f got %f", fieldA, *fieldA2)
63	}
64	fieldA3Interface, err := proto.GetUnsafeExtension(m, ext.Field)
65	if err != nil {
66		t.Fatal(err)
67	}
68	fieldA3 := fieldA3Interface.(*float64)
69	if fieldA != *fieldA3 {
70		t.Fatalf("Expected %f got %f", fieldA, *fieldA3)
71	}
72	proto.ClearExtension(m, ext)
73	if proto.HasExtension(m, ext) {
74		t.Fatalf("expected extension to be cleared")
75	}
76}
77
78var fieldA float64
79var fieldABytes []byte
80var extr = math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
81
82func init() {
83	fieldA = float64(1.1)
84	fieldABits := math.Float64bits(fieldA)
85	x := uint64(uint32(100)<<3 | uint32(proto.WireFixed64))
86	fieldABytes = encodeVarintPopulateThetest(nil, x)
87	fieldABytes = append(fieldABytes, uint8(fieldABits))
88	fieldABytes = append(fieldABytes, uint8(fieldABits>>8))
89	fieldABytes = append(fieldABytes, uint8(fieldABits>>16))
90	fieldABytes = append(fieldABytes, uint8(fieldABits>>24))
91	fieldABytes = append(fieldABytes, uint8(fieldABits>>32))
92	fieldABytes = append(fieldABytes, uint8(fieldABits>>40))
93	fieldABytes = append(fieldABytes, uint8(fieldABits>>48))
94	fieldABytes = append(fieldABytes, uint8(fieldABits>>56))
95}
96
97func TestExtensionsMyExtendable(t *testing.T) {
98	m := NewPopulatedMyExtendable(extr, false)
99	err := proto.SetExtension(m, E_FieldA, &fieldA)
100	if err != nil {
101		t.Fatal(err)
102	}
103	check(t, m, fieldA, E_FieldA)
104	proto.SetRawExtension(m, 100, fieldABytes)
105	check(t, m, fieldA, E_FieldA)
106}
107
108func TestExtensionsNoExtensionsMapSetExtension(t *testing.T) {
109	mm := NewPopulatedMyExtendable(extr, false)
110	for {
111		_, err := proto.GetExtension(mm, E_FieldA)
112		if err == proto.ErrMissingExtension {
113			// make sure the field that we are going to try to set is not set,
114			// since the random generator is not smart enough to generate the correct wire type for a defined extended field.
115			break
116		}
117		mm = NewPopulatedMyExtendable(extr, false)
118	}
119	data, err := proto.Marshal(mm)
120	if err != nil {
121		t.Fatal(err)
122	}
123	m := &NoExtensionsMap{}
124	if err := proto.Unmarshal(data, m); err != nil {
125		t.Fatal(err)
126	}
127	if err := proto.SetExtension(m, E_FieldA1, &fieldA); err != nil {
128		t.Fatal(err)
129	}
130	check(t, m, fieldA, E_FieldA1)
131}
132
133func TestExtensionsNoExtensionsMapSetRawExtension(t *testing.T) {
134	m := NewPopulatedNoExtensionsMap(extr, false)
135	proto.SetRawExtension(m, 100, fieldABytes)
136	check(t, m, fieldA, E_FieldA1)
137}
138
139func TestUnsafeExtension(t *testing.T) {
140	m := NewPopulatedMyExtendable(extr, false)
141	err := proto.SetUnsafeExtension(m, E_FieldA.Field, &fieldA)
142	if err != nil {
143		t.Fatal(err)
144	}
145	check(t, m, fieldA, E_FieldA)
146}
147
148//See another version of this test in proto/extensions_test.go
149func TestGetExtensionStability(t *testing.T) {
150	check := func(m *NoExtensionsMap) bool {
151		ext1, err := proto.GetExtension(m, E_FieldB1)
152		if err != nil {
153			t.Fatalf("GetExtension() 1 failed: %v", err)
154		}
155		ext2, err := proto.GetExtension(m, E_FieldB1)
156		if err != nil {
157			t.Fatalf("GetExtension() 2 failed: %v", err)
158		}
159		return ext1.(*NinOptNative).Equal(ext2)
160	}
161	msg := &NoExtensionsMap{Field1: proto.Int64(2)}
162	ext0 := &NinOptNative{Field1: proto.Float64(1)}
163	if err := proto.SetExtension(msg, E_FieldB1, ext0); err != nil {
164		t.Fatalf("Could not set ext1: %s", ext0)
165	}
166	if !check(msg) {
167		t.Errorf("GetExtension() not stable before marshaling")
168	}
169	bb, err := proto.Marshal(msg)
170	if err != nil {
171		t.Fatalf("Marshal() failed: %s", err)
172	}
173	msg1 := &NoExtensionsMap{}
174	err = proto.Unmarshal(bb, msg1)
175	if err != nil {
176		t.Fatalf("Unmarshal() failed: %s", err)
177	}
178	if !check(msg1) {
179		t.Errorf("GetExtension() not stable after unmarshaling")
180	}
181}
182