Debian Codegraph @

go/pkg/amqp/interop_test.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 // Test that conversion of Go type to/from AMQP is compatible with other
21 // bindings.
22 //
23 package amqp
24
25 import (
26 "bytes"
27 "io"
28 "io/ioutil"
29 "os"
30 "reflect"
31 "strings"
32 "testing"
33
34 "github.com/apache/qpid-proton/go/pkg/internal/test"
35 )
36
37 var skipped = false
38
39 func getReader(t *testing.T, name string) (r io.Reader) {
40 dir := os.Getenv("PN_INTEROP_DIR")
41 if dir == "" {
42 if !skipped {
43 skipped = true // Don't keep repeating
44 t.Skip("no PN_INTEROP_DIR in environment")
45 } else {
46 t.SkipNow()
47 }
48 }
49 r, err := os.Open(dir + "/" + name + ".amqp")
50 if err != nil {
51 t.Fatalf("can't open %#v: %v", name, err)
52 }
53 return
54 }
55
56 func remaining(d *Decoder) string {
57 remainder, _ := ioutil.ReadAll(io.MultiReader(d.Buffered(), d.reader))
58 return string(remainder)
59 }
60
61 // checkDecode: want is the expected value, gotPtr is a pointer to a
62 // instance of the same type for Decode.
63 func checkDecode(d *Decoder, want interface{}, gotPtr interface{}, t *testing.T) {
64
65 if err := d.Decode(gotPtr); err != nil {
66 t.Error("Decode failed", err)
67 return
68 }
69 got := reflect.ValueOf(gotPtr).Elem().Interface()
70 if err := test.Differ(want, got); err != nil {
71 t.Error("Decode bad value:", err)
72 return
73 }
74
75 // Try round trip encoding
76 bytes, err := Marshal(want, nil)
77 if err != nil {
78 t.Error("Marshal failed", err)
79 return
80 }
81 n, err := Unmarshal(bytes, gotPtr)
82 if err != nil {
83 t.Error("Unmarshal failed", err)
84 return
85 }
86 err = test.Differ(n, len(bytes))
87 if err != nil {
88 t.Error("Bad unmarshal length", err)
89 return
90 }
91 got = reflect.ValueOf(gotPtr).Elem().Interface()
92 if err = test.Differ(want, got); err != nil {
93 t.Error("Bad unmarshal value", err)
94 return
95 }
96 }
97
98 func TestUnmarshal(t *testing.T) {
99 bytes, err := ioutil.ReadAll(getReader(t, "strings"))
100 if err != nil {
101 t.Error(err)
102 }
103 for _, want := range []string{"abc\000defg", "abcdefg", "abcdefg", "", "", ""} {
104 var got string
105 n, err := Unmarshal(bytes, &got)
106 if err != nil {
107 t.Error(err)
108 }
109 if want != got {
110 t.Errorf("%#v != %#v", want, got)
111 }
112 bytes = bytes[n:]
113 }
114 }
115
116 func TestPrimitivesExact(t *testing.T) {
117 d := NewDecoder(getReader(t, "primitives"))
118 // Decoding into exact types
119 var b bool
120 checkDecode(d, true, &b, t)
121 checkDecode(d, false, &b, t)
122 var u8 uint8
123 checkDecode(d, uint8(42), &u8, t)
124 var u16 uint16
125 checkDecode(d, uint16(42), &u16, t)
126 var i16 int16
127 checkDecode(d, int16(-42), &i16, t)
128 var u32 uint32
129 checkDecode(d, uint32(12345), &u32, t)
130 var i32 int32
131 checkDecode(d, int32(-12345), &i32, t)
132 var u64 uint64
133 checkDecode(d, uint64(12345), &u64, t)
134 var i64 int64
135 checkDecode(d, int64(-12345), &i64, t)
136 var f32 float32
137 checkDecode(d, float32(0.125), &f32, t)
138 var f64 float64
139 checkDecode(d, float64(0.125), &f64, t)
140 }
141
142 func TestPrimitivesCompatible(t *testing.T) {
143 d := NewDecoder(getReader(t, "primitives"))
144 // Decoding into compatible types
145 var b bool
146 var i int
147 var u uint
148 var f float64
149 checkDecode(d, true, &b, t)
150 checkDecode(d, false, &b, t)
151 checkDecode(d, uint(42), &u, t)
152 checkDecode(d, uint(42), &u, t)
153 checkDecode(d, -42, &i, t)
154 checkDecode(d, uint(12345), &u, t)
155 checkDecode(d, -12345, &i, t)
156 checkDecode(d, uint(12345), &u, t)
157 checkDecode(d, -12345, &i, t)
158 checkDecode(d, 0.125, &f, t)
159 checkDecode(d, 0.125, &f, t)
160 }
161
162 // checkDecodeValue: want is the expected value, decode into a reflect.Value
163 func checkDecodeInterface(d *Decoder, want interface{}, t *testing.T) {
164
165 var got, got2 interface{}
166 if err := d.Decode(&got); err != nil {
167 t.Error("Decode failed", err)
168 return
169 }
170 if err := test.Differ(want, got); err != nil {
171 t.Error(err)
172 return
173 }
174 // Try round trip encoding
175 bytes, err := Marshal(got, nil)
176 if err != nil {
177 t.Error(err)
178 return
179 }
180 n, err := Unmarshal(bytes, &got2)
181 if err != nil {
182 t.Error(err)
183 return
184 }
185 if err := test.Differ(n, len(bytes)); err != nil {
186 t.Error(err)
187 return
188 }
189 if err := test.Differ(want, got2); err != nil {
190 t.Error(err)
191 return
192 }
193 }
194
195 func TestPrimitivesInterface(t *testing.T) {
196 d := NewDecoder(getReader(t, "primitives"))
197 checkDecodeInterface(d, true, t)
198 checkDecodeInterface(d, false, t)
199 checkDecodeInterface(d, uint8(42), t)
200 checkDecodeInterface(d, uint16(42), t)
201 checkDecodeInterface(d, int16(-42), t)
202 checkDecodeInterface(d, uint32(12345), t)
203 checkDecodeInterface(d, int32(-12345), t)
204 checkDecodeInterface(d, uint64(12345), t)
205 checkDecodeInterface(d, int64(-12345), t)
206 checkDecodeInterface(d, float32(0.125), t)
207 checkDecodeInterface(d, float64(0.125), t)
208 }
209
210 func TestStrings(t *testing.T) {
211 d := NewDecoder(getReader(t, "strings"))
212 // Test decoding as plain Go strings
213 for _, want := range []string{"abc\000defg", "abcdefg", "abcdefg", "", "", ""} {
214 var got string
215 checkDecode(d, want, &got, t)
216 }
217 remains := remaining(d)
218 if remains != "" {
219 t.Errorf("leftover: %s", remains)
220 }
221
222 // Test decoding as specific string types
223 d = NewDecoder(getReader(t, "strings"))
224 var bytes []byte
225 var str, sym string
226 checkDecode(d, []byte("abc\000defg"), &bytes, t)
227 checkDecode(d, "abcdefg", &str, t)
228 checkDecode(d, "abcdefg", &sym, t)
229 checkDecode(d, make([]byte, 0), &bytes, t)
230 checkDecode(d, "", &str, t)
231 checkDecode(d, "", &sym, t)
232 remains = remaining(d)
233 if remains != "" {
234 t.Fatalf("leftover: %s", remains)
235 }
236
237 // Test some error handling
238 d = NewDecoder(getReader(t, "strings"))
239 var s string
240 err := d.Decode(s)
241 if err == nil {
242 t.Fatal("Expected error")
243 }
244 if !strings.Contains(err.Error(), "not a pointer") {
245 t.Error(err)
246 }
247 var i int
248 err = d.Decode(&i)
249 if !strings.Contains(err.Error(), "cannot unmarshal") {
250 t.Error(err)
251 }
252 _, err = Unmarshal([]byte{}, nil)
253 test.ErrorIf(t, test.Differ(err, EndOfData))
254 _, err = Unmarshal([]byte("foobar"), nil)
255 if !strings.Contains(err.Error(), "invalid-argument") {
256 t.Error(err)
257 }
258 }
259
260 func TestEncodeDecode(t *testing.T) {
261 type data struct {
262 s string
263 i int
264 u8 uint8
265 b bool
266 f float32
267 v interface{}
268 }
269
270 in := data{"foo", 42, 9, true, 1.234, "thing"}
271
272 buf := bytes.Buffer{}
273 e := NewEncoder(&buf)
274 if err := e.Encode(in.s); err != nil {
275 t.Error(err)
276 }
277 if err := e.Encode(in.i); err != nil {
278 t.Error(err)
279 }
280 if err := e.Encode(in.u8); err != nil {
281 t.Error(err)
282 }
283 if err := e.Encode(in.b); err != nil {
284 t.Error(err)
285 }
286 if err := e.Encode(in.f); err != nil {
287 t.Error(err)
288 }
289 if err := e.Encode(in.v); err != nil {
290 t.Error(err)
291 }
292
293 var out data
294 d := NewDecoder(&buf)
295 if err := d.Decode(&out.s); err != nil {
296 t.Error(err)
297 }
298 if err := d.Decode(&out.i); err != nil {
299 t.Error(err)
300 }
301 if err := d.Decode(&out.u8); err != nil {
302 t.Error(err)
303 }
304 if err := d.Decode(&out.b); err != nil {
305 t.Error(err)
306 }
307 if err := d.Decode(&out.f); err != nil {
308 t.Error(err)
309 }
310 if err := d.Decode(&out.v); err != nil {
311 t.Error(err)
312 }
313
314 if err := test.Differ(in, out); err != nil {
315 t.Error(err)
316 }
317 }
318
319 func TestMap(t *testing.T) {
320 d := NewDecoder(getReader(t, "maps"))
321
322 // Generic map
323 var m Map
324 checkDecode(d, Map{"one": int32(1), "two": int32(2), "three": int32(3)}, &m, t)
325
326 // Interface as map
327 var i interface{}
328 checkDecode(d, Map{int32(1): "one", int32(2): "two", int32(3): "three"}, &i, t)
329
330 d = NewDecoder(getReader(t, "maps"))
331 // Specific typed map
332 var m2 map[string]int
333 checkDecode(d, map[string]int{"one": 1, "two": 2, "three": 3}, &m2, t)
334
335 // Nested map
336 m = Map{int64(1): "one", "two": int32(2), true: Map{uint8(1): true, uint8(2): false}}
337 bytes, err := Marshal(m, nil)
338 if err != nil {
339 t.Fatal(err)
340 }
341 _, err = Unmarshal(bytes, &i)
342 if err != nil {
343 t.Fatal(err)
344 }
345 if err = test.Differ(m, i); err != nil {
346 t.Fatal(err)
347 }
348 }
349
350 func TestList(t *testing.T) {
351 d := NewDecoder(getReader(t, "lists"))
352 var l List
353 checkDecode(d, List{int32(32), "foo", true}, &l, t)
354 checkDecode(d, List{}, &l, t)
355 }
356
357 // TODO aconway 2015-09-08: the message.amqp file seems to be incorrectly coded as
358 // as an AMQP string *inside* an AMQP binary?? Skip the test for now.
359 func TODO_TestMessage(t *testing.T) {
360 bytes, err := ioutil.ReadAll(getReader(t, "message"))
361 if err != nil {
362 t.Fatal(err)
363 }
364
365 m, err := DecodeMessage(bytes)
366 if err != nil {
367 t.Fatal(err)
368 } else {
369 if err = test.Differ(m.Body(), "hello"); err != nil {
370 t.Error(err)
371 }
372 }
373
374 m2 := NewMessageWith("hello")
375 bytes2, err := m2.Encode(nil)
376 if err != nil {
377 t.Error(err)
378 } else {
379 if err = test.Differ(bytes, bytes2); err != nil {
380 t.Error(err)
381 }
382 }
383 }
384
385 // TODO aconway 2015-03-13: finish the full interop test