blob: 4b29b27129d697acbeeb5190325ed398e950b62d [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 Google Inc. All rights reserved.
4// http://code.google.com/p/goprotobuf/
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
Rob Pikeaaa3a622010-03-20 22:32:34 -070032
33package proto_test
34
35import (
36 "bytes"
37 "fmt"
David Symonds002ec402011-09-29 17:24:20 -070038 "json"
David Symondsb79d99b2011-08-29 16:38:49 +100039 "math"
David Symonds5b7775e2010-12-01 10:09:04 +110040 "reflect"
41 "strings"
Rob Pikeaaa3a622010-03-20 22:32:34 -070042 "testing"
43
44 . "goprotobuf.googlecode.com/hg/proto"
45 . "./testdata/_obj/test_proto"
46)
47
48var globalO *Buffer
49
50func old() *Buffer {
51 if globalO == nil {
52 globalO = NewBuffer(nil)
53 }
54 globalO.Reset()
55 return globalO
56}
57
58func equalbytes(b1, b2 []byte, t *testing.T) {
59 if len(b1) != len(b2) {
60 t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
61 return
62 }
63 for i := 0; i < len(b1); i++ {
64 if b1[i] != b2[i] {
65 t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
66 }
67 }
68}
69
70func initGoTestField() *GoTestField {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070071 f := new(GoTestField)
Rob Pikeaaa3a622010-03-20 22:32:34 -070072 f.Label = String("label")
73 f.Type = String("type")
74 return f
75}
76
77// These are all structurally equivalent but the tag numbers differ.
78// (It's remarkable that required, optional, and repeated all have
79// 8 letters.)
80func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070081 return &GoTest_RequiredGroup{
82 RequiredField: String("required"),
83 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070084}
85
86func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070087 return &GoTest_OptionalGroup{
88 RequiredField: String("optional"),
89 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070090}
91
92func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070093 return &GoTest_RepeatedGroup{
94 RequiredField: String("repeated"),
95 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070096}
97
98func initGoTest(setdefaults bool) *GoTest {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070099 pb := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700100 if setdefaults {
101 pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
102 pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
103 pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
104 pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
105 pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
106 pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
107 pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
108 pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
109 pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
110 pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
111 pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
112 pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
113 pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
114 }
115
David Symondsdd9ca042011-08-29 16:07:16 +1000116 pb.Kind = NewGoTest_KIND(GoTest_TIME)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700117 pb.RequiredField = initGoTestField()
118 pb.F_BoolRequired = Bool(true)
119 pb.F_Int32Required = Int32(3)
120 pb.F_Int64Required = Int64(6)
121 pb.F_Fixed32Required = Uint32(32)
122 pb.F_Fixed64Required = Uint64(64)
123 pb.F_Uint32Required = Uint32(3232)
124 pb.F_Uint64Required = Uint64(6464)
125 pb.F_FloatRequired = Float32(3232)
126 pb.F_DoubleRequired = Float64(6464)
127 pb.F_StringRequired = String("string")
128 pb.F_BytesRequired = []byte("bytes")
129 pb.F_Sint32Required = Int32(-32)
130 pb.F_Sint64Required = Int64(-64)
131 pb.Requiredgroup = initGoTest_RequiredGroup()
132
133 return pb
134}
135
136func fail(msg string, b *bytes.Buffer, s string, t *testing.T) {
137 data := b.Bytes()
138 ld := len(data)
139 ls := len(s) / 2
140
141 fmt.Printf("fail %s ld=%d ls=%d\n", msg, ld, ls)
142
143 // find the interesting spot - n
144 n := ls
145 if ld < ls {
146 n = ld
147 }
148 j := 0
149 for i := 0; i < n; i++ {
150 bs := hex(s[j])*16 + hex(s[j+1])
151 j += 2
152 if data[i] == bs {
153 continue
154 }
155 n = i
156 break
157 }
158 l := n - 10
159 if l < 0 {
160 l = 0
161 }
162 h := n + 10
163
164 // find the interesting spot - n
165 fmt.Printf("is[%d]:", l)
166 for i := l; i < h; i++ {
167 if i >= ld {
168 fmt.Printf(" --")
169 continue
170 }
171 fmt.Printf(" %.2x", data[i])
172 }
173 fmt.Printf("\n")
174
175 fmt.Printf("sb[%d]:", l)
176 for i := l; i < h; i++ {
177 if i >= ls {
178 fmt.Printf(" --")
179 continue
180 }
181 bs := hex(s[j])*16 + hex(s[j+1])
182 j += 2
183 fmt.Printf(" %.2x", bs)
184 }
185 fmt.Printf("\n")
186
187 t.Fail()
188
Rob Pikeaaa3a622010-03-20 22:32:34 -0700189 // t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes())
190 // Print the output in a partially-decoded format; can
191 // be helpful when updating the test. It produces the output
192 // that is pasted, with minor edits, into the argument to verify().
193 // data := b.Bytes()
194 // nesting := 0
195 // for b.Len() > 0 {
196 // start := len(data) - b.Len()
197 // var u uint64
198 // u, err := DecodeVarint(b)
199 // if err != nil {
200 // fmt.Printf("decode error on varint:", err)
201 // return
202 // }
203 // wire := u & 0x7
204 // tag := u >> 3
205 // switch wire {
206 // case WireVarint:
207 // v, err := DecodeVarint(b)
208 // if err != nil {
209 // fmt.Printf("decode error on varint:", err)
210 // return
211 // }
212 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
213 // data[start:len(data)-b.Len()], tag, wire, v)
214 // case WireFixed32:
215 // v, err := DecodeFixed32(b)
216 // if err != nil {
217 // fmt.Printf("decode error on fixed32:", err)
218 // return
219 // }
220 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
221 // data[start:len(data)-b.Len()], tag, wire, v)
222 // case WireFixed64:
223 // v, err := DecodeFixed64(b)
224 // if err != nil {
225 // fmt.Printf("decode error on fixed64:", err)
226 // return
227 // }
228 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
229 // data[start:len(data)-b.Len()], tag, wire, v)
230 // case WireBytes:
231 // nb, err := DecodeVarint(b)
232 // if err != nil {
233 // fmt.Printf("decode error on bytes:", err)
234 // return
235 // }
236 // after_tag := len(data) - b.Len()
237 // str := make([]byte, nb)
238 // _, err = b.Read(str)
239 // if err != nil {
240 // fmt.Printf("decode error on bytes:", err)
241 // return
242 // }
243 // fmt.Printf("\t\t\"%x\" \"%x\" // field %d, encoding %d (FIELD)\n",
244 // data[start:after_tag], str, tag, wire)
245 // case WireStartGroup:
246 // nesting++
247 // fmt.Printf("\t\t\"%x\"\t\t// start group field %d level %d\n",
248 // data[start:len(data)-b.Len()], tag, nesting)
249 // case WireEndGroup:
250 // fmt.Printf("\t\t\"%x\"\t\t// end group field %d level %d\n",
251 // data[start:len(data)-b.Len()], tag, nesting)
252 // nesting--
253 // default:
254 // fmt.Printf("unrecognized wire type %d\n", wire)
255 // return
256 // }
257 // }
258}
259
260func hex(c uint8) uint8 {
261 if '0' <= c && c <= '9' {
262 return c - '0'
263 }
264 if 'a' <= c && c <= 'f' {
265 return 10 + c - 'a'
266 }
267 if 'A' <= c && c <= 'F' {
268 return 10 + c - 'A'
269 }
270 return 0
271}
272
273func equal(b []byte, s string, t *testing.T) bool {
274 if 2*len(b) != len(s) {
275 // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
276 fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
277 return false
278 }
279 for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
280 x := hex(s[j])*16 + hex(s[j+1])
281 if b[i] != x {
282 // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
283 fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
284 return false
285 }
286 }
287 return true
288}
289
290func overify(t *testing.T, pb *GoTest, expected string) {
291 o := old()
292 err := o.Marshal(pb)
293 if err != nil {
294 fmt.Printf("overify marshal-1 err = %v", err)
295 o.DebugPrint("", o.Bytes())
296 t.Fatalf("expected = %s", expected)
297 }
298 if !equal(o.Bytes(), expected, t) {
299 o.DebugPrint("overify neq 1", o.Bytes())
300 t.Fatalf("expected = %s", expected)
301 }
302
303 // Now test Unmarshal by recreating the original buffer.
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700304 pbd := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700305 err = o.Unmarshal(pbd)
306 if err != nil {
307 t.Fatalf("overify unmarshal err = %v", err)
308 o.DebugPrint("", o.Bytes())
309 t.Fatalf("string = %s", expected)
310 }
311 o.Reset()
312 err = o.Marshal(pbd)
313 if err != nil {
314 t.Errorf("overify marshal-2 err = %v", err)
315 o.DebugPrint("", o.Bytes())
316 t.Fatalf("string = %s", expected)
317 }
318 if !equal(o.Bytes(), expected, t) {
319 o.DebugPrint("overify neq 2", o.Bytes())
320 t.Fatalf("string = %s", expected)
321 }
322}
323
324// Simple tests for numeric encode/decode primitives (varint, etc.)
325func TestNumericPrimitives(t *testing.T) {
326 for i := uint64(0); i < 1e6; i += 111 {
327 o := old()
328 if o.EncodeVarint(i) != nil {
329 t.Error("EncodeVarint")
330 break
331 }
332 x, e := o.DecodeVarint()
333 if e != nil {
334 t.Fatal("DecodeVarint")
335 }
336 if x != i {
337 t.Fatal("varint decode fail:", i, x)
338 }
339
340 o = old()
341 if o.EncodeFixed32(i) != nil {
342 t.Fatal("encFixed32")
343 }
344 x, e = o.DecodeFixed32()
345 if e != nil {
346 t.Fatal("decFixed32")
347 }
348 if x != i {
349 t.Fatal("fixed32 decode fail:", i, x)
350 }
351
352 o = old()
353 if o.EncodeFixed64(i*1234567) != nil {
354 t.Error("encFixed64")
355 break
356 }
357 x, e = o.DecodeFixed64()
358 if e != nil {
359 t.Error("decFixed64")
360 break
361 }
362 if x != i*1234567 {
363 t.Error("fixed64 decode fail:", i*1234567, x)
364 break
365 }
366
367 o = old()
368 i32 := int32(i - 12345)
369 if o.EncodeZigzag32(uint64(i32)) != nil {
370 t.Fatal("EncodeZigzag32")
371 }
372 x, e = o.DecodeZigzag32()
373 if e != nil {
374 t.Fatal("DecodeZigzag32")
375 }
376 if x != uint64(uint32(i32)) {
377 t.Fatal("zigzag32 decode fail:", i32, x)
378 }
379
380 o = old()
381 i64 := int64(i - 12345)
382 if o.EncodeZigzag64(uint64(i64)) != nil {
383 t.Fatal("EncodeZigzag64")
384 }
385 x, e = o.DecodeZigzag64()
386 if e != nil {
387 t.Fatal("DecodeZigzag64")
388 }
389 if x != uint64(i64) {
390 t.Fatal("zigzag64 decode fail:", i64, x)
391 }
392 }
393}
394
395// Simple tests for bytes
396func TestBytesPrimitives(t *testing.T) {
397 o := old()
398 bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
399 if o.EncodeRawBytes(bytes) != nil {
400 t.Error("EncodeRawBytes")
401 }
402 decb, e := o.DecodeRawBytes(false)
403 if e != nil {
404 t.Error("DecodeRawBytes")
405 }
406 equalbytes(bytes, decb, t)
407}
408
409// Simple tests for strings
410func TestStringPrimitives(t *testing.T) {
411 o := old()
412 s := "now is the time"
413 if o.EncodeStringBytes(s) != nil {
414 t.Error("enc_string")
415 }
416 decs, e := o.DecodeStringBytes()
417 if e != nil {
418 t.Error("dec_string")
419 }
420 if s != decs {
421 t.Error("string encode/decode fail:", s, decs)
422 }
423}
424
425// Do we catch the "required bit not set" case?
426func TestRequiredBit(t *testing.T) {
427 o := old()
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700428 pb := new(GoTest)
David Symonds5b7775e2010-12-01 10:09:04 +1100429 err := o.Marshal(pb)
430 if err == nil {
431 t.Error("did not catch missing required fields")
Rob Pikea17fdd92011-11-02 12:43:05 -0700432 } else if strings.Index(err.Error(), "GoTest") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +1100433 t.Error("wrong error type:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700434 }
435}
436
437// Check that all fields are nil.
438// Clearly silly, and a residue from a more interesting test with an earlier,
439// different initialization property, but it once caught a compiler bug so
440// it lives.
441func checkInitialized(pb *GoTest, t *testing.T) {
442 if pb.F_BoolDefaulted != nil {
443 t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
444 }
445 if pb.F_Int32Defaulted != nil {
446 t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
447 }
448 if pb.F_Int64Defaulted != nil {
449 t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
450 }
451 if pb.F_Fixed32Defaulted != nil {
452 t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
453 }
454 if pb.F_Fixed64Defaulted != nil {
455 t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
456 }
457 if pb.F_Uint32Defaulted != nil {
458 t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
459 }
460 if pb.F_Uint64Defaulted != nil {
461 t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
462 }
463 if pb.F_FloatDefaulted != nil {
464 t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
465 }
466 if pb.F_DoubleDefaulted != nil {
467 t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
468 }
469 if pb.F_StringDefaulted != nil {
470 t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
471 }
472 if pb.F_BytesDefaulted != nil {
473 t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
474 }
475 if pb.F_Sint32Defaulted != nil {
476 t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
477 }
478 if pb.F_Sint64Defaulted != nil {
479 t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
480 }
481}
482
483// Does Reset() reset?
484func TestReset(t *testing.T) {
485 pb := initGoTest(true)
486 // muck with some values
487 pb.F_BoolDefaulted = Bool(false)
488 pb.F_Int32Defaulted = Int32(237)
489 pb.F_Int64Defaulted = Int64(12346)
490 pb.F_Fixed32Defaulted = Uint32(32000)
491 pb.F_Fixed64Defaulted = Uint64(666)
492 pb.F_Uint32Defaulted = Uint32(323232)
493 pb.F_Uint64Defaulted = nil
494 pb.F_FloatDefaulted = nil
495 pb.F_DoubleDefaulted = Float64(0)
496 pb.F_StringDefaulted = String("gotcha")
497 pb.F_BytesDefaulted = []byte("asdfasdf")
498 pb.F_Sint32Defaulted = Int32(123)
499 pb.F_Sint64Defaulted = Int64(789)
500 pb.Reset()
501 checkInitialized(pb, t)
502}
503
504// All required fields set, no defaults provided.
505func TestEncodeDecode1(t *testing.T) {
506 pb := initGoTest(false)
507 overify(t, pb,
508 "0807"+ // field 1, encoding 0, value 7
509 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
510 "5001"+ // field 10, encoding 0, value 1
511 "5803"+ // field 11, encoding 0, value 3
512 "6006"+ // field 12, encoding 0, value 6
513 "6d20000000"+ // field 13, encoding 5, value 0x20
514 "714000000000000000"+ // field 14, encoding 1, value 0x40
515 "78a019"+ // field 15, encoding 0, value 0xca0 = 3232
516 "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
517 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
518 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
519 "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700520 "b304"+ // field 70, encoding 3, start group
521 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700522 "b404"+ // field 70, encoding 4, end group
523 "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
524 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
525 "b8067f") // field 103, encoding 0, 0x7f zigzag64
Rob Pikeaaa3a622010-03-20 22:32:34 -0700526}
527
528// All required fields set, defaults provided.
529func TestEncodeDecode2(t *testing.T) {
530 pb := initGoTest(true)
531 overify(t, pb,
532 "0807"+ // field 1, encoding 0, value 7
533 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
534 "5001"+ // field 10, encoding 0, value 1
535 "5803"+ // field 11, encoding 0, value 3
536 "6006"+ // field 12, encoding 0, value 6
537 "6d20000000"+ // field 13, encoding 5, value 32
538 "714000000000000000"+ // field 14, encoding 1, value 64
539 "78a019"+ // field 15, encoding 0, value 3232
540 "8001c032"+ // field 16, encoding 0, value 6464
541 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
542 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
543 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700544 "c00201"+ // field 40, encoding 0, value 1
545 "c80220"+ // field 41, encoding 0, value 32
546 "d00240"+ // field 42, encoding 0, value 64
547 "dd0240010000"+ // field 43, encoding 5, value 320
548 "e1028002000000000000"+ // field 44, encoding 1, value 640
549 "e8028019"+ // field 45, encoding 0, value 3200
550 "f0028032"+ // field 46, encoding 0, value 6400
551 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
552 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
553 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700554 "b304"+ // start group field 70 level 1
555 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700556 "b404"+ // end group field 70 level 1
557 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
558 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
559 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
560 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
561 "90193f"+ // field 402, encoding 0, value 63
562 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700563
564}
565
566// All default fields set to their default value by hand
567func TestEncodeDecode3(t *testing.T) {
568 pb := initGoTest(false)
569 pb.F_BoolDefaulted = Bool(true)
570 pb.F_Int32Defaulted = Int32(32)
571 pb.F_Int64Defaulted = Int64(64)
572 pb.F_Fixed32Defaulted = Uint32(320)
573 pb.F_Fixed64Defaulted = Uint64(640)
574 pb.F_Uint32Defaulted = Uint32(3200)
575 pb.F_Uint64Defaulted = Uint64(6400)
576 pb.F_FloatDefaulted = Float32(314159)
577 pb.F_DoubleDefaulted = Float64(271828)
578 pb.F_StringDefaulted = String("hello, \"world!\"\n")
579 pb.F_BytesDefaulted = []byte("Bignose")
580 pb.F_Sint32Defaulted = Int32(-32)
581 pb.F_Sint64Defaulted = Int64(-64)
582
583 overify(t, pb,
584 "0807"+ // field 1, encoding 0, value 7
585 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
586 "5001"+ // field 10, encoding 0, value 1
587 "5803"+ // field 11, encoding 0, value 3
588 "6006"+ // field 12, encoding 0, value 6
589 "6d20000000"+ // field 13, encoding 5, value 32
590 "714000000000000000"+ // field 14, encoding 1, value 64
591 "78a019"+ // field 15, encoding 0, value 3232
592 "8001c032"+ // field 16, encoding 0, value 6464
593 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
594 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
595 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700596 "c00201"+ // field 40, encoding 0, value 1
597 "c80220"+ // field 41, encoding 0, value 32
598 "d00240"+ // field 42, encoding 0, value 64
599 "dd0240010000"+ // field 43, encoding 5, value 320
600 "e1028002000000000000"+ // field 44, encoding 1, value 640
601 "e8028019"+ // field 45, encoding 0, value 3200
602 "f0028032"+ // field 46, encoding 0, value 6400
603 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
604 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
605 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700606 "b304"+ // start group field 70 level 1
607 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700608 "b404"+ // end group field 70 level 1
609 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
610 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
611 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
612 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
613 "90193f"+ // field 402, encoding 0, value 63
614 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700615
616}
617
618// All required fields set, defaults provided, all non-defaulted optional fields have values.
619func TestEncodeDecode4(t *testing.T) {
620 pb := initGoTest(true)
621 pb.Table = String("hello")
622 pb.Param = Int32(7)
623 pb.OptionalField = initGoTestField()
624 pb.F_BoolOptional = Bool(true)
625 pb.F_Int32Optional = Int32(32)
626 pb.F_Int64Optional = Int64(64)
627 pb.F_Fixed32Optional = Uint32(3232)
628 pb.F_Fixed64Optional = Uint64(6464)
629 pb.F_Uint32Optional = Uint32(323232)
630 pb.F_Uint64Optional = Uint64(646464)
631 pb.F_FloatOptional = Float32(32.)
632 pb.F_DoubleOptional = Float64(64.)
633 pb.F_StringOptional = String("hello")
634 pb.F_BytesOptional = []byte("Bignose")
635 pb.F_Sint32Optional = Int32(-32)
636 pb.F_Sint64Optional = Int64(-64)
637 pb.Optionalgroup = initGoTest_OptionalGroup()
638
639 overify(t, pb,
640 "0807"+ // field 1, encoding 0, value 7
641 "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
642 "1807"+ // field 3, encoding 0, value 7
643 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
644 "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
645 "5001"+ // field 10, encoding 0, value 1
646 "5803"+ // field 11, encoding 0, value 3
647 "6006"+ // field 12, encoding 0, value 6
648 "6d20000000"+ // field 13, encoding 5, value 32
649 "714000000000000000"+ // field 14, encoding 1, value 64
650 "78a019"+ // field 15, encoding 0, value 3232
651 "8001c032"+ // field 16, encoding 0, value 6464
652 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
653 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
654 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700655 "f00101"+ // field 30, encoding 0, value 1
656 "f80120"+ // field 31, encoding 0, value 32
657 "800240"+ // field 32, encoding 0, value 64
658 "8d02a00c0000"+ // field 33, encoding 5, value 3232
659 "91024019000000000000"+ // field 34, encoding 1, value 6464
660 "9802a0dd13"+ // field 35, encoding 0, value 323232
661 "a002c0ba27"+ // field 36, encoding 0, value 646464
662 "ad0200000042"+ // field 37, encoding 5, value 32.0
663 "b1020000000000005040"+ // field 38, encoding 1, value 64.0
664 "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700665 "c00201"+ // field 40, encoding 0, value 1
666 "c80220"+ // field 41, encoding 0, value 32
667 "d00240"+ // field 42, encoding 0, value 64
668 "dd0240010000"+ // field 43, encoding 5, value 320
669 "e1028002000000000000"+ // field 44, encoding 1, value 640
670 "e8028019"+ // field 45, encoding 0, value 3200
671 "f0028032"+ // field 46, encoding 0, value 6400
672 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
673 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
674 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700675 "b304"+ // start group field 70 level 1
676 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
677 "b404"+ // end group field 70 level 1
678 "d305"+ // start group field 90 level 1
679 "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
David Symondsd15e81b2011-10-03 14:31:12 -0700680 "d405"+ // end group field 90 level 1
681 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
682 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
683 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
684 "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
685 "f0123f"+ // field 302, encoding 0, value 63
686 "f8127f"+ // field 303, encoding 0, value 127
687 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
688 "90193f"+ // field 402, encoding 0, value 63
689 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700690
691}
692
693// All required fields set, defaults provided, all repeated fields given two values.
694func TestEncodeDecode5(t *testing.T) {
695 pb := initGoTest(true)
696 pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
697 pb.F_BoolRepeated = []bool{false, true}
698 pb.F_Int32Repeated = []int32{32, 33}
699 pb.F_Int64Repeated = []int64{64, 65}
700 pb.F_Fixed32Repeated = []uint32{3232, 3333}
701 pb.F_Fixed64Repeated = []uint64{6464, 6565}
702 pb.F_Uint32Repeated = []uint32{323232, 333333}
703 pb.F_Uint64Repeated = []uint64{646464, 656565}
704 pb.F_FloatRepeated = []float32{32., 33.}
705 pb.F_DoubleRepeated = []float64{64., 65.}
706 pb.F_StringRepeated = []string{"hello", "sailor"}
707 pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
708 pb.F_Sint32Repeated = []int32{32, -32}
709 pb.F_Sint64Repeated = []int64{64, -64}
710 pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
711
712 overify(t, pb,
713 "0807"+ // field 1, encoding 0, value 7
714 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
715 "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
716 "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
717 "5001"+ // field 10, encoding 0, value 1
718 "5803"+ // field 11, encoding 0, value 3
719 "6006"+ // field 12, encoding 0, value 6
720 "6d20000000"+ // field 13, encoding 5, value 32
721 "714000000000000000"+ // field 14, encoding 1, value 64
722 "78a019"+ // field 15, encoding 0, value 3232
723 "8001c032"+ // field 16, encoding 0, value 6464
724 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
725 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
726 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700727 "a00100"+ // field 20, encoding 0, value 0
728 "a00101"+ // field 20, encoding 0, value 1
729 "a80120"+ // field 21, encoding 0, value 32
730 "a80121"+ // field 21, encoding 0, value 33
731 "b00140"+ // field 22, encoding 0, value 64
732 "b00141"+ // field 22, encoding 0, value 65
733 "bd01a00c0000"+ // field 23, encoding 5, value 3232
734 "bd01050d0000"+ // field 23, encoding 5, value 3333
735 "c1014019000000000000"+ // field 24, encoding 1, value 6464
736 "c101a519000000000000"+ // field 24, encoding 1, value 6565
737 "c801a0dd13"+ // field 25, encoding 0, value 323232
738 "c80195ac14"+ // field 25, encoding 0, value 333333
739 "d001c0ba27"+ // field 26, encoding 0, value 646464
740 "d001b58928"+ // field 26, encoding 0, value 656565
741 "dd0100000042"+ // field 27, encoding 5, value 32.0
742 "dd0100000442"+ // field 27, encoding 5, value 33.0
743 "e1010000000000005040"+ // field 28, encoding 1, value 64.0
744 "e1010000000000405040"+ // field 28, encoding 1, value 65.0
745 "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
746 "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700747 "c00201"+ // field 40, encoding 0, value 1
748 "c80220"+ // field 41, encoding 0, value 32
749 "d00240"+ // field 42, encoding 0, value 64
750 "dd0240010000"+ // field 43, encoding 5, value 320
751 "e1028002000000000000"+ // field 44, encoding 1, value 640
752 "e8028019"+ // field 45, encoding 0, value 3200
753 "f0028032"+ // field 46, encoding 0, value 6400
754 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
755 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
756 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700757 "b304"+ // start group field 70 level 1
758 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
759 "b404"+ // end group field 70 level 1
760 "8305"+ // start group field 80 level 1
761 "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
762 "8405"+ // end group field 80 level 1
763 "8305"+ // start group field 80 level 1
764 "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
David Symondsd15e81b2011-10-03 14:31:12 -0700765 "8405"+ // end group field 80 level 1
766 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
767 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
768 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
769 "ca0c03"+"626967"+ // field 201, encoding 2, string "big"
770 "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
771 "d00c40"+ // field 202, encoding 0, value 32
772 "d00c3f"+ // field 202, encoding 0, value -32
773 "d80c8001"+ // field 203, encoding 0, value 64
774 "d80c7f"+ // field 203, encoding 0, value -64
775 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
776 "90193f"+ // field 402, encoding 0, value 63
777 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700778
779}
780
David Symonds5b7775e2010-12-01 10:09:04 +1100781// All required fields set, all packed repeated fields given two values.
782func TestEncodeDecode6(t *testing.T) {
783 pb := initGoTest(false)
784 pb.F_BoolRepeatedPacked = []bool{false, true}
785 pb.F_Int32RepeatedPacked = []int32{32, 33}
786 pb.F_Int64RepeatedPacked = []int64{64, 65}
787 pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
788 pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
789 pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
790 pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
791 pb.F_FloatRepeatedPacked = []float32{32., 33.}
792 pb.F_DoubleRepeatedPacked = []float64{64., 65.}
793 pb.F_Sint32RepeatedPacked = []int32{32, -32}
794 pb.F_Sint64RepeatedPacked = []int64{64, -64}
795
796 overify(t, pb,
797 "0807"+ // field 1, encoding 0, value 7
798 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
799 "5001"+ // field 10, encoding 0, value 1
800 "5803"+ // field 11, encoding 0, value 3
801 "6006"+ // field 12, encoding 0, value 6
802 "6d20000000"+ // field 13, encoding 5, value 32
803 "714000000000000000"+ // field 14, encoding 1, value 64
804 "78a019"+ // field 15, encoding 0, value 3232
805 "8001c032"+ // field 16, encoding 0, value 6464
806 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
807 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
808 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
David Symonds5b7775e2010-12-01 10:09:04 +1100809 "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
810 "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
811 "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
812 "aa0308"+ // field 53, encoding 2, 8 bytes
813 "a00c0000050d0000"+ // value 3232, value 3333
814 "b20310"+ // field 54, encoding 2, 16 bytes
815 "4019000000000000a519000000000000"+ // value 6464, value 6565
816 "ba0306"+ // field 55, encoding 2, 6 bytes
817 "a0dd1395ac14"+ // value 323232, value 333333
818 "c20306"+ // field 56, encoding 2, 6 bytes
819 "c0ba27b58928"+ // value 646464, value 656565
820 "ca0308"+ // field 57, encoding 2, 8 bytes
821 "0000004200000442"+ // value 32.0, value 33.0
822 "d20310"+ // field 58, encoding 2, 16 bytes
823 "00000000000050400000000000405040"+ // value 64.0, value 65.0
David Symondsd15e81b2011-10-03 14:31:12 -0700824 "b304"+ // start group field 70 level 1
825 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
826 "b404"+ // end group field 70 level 1
827 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
828 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
829 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
David Symonds5b7775e2010-12-01 10:09:04 +1100830 "b21f02"+ // field 502, encoding 2, 2 bytes
831 "403f"+ // value 32, value -32
832 "ba1f03"+ // field 503, encoding 2, 3 bytes
David Symondsd15e81b2011-10-03 14:31:12 -0700833 "80017f") // value 64, value -64
David Symonds5b7775e2010-12-01 10:09:04 +1100834}
835
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800836// Test that we can encode empty bytes fields.
837func TestEncodeDecodeBytes1(t *testing.T) {
838 pb := initGoTest(false)
839
840 // Create our bytes
841 pb.F_BytesRequired = []byte{}
David Symondsd9da6ba2011-08-30 14:41:30 +1000842 pb.F_BytesRepeated = [][]byte{{}}
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800843 pb.F_BytesOptional = []byte{}
844
845 d, err := Marshal(pb)
846 if err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700847 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800848 }
849
850 pbd := new(GoTest)
851 if err := Unmarshal(d, pbd); err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700852 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800853 }
854
855 if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
Rob Pikea17fdd92011-11-02 12:43:05 -0700856 t.Error("required empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800857 }
858 if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700859 t.Error("repeated empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800860 }
861 if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
Rob Pikea17fdd92011-11-02 12:43:05 -0700862 t.Error("optional empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800863 }
864}
865
866// Test that we encode nil-valued fields of a repeated bytes field correctly.
867// Since entries in a repeated field cannot be nil, nil must mean empty value.
868func TestEncodeDecodeBytes2(t *testing.T) {
869 pb := initGoTest(false)
870
871 // Create our bytes
David Symonds5b7775e2010-12-01 10:09:04 +1100872 pb.F_BytesRepeated = [][]byte{nil}
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800873
874 d, err := Marshal(pb)
David Symonds5b7775e2010-12-01 10:09:04 +1100875 if err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700876 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800877 }
878
879 pbd := new(GoTest)
880 if err := Unmarshal(d, pbd); err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700881 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800882 }
883
884 if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700885 t.Error("Unexpected value for repeated bytes field")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800886 }
887}
888
Rob Pikeaaa3a622010-03-20 22:32:34 -0700889// All required fields set, defaults provided, all repeated fields given two values.
890func TestSkippingUnrecognizedFields(t *testing.T) {
891 o := old()
892 pb := initGoTestField()
893
894 // Marshal it normally.
895 o.Marshal(pb)
896
897 // Now new a GoSkipTest record.
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700898 skip := &GoSkipTest{
899 SkipInt32: Int32(32),
900 SkipFixed32: Uint32(3232),
901 SkipFixed64: Uint64(6464),
902 SkipString: String("skipper"),
903 Skipgroup: &GoSkipTest_SkipGroup{
904 GroupInt32: Int32(75),
905 GroupString: String("wxyz"),
906 },
907 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700908
909 // Marshal it into same buffer.
910 o.Marshal(skip)
911
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700912 pbd := new(GoTestField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700913 o.Unmarshal(pbd)
914
915 // The __unrecognized field should be a marshaling of GoSkipTest
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700916 skipd := new(GoSkipTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700917
918 o.SetBuf(pbd.XXX_unrecognized)
919 o.Unmarshal(skipd)
920
921 if *skipd.SkipInt32 != *skip.SkipInt32 {
922 t.Error("skip int32", skipd.SkipInt32)
923 }
924 if *skipd.SkipFixed32 != *skip.SkipFixed32 {
925 t.Error("skip fixed32", skipd.SkipFixed32)
926 }
927 if *skipd.SkipFixed64 != *skip.SkipFixed64 {
928 t.Error("skip fixed64", skipd.SkipFixed64)
929 }
930 if *skipd.SkipString != *skip.SkipString {
931 t.Error("skip string", *skipd.SkipString)
932 }
933 if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
934 t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
935 }
936 if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
937 t.Error("skip group string", *skipd.Skipgroup.GroupString)
938 }
939}
940
941// Check that we can grow an array (repeated field) to have many elements.
942// This test doesn't depend only on our encoding; for variety, it makes sure
943// we create, encode, and decode the correct contents explicitly. It's therefore
944// a bit messier.
945// This test also uses (and hence tests) the Marshal/Unmarshal functions
946// instead of the methods.
947func TestBigRepeated(t *testing.T) {
948 pb := initGoTest(true)
949
950 // Create the arrays
951 const N = 50 // Internally the library starts much smaller.
952 pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
953 pb.F_Sint64Repeated = make([]int64, N)
954 pb.F_Sint32Repeated = make([]int32, N)
955 pb.F_BytesRepeated = make([][]byte, N)
956 pb.F_StringRepeated = make([]string, N)
957 pb.F_DoubleRepeated = make([]float64, N)
958 pb.F_FloatRepeated = make([]float32, N)
959 pb.F_Uint64Repeated = make([]uint64, N)
960 pb.F_Uint32Repeated = make([]uint32, N)
961 pb.F_Fixed64Repeated = make([]uint64, N)
962 pb.F_Fixed32Repeated = make([]uint32, N)
963 pb.F_Int64Repeated = make([]int64, N)
964 pb.F_Int32Repeated = make([]int32, N)
965 pb.F_BoolRepeated = make([]bool, N)
966 pb.RepeatedField = make([]*GoTestField, N)
967
968 // Fill in the arrays with checkable values.
969 igtf := initGoTestField()
970 igtrg := initGoTest_RepeatedGroup()
971 for i := 0; i < N; i++ {
972 pb.Repeatedgroup[i] = igtrg
973 pb.F_Sint64Repeated[i] = int64(i)
974 pb.F_Sint32Repeated[i] = int32(i)
975 s := fmt.Sprint(i)
976 pb.F_BytesRepeated[i] = []byte(s)
977 pb.F_StringRepeated[i] = s
978 pb.F_DoubleRepeated[i] = float64(i)
979 pb.F_FloatRepeated[i] = float32(i)
980 pb.F_Uint64Repeated[i] = uint64(i)
981 pb.F_Uint32Repeated[i] = uint32(i)
982 pb.F_Fixed64Repeated[i] = uint64(i)
983 pb.F_Fixed32Repeated[i] = uint32(i)
984 pb.F_Int64Repeated[i] = int64(i)
985 pb.F_Int32Repeated[i] = int32(i)
986 pb.F_BoolRepeated[i] = i%2 == 0
987 pb.RepeatedField[i] = igtf
988 }
989
990 // Marshal.
991 buf, _ := Marshal(pb)
992
993 // Now test Unmarshal by recreating the original buffer.
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700994 pbd := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700995 Unmarshal(buf, pbd)
996
997 // Check the checkable values
998 for i := uint64(0); i < N; i++ {
999 if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
1000 t.Error("pbd.Repeatedgroup bad")
1001 }
1002 var x uint64
1003 x = uint64(pbd.F_Sint64Repeated[i])
1004 if x != i {
1005 t.Error("pbd.F_Sint64Repeated bad", x, i)
1006 }
1007 x = uint64(pbd.F_Sint32Repeated[i])
1008 if x != i {
1009 t.Error("pbd.F_Sint32Repeated bad", x, i)
1010 }
1011 s := fmt.Sprint(i)
1012 equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
1013 if pbd.F_StringRepeated[i] != s {
1014 t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
1015 }
1016 x = uint64(pbd.F_DoubleRepeated[i])
1017 if x != i {
1018 t.Error("pbd.F_DoubleRepeated bad", x, i)
1019 }
1020 x = uint64(pbd.F_FloatRepeated[i])
1021 if x != i {
1022 t.Error("pbd.F_FloatRepeated bad", x, i)
1023 }
1024 x = pbd.F_Uint64Repeated[i]
1025 if x != i {
1026 t.Error("pbd.F_Uint64Repeated bad", x, i)
1027 }
1028 x = uint64(pbd.F_Uint32Repeated[i])
1029 if x != i {
1030 t.Error("pbd.F_Uint32Repeated bad", x, i)
1031 }
1032 x = pbd.F_Fixed64Repeated[i]
1033 if x != i {
1034 t.Error("pbd.F_Fixed64Repeated bad", x, i)
1035 }
1036 x = uint64(pbd.F_Fixed32Repeated[i])
1037 if x != i {
1038 t.Error("pbd.F_Fixed32Repeated bad", x, i)
1039 }
1040 x = uint64(pbd.F_Int64Repeated[i])
1041 if x != i {
1042 t.Error("pbd.F_Int64Repeated bad", x, i)
1043 }
1044 x = uint64(pbd.F_Int32Repeated[i])
1045 if x != i {
1046 t.Error("pbd.F_Int32Repeated bad", x, i)
1047 }
1048 if pbd.F_BoolRepeated[i] != (i%2 == 0) {
1049 t.Error("pbd.F_BoolRepeated bad", x, i)
1050 }
1051 if pbd.RepeatedField[i] == nil { // TODO: more checking?
1052 t.Error("pbd.RepeatedField bad")
1053 }
1054 }
1055}
1056
1057// Verify we give a useful message when decoding to the wrong structure type.
1058func TestTypeMismatch(t *testing.T) {
1059 pb1 := initGoTest(true)
1060
1061 // Marshal
1062 o := old()
1063 o.Marshal(pb1)
1064
1065 // Now Unmarshal it to the wrong type.
1066 pb2 := initGoTestField()
1067 err := o.Unmarshal(pb2)
1068 switch err {
1069 case ErrWrongType:
1070 // fine
1071 case nil:
1072 t.Error("expected wrong type error, got no error")
1073 default:
1074 t.Error("expected wrong type error, got", err)
1075 }
1076}
1077
David Symonds5b7775e2010-12-01 10:09:04 +11001078func encodeDecode(t *testing.T, in, out interface{}, msg string) {
1079 buf, err := Marshal(in)
1080 if err != nil {
1081 t.Fatalf("failed marshaling %v: %v", msg, err)
1082 }
1083 if err := Unmarshal(buf, out); err != nil {
1084 t.Fatalf("failed unmarshaling %v: %v", msg, err)
1085 }
1086}
1087
1088func TestPackedNonPackedDecoderSwitching(t *testing.T) {
1089 np, p := new(NonPackedTest), new(PackedTest)
1090
1091 // non-packed -> packed
1092 np.A = []int32{0, 1, 1, 2, 3, 5}
1093 encodeDecode(t, np, p, "non-packed -> packed")
1094 if !reflect.DeepEqual(np.A, p.B) {
1095 t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
1096 }
1097
1098 // packed -> non-packed
1099 np.Reset()
1100 p.B = []int32{3, 1, 4, 1, 5, 9}
1101 encodeDecode(t, p, np, "packed -> non-packed")
1102 if !reflect.DeepEqual(p.B, np.A) {
1103 t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
1104 }
1105}
1106
Rob Pikeaaa3a622010-03-20 22:32:34 -07001107func TestProto1RepeatedGroup(t *testing.T) {
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001108 pb := &MessageList{
1109 Message: []*MessageList_Message{
1110 &MessageList_Message{
1111 Name: String("blah"),
1112 Count: Int32(7),
1113 },
1114 // NOTE: pb.Message[1] is a nil
1115 nil,
1116 },
1117 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001118
1119 o := old()
1120 if err := o.Marshal(pb); err != ErrRepeatedHasNil {
1121 t.Fatalf("unexpected or no error when marshaling: %v", err)
1122 }
1123}
1124
Rob Pikeaaa3a622010-03-20 22:32:34 -07001125// Test that enums work. Checks for a bug introduced by making enums
1126// named types instead of int32: newInt32FromUint64 would crash with
1127// a type mismatch in reflect.PointTo.
1128func TestEnum(t *testing.T) {
1129 pb := new(GoEnum)
1130 pb.Foo = NewFOO(FOO_FOO1)
1131 o := old()
1132 if err := o.Marshal(pb); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001133 t.Fatal("error encoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001134 }
1135 pb1 := new(GoEnum)
1136 if err := o.Unmarshal(pb1); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001137 t.Fatal("error decoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001138 }
1139 if *pb1.Foo != FOO_FOO1 {
Rob Pike853f9112010-12-17 13:56:46 -08001140 t.Error("expected 7 but got ", *pb1.Foo)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001141 }
1142}
1143
David Symondse37856c2011-06-22 12:52:53 +10001144// Enum types have String methods. Check that enum fields can be printed.
1145// We don't care what the value actually is, just as long as it doesn't crash.
1146func TestPrintingNilEnumFields(t *testing.T) {
1147 pb := new(GoEnum)
1148 fmt.Sprintf("%+v", pb)
1149}
1150
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001151// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1152func TestRequiredFieldEnforcement(t *testing.T) {
1153 pb := new(GoTestField)
1154 _, err := Marshal(pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001155 if err == nil {
1156 t.Error("marshal: expected error, got nil")
Rob Pikea17fdd92011-11-02 12:43:05 -07001157 } else if strings.Index(err.Error(), "GoTestField") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001158 t.Errorf("marshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001159 }
1160
1161 // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1162 // so simply counting the required fields is insufficient.
1163 // field 1, encoding 2, value "hi"
1164 buf := []byte("\x0A\x02hi\x0A\x02hi")
1165 err = Unmarshal(buf, pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001166 if err == nil {
1167 t.Error("unmarshal: expected error, got nil")
Rob Pikea17fdd92011-11-02 12:43:05 -07001168 } else if strings.Index(err.Error(), "GoTestField") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001169 t.Errorf("unmarshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001170 }
1171}
1172
David Symonds03c9d412010-08-26 14:23:18 +10001173// A type that implements the Marshaler interface, but is not nillable.
1174type nonNillableInt uint64
1175
Rob Pikea17fdd92011-11-02 12:43:05 -07001176func (nni nonNillableInt) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001177 return EncodeVarint(uint64(nni)), nil
1178}
1179
1180type NNIMessage struct {
1181 nni nonNillableInt
1182}
1183
1184// A type that implements the Marshaler interface and is nillable.
1185type nillableMessage struct {
1186 x uint64
1187}
1188
Rob Pikea17fdd92011-11-02 12:43:05 -07001189func (nm *nillableMessage) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001190 return EncodeVarint(nm.x), nil
1191}
1192
1193type NMMessage struct {
1194 nm *nillableMessage
1195}
1196
1197// Verify a type that uses the Marshaler interface, but has a nil pointer.
1198func TestNilMarshaler(t *testing.T) {
1199 // Try a struct with a Marshaler field that is nil.
1200 // It should be directly marshable.
1201 nmm := new(NMMessage)
1202 if _, err := Marshal(nmm); err != nil {
1203 t.Error("unexpected error marshaling nmm: ", err)
1204 }
1205
1206 // Try a struct with a Marshaler field that is not nillable.
1207 nnim := new(NNIMessage)
1208 nnim.nni = 7
1209 var _ Marshaler = nnim.nni // verify it is truly a Marshaler
1210 if _, err := Marshal(nnim); err != nil {
1211 t.Error("unexpected error marshaling nnim: ", err)
1212 }
1213}
1214
David Symonds7656e742011-07-22 14:54:17 +10001215// Check that passing a struct to Marshal returns a good error,
1216// rather than panicking.
1217func TestStructMarshaling(t *testing.T) {
1218 _, err := Marshal(OtherMessage{})
1219 if err != ErrNotPtr {
1220 t.Errorf("got %v, expected %v", err, ErrNotPtr)
1221 }
1222}
1223
David Symondsb79d99b2011-08-29 16:38:49 +10001224func TestAllSetDefaults(t *testing.T) {
1225 // Exercise SetDefaults with all scalar field types.
1226 m := &Defaults{
1227 // NaN != NaN, so override that here.
1228 F_Nan: Float32(1.7),
1229 }
1230 expected := &Defaults{
1231 F_Bool: Bool(true),
1232 F_Int32: Int32(32),
1233 F_Int64: Int64(64),
1234 F_Fixed32: Uint32(320),
1235 F_Fixed64: Uint64(640),
1236 F_Uint32: Uint32(3200),
1237 F_Uint64: Uint64(6400),
1238 F_Float: Float32(314159),
1239 F_Double: Float64(271828),
1240 F_String: String(`hello, "world!"` + "\n"),
1241 F_Bytes: []byte("Bignose"),
1242 F_Sint32: Int32(-32),
1243 F_Sint64: Int64(-64),
1244 F_Enum: NewDefaults_Color(Defaults_GREEN),
1245 F_Pinf: Float32(float32(math.Inf(1))),
1246 F_Ninf: Float32(float32(math.Inf(-1))),
1247 F_Nan: Float32(1.7),
1248 }
1249 SetDefaults(m)
1250 if !Equal(m, expected) {
1251 t.Errorf(" got %v\nwant %v", m, expected)
1252 }
1253}
1254
1255func TestSetDefaultsWithSetField(t *testing.T) {
1256 // Check that a set value is not overridden.
1257 m := &Defaults{
1258 F_Int32: Int32(12),
1259 }
1260 SetDefaults(m)
1261 if v := GetInt32(m.F_Int32); v != 12 {
1262 t.Errorf("m.FInt32 = %v, want 12", v)
1263 }
1264}
1265
1266func TestSetDefaultsWithSubMessage(t *testing.T) {
1267 m := &OtherMessage{
1268 Key: Int64(123),
1269 Inner: &InnerMessage{
1270 Host: String("gopher"),
1271 },
1272 }
1273 expected := &OtherMessage{
1274 Key: Int64(123),
1275 Inner: &InnerMessage{
1276 Host: String("gopher"),
1277 Port: Int32(4000),
1278 },
1279 }
1280 SetDefaults(m)
1281 if !Equal(m, expected) {
1282 t.Errorf(" got %v\nwant %v", m, expected)
1283 }
1284}
1285
David Symondsd73d7b12011-09-28 10:56:43 -07001286func TestMaximumTagNumber(t *testing.T) {
1287 m := &MaxTag{
1288 LastField: String("natural goat essence"),
1289 }
1290 buf, err := Marshal(m)
1291 if err != nil {
1292 t.Fatalf("proto.Marshal failed: %v", err)
1293 }
1294 m2 := new(MaxTag)
1295 if err := Unmarshal(buf, m2); err != nil {
1296 t.Fatalf("proto.Unmarshal failed: %v", err)
1297 }
1298 if got, want := GetString(m2.LastField), *m.LastField; got != want {
1299 t.Errorf("got %q, want %q", got, want)
1300 }
1301}
1302
David Symonds002ec402011-09-29 17:24:20 -07001303func TestJSON(t *testing.T) {
1304 m := &MyMessage{
1305 Count: Int32(4),
1306 Pet: []string{"bunny", "kitty"},
1307 Inner: &InnerMessage{
1308 Host: String("cauchy"),
1309 },
1310 }
1311 const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"}}`
1312
1313 b, err := json.Marshal(m)
1314 if err != nil {
1315 t.Fatalf("json.Marshal failed: %v", err)
1316 }
1317 s := string(b)
1318 if s != expected {
1319 t.Errorf("got %s\nwant %s", s, expected)
1320 }
1321}
1322
Rob Pikeaaa3a622010-03-20 22:32:34 -07001323func BenchmarkMarshal(b *testing.B) {
1324 b.StopTimer()
1325
1326 pb := initGoTest(true)
1327
1328 // Create an array
1329 const N = 1000 // Internally the library starts much smaller.
1330 pb.F_Int32Repeated = make([]int32, N)
David Symondsd9da6ba2011-08-30 14:41:30 +10001331 pb.F_DoubleRepeated = make([]float64, N)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001332
1333 // Fill in the array with some values.
1334 for i := 0; i < N; i++ {
1335 pb.F_Int32Repeated[i] = int32(i)
David Symondsd9da6ba2011-08-30 14:41:30 +10001336 pb.F_DoubleRepeated[i] = float64(i)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001337 }
David Symondsd9da6ba2011-08-30 14:41:30 +10001338
Rob Pikeaaa3a622010-03-20 22:32:34 -07001339 p := NewBuffer(nil)
1340
1341 b.StartTimer()
1342 for i := 0; i < b.N; i++ {
1343 p.Reset()
1344 p.Marshal(pb)
1345 }
1346}
1347
1348func BenchmarkUnmarshal(b *testing.B) {
1349 b.StopTimer()
1350
1351 pb := initGoTest(true)
1352
1353 // Create an array
1354 const N = 1000 // Internally the library starts much smaller.
1355 pb.F_Int32Repeated = make([]int32, N)
1356
1357 // Fill in the array with some values.
1358 for i := 0; i < N; i++ {
1359 pb.F_Int32Repeated[i] = int32(i)
1360 }
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001361 pbd := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001362 p := NewBuffer(nil)
1363 p.Marshal(pb)
1364 p2 := NewBuffer(nil)
1365
1366 b.StartTimer()
1367 for i := 0; i < b.N; i++ {
1368 p2.SetBuf(p.Bytes())
1369 p2.Unmarshal(pbd)
1370 }
1371}