blob: 1ac9587bad157e3dc76e992590cf099c126b05e0 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2010 The Go Authors. All rights reserved.
Rob Pikeaaa3a622010-03-20 22:32:34 -07004// 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 -070032package proto_test
33
34import (
35 "bytes"
Rob Pike022b1012011-11-08 16:33:51 -080036 "encoding/json"
David Symonds29bcc892014-04-15 14:01:13 +100037 "errors"
Rob Pikeaaa3a622010-03-20 22:32:34 -070038 "fmt"
David Symondsb79d99b2011-08-29 16:38:49 +100039 "math"
David Symonds22ac1502012-01-18 12:37:12 +110040 "math/rand"
David Symonds5b7775e2010-12-01 10:09:04 +110041 "reflect"
David Symonds22ac1502012-01-18 12:37:12 +110042 "runtime/debug"
David Symonds5b7775e2010-12-01 10:09:04 +110043 "strings"
Rob Pikeaaa3a622010-03-20 22:32:34 -070044 "testing"
David Symonds22ac1502012-01-18 12:37:12 +110045 "time"
Rob Pikeaaa3a622010-03-20 22:32:34 -070046
David Symonds704096f2012-03-15 15:10:26 +110047 . "./testdata"
Rob Pike3f6f2d82011-12-18 13:55:35 -080048 . "code.google.com/p/goprotobuf/proto"
Rob Pikeaaa3a622010-03-20 22:32:34 -070049)
50
51var globalO *Buffer
52
53func old() *Buffer {
54 if globalO == nil {
55 globalO = NewBuffer(nil)
56 }
57 globalO.Reset()
58 return globalO
59}
60
61func equalbytes(b1, b2 []byte, t *testing.T) {
62 if len(b1) != len(b2) {
63 t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
64 return
65 }
66 for i := 0; i < len(b1); i++ {
67 if b1[i] != b2[i] {
68 t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
69 }
70 }
71}
72
73func initGoTestField() *GoTestField {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070074 f := new(GoTestField)
Rob Pikeaaa3a622010-03-20 22:32:34 -070075 f.Label = String("label")
76 f.Type = String("type")
77 return f
78}
79
80// These are all structurally equivalent but the tag numbers differ.
81// (It's remarkable that required, optional, and repeated all have
82// 8 letters.)
83func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070084 return &GoTest_RequiredGroup{
85 RequiredField: String("required"),
86 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070087}
88
89func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070090 return &GoTest_OptionalGroup{
91 RequiredField: String("optional"),
92 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070093}
94
95func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
Rob Pikec6d8e4a2010-07-28 15:34:32 -070096 return &GoTest_RepeatedGroup{
97 RequiredField: String("repeated"),
98 }
Rob Pikeaaa3a622010-03-20 22:32:34 -070099}
100
101func initGoTest(setdefaults bool) *GoTest {
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700102 pb := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700103 if setdefaults {
104 pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
105 pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
106 pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
107 pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
108 pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
109 pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
110 pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
111 pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
112 pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
113 pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
114 pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
115 pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
116 pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
117 }
118
David Symondsefeca9a2012-05-08 10:36:04 +1000119 pb.Kind = GoTest_TIME.Enum()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700120 pb.RequiredField = initGoTestField()
121 pb.F_BoolRequired = Bool(true)
122 pb.F_Int32Required = Int32(3)
123 pb.F_Int64Required = Int64(6)
124 pb.F_Fixed32Required = Uint32(32)
125 pb.F_Fixed64Required = Uint64(64)
126 pb.F_Uint32Required = Uint32(3232)
127 pb.F_Uint64Required = Uint64(6464)
128 pb.F_FloatRequired = Float32(3232)
129 pb.F_DoubleRequired = Float64(6464)
130 pb.F_StringRequired = String("string")
131 pb.F_BytesRequired = []byte("bytes")
132 pb.F_Sint32Required = Int32(-32)
133 pb.F_Sint64Required = Int64(-64)
134 pb.Requiredgroup = initGoTest_RequiredGroup()
135
136 return pb
137}
138
139func fail(msg string, b *bytes.Buffer, s string, t *testing.T) {
140 data := b.Bytes()
141 ld := len(data)
142 ls := len(s) / 2
143
144 fmt.Printf("fail %s ld=%d ls=%d\n", msg, ld, ls)
145
146 // find the interesting spot - n
147 n := ls
148 if ld < ls {
149 n = ld
150 }
151 j := 0
152 for i := 0; i < n; i++ {
153 bs := hex(s[j])*16 + hex(s[j+1])
154 j += 2
155 if data[i] == bs {
156 continue
157 }
158 n = i
159 break
160 }
161 l := n - 10
162 if l < 0 {
163 l = 0
164 }
165 h := n + 10
166
167 // find the interesting spot - n
168 fmt.Printf("is[%d]:", l)
169 for i := l; i < h; i++ {
170 if i >= ld {
171 fmt.Printf(" --")
172 continue
173 }
174 fmt.Printf(" %.2x", data[i])
175 }
176 fmt.Printf("\n")
177
178 fmt.Printf("sb[%d]:", l)
179 for i := l; i < h; i++ {
180 if i >= ls {
181 fmt.Printf(" --")
182 continue
183 }
184 bs := hex(s[j])*16 + hex(s[j+1])
185 j += 2
186 fmt.Printf(" %.2x", bs)
187 }
188 fmt.Printf("\n")
189
190 t.Fail()
191
Rob Pikeaaa3a622010-03-20 22:32:34 -0700192 // t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes())
193 // Print the output in a partially-decoded format; can
194 // be helpful when updating the test. It produces the output
195 // that is pasted, with minor edits, into the argument to verify().
196 // data := b.Bytes()
197 // nesting := 0
198 // for b.Len() > 0 {
199 // start := len(data) - b.Len()
200 // var u uint64
201 // u, err := DecodeVarint(b)
202 // if err != nil {
203 // fmt.Printf("decode error on varint:", err)
204 // return
205 // }
206 // wire := u & 0x7
207 // tag := u >> 3
208 // switch wire {
209 // case WireVarint:
210 // v, err := DecodeVarint(b)
211 // if err != nil {
212 // fmt.Printf("decode error on varint:", err)
213 // return
214 // }
215 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
216 // data[start:len(data)-b.Len()], tag, wire, v)
217 // case WireFixed32:
218 // v, err := DecodeFixed32(b)
219 // if err != nil {
220 // fmt.Printf("decode error on fixed32:", err)
221 // return
222 // }
223 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
224 // data[start:len(data)-b.Len()], tag, wire, v)
225 // case WireFixed64:
226 // v, err := DecodeFixed64(b)
227 // if err != nil {
228 // fmt.Printf("decode error on fixed64:", err)
229 // return
230 // }
231 // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
232 // data[start:len(data)-b.Len()], tag, wire, v)
233 // case WireBytes:
234 // nb, err := DecodeVarint(b)
235 // if err != nil {
236 // fmt.Printf("decode error on bytes:", err)
237 // return
238 // }
239 // after_tag := len(data) - b.Len()
240 // str := make([]byte, nb)
241 // _, err = b.Read(str)
242 // if err != nil {
243 // fmt.Printf("decode error on bytes:", err)
244 // return
245 // }
246 // fmt.Printf("\t\t\"%x\" \"%x\" // field %d, encoding %d (FIELD)\n",
247 // data[start:after_tag], str, tag, wire)
248 // case WireStartGroup:
249 // nesting++
250 // fmt.Printf("\t\t\"%x\"\t\t// start group field %d level %d\n",
251 // data[start:len(data)-b.Len()], tag, nesting)
252 // case WireEndGroup:
253 // fmt.Printf("\t\t\"%x\"\t\t// end group field %d level %d\n",
254 // data[start:len(data)-b.Len()], tag, nesting)
255 // nesting--
256 // default:
257 // fmt.Printf("unrecognized wire type %d\n", wire)
258 // return
259 // }
260 // }
261}
262
263func hex(c uint8) uint8 {
264 if '0' <= c && c <= '9' {
265 return c - '0'
266 }
267 if 'a' <= c && c <= 'f' {
268 return 10 + c - 'a'
269 }
270 if 'A' <= c && c <= 'F' {
271 return 10 + c - 'A'
272 }
273 return 0
274}
275
276func equal(b []byte, s string, t *testing.T) bool {
277 if 2*len(b) != len(s) {
278 // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
279 fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
280 return false
281 }
282 for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
283 x := hex(s[j])*16 + hex(s[j+1])
284 if b[i] != x {
285 // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
286 fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
287 return false
288 }
289 }
290 return true
291}
292
293func overify(t *testing.T, pb *GoTest, expected string) {
294 o := old()
295 err := o.Marshal(pb)
296 if err != nil {
297 fmt.Printf("overify marshal-1 err = %v", err)
298 o.DebugPrint("", o.Bytes())
299 t.Fatalf("expected = %s", expected)
300 }
301 if !equal(o.Bytes(), expected, t) {
302 o.DebugPrint("overify neq 1", o.Bytes())
303 t.Fatalf("expected = %s", expected)
304 }
305
306 // Now test Unmarshal by recreating the original buffer.
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700307 pbd := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700308 err = o.Unmarshal(pbd)
309 if err != nil {
310 t.Fatalf("overify unmarshal err = %v", err)
311 o.DebugPrint("", o.Bytes())
312 t.Fatalf("string = %s", expected)
313 }
314 o.Reset()
315 err = o.Marshal(pbd)
316 if err != nil {
317 t.Errorf("overify marshal-2 err = %v", err)
318 o.DebugPrint("", o.Bytes())
319 t.Fatalf("string = %s", expected)
320 }
321 if !equal(o.Bytes(), expected, t) {
322 o.DebugPrint("overify neq 2", o.Bytes())
323 t.Fatalf("string = %s", expected)
324 }
325}
326
327// Simple tests for numeric encode/decode primitives (varint, etc.)
328func TestNumericPrimitives(t *testing.T) {
329 for i := uint64(0); i < 1e6; i += 111 {
330 o := old()
331 if o.EncodeVarint(i) != nil {
332 t.Error("EncodeVarint")
333 break
334 }
335 x, e := o.DecodeVarint()
336 if e != nil {
337 t.Fatal("DecodeVarint")
338 }
339 if x != i {
340 t.Fatal("varint decode fail:", i, x)
341 }
342
343 o = old()
344 if o.EncodeFixed32(i) != nil {
345 t.Fatal("encFixed32")
346 }
347 x, e = o.DecodeFixed32()
348 if e != nil {
349 t.Fatal("decFixed32")
350 }
351 if x != i {
352 t.Fatal("fixed32 decode fail:", i, x)
353 }
354
355 o = old()
356 if o.EncodeFixed64(i*1234567) != nil {
357 t.Error("encFixed64")
358 break
359 }
360 x, e = o.DecodeFixed64()
361 if e != nil {
362 t.Error("decFixed64")
363 break
364 }
365 if x != i*1234567 {
366 t.Error("fixed64 decode fail:", i*1234567, x)
367 break
368 }
369
370 o = old()
371 i32 := int32(i - 12345)
372 if o.EncodeZigzag32(uint64(i32)) != nil {
373 t.Fatal("EncodeZigzag32")
374 }
375 x, e = o.DecodeZigzag32()
376 if e != nil {
377 t.Fatal("DecodeZigzag32")
378 }
379 if x != uint64(uint32(i32)) {
380 t.Fatal("zigzag32 decode fail:", i32, x)
381 }
382
383 o = old()
384 i64 := int64(i - 12345)
385 if o.EncodeZigzag64(uint64(i64)) != nil {
386 t.Fatal("EncodeZigzag64")
387 }
388 x, e = o.DecodeZigzag64()
389 if e != nil {
390 t.Fatal("DecodeZigzag64")
391 }
392 if x != uint64(i64) {
393 t.Fatal("zigzag64 decode fail:", i64, x)
394 }
395 }
396}
397
David Symonds29bcc892014-04-15 14:01:13 +1000398// fakeMarshaler is a simple struct implementing Marshaler and Message interfaces.
399type fakeMarshaler struct {
400 b []byte
401 err error
402}
403
404func (f fakeMarshaler) Marshal() ([]byte, error) {
405 return f.b, f.err
406}
407
408func (f fakeMarshaler) String() string {
409 return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err)
410}
411
412func (f fakeMarshaler) ProtoMessage() {}
413
414func (f fakeMarshaler) Reset() {}
415
416// Simple tests for proto messages that implement the Marshaler interface.
417func TestMarshalerEncoding(t *testing.T) {
418 tests := []struct {
419 name string
420 m Message
421 want []byte
422 wantErr error
423 }{
424 {
425 name: "Marshaler that fails",
426 m: fakeMarshaler{
427 err: errors.New("some marshal err"),
428 b: []byte{5, 6, 7},
429 },
430 // Since there's an error, nothing should be written to buffer.
431 want: nil,
432 wantErr: errors.New("some marshal err"),
433 },
434 {
435 name: "Marshaler that succeeds",
436 m: fakeMarshaler{
437 b: []byte{0, 1, 2, 3, 4, 127, 255},
438 },
439 want: []byte{0, 1, 2, 3, 4, 127, 255},
440 wantErr: nil,
441 },
442 }
443 for _, test := range tests {
444 b := NewBuffer(nil)
445 err := b.Marshal(test.m)
446 if !reflect.DeepEqual(test.wantErr, err) {
447 t.Errorf("%s: got err %v wanted %v", test.name, err, test.wantErr)
448 }
449 if !reflect.DeepEqual(test.want, b.Bytes()) {
450 t.Errorf("%s: got bytes %v wanted %v", test.name, b.Bytes(), test.want)
451 }
452 }
453}
454
Rob Pikeaaa3a622010-03-20 22:32:34 -0700455// Simple tests for bytes
456func TestBytesPrimitives(t *testing.T) {
457 o := old()
458 bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
459 if o.EncodeRawBytes(bytes) != nil {
460 t.Error("EncodeRawBytes")
461 }
462 decb, e := o.DecodeRawBytes(false)
463 if e != nil {
464 t.Error("DecodeRawBytes")
465 }
466 equalbytes(bytes, decb, t)
467}
468
469// Simple tests for strings
470func TestStringPrimitives(t *testing.T) {
471 o := old()
472 s := "now is the time"
473 if o.EncodeStringBytes(s) != nil {
474 t.Error("enc_string")
475 }
476 decs, e := o.DecodeStringBytes()
477 if e != nil {
478 t.Error("dec_string")
479 }
480 if s != decs {
481 t.Error("string encode/decode fail:", s, decs)
482 }
483}
484
485// Do we catch the "required bit not set" case?
486func TestRequiredBit(t *testing.T) {
487 o := old()
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700488 pb := new(GoTest)
David Symonds5b7775e2010-12-01 10:09:04 +1100489 err := o.Marshal(pb)
490 if err == nil {
491 t.Error("did not catch missing required fields")
David Symonds4646c372013-09-09 13:18:58 +1000492 } else if strings.Index(err.Error(), "Kind") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +1100493 t.Error("wrong error type:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700494 }
495}
496
497// Check that all fields are nil.
498// Clearly silly, and a residue from a more interesting test with an earlier,
499// different initialization property, but it once caught a compiler bug so
500// it lives.
501func checkInitialized(pb *GoTest, t *testing.T) {
502 if pb.F_BoolDefaulted != nil {
503 t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
504 }
505 if pb.F_Int32Defaulted != nil {
506 t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
507 }
508 if pb.F_Int64Defaulted != nil {
509 t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
510 }
511 if pb.F_Fixed32Defaulted != nil {
512 t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
513 }
514 if pb.F_Fixed64Defaulted != nil {
515 t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
516 }
517 if pb.F_Uint32Defaulted != nil {
518 t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
519 }
520 if pb.F_Uint64Defaulted != nil {
521 t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
522 }
523 if pb.F_FloatDefaulted != nil {
524 t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
525 }
526 if pb.F_DoubleDefaulted != nil {
527 t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
528 }
529 if pb.F_StringDefaulted != nil {
530 t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
531 }
532 if pb.F_BytesDefaulted != nil {
533 t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
534 }
535 if pb.F_Sint32Defaulted != nil {
536 t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
537 }
538 if pb.F_Sint64Defaulted != nil {
539 t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
540 }
541}
542
543// Does Reset() reset?
544func TestReset(t *testing.T) {
545 pb := initGoTest(true)
546 // muck with some values
547 pb.F_BoolDefaulted = Bool(false)
548 pb.F_Int32Defaulted = Int32(237)
549 pb.F_Int64Defaulted = Int64(12346)
550 pb.F_Fixed32Defaulted = Uint32(32000)
551 pb.F_Fixed64Defaulted = Uint64(666)
552 pb.F_Uint32Defaulted = Uint32(323232)
553 pb.F_Uint64Defaulted = nil
554 pb.F_FloatDefaulted = nil
555 pb.F_DoubleDefaulted = Float64(0)
556 pb.F_StringDefaulted = String("gotcha")
557 pb.F_BytesDefaulted = []byte("asdfasdf")
558 pb.F_Sint32Defaulted = Int32(123)
559 pb.F_Sint64Defaulted = Int64(789)
560 pb.Reset()
561 checkInitialized(pb, t)
562}
563
564// All required fields set, no defaults provided.
565func TestEncodeDecode1(t *testing.T) {
566 pb := initGoTest(false)
567 overify(t, pb,
568 "0807"+ // field 1, encoding 0, value 7
569 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
570 "5001"+ // field 10, encoding 0, value 1
571 "5803"+ // field 11, encoding 0, value 3
572 "6006"+ // field 12, encoding 0, value 6
573 "6d20000000"+ // field 13, encoding 5, value 0x20
574 "714000000000000000"+ // field 14, encoding 1, value 0x40
575 "78a019"+ // field 15, encoding 0, value 0xca0 = 3232
576 "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
577 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
578 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
579 "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700580 "b304"+ // field 70, encoding 3, start group
581 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700582 "b404"+ // field 70, encoding 4, end group
583 "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
584 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
585 "b8067f") // field 103, encoding 0, 0x7f zigzag64
Rob Pikeaaa3a622010-03-20 22:32:34 -0700586}
587
588// All required fields set, defaults provided.
589func TestEncodeDecode2(t *testing.T) {
590 pb := initGoTest(true)
591 overify(t, pb,
592 "0807"+ // field 1, encoding 0, value 7
593 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
594 "5001"+ // field 10, encoding 0, value 1
595 "5803"+ // field 11, encoding 0, value 3
596 "6006"+ // field 12, encoding 0, value 6
597 "6d20000000"+ // field 13, encoding 5, value 32
598 "714000000000000000"+ // field 14, encoding 1, value 64
599 "78a019"+ // field 15, encoding 0, value 3232
600 "8001c032"+ // field 16, encoding 0, value 6464
601 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
602 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
603 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700604 "c00201"+ // field 40, encoding 0, value 1
605 "c80220"+ // field 41, encoding 0, value 32
606 "d00240"+ // field 42, encoding 0, value 64
607 "dd0240010000"+ // field 43, encoding 5, value 320
608 "e1028002000000000000"+ // field 44, encoding 1, value 640
609 "e8028019"+ // field 45, encoding 0, value 3200
610 "f0028032"+ // field 46, encoding 0, value 6400
611 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
612 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
613 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700614 "b304"+ // start group field 70 level 1
615 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700616 "b404"+ // end group field 70 level 1
617 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
618 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
619 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
620 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
621 "90193f"+ // field 402, encoding 0, value 63
622 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700623
624}
625
626// All default fields set to their default value by hand
627func TestEncodeDecode3(t *testing.T) {
628 pb := initGoTest(false)
629 pb.F_BoolDefaulted = Bool(true)
630 pb.F_Int32Defaulted = Int32(32)
631 pb.F_Int64Defaulted = Int64(64)
632 pb.F_Fixed32Defaulted = Uint32(320)
633 pb.F_Fixed64Defaulted = Uint64(640)
634 pb.F_Uint32Defaulted = Uint32(3200)
635 pb.F_Uint64Defaulted = Uint64(6400)
636 pb.F_FloatDefaulted = Float32(314159)
637 pb.F_DoubleDefaulted = Float64(271828)
638 pb.F_StringDefaulted = String("hello, \"world!\"\n")
639 pb.F_BytesDefaulted = []byte("Bignose")
640 pb.F_Sint32Defaulted = Int32(-32)
641 pb.F_Sint64Defaulted = Int64(-64)
642
643 overify(t, pb,
644 "0807"+ // field 1, encoding 0, value 7
645 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
646 "5001"+ // field 10, encoding 0, value 1
647 "5803"+ // field 11, encoding 0, value 3
648 "6006"+ // field 12, encoding 0, value 6
649 "6d20000000"+ // field 13, encoding 5, value 32
650 "714000000000000000"+ // field 14, encoding 1, value 64
651 "78a019"+ // field 15, encoding 0, value 3232
652 "8001c032"+ // field 16, encoding 0, value 6464
653 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
654 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
655 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700656 "c00201"+ // field 40, encoding 0, value 1
657 "c80220"+ // field 41, encoding 0, value 32
658 "d00240"+ // field 42, encoding 0, value 64
659 "dd0240010000"+ // field 43, encoding 5, value 320
660 "e1028002000000000000"+ // field 44, encoding 1, value 640
661 "e8028019"+ // field 45, encoding 0, value 3200
662 "f0028032"+ // field 46, encoding 0, value 6400
663 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
664 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
665 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700666 "b304"+ // start group field 70 level 1
667 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
David Symondsd15e81b2011-10-03 14:31:12 -0700668 "b404"+ // end group field 70 level 1
669 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
670 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
671 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
672 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
673 "90193f"+ // field 402, encoding 0, value 63
674 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700675
676}
677
678// All required fields set, defaults provided, all non-defaulted optional fields have values.
679func TestEncodeDecode4(t *testing.T) {
680 pb := initGoTest(true)
681 pb.Table = String("hello")
682 pb.Param = Int32(7)
683 pb.OptionalField = initGoTestField()
684 pb.F_BoolOptional = Bool(true)
685 pb.F_Int32Optional = Int32(32)
686 pb.F_Int64Optional = Int64(64)
687 pb.F_Fixed32Optional = Uint32(3232)
688 pb.F_Fixed64Optional = Uint64(6464)
689 pb.F_Uint32Optional = Uint32(323232)
690 pb.F_Uint64Optional = Uint64(646464)
691 pb.F_FloatOptional = Float32(32.)
692 pb.F_DoubleOptional = Float64(64.)
693 pb.F_StringOptional = String("hello")
694 pb.F_BytesOptional = []byte("Bignose")
695 pb.F_Sint32Optional = Int32(-32)
696 pb.F_Sint64Optional = Int64(-64)
697 pb.Optionalgroup = initGoTest_OptionalGroup()
698
699 overify(t, pb,
700 "0807"+ // field 1, encoding 0, value 7
701 "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
702 "1807"+ // field 3, encoding 0, value 7
703 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
704 "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
705 "5001"+ // field 10, encoding 0, value 1
706 "5803"+ // field 11, encoding 0, value 3
707 "6006"+ // field 12, encoding 0, value 6
708 "6d20000000"+ // field 13, encoding 5, value 32
709 "714000000000000000"+ // field 14, encoding 1, value 64
710 "78a019"+ // field 15, encoding 0, value 3232
711 "8001c032"+ // field 16, encoding 0, value 6464
712 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
713 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
714 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700715 "f00101"+ // field 30, encoding 0, value 1
716 "f80120"+ // field 31, encoding 0, value 32
717 "800240"+ // field 32, encoding 0, value 64
718 "8d02a00c0000"+ // field 33, encoding 5, value 3232
719 "91024019000000000000"+ // field 34, encoding 1, value 6464
720 "9802a0dd13"+ // field 35, encoding 0, value 323232
721 "a002c0ba27"+ // field 36, encoding 0, value 646464
722 "ad0200000042"+ // field 37, encoding 5, value 32.0
723 "b1020000000000005040"+ // field 38, encoding 1, value 64.0
724 "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700725 "c00201"+ // field 40, encoding 0, value 1
726 "c80220"+ // field 41, encoding 0, value 32
727 "d00240"+ // field 42, encoding 0, value 64
728 "dd0240010000"+ // field 43, encoding 5, value 320
729 "e1028002000000000000"+ // field 44, encoding 1, value 640
730 "e8028019"+ // field 45, encoding 0, value 3200
731 "f0028032"+ // field 46, encoding 0, value 6400
732 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
733 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
734 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700735 "b304"+ // start group field 70 level 1
736 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
737 "b404"+ // end group field 70 level 1
738 "d305"+ // start group field 90 level 1
739 "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
David Symondsd15e81b2011-10-03 14:31:12 -0700740 "d405"+ // end group field 90 level 1
741 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
742 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
743 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
744 "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
745 "f0123f"+ // field 302, encoding 0, value 63
746 "f8127f"+ // field 303, encoding 0, value 127
747 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
748 "90193f"+ // field 402, encoding 0, value 63
749 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700750
751}
752
753// All required fields set, defaults provided, all repeated fields given two values.
754func TestEncodeDecode5(t *testing.T) {
755 pb := initGoTest(true)
756 pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
757 pb.F_BoolRepeated = []bool{false, true}
758 pb.F_Int32Repeated = []int32{32, 33}
759 pb.F_Int64Repeated = []int64{64, 65}
760 pb.F_Fixed32Repeated = []uint32{3232, 3333}
761 pb.F_Fixed64Repeated = []uint64{6464, 6565}
762 pb.F_Uint32Repeated = []uint32{323232, 333333}
763 pb.F_Uint64Repeated = []uint64{646464, 656565}
764 pb.F_FloatRepeated = []float32{32., 33.}
765 pb.F_DoubleRepeated = []float64{64., 65.}
766 pb.F_StringRepeated = []string{"hello", "sailor"}
767 pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
768 pb.F_Sint32Repeated = []int32{32, -32}
769 pb.F_Sint64Repeated = []int64{64, -64}
770 pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
771
772 overify(t, pb,
773 "0807"+ // field 1, encoding 0, value 7
774 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
775 "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
776 "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
777 "5001"+ // field 10, encoding 0, value 1
778 "5803"+ // field 11, encoding 0, value 3
779 "6006"+ // field 12, encoding 0, value 6
780 "6d20000000"+ // field 13, encoding 5, value 32
781 "714000000000000000"+ // field 14, encoding 1, value 64
782 "78a019"+ // field 15, encoding 0, value 3232
783 "8001c032"+ // field 16, encoding 0, value 6464
784 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
785 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
786 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700787 "a00100"+ // field 20, encoding 0, value 0
788 "a00101"+ // field 20, encoding 0, value 1
789 "a80120"+ // field 21, encoding 0, value 32
790 "a80121"+ // field 21, encoding 0, value 33
791 "b00140"+ // field 22, encoding 0, value 64
792 "b00141"+ // field 22, encoding 0, value 65
793 "bd01a00c0000"+ // field 23, encoding 5, value 3232
794 "bd01050d0000"+ // field 23, encoding 5, value 3333
795 "c1014019000000000000"+ // field 24, encoding 1, value 6464
796 "c101a519000000000000"+ // field 24, encoding 1, value 6565
797 "c801a0dd13"+ // field 25, encoding 0, value 323232
798 "c80195ac14"+ // field 25, encoding 0, value 333333
799 "d001c0ba27"+ // field 26, encoding 0, value 646464
800 "d001b58928"+ // field 26, encoding 0, value 656565
801 "dd0100000042"+ // field 27, encoding 5, value 32.0
802 "dd0100000442"+ // field 27, encoding 5, value 33.0
803 "e1010000000000005040"+ // field 28, encoding 1, value 64.0
804 "e1010000000000405040"+ // field 28, encoding 1, value 65.0
805 "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
806 "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700807 "c00201"+ // field 40, encoding 0, value 1
808 "c80220"+ // field 41, encoding 0, value 32
809 "d00240"+ // field 42, encoding 0, value 64
810 "dd0240010000"+ // field 43, encoding 5, value 320
811 "e1028002000000000000"+ // field 44, encoding 1, value 640
812 "e8028019"+ // field 45, encoding 0, value 3200
813 "f0028032"+ // field 46, encoding 0, value 6400
814 "fd02e0659948"+ // field 47, encoding 5, value 314159.0
815 "81030000000050971041"+ // field 48, encoding 1, value 271828.0
816 "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
Rob Pikeaaa3a622010-03-20 22:32:34 -0700817 "b304"+ // start group field 70 level 1
818 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
819 "b404"+ // end group field 70 level 1
820 "8305"+ // start group field 80 level 1
821 "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
822 "8405"+ // end group field 80 level 1
823 "8305"+ // start group field 80 level 1
824 "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
David Symondsd15e81b2011-10-03 14:31:12 -0700825 "8405"+ // end group field 80 level 1
826 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
827 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
828 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
829 "ca0c03"+"626967"+ // field 201, encoding 2, string "big"
830 "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
831 "d00c40"+ // field 202, encoding 0, value 32
832 "d00c3f"+ // field 202, encoding 0, value -32
833 "d80c8001"+ // field 203, encoding 0, value 64
834 "d80c7f"+ // field 203, encoding 0, value -64
835 "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
836 "90193f"+ // field 402, encoding 0, value 63
837 "98197f") // field 403, encoding 0, value 127
Rob Pikeaaa3a622010-03-20 22:32:34 -0700838
839}
840
David Symonds5b7775e2010-12-01 10:09:04 +1100841// All required fields set, all packed repeated fields given two values.
842func TestEncodeDecode6(t *testing.T) {
843 pb := initGoTest(false)
844 pb.F_BoolRepeatedPacked = []bool{false, true}
845 pb.F_Int32RepeatedPacked = []int32{32, 33}
846 pb.F_Int64RepeatedPacked = []int64{64, 65}
847 pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
848 pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
849 pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
850 pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
851 pb.F_FloatRepeatedPacked = []float32{32., 33.}
852 pb.F_DoubleRepeatedPacked = []float64{64., 65.}
853 pb.F_Sint32RepeatedPacked = []int32{32, -32}
854 pb.F_Sint64RepeatedPacked = []int64{64, -64}
855
856 overify(t, pb,
857 "0807"+ // field 1, encoding 0, value 7
858 "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
859 "5001"+ // field 10, encoding 0, value 1
860 "5803"+ // field 11, encoding 0, value 3
861 "6006"+ // field 12, encoding 0, value 6
862 "6d20000000"+ // field 13, encoding 5, value 32
863 "714000000000000000"+ // field 14, encoding 1, value 64
864 "78a019"+ // field 15, encoding 0, value 3232
865 "8001c032"+ // field 16, encoding 0, value 6464
866 "8d0100004a45"+ // field 17, encoding 5, value 3232.0
867 "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
868 "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
David Symonds5b7775e2010-12-01 10:09:04 +1100869 "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
870 "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
871 "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
872 "aa0308"+ // field 53, encoding 2, 8 bytes
873 "a00c0000050d0000"+ // value 3232, value 3333
874 "b20310"+ // field 54, encoding 2, 16 bytes
875 "4019000000000000a519000000000000"+ // value 6464, value 6565
876 "ba0306"+ // field 55, encoding 2, 6 bytes
877 "a0dd1395ac14"+ // value 323232, value 333333
878 "c20306"+ // field 56, encoding 2, 6 bytes
879 "c0ba27b58928"+ // value 646464, value 656565
880 "ca0308"+ // field 57, encoding 2, 8 bytes
881 "0000004200000442"+ // value 32.0, value 33.0
882 "d20310"+ // field 58, encoding 2, 16 bytes
883 "00000000000050400000000000405040"+ // value 64.0, value 65.0
David Symondsd15e81b2011-10-03 14:31:12 -0700884 "b304"+ // start group field 70 level 1
885 "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
886 "b404"+ // end group field 70 level 1
887 "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
888 "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
889 "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
David Symonds5b7775e2010-12-01 10:09:04 +1100890 "b21f02"+ // field 502, encoding 2, 2 bytes
891 "403f"+ // value 32, value -32
892 "ba1f03"+ // field 503, encoding 2, 3 bytes
David Symondsd15e81b2011-10-03 14:31:12 -0700893 "80017f") // value 64, value -64
David Symonds5b7775e2010-12-01 10:09:04 +1100894}
895
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800896// Test that we can encode empty bytes fields.
897func TestEncodeDecodeBytes1(t *testing.T) {
898 pb := initGoTest(false)
899
900 // Create our bytes
901 pb.F_BytesRequired = []byte{}
David Symondsd9da6ba2011-08-30 14:41:30 +1000902 pb.F_BytesRepeated = [][]byte{{}}
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800903 pb.F_BytesOptional = []byte{}
904
905 d, err := Marshal(pb)
906 if err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700907 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800908 }
909
910 pbd := new(GoTest)
911 if err := Unmarshal(d, pbd); err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700912 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800913 }
914
915 if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
Rob Pikea17fdd92011-11-02 12:43:05 -0700916 t.Error("required empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800917 }
918 if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700919 t.Error("repeated empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800920 }
921 if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
Rob Pikea17fdd92011-11-02 12:43:05 -0700922 t.Error("optional empty bytes field is incorrect")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800923 }
924}
925
926// Test that we encode nil-valued fields of a repeated bytes field correctly.
927// Since entries in a repeated field cannot be nil, nil must mean empty value.
928func TestEncodeDecodeBytes2(t *testing.T) {
929 pb := initGoTest(false)
930
931 // Create our bytes
David Symonds5b7775e2010-12-01 10:09:04 +1100932 pb.F_BytesRepeated = [][]byte{nil}
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800933
934 d, err := Marshal(pb)
David Symonds5b7775e2010-12-01 10:09:04 +1100935 if err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700936 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800937 }
938
939 pbd := new(GoTest)
940 if err := Unmarshal(d, pbd); err != nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700941 t.Error(err)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800942 }
943
944 if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
Rob Pikea17fdd92011-11-02 12:43:05 -0700945 t.Error("Unexpected value for repeated bytes field")
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800946 }
947}
948
Rob Pikeaaa3a622010-03-20 22:32:34 -0700949// All required fields set, defaults provided, all repeated fields given two values.
950func TestSkippingUnrecognizedFields(t *testing.T) {
951 o := old()
952 pb := initGoTestField()
953
954 // Marshal it normally.
955 o.Marshal(pb)
956
957 // Now new a GoSkipTest record.
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700958 skip := &GoSkipTest{
959 SkipInt32: Int32(32),
960 SkipFixed32: Uint32(3232),
961 SkipFixed64: Uint64(6464),
962 SkipString: String("skipper"),
963 Skipgroup: &GoSkipTest_SkipGroup{
964 GroupInt32: Int32(75),
965 GroupString: String("wxyz"),
966 },
967 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700968
969 // Marshal it into same buffer.
970 o.Marshal(skip)
971
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700972 pbd := new(GoTestField)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700973 o.Unmarshal(pbd)
974
975 // The __unrecognized field should be a marshaling of GoSkipTest
Rob Pikec6d8e4a2010-07-28 15:34:32 -0700976 skipd := new(GoSkipTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700977
978 o.SetBuf(pbd.XXX_unrecognized)
979 o.Unmarshal(skipd)
980
981 if *skipd.SkipInt32 != *skip.SkipInt32 {
982 t.Error("skip int32", skipd.SkipInt32)
983 }
984 if *skipd.SkipFixed32 != *skip.SkipFixed32 {
985 t.Error("skip fixed32", skipd.SkipFixed32)
986 }
987 if *skipd.SkipFixed64 != *skip.SkipFixed64 {
988 t.Error("skip fixed64", skipd.SkipFixed64)
989 }
990 if *skipd.SkipString != *skip.SkipString {
991 t.Error("skip string", *skipd.SkipString)
992 }
993 if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
994 t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
995 }
996 if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
997 t.Error("skip group string", *skipd.Skipgroup.GroupString)
998 }
999}
1000
David Symonds10c93ba2012-08-04 16:38:08 +10001001// Check that unrecognized fields of a submessage are preserved.
1002func TestSubmessageUnrecognizedFields(t *testing.T) {
1003 nm := &NewMessage{
1004 Nested: &NewMessage_Nested{
1005 Name: String("Nigel"),
1006 FoodGroup: String("carbs"),
1007 },
1008 }
1009 b, err := Marshal(nm)
1010 if err != nil {
1011 t.Fatalf("Marshal of NewMessage: %v", err)
1012 }
1013
1014 // Unmarshal into an OldMessage.
1015 om := new(OldMessage)
1016 if err := Unmarshal(b, om); err != nil {
1017 t.Fatalf("Unmarshal to OldMessage: %v", err)
1018 }
1019 exp := &OldMessage{
1020 Nested: &OldMessage_Nested{
1021 Name: String("Nigel"),
1022 // normal protocol buffer users should not do this
1023 XXX_unrecognized: []byte("\x12\x05carbs"),
1024 },
1025 }
1026 if !Equal(om, exp) {
1027 t.Errorf("om = %v, want %v", om, exp)
1028 }
1029
1030 // Clone the OldMessage.
1031 om = Clone(om).(*OldMessage)
1032 if !Equal(om, exp) {
1033 t.Errorf("Clone(om) = %v, want %v", om, exp)
1034 }
1035
1036 // Marshal the OldMessage, then unmarshal it into an empty NewMessage.
1037 if b, err = Marshal(om); err != nil {
1038 t.Fatalf("Marshal of OldMessage: %v", err)
1039 }
1040 t.Logf("Marshal(%v) -> %q", om, b)
1041 nm2 := new(NewMessage)
1042 if err := Unmarshal(b, nm2); err != nil {
1043 t.Fatalf("Unmarshal to NewMessage: %v", err)
1044 }
1045 if !Equal(nm, nm2) {
1046 t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
1047 }
1048}
1049
David Symondsf054e842014-07-22 14:06:27 +10001050// Check that an int32 field can be upgraded to an int64 field.
1051func TestNegativeInt32(t *testing.T) {
1052 om := &OldMessage{
1053 Num: Int32(-1),
1054 }
1055 b, err := Marshal(om)
1056 if err != nil {
1057 t.Fatalf("Marshal of OldMessage: %v", err)
1058 }
1059
1060 // Check the size. It should be 11 bytes;
1061 // 1 for the field/wire type, and 10 for the negative number.
1062 if len(b) != 11 {
1063 t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
1064 }
1065
1066 // Unmarshal into a NewMessage.
1067 nm := new(NewMessage)
1068 if err := Unmarshal(b, nm); err != nil {
1069 t.Fatalf("Unmarshal to NewMessage: %v", err)
1070 }
1071 want := &NewMessage{
1072 Num: Int64(-1),
1073 }
1074 if !Equal(nm, want) {
1075 t.Errorf("nm = %v, want %v", nm, want)
1076 }
1077}
1078
Rob Pikeaaa3a622010-03-20 22:32:34 -07001079// Check that we can grow an array (repeated field) to have many elements.
1080// This test doesn't depend only on our encoding; for variety, it makes sure
1081// we create, encode, and decode the correct contents explicitly. It's therefore
1082// a bit messier.
1083// This test also uses (and hence tests) the Marshal/Unmarshal functions
1084// instead of the methods.
1085func TestBigRepeated(t *testing.T) {
1086 pb := initGoTest(true)
1087
1088 // Create the arrays
1089 const N = 50 // Internally the library starts much smaller.
1090 pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
1091 pb.F_Sint64Repeated = make([]int64, N)
1092 pb.F_Sint32Repeated = make([]int32, N)
1093 pb.F_BytesRepeated = make([][]byte, N)
1094 pb.F_StringRepeated = make([]string, N)
1095 pb.F_DoubleRepeated = make([]float64, N)
1096 pb.F_FloatRepeated = make([]float32, N)
1097 pb.F_Uint64Repeated = make([]uint64, N)
1098 pb.F_Uint32Repeated = make([]uint32, N)
1099 pb.F_Fixed64Repeated = make([]uint64, N)
1100 pb.F_Fixed32Repeated = make([]uint32, N)
1101 pb.F_Int64Repeated = make([]int64, N)
1102 pb.F_Int32Repeated = make([]int32, N)
1103 pb.F_BoolRepeated = make([]bool, N)
1104 pb.RepeatedField = make([]*GoTestField, N)
1105
1106 // Fill in the arrays with checkable values.
1107 igtf := initGoTestField()
1108 igtrg := initGoTest_RepeatedGroup()
1109 for i := 0; i < N; i++ {
1110 pb.Repeatedgroup[i] = igtrg
1111 pb.F_Sint64Repeated[i] = int64(i)
1112 pb.F_Sint32Repeated[i] = int32(i)
1113 s := fmt.Sprint(i)
1114 pb.F_BytesRepeated[i] = []byte(s)
1115 pb.F_StringRepeated[i] = s
1116 pb.F_DoubleRepeated[i] = float64(i)
1117 pb.F_FloatRepeated[i] = float32(i)
1118 pb.F_Uint64Repeated[i] = uint64(i)
1119 pb.F_Uint32Repeated[i] = uint32(i)
1120 pb.F_Fixed64Repeated[i] = uint64(i)
1121 pb.F_Fixed32Repeated[i] = uint32(i)
1122 pb.F_Int64Repeated[i] = int64(i)
1123 pb.F_Int32Repeated[i] = int32(i)
1124 pb.F_BoolRepeated[i] = i%2 == 0
1125 pb.RepeatedField[i] = igtf
1126 }
1127
1128 // Marshal.
1129 buf, _ := Marshal(pb)
1130
1131 // Now test Unmarshal by recreating the original buffer.
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001132 pbd := new(GoTest)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001133 Unmarshal(buf, pbd)
1134
1135 // Check the checkable values
1136 for i := uint64(0); i < N; i++ {
1137 if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
1138 t.Error("pbd.Repeatedgroup bad")
1139 }
1140 var x uint64
1141 x = uint64(pbd.F_Sint64Repeated[i])
1142 if x != i {
1143 t.Error("pbd.F_Sint64Repeated bad", x, i)
1144 }
1145 x = uint64(pbd.F_Sint32Repeated[i])
1146 if x != i {
1147 t.Error("pbd.F_Sint32Repeated bad", x, i)
1148 }
1149 s := fmt.Sprint(i)
1150 equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
1151 if pbd.F_StringRepeated[i] != s {
1152 t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
1153 }
1154 x = uint64(pbd.F_DoubleRepeated[i])
1155 if x != i {
1156 t.Error("pbd.F_DoubleRepeated bad", x, i)
1157 }
1158 x = uint64(pbd.F_FloatRepeated[i])
1159 if x != i {
1160 t.Error("pbd.F_FloatRepeated bad", x, i)
1161 }
1162 x = pbd.F_Uint64Repeated[i]
1163 if x != i {
1164 t.Error("pbd.F_Uint64Repeated bad", x, i)
1165 }
1166 x = uint64(pbd.F_Uint32Repeated[i])
1167 if x != i {
1168 t.Error("pbd.F_Uint32Repeated bad", x, i)
1169 }
1170 x = pbd.F_Fixed64Repeated[i]
1171 if x != i {
1172 t.Error("pbd.F_Fixed64Repeated bad", x, i)
1173 }
1174 x = uint64(pbd.F_Fixed32Repeated[i])
1175 if x != i {
1176 t.Error("pbd.F_Fixed32Repeated bad", x, i)
1177 }
1178 x = uint64(pbd.F_Int64Repeated[i])
1179 if x != i {
1180 t.Error("pbd.F_Int64Repeated bad", x, i)
1181 }
1182 x = uint64(pbd.F_Int32Repeated[i])
1183 if x != i {
1184 t.Error("pbd.F_Int32Repeated bad", x, i)
1185 }
1186 if pbd.F_BoolRepeated[i] != (i%2 == 0) {
1187 t.Error("pbd.F_BoolRepeated bad", x, i)
1188 }
1189 if pbd.RepeatedField[i] == nil { // TODO: more checking?
1190 t.Error("pbd.RepeatedField bad")
1191 }
1192 }
1193}
1194
1195// Verify we give a useful message when decoding to the wrong structure type.
1196func TestTypeMismatch(t *testing.T) {
1197 pb1 := initGoTest(true)
1198
1199 // Marshal
1200 o := old()
1201 o.Marshal(pb1)
1202
1203 // Now Unmarshal it to the wrong type.
1204 pb2 := initGoTestField()
1205 err := o.Unmarshal(pb2)
David Symondsbaeae8b2014-06-23 09:41:27 +10001206 if err == nil {
1207 t.Error("expected error, got no error")
1208 } else if !strings.Contains(err.Error(), "bad wiretype") {
1209 t.Error("expected bad wiretype error, got", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001210 }
1211}
1212
David Symonds9f60f432012-06-14 09:45:25 +10001213func encodeDecode(t *testing.T, in, out Message, msg string) {
David Symonds5b7775e2010-12-01 10:09:04 +11001214 buf, err := Marshal(in)
1215 if err != nil {
1216 t.Fatalf("failed marshaling %v: %v", msg, err)
1217 }
1218 if err := Unmarshal(buf, out); err != nil {
1219 t.Fatalf("failed unmarshaling %v: %v", msg, err)
1220 }
1221}
1222
1223func TestPackedNonPackedDecoderSwitching(t *testing.T) {
1224 np, p := new(NonPackedTest), new(PackedTest)
1225
1226 // non-packed -> packed
1227 np.A = []int32{0, 1, 1, 2, 3, 5}
1228 encodeDecode(t, np, p, "non-packed -> packed")
1229 if !reflect.DeepEqual(np.A, p.B) {
1230 t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
1231 }
1232
1233 // packed -> non-packed
1234 np.Reset()
1235 p.B = []int32{3, 1, 4, 1, 5, 9}
1236 encodeDecode(t, p, np, "packed -> non-packed")
1237 if !reflect.DeepEqual(p.B, np.A) {
1238 t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
1239 }
1240}
1241
Rob Pikeaaa3a622010-03-20 22:32:34 -07001242func TestProto1RepeatedGroup(t *testing.T) {
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001243 pb := &MessageList{
1244 Message: []*MessageList_Message{
Albert Strasheim4676f6a2013-04-07 08:59:06 +10001245 {
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001246 Name: String("blah"),
1247 Count: Int32(7),
1248 },
1249 // NOTE: pb.Message[1] is a nil
1250 nil,
1251 },
1252 }
Rob Pikeaaa3a622010-03-20 22:32:34 -07001253
1254 o := old()
1255 if err := o.Marshal(pb); err != ErrRepeatedHasNil {
1256 t.Fatalf("unexpected or no error when marshaling: %v", err)
1257 }
1258}
1259
Rob Pikeaaa3a622010-03-20 22:32:34 -07001260// Test that enums work. Checks for a bug introduced by making enums
1261// named types instead of int32: newInt32FromUint64 would crash with
1262// a type mismatch in reflect.PointTo.
1263func TestEnum(t *testing.T) {
1264 pb := new(GoEnum)
David Symondsefeca9a2012-05-08 10:36:04 +10001265 pb.Foo = FOO_FOO1.Enum()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001266 o := old()
1267 if err := o.Marshal(pb); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001268 t.Fatal("error encoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001269 }
1270 pb1 := new(GoEnum)
1271 if err := o.Unmarshal(pb1); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001272 t.Fatal("error decoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001273 }
1274 if *pb1.Foo != FOO_FOO1 {
Rob Pike853f9112010-12-17 13:56:46 -08001275 t.Error("expected 7 but got ", *pb1.Foo)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001276 }
1277}
1278
David Symondse37856c2011-06-22 12:52:53 +10001279// Enum types have String methods. Check that enum fields can be printed.
1280// We don't care what the value actually is, just as long as it doesn't crash.
1281func TestPrintingNilEnumFields(t *testing.T) {
1282 pb := new(GoEnum)
1283 fmt.Sprintf("%+v", pb)
1284}
1285
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001286// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1287func TestRequiredFieldEnforcement(t *testing.T) {
1288 pb := new(GoTestField)
1289 _, err := Marshal(pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001290 if err == nil {
1291 t.Error("marshal: expected error, got nil")
David Symonds4646c372013-09-09 13:18:58 +10001292 } else if strings.Index(err.Error(), "Label") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001293 t.Errorf("marshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001294 }
1295
1296 // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1297 // so simply counting the required fields is insufficient.
1298 // field 1, encoding 2, value "hi"
1299 buf := []byte("\x0A\x02hi\x0A\x02hi")
1300 err = Unmarshal(buf, pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001301 if err == nil {
1302 t.Error("unmarshal: expected error, got nil")
David Symonds4646c372013-09-09 13:18:58 +10001303 } else if strings.Index(err.Error(), "{Unknown}") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001304 t.Errorf("unmarshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001305 }
1306}
1307
David Symondsd4661c52012-08-30 15:17:53 +10001308func TestTypedNilMarshal(t *testing.T) {
1309 // A typed nil should return ErrNil and not crash.
1310 _, err := Marshal((*GoEnum)(nil))
1311 if err != ErrNil {
1312 t.Errorf("Marshal: got err %v, want ErrNil", err)
1313 }
1314}
1315
David Symonds03c9d412010-08-26 14:23:18 +10001316// A type that implements the Marshaler interface, but is not nillable.
1317type nonNillableInt uint64
1318
Rob Pikea17fdd92011-11-02 12:43:05 -07001319func (nni nonNillableInt) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001320 return EncodeVarint(uint64(nni)), nil
1321}
1322
1323type NNIMessage struct {
1324 nni nonNillableInt
1325}
1326
David Symonds9f60f432012-06-14 09:45:25 +10001327func (*NNIMessage) Reset() {}
1328func (*NNIMessage) String() string { return "" }
1329func (*NNIMessage) ProtoMessage() {}
1330
David Symonds03c9d412010-08-26 14:23:18 +10001331// A type that implements the Marshaler interface and is nillable.
1332type nillableMessage struct {
1333 x uint64
1334}
1335
Rob Pikea17fdd92011-11-02 12:43:05 -07001336func (nm *nillableMessage) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001337 return EncodeVarint(nm.x), nil
1338}
1339
1340type NMMessage struct {
1341 nm *nillableMessage
1342}
1343
David Symonds9f60f432012-06-14 09:45:25 +10001344func (*NMMessage) Reset() {}
1345func (*NMMessage) String() string { return "" }
1346func (*NMMessage) ProtoMessage() {}
1347
David Symonds03c9d412010-08-26 14:23:18 +10001348// Verify a type that uses the Marshaler interface, but has a nil pointer.
1349func TestNilMarshaler(t *testing.T) {
1350 // Try a struct with a Marshaler field that is nil.
1351 // It should be directly marshable.
1352 nmm := new(NMMessage)
1353 if _, err := Marshal(nmm); err != nil {
1354 t.Error("unexpected error marshaling nmm: ", err)
1355 }
1356
1357 // Try a struct with a Marshaler field that is not nillable.
1358 nnim := new(NNIMessage)
1359 nnim.nni = 7
1360 var _ Marshaler = nnim.nni // verify it is truly a Marshaler
1361 if _, err := Marshal(nnim); err != nil {
1362 t.Error("unexpected error marshaling nnim: ", err)
1363 }
1364}
1365
David Symondsb79d99b2011-08-29 16:38:49 +10001366func TestAllSetDefaults(t *testing.T) {
1367 // Exercise SetDefaults with all scalar field types.
1368 m := &Defaults{
1369 // NaN != NaN, so override that here.
1370 F_Nan: Float32(1.7),
1371 }
1372 expected := &Defaults{
1373 F_Bool: Bool(true),
1374 F_Int32: Int32(32),
1375 F_Int64: Int64(64),
1376 F_Fixed32: Uint32(320),
1377 F_Fixed64: Uint64(640),
1378 F_Uint32: Uint32(3200),
1379 F_Uint64: Uint64(6400),
1380 F_Float: Float32(314159),
1381 F_Double: Float64(271828),
1382 F_String: String(`hello, "world!"` + "\n"),
1383 F_Bytes: []byte("Bignose"),
1384 F_Sint32: Int32(-32),
1385 F_Sint64: Int64(-64),
David Symondsefeca9a2012-05-08 10:36:04 +10001386 F_Enum: Defaults_GREEN.Enum(),
David Symondsb79d99b2011-08-29 16:38:49 +10001387 F_Pinf: Float32(float32(math.Inf(1))),
1388 F_Ninf: Float32(float32(math.Inf(-1))),
1389 F_Nan: Float32(1.7),
1390 }
1391 SetDefaults(m)
1392 if !Equal(m, expected) {
1393 t.Errorf(" got %v\nwant %v", m, expected)
1394 }
1395}
1396
1397func TestSetDefaultsWithSetField(t *testing.T) {
1398 // Check that a set value is not overridden.
1399 m := &Defaults{
1400 F_Int32: Int32(12),
1401 }
1402 SetDefaults(m)
David Symonds0e084922012-07-02 16:04:40 -07001403 if v := m.GetF_Int32(); v != 12 {
David Symondsb79d99b2011-08-29 16:38:49 +10001404 t.Errorf("m.FInt32 = %v, want 12", v)
1405 }
1406}
1407
1408func TestSetDefaultsWithSubMessage(t *testing.T) {
1409 m := &OtherMessage{
1410 Key: Int64(123),
1411 Inner: &InnerMessage{
1412 Host: String("gopher"),
1413 },
1414 }
1415 expected := &OtherMessage{
1416 Key: Int64(123),
1417 Inner: &InnerMessage{
1418 Host: String("gopher"),
1419 Port: Int32(4000),
1420 },
1421 }
1422 SetDefaults(m)
1423 if !Equal(m, expected) {
David Symonds814b9362011-12-13 09:10:47 +11001424 t.Errorf("\n got %v\nwant %v", m, expected)
David Symondsb79d99b2011-08-29 16:38:49 +10001425 }
1426}
1427
David Symonds472e2592013-07-15 11:10:07 +10001428func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
1429 m := &MyMessage{
1430 RepInner: []*InnerMessage{{}},
1431 }
1432 expected := &MyMessage{
1433 RepInner: []*InnerMessage{{
1434 Port: Int32(4000),
1435 }},
1436 }
1437 SetDefaults(m)
1438 if !Equal(m, expected) {
1439 t.Errorf("\n got %v\nwant %v", m, expected)
1440 }
1441}
1442
David Symondsd73d7b12011-09-28 10:56:43 -07001443func TestMaximumTagNumber(t *testing.T) {
1444 m := &MaxTag{
1445 LastField: String("natural goat essence"),
1446 }
1447 buf, err := Marshal(m)
1448 if err != nil {
1449 t.Fatalf("proto.Marshal failed: %v", err)
1450 }
1451 m2 := new(MaxTag)
1452 if err := Unmarshal(buf, m2); err != nil {
1453 t.Fatalf("proto.Unmarshal failed: %v", err)
1454 }
David Symonds0e084922012-07-02 16:04:40 -07001455 if got, want := m2.GetLastField(), *m.LastField; got != want {
David Symondsd73d7b12011-09-28 10:56:43 -07001456 t.Errorf("got %q, want %q", got, want)
1457 }
1458}
1459
David Symonds002ec402011-09-29 17:24:20 -07001460func TestJSON(t *testing.T) {
1461 m := &MyMessage{
1462 Count: Int32(4),
1463 Pet: []string{"bunny", "kitty"},
1464 Inner: &InnerMessage{
1465 Host: String("cauchy"),
1466 },
David Symonds62539862012-08-04 10:06:55 +10001467 Bikeshed: MyMessage_GREEN.Enum(),
David Symonds002ec402011-09-29 17:24:20 -07001468 }
David Symonds4af5f1f2013-10-11 10:08:35 +11001469 const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
David Symonds002ec402011-09-29 17:24:20 -07001470
1471 b, err := json.Marshal(m)
1472 if err != nil {
1473 t.Fatalf("json.Marshal failed: %v", err)
1474 }
1475 s := string(b)
1476 if s != expected {
1477 t.Errorf("got %s\nwant %s", s, expected)
1478 }
David Symonds62539862012-08-04 10:06:55 +10001479
1480 received := new(MyMessage)
1481 if err := json.Unmarshal(b, received); err != nil {
1482 t.Fatalf("json.Unmarshal failed: %v", err)
1483 }
1484 if !Equal(received, m) {
1485 t.Fatalf("got %s, want %s", received, m)
1486 }
1487
David Symonds4af5f1f2013-10-11 10:08:35 +11001488 // Test unmarshalling of JSON with symbolic enum name.
1489 const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
David Symonds62539862012-08-04 10:06:55 +10001490 received.Reset()
1491 if err := json.Unmarshal([]byte(old), received); err != nil {
1492 t.Fatalf("json.Unmarshal failed: %v", err)
1493 }
1494 if !Equal(received, m) {
1495 t.Fatalf("got %s, want %s", received, m)
1496 }
David Symonds002ec402011-09-29 17:24:20 -07001497}
1498
David Symonds22ac1502012-01-18 12:37:12 +11001499func TestBadWireType(t *testing.T) {
1500 b := []byte{7<<3 | 6} // field 7, wire type 6
1501 pb := new(OtherMessage)
1502 if err := Unmarshal(b, pb); err == nil {
1503 t.Errorf("Unmarshal did not fail")
1504 } else if !strings.Contains(err.Error(), "unknown wire type") {
1505 t.Errorf("wrong error: %v", err)
1506 }
1507}
1508
1509func TestBytesWithInvalidLength(t *testing.T) {
1510 // If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
1511 b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
1512 Unmarshal(b, new(MyMessage))
1513}
1514
Adam Langley28c83cb2013-07-13 14:54:54 +10001515func TestLengthOverflow(t *testing.T) {
1516 // Overflowing a length should not panic.
1517 b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
1518 Unmarshal(b, new(MyMessage))
1519}
1520
1521func TestVarintOverflow(t *testing.T) {
1522 // Overflowing a 64-bit length should not be allowed.
1523 b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
1524 if err := Unmarshal(b, new(MyMessage)); err == nil {
1525 t.Fatalf("Overflowed uint64 length without error")
1526 }
1527}
1528
David Symonds22ac1502012-01-18 12:37:12 +11001529func TestUnmarshalFuzz(t *testing.T) {
1530 const N = 1000
1531 seed := time.Now().UnixNano()
1532 t.Logf("RNG seed is %d", seed)
1533 rng := rand.New(rand.NewSource(seed))
1534 buf := make([]byte, 20)
1535 for i := 0; i < N; i++ {
1536 for j := range buf {
1537 buf[j] = byte(rng.Intn(256))
1538 }
1539 fuzzUnmarshal(t, buf)
1540 }
1541}
1542
David Symondse182aaf2013-01-30 17:04:37 +11001543func TestMergeMessages(t *testing.T) {
David Symonds525838c2012-07-20 15:42:49 +10001544 pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
1545 data, err := Marshal(pb)
1546 if err != nil {
1547 t.Fatalf("Marshal: %v", err)
1548 }
1549
1550 pb1 := new(MessageList)
1551 if err := Unmarshal(data, pb1); err != nil {
1552 t.Fatalf("first Unmarshal: %v", err)
1553 }
1554 if err := Unmarshal(data, pb1); err != nil {
1555 t.Fatalf("second Unmarshal: %v", err)
1556 }
1557 if len(pb1.Message) != 1 {
1558 t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
1559 }
1560
1561 pb2 := new(MessageList)
David Symondsd4b52d02013-01-15 14:30:26 +11001562 if err := UnmarshalMerge(data, pb2); err != nil {
1563 t.Fatalf("first UnmarshalMerge: %v", err)
David Symonds525838c2012-07-20 15:42:49 +10001564 }
David Symondsd4b52d02013-01-15 14:30:26 +11001565 if err := UnmarshalMerge(data, pb2); err != nil {
1566 t.Fatalf("second UnmarshalMerge: %v", err)
David Symonds525838c2012-07-20 15:42:49 +10001567 }
1568 if len(pb2.Message) != 2 {
David Symondsd4b52d02013-01-15 14:30:26 +11001569 t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
David Symonds525838c2012-07-20 15:42:49 +10001570 }
1571}
1572
David Symondsdf583ae2012-12-06 14:06:53 +11001573func TestExtensionMarshalOrder(t *testing.T) {
David Symonds0c1184e2013-06-08 15:42:12 +10001574 m := &MyMessage{Count: Int(123)}
David Symondsdf583ae2012-12-06 14:06:53 +11001575 if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
1576 t.Fatalf("SetExtension: %v", err)
1577 }
1578 if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
1579 t.Fatalf("SetExtension: %v", err)
1580 }
1581 if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
1582 t.Fatalf("SetExtension: %v", err)
1583 }
1584
1585 // Serialize m several times, and check we get the same bytes each time.
1586 var orig []byte
David Symonds0c1184e2013-06-08 15:42:12 +10001587 for i := 0; i < 100; i++ {
David Symondsdf583ae2012-12-06 14:06:53 +11001588 b, err := Marshal(m)
1589 if err != nil {
1590 t.Fatalf("Marshal: %v", err)
1591 }
1592 if i == 0 {
1593 orig = b
1594 continue
1595 }
1596 if !bytes.Equal(b, orig) {
1597 t.Errorf("Bytes differ on attempt #%d", i)
1598 }
1599 }
1600}
1601
David Symonds0c1184e2013-06-08 15:42:12 +10001602// Many extensions, because small maps might not iterate differently on each iteration.
1603var exts = []*ExtensionDesc{
1604 E_X201,
1605 E_X202,
1606 E_X203,
1607 E_X204,
1608 E_X205,
1609 E_X206,
1610 E_X207,
1611 E_X208,
1612 E_X209,
1613 E_X210,
1614 E_X211,
1615 E_X212,
1616 E_X213,
1617 E_X214,
1618 E_X215,
1619 E_X216,
1620 E_X217,
1621 E_X218,
1622 E_X219,
1623 E_X220,
1624 E_X221,
1625 E_X222,
1626 E_X223,
1627 E_X224,
1628 E_X225,
1629 E_X226,
1630 E_X227,
1631 E_X228,
1632 E_X229,
1633 E_X230,
1634 E_X231,
1635 E_X232,
1636 E_X233,
1637 E_X234,
1638 E_X235,
1639 E_X236,
1640 E_X237,
1641 E_X238,
1642 E_X239,
1643 E_X240,
1644 E_X241,
1645 E_X242,
1646 E_X243,
1647 E_X244,
1648 E_X245,
1649 E_X246,
1650 E_X247,
1651 E_X248,
1652 E_X249,
1653 E_X250,
1654}
1655
1656func TestMessageSetMarshalOrder(t *testing.T) {
1657 m := &MyMessageSet{}
1658 for _, x := range exts {
1659 if err := SetExtension(m, x, &Empty{}); err != nil {
1660 t.Fatalf("SetExtension: %v", err)
1661 }
1662 }
1663
1664 buf, err := Marshal(m)
1665 if err != nil {
1666 t.Fatalf("Marshal: %v", err)
1667 }
1668
1669 // Serialize m several times, and check we get the same bytes each time.
1670 for i := 0; i < 10; i++ {
1671 b1, err := Marshal(m)
1672 if err != nil {
1673 t.Fatalf("Marshal: %v", err)
1674 }
1675 if !bytes.Equal(b1, buf) {
1676 t.Errorf("Bytes differ on re-Marshal #%d", i)
1677 }
1678
1679 m2 := &MyMessageSet{}
1680 if err := Unmarshal(buf, m2); err != nil {
1681 t.Errorf("Unmarshal: %v", err)
1682 }
1683 b2, err := Marshal(m2)
1684 if err != nil {
1685 t.Errorf("re-Marshal: %v", err)
1686 }
1687 if !bytes.Equal(b2, buf) {
1688 t.Errorf("Bytes differ on round-trip #%d", i)
1689 }
1690 }
1691}
1692
David Symonds2ce8ed42013-06-20 13:22:17 +10001693func TestUnmarshalMergesMessages(t *testing.T) {
1694 // If a nested message occurs twice in the input,
1695 // the fields should be merged when decoding.
1696 a := &OtherMessage{
1697 Key: Int64(123),
1698 Inner: &InnerMessage{
1699 Host: String("polhode"),
1700 Port: Int32(1234),
1701 },
1702 }
1703 aData, err := Marshal(a)
1704 if err != nil {
1705 t.Fatalf("Marshal(a): %v", err)
1706 }
1707 b := &OtherMessage{
1708 Weight: Float32(1.2),
1709 Inner: &InnerMessage{
1710 Host: String("herpolhode"),
1711 Connected: Bool(true),
1712 },
1713 }
1714 bData, err := Marshal(b)
1715 if err != nil {
1716 t.Fatalf("Marshal(b): %v", err)
1717 }
1718 want := &OtherMessage{
1719 Key: Int64(123),
1720 Weight: Float32(1.2),
1721 Inner: &InnerMessage{
1722 Host: String("herpolhode"),
1723 Port: Int32(1234),
1724 Connected: Bool(true),
1725 },
1726 }
1727 got := new(OtherMessage)
1728 if err := Unmarshal(append(aData, bData...), got); err != nil {
1729 t.Fatalf("Unmarshal: %v", err)
1730 }
1731 if !Equal(got, want) {
1732 t.Errorf("\n got %v\nwant %v", got, want)
1733 }
1734}
1735
David Symondsc31645c2013-06-22 17:57:36 +10001736func TestEncodingSizes(t *testing.T) {
1737 tests := []struct {
1738 m Message
1739 n int
1740 }{
1741 {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
David Symondsf054e842014-07-22 14:06:27 +10001742 {&Defaults{F_Int32: Int32(math.MinInt32)}, 11},
1743 {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6},
David Symondsc31645c2013-06-22 17:57:36 +10001744 {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
1745 }
1746 for _, test := range tests {
1747 b, err := Marshal(test.m)
1748 if err != nil {
1749 t.Errorf("Marshal(%v): %v", test.m, err)
1750 continue
1751 }
1752 if len(b) != test.n {
1753 t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
1754 }
1755 }
1756}
1757
David Symondse583a5f2013-09-27 10:02:37 +10001758func TestRequiredNotSetError(t *testing.T) {
David Symonds4646c372013-09-09 13:18:58 +10001759 pb := initGoTest(false)
1760 pb.RequiredField.Label = nil
1761 pb.F_Int32Required = nil
1762 pb.F_Int64Required = nil
1763
1764 expected := "0807" + // field 1, encoding 0, value 7
1765 "2206" + "120474797065" + // field 4, encoding 2 (GoTestField)
1766 "5001" + // field 10, encoding 0, value 1
1767 "6d20000000" + // field 13, encoding 5, value 0x20
1768 "714000000000000000" + // field 14, encoding 1, value 0x40
1769 "78a019" + // field 15, encoding 0, value 0xca0 = 3232
1770 "8001c032" + // field 16, encoding 0, value 0x1940 = 6464
1771 "8d0100004a45" + // field 17, encoding 5, value 3232.0
1772 "9101000000000040b940" + // field 18, encoding 1, value 6464.0
1773 "9a0106" + "737472696e67" + // field 19, encoding 2, string "string"
1774 "b304" + // field 70, encoding 3, start group
1775 "ba0408" + "7265717569726564" + // field 71, encoding 2, string "required"
1776 "b404" + // field 70, encoding 4, end group
1777 "aa0605" + "6279746573" + // field 101, encoding 2, string "bytes"
1778 "b0063f" + // field 102, encoding 0, 0x3f zigzag32
1779 "b8067f" // field 103, encoding 0, 0x7f zigzag64
1780
1781 o := old()
1782 bytes, err := Marshal(pb)
David Symondse583a5f2013-09-27 10:02:37 +10001783 if _, ok := err.(*RequiredNotSetError); !ok {
1784 fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001785 o.DebugPrint("", bytes)
1786 t.Fatalf("expected = %s", expected)
1787 }
1788 if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1789 t.Errorf("marshal-1 wrong err msg: %v", err)
1790 }
1791 if !equal(bytes, expected, t) {
1792 o.DebugPrint("neq 1", bytes)
1793 t.Fatalf("expected = %s", expected)
1794 }
1795
1796 // Now test Unmarshal by recreating the original buffer.
1797 pbd := new(GoTest)
1798 err = Unmarshal(bytes, pbd)
David Symondse583a5f2013-09-27 10:02:37 +10001799 if _, ok := err.(*RequiredNotSetError); !ok {
1800 t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001801 o.DebugPrint("", bytes)
1802 t.Fatalf("string = %s", expected)
1803 }
1804 if strings.Index(err.Error(), "RequiredField.{Unknown}") < 0 {
1805 t.Errorf("unmarshal wrong err msg: %v", err)
1806 }
1807 bytes, err = Marshal(pbd)
David Symondse583a5f2013-09-27 10:02:37 +10001808 if _, ok := err.(*RequiredNotSetError); !ok {
1809 t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001810 o.DebugPrint("", bytes)
1811 t.Fatalf("string = %s", expected)
1812 }
1813 if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1814 t.Errorf("marshal-2 wrong err msg: %v", err)
1815 }
1816 if !equal(bytes, expected, t) {
1817 o.DebugPrint("neq 2", bytes)
1818 t.Fatalf("string = %s", expected)
1819 }
1820}
1821
David Symonds22ac1502012-01-18 12:37:12 +11001822func fuzzUnmarshal(t *testing.T, data []byte) {
1823 defer func() {
1824 if e := recover(); e != nil {
1825 t.Errorf("These bytes caused a panic: %+v", data)
1826 t.Logf("Stack:\n%s", debug.Stack())
1827 t.FailNow()
1828 }
1829 }()
1830
1831 pb := new(MyMessage)
1832 Unmarshal(data, pb)
1833}
1834
David Symonds0bf1ad52013-10-11 09:07:50 +11001835// Benchmarks
1836
1837func testMsg() *GoTest {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001838 pb := initGoTest(true)
David Symonds0bf1ad52013-10-11 09:07:50 +11001839 const N = 1000 // Internally the library starts much smaller.
1840 pb.F_Int32Repeated = make([]int32, N)
1841 pb.F_DoubleRepeated = make([]float64, N)
1842 for i := 0; i < N; i++ {
1843 pb.F_Int32Repeated[i] = int32(i)
1844 pb.F_DoubleRepeated[i] = float64(i)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001845 }
Russ Coxd4ce3f12012-09-12 10:36:26 +10001846 return pb
1847}
David Symondsd9da6ba2011-08-30 14:41:30 +10001848
David Symonds0bf1ad52013-10-11 09:07:50 +11001849func bytesMsg() *GoTest {
1850 pb := initGoTest(true)
1851 buf := make([]byte, 4000)
1852 for i := range buf {
1853 buf[i] = byte(i)
1854 }
1855 pb.F_BytesDefaulted = buf
1856 return pb
1857}
1858
1859func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) {
1860 d, _ := marshal(pb)
1861 b.SetBytes(int64(len(d)))
1862 b.ResetTimer()
1863 for i := 0; i < b.N; i++ {
1864 marshal(pb)
1865 }
1866}
1867
1868func benchmarkBufferMarshal(b *testing.B, pb Message) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001869 p := NewBuffer(nil)
David Symonds0bf1ad52013-10-11 09:07:50 +11001870 benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1871 p.Reset()
1872 err := p.Marshal(pb0)
1873 return p.Bytes(), err
1874 })
1875}
1876
1877func benchmarkSize(b *testing.B, pb Message) {
1878 benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1879 Size(pb)
1880 return nil, nil
1881 })
1882}
1883
1884func newOf(pb Message) Message {
1885 in := reflect.ValueOf(pb)
1886 if in.IsNil() {
1887 return pb
1888 }
1889 return reflect.New(in.Type().Elem()).Interface().(Message)
1890}
1891
1892func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) {
1893 d, _ := Marshal(pb)
1894 b.SetBytes(int64(len(d)))
1895 pbd := newOf(pb)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001896
Russ Coxd4ce3f12012-09-12 10:36:26 +10001897 b.ResetTimer()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001898 for i := 0; i < b.N; i++ {
David Symonds0bf1ad52013-10-11 09:07:50 +11001899 unmarshal(d, pbd)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001900 }
David Symonds0bf1ad52013-10-11 09:07:50 +11001901}
1902
1903func benchmarkBufferUnmarshal(b *testing.B, pb Message) {
1904 p := NewBuffer(nil)
1905 benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error {
1906 p.SetBuf(d)
1907 return p.Unmarshal(pb0)
1908 })
1909}
1910
1911// Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes}
1912
1913func BenchmarkMarshal(b *testing.B) {
1914 benchmarkMarshal(b, testMsg(), Marshal)
1915}
1916
1917func BenchmarkBufferMarshal(b *testing.B) {
1918 benchmarkBufferMarshal(b, testMsg())
1919}
1920
1921func BenchmarkSize(b *testing.B) {
1922 benchmarkSize(b, testMsg())
Rob Pikeaaa3a622010-03-20 22:32:34 -07001923}
1924
1925func BenchmarkUnmarshal(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001926 benchmarkUnmarshal(b, testMsg(), Unmarshal)
1927}
Rob Pikeaaa3a622010-03-20 22:32:34 -07001928
David Symonds0bf1ad52013-10-11 09:07:50 +11001929func BenchmarkBufferUnmarshal(b *testing.B) {
1930 benchmarkBufferUnmarshal(b, testMsg())
Russ Coxd4ce3f12012-09-12 10:36:26 +10001931}
1932
1933func BenchmarkMarshalBytes(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001934 benchmarkMarshal(b, bytesMsg(), Marshal)
1935}
Russ Coxd4ce3f12012-09-12 10:36:26 +10001936
David Symonds0bf1ad52013-10-11 09:07:50 +11001937func BenchmarkBufferMarshalBytes(b *testing.B) {
1938 benchmarkBufferMarshal(b, bytesMsg())
1939}
1940
1941func BenchmarkSizeBytes(b *testing.B) {
1942 benchmarkSize(b, bytesMsg())
Russ Coxd4ce3f12012-09-12 10:36:26 +10001943}
1944
1945func BenchmarkUnmarshalBytes(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11001946 benchmarkUnmarshal(b, bytesMsg(), Unmarshal)
1947}
Russ Coxd4ce3f12012-09-12 10:36:26 +10001948
David Symonds0bf1ad52013-10-11 09:07:50 +11001949func BenchmarkBufferUnmarshalBytes(b *testing.B) {
1950 benchmarkBufferUnmarshal(b, bytesMsg())
Rob Pikeaaa3a622010-03-20 22:32:34 -07001951}
David Symondsc0287172012-08-15 11:10:30 +10001952
1953func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
1954 b.StopTimer()
1955 pb := initGoTestField()
1956 skip := &GoSkipTest{
1957 SkipInt32: Int32(32),
1958 SkipFixed32: Uint32(3232),
1959 SkipFixed64: Uint64(6464),
1960 SkipString: String("skipper"),
1961 Skipgroup: &GoSkipTest_SkipGroup{
1962 GroupInt32: Int32(75),
1963 GroupString: String("wxyz"),
1964 },
1965 }
1966
1967 pbd := new(GoTestField)
1968 p := NewBuffer(nil)
1969 p.Marshal(pb)
1970 p.Marshal(skip)
1971 p2 := NewBuffer(nil)
1972
1973 b.StartTimer()
1974 for i := 0; i < b.N; i++ {
1975 p2.SetBuf(p.Bytes())
1976 p2.Unmarshal(pbd)
1977 }
1978}