blob: aa67b10cde9cbe64446464852760fe594f7aa268 [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.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
Rob Pikeaaa3a622010-03-20 22:32:34 -07005//
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"
David Symonds558f13f2014-11-24 10:28:53 +110048 . "github.com/golang/protobuf/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()
David Symondsf62db482015-02-23 12:37:00 +11001255 err := o.Marshal(pb)
1256 if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001257 t.Fatalf("unexpected or no error when marshaling: %v", err)
1258 }
1259}
1260
Rob Pikeaaa3a622010-03-20 22:32:34 -07001261// Test that enums work. Checks for a bug introduced by making enums
1262// named types instead of int32: newInt32FromUint64 would crash with
1263// a type mismatch in reflect.PointTo.
1264func TestEnum(t *testing.T) {
1265 pb := new(GoEnum)
David Symondsefeca9a2012-05-08 10:36:04 +10001266 pb.Foo = FOO_FOO1.Enum()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001267 o := old()
1268 if err := o.Marshal(pb); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001269 t.Fatal("error encoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001270 }
1271 pb1 := new(GoEnum)
1272 if err := o.Unmarshal(pb1); err != nil {
Rob Pike853f9112010-12-17 13:56:46 -08001273 t.Fatal("error decoding enum:", err)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001274 }
1275 if *pb1.Foo != FOO_FOO1 {
Rob Pike853f9112010-12-17 13:56:46 -08001276 t.Error("expected 7 but got ", *pb1.Foo)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001277 }
1278}
1279
David Symondse37856c2011-06-22 12:52:53 +10001280// Enum types have String methods. Check that enum fields can be printed.
1281// We don't care what the value actually is, just as long as it doesn't crash.
1282func TestPrintingNilEnumFields(t *testing.T) {
1283 pb := new(GoEnum)
1284 fmt.Sprintf("%+v", pb)
1285}
1286
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001287// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1288func TestRequiredFieldEnforcement(t *testing.T) {
1289 pb := new(GoTestField)
1290 _, err := Marshal(pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001291 if err == nil {
1292 t.Error("marshal: expected error, got nil")
David Symonds4646c372013-09-09 13:18:58 +10001293 } else if strings.Index(err.Error(), "Label") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001294 t.Errorf("marshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001295 }
1296
1297 // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1298 // so simply counting the required fields is insufficient.
1299 // field 1, encoding 2, value "hi"
1300 buf := []byte("\x0A\x02hi\x0A\x02hi")
1301 err = Unmarshal(buf, pb)
David Symonds5b7775e2010-12-01 10:09:04 +11001302 if err == nil {
1303 t.Error("unmarshal: expected error, got nil")
David Symonds4646c372013-09-09 13:18:58 +10001304 } else if strings.Index(err.Error(), "{Unknown}") < 0 {
David Symonds5b7775e2010-12-01 10:09:04 +11001305 t.Errorf("unmarshal: bad error type: %v", err)
Rob Pikec6d8e4a2010-07-28 15:34:32 -07001306 }
1307}
1308
David Symondsd4661c52012-08-30 15:17:53 +10001309func TestTypedNilMarshal(t *testing.T) {
1310 // A typed nil should return ErrNil and not crash.
1311 _, err := Marshal((*GoEnum)(nil))
1312 if err != ErrNil {
1313 t.Errorf("Marshal: got err %v, want ErrNil", err)
1314 }
1315}
1316
David Symonds03c9d412010-08-26 14:23:18 +10001317// A type that implements the Marshaler interface, but is not nillable.
1318type nonNillableInt uint64
1319
Rob Pikea17fdd92011-11-02 12:43:05 -07001320func (nni nonNillableInt) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001321 return EncodeVarint(uint64(nni)), nil
1322}
1323
1324type NNIMessage struct {
1325 nni nonNillableInt
1326}
1327
David Symonds9f60f432012-06-14 09:45:25 +10001328func (*NNIMessage) Reset() {}
1329func (*NNIMessage) String() string { return "" }
1330func (*NNIMessage) ProtoMessage() {}
1331
David Symonds03c9d412010-08-26 14:23:18 +10001332// A type that implements the Marshaler interface and is nillable.
1333type nillableMessage struct {
1334 x uint64
1335}
1336
Rob Pikea17fdd92011-11-02 12:43:05 -07001337func (nm *nillableMessage) Marshal() ([]byte, error) {
David Symonds03c9d412010-08-26 14:23:18 +10001338 return EncodeVarint(nm.x), nil
1339}
1340
1341type NMMessage struct {
1342 nm *nillableMessage
1343}
1344
David Symonds9f60f432012-06-14 09:45:25 +10001345func (*NMMessage) Reset() {}
1346func (*NMMessage) String() string { return "" }
1347func (*NMMessage) ProtoMessage() {}
1348
David Symonds03c9d412010-08-26 14:23:18 +10001349// Verify a type that uses the Marshaler interface, but has a nil pointer.
1350func TestNilMarshaler(t *testing.T) {
1351 // Try a struct with a Marshaler field that is nil.
1352 // It should be directly marshable.
1353 nmm := new(NMMessage)
1354 if _, err := Marshal(nmm); err != nil {
1355 t.Error("unexpected error marshaling nmm: ", err)
1356 }
1357
1358 // Try a struct with a Marshaler field that is not nillable.
1359 nnim := new(NNIMessage)
1360 nnim.nni = 7
1361 var _ Marshaler = nnim.nni // verify it is truly a Marshaler
1362 if _, err := Marshal(nnim); err != nil {
1363 t.Error("unexpected error marshaling nnim: ", err)
1364 }
1365}
1366
David Symondsb79d99b2011-08-29 16:38:49 +10001367func TestAllSetDefaults(t *testing.T) {
1368 // Exercise SetDefaults with all scalar field types.
1369 m := &Defaults{
1370 // NaN != NaN, so override that here.
1371 F_Nan: Float32(1.7),
1372 }
1373 expected := &Defaults{
1374 F_Bool: Bool(true),
1375 F_Int32: Int32(32),
1376 F_Int64: Int64(64),
1377 F_Fixed32: Uint32(320),
1378 F_Fixed64: Uint64(640),
1379 F_Uint32: Uint32(3200),
1380 F_Uint64: Uint64(6400),
1381 F_Float: Float32(314159),
1382 F_Double: Float64(271828),
1383 F_String: String(`hello, "world!"` + "\n"),
1384 F_Bytes: []byte("Bignose"),
1385 F_Sint32: Int32(-32),
1386 F_Sint64: Int64(-64),
David Symondsefeca9a2012-05-08 10:36:04 +10001387 F_Enum: Defaults_GREEN.Enum(),
David Symondsb79d99b2011-08-29 16:38:49 +10001388 F_Pinf: Float32(float32(math.Inf(1))),
1389 F_Ninf: Float32(float32(math.Inf(-1))),
1390 F_Nan: Float32(1.7),
David Symonds7e810982014-08-12 13:11:17 +10001391 StrZero: String(""),
David Symondsb79d99b2011-08-29 16:38:49 +10001392 }
1393 SetDefaults(m)
1394 if !Equal(m, expected) {
David Symonds7e810982014-08-12 13:11:17 +10001395 t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected)
David Symondsb79d99b2011-08-29 16:38:49 +10001396 }
1397}
1398
1399func TestSetDefaultsWithSetField(t *testing.T) {
1400 // Check that a set value is not overridden.
1401 m := &Defaults{
1402 F_Int32: Int32(12),
1403 }
1404 SetDefaults(m)
David Symonds0e084922012-07-02 16:04:40 -07001405 if v := m.GetF_Int32(); v != 12 {
David Symondsb79d99b2011-08-29 16:38:49 +10001406 t.Errorf("m.FInt32 = %v, want 12", v)
1407 }
1408}
1409
1410func TestSetDefaultsWithSubMessage(t *testing.T) {
1411 m := &OtherMessage{
1412 Key: Int64(123),
1413 Inner: &InnerMessage{
1414 Host: String("gopher"),
1415 },
1416 }
1417 expected := &OtherMessage{
1418 Key: Int64(123),
1419 Inner: &InnerMessage{
1420 Host: String("gopher"),
1421 Port: Int32(4000),
1422 },
1423 }
1424 SetDefaults(m)
1425 if !Equal(m, expected) {
David Symonds814b9362011-12-13 09:10:47 +11001426 t.Errorf("\n got %v\nwant %v", m, expected)
David Symondsb79d99b2011-08-29 16:38:49 +10001427 }
1428}
1429
David Symonds472e2592013-07-15 11:10:07 +10001430func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
1431 m := &MyMessage{
1432 RepInner: []*InnerMessage{{}},
1433 }
1434 expected := &MyMessage{
1435 RepInner: []*InnerMessage{{
1436 Port: Int32(4000),
1437 }},
1438 }
1439 SetDefaults(m)
1440 if !Equal(m, expected) {
1441 t.Errorf("\n got %v\nwant %v", m, expected)
1442 }
1443}
1444
David Symondsd73d7b12011-09-28 10:56:43 -07001445func TestMaximumTagNumber(t *testing.T) {
1446 m := &MaxTag{
1447 LastField: String("natural goat essence"),
1448 }
1449 buf, err := Marshal(m)
1450 if err != nil {
1451 t.Fatalf("proto.Marshal failed: %v", err)
1452 }
1453 m2 := new(MaxTag)
1454 if err := Unmarshal(buf, m2); err != nil {
1455 t.Fatalf("proto.Unmarshal failed: %v", err)
1456 }
David Symonds0e084922012-07-02 16:04:40 -07001457 if got, want := m2.GetLastField(), *m.LastField; got != want {
David Symondsd73d7b12011-09-28 10:56:43 -07001458 t.Errorf("got %q, want %q", got, want)
1459 }
1460}
1461
David Symonds002ec402011-09-29 17:24:20 -07001462func TestJSON(t *testing.T) {
1463 m := &MyMessage{
1464 Count: Int32(4),
1465 Pet: []string{"bunny", "kitty"},
1466 Inner: &InnerMessage{
1467 Host: String("cauchy"),
1468 },
David Symonds62539862012-08-04 10:06:55 +10001469 Bikeshed: MyMessage_GREEN.Enum(),
David Symonds002ec402011-09-29 17:24:20 -07001470 }
David Symonds4af5f1f2013-10-11 10:08:35 +11001471 const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
David Symonds002ec402011-09-29 17:24:20 -07001472
1473 b, err := json.Marshal(m)
1474 if err != nil {
1475 t.Fatalf("json.Marshal failed: %v", err)
1476 }
1477 s := string(b)
1478 if s != expected {
1479 t.Errorf("got %s\nwant %s", s, expected)
1480 }
David Symonds62539862012-08-04 10:06:55 +10001481
1482 received := new(MyMessage)
1483 if err := json.Unmarshal(b, received); err != nil {
1484 t.Fatalf("json.Unmarshal failed: %v", err)
1485 }
1486 if !Equal(received, m) {
1487 t.Fatalf("got %s, want %s", received, m)
1488 }
1489
David Symonds4af5f1f2013-10-11 10:08:35 +11001490 // Test unmarshalling of JSON with symbolic enum name.
1491 const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
David Symonds62539862012-08-04 10:06:55 +10001492 received.Reset()
1493 if err := json.Unmarshal([]byte(old), received); err != nil {
1494 t.Fatalf("json.Unmarshal failed: %v", err)
1495 }
1496 if !Equal(received, m) {
1497 t.Fatalf("got %s, want %s", received, m)
1498 }
David Symonds002ec402011-09-29 17:24:20 -07001499}
1500
David Symonds22ac1502012-01-18 12:37:12 +11001501func TestBadWireType(t *testing.T) {
1502 b := []byte{7<<3 | 6} // field 7, wire type 6
1503 pb := new(OtherMessage)
1504 if err := Unmarshal(b, pb); err == nil {
1505 t.Errorf("Unmarshal did not fail")
1506 } else if !strings.Contains(err.Error(), "unknown wire type") {
1507 t.Errorf("wrong error: %v", err)
1508 }
1509}
1510
1511func TestBytesWithInvalidLength(t *testing.T) {
1512 // If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
1513 b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
1514 Unmarshal(b, new(MyMessage))
1515}
1516
Adam Langley28c83cb2013-07-13 14:54:54 +10001517func TestLengthOverflow(t *testing.T) {
1518 // Overflowing a length should not panic.
1519 b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
1520 Unmarshal(b, new(MyMessage))
1521}
1522
1523func TestVarintOverflow(t *testing.T) {
1524 // Overflowing a 64-bit length should not be allowed.
1525 b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
1526 if err := Unmarshal(b, new(MyMessage)); err == nil {
1527 t.Fatalf("Overflowed uint64 length without error")
1528 }
1529}
1530
David Symonds22ac1502012-01-18 12:37:12 +11001531func TestUnmarshalFuzz(t *testing.T) {
1532 const N = 1000
1533 seed := time.Now().UnixNano()
1534 t.Logf("RNG seed is %d", seed)
1535 rng := rand.New(rand.NewSource(seed))
1536 buf := make([]byte, 20)
1537 for i := 0; i < N; i++ {
1538 for j := range buf {
1539 buf[j] = byte(rng.Intn(256))
1540 }
1541 fuzzUnmarshal(t, buf)
1542 }
1543}
1544
David Symondse182aaf2013-01-30 17:04:37 +11001545func TestMergeMessages(t *testing.T) {
David Symonds525838c2012-07-20 15:42:49 +10001546 pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
1547 data, err := Marshal(pb)
1548 if err != nil {
1549 t.Fatalf("Marshal: %v", err)
1550 }
1551
1552 pb1 := new(MessageList)
1553 if err := Unmarshal(data, pb1); err != nil {
1554 t.Fatalf("first Unmarshal: %v", err)
1555 }
1556 if err := Unmarshal(data, pb1); err != nil {
1557 t.Fatalf("second Unmarshal: %v", err)
1558 }
1559 if len(pb1.Message) != 1 {
1560 t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
1561 }
1562
1563 pb2 := new(MessageList)
David Symondsd4b52d02013-01-15 14:30:26 +11001564 if err := UnmarshalMerge(data, pb2); err != nil {
1565 t.Fatalf("first UnmarshalMerge: %v", err)
David Symonds525838c2012-07-20 15:42:49 +10001566 }
David Symondsd4b52d02013-01-15 14:30:26 +11001567 if err := UnmarshalMerge(data, pb2); err != nil {
1568 t.Fatalf("second UnmarshalMerge: %v", err)
David Symonds525838c2012-07-20 15:42:49 +10001569 }
1570 if len(pb2.Message) != 2 {
David Symondsd4b52d02013-01-15 14:30:26 +11001571 t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
David Symonds525838c2012-07-20 15:42:49 +10001572 }
1573}
1574
David Symondsdf583ae2012-12-06 14:06:53 +11001575func TestExtensionMarshalOrder(t *testing.T) {
David Symonds0c1184e2013-06-08 15:42:12 +10001576 m := &MyMessage{Count: Int(123)}
David Symondsdf583ae2012-12-06 14:06:53 +11001577 if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
1578 t.Fatalf("SetExtension: %v", err)
1579 }
1580 if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
1581 t.Fatalf("SetExtension: %v", err)
1582 }
1583 if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
1584 t.Fatalf("SetExtension: %v", err)
1585 }
1586
1587 // Serialize m several times, and check we get the same bytes each time.
1588 var orig []byte
David Symonds0c1184e2013-06-08 15:42:12 +10001589 for i := 0; i < 100; i++ {
David Symondsdf583ae2012-12-06 14:06:53 +11001590 b, err := Marshal(m)
1591 if err != nil {
1592 t.Fatalf("Marshal: %v", err)
1593 }
1594 if i == 0 {
1595 orig = b
1596 continue
1597 }
1598 if !bytes.Equal(b, orig) {
1599 t.Errorf("Bytes differ on attempt #%d", i)
1600 }
1601 }
1602}
1603
David Symonds0c1184e2013-06-08 15:42:12 +10001604// Many extensions, because small maps might not iterate differently on each iteration.
1605var exts = []*ExtensionDesc{
1606 E_X201,
1607 E_X202,
1608 E_X203,
1609 E_X204,
1610 E_X205,
1611 E_X206,
1612 E_X207,
1613 E_X208,
1614 E_X209,
1615 E_X210,
1616 E_X211,
1617 E_X212,
1618 E_X213,
1619 E_X214,
1620 E_X215,
1621 E_X216,
1622 E_X217,
1623 E_X218,
1624 E_X219,
1625 E_X220,
1626 E_X221,
1627 E_X222,
1628 E_X223,
1629 E_X224,
1630 E_X225,
1631 E_X226,
1632 E_X227,
1633 E_X228,
1634 E_X229,
1635 E_X230,
1636 E_X231,
1637 E_X232,
1638 E_X233,
1639 E_X234,
1640 E_X235,
1641 E_X236,
1642 E_X237,
1643 E_X238,
1644 E_X239,
1645 E_X240,
1646 E_X241,
1647 E_X242,
1648 E_X243,
1649 E_X244,
1650 E_X245,
1651 E_X246,
1652 E_X247,
1653 E_X248,
1654 E_X249,
1655 E_X250,
1656}
1657
1658func TestMessageSetMarshalOrder(t *testing.T) {
1659 m := &MyMessageSet{}
1660 for _, x := range exts {
1661 if err := SetExtension(m, x, &Empty{}); err != nil {
1662 t.Fatalf("SetExtension: %v", err)
1663 }
1664 }
1665
1666 buf, err := Marshal(m)
1667 if err != nil {
1668 t.Fatalf("Marshal: %v", err)
1669 }
1670
1671 // Serialize m several times, and check we get the same bytes each time.
1672 for i := 0; i < 10; i++ {
1673 b1, err := Marshal(m)
1674 if err != nil {
1675 t.Fatalf("Marshal: %v", err)
1676 }
1677 if !bytes.Equal(b1, buf) {
1678 t.Errorf("Bytes differ on re-Marshal #%d", i)
1679 }
1680
1681 m2 := &MyMessageSet{}
1682 if err := Unmarshal(buf, m2); err != nil {
1683 t.Errorf("Unmarshal: %v", err)
1684 }
1685 b2, err := Marshal(m2)
1686 if err != nil {
1687 t.Errorf("re-Marshal: %v", err)
1688 }
1689 if !bytes.Equal(b2, buf) {
1690 t.Errorf("Bytes differ on round-trip #%d", i)
1691 }
1692 }
1693}
1694
David Symonds2ce8ed42013-06-20 13:22:17 +10001695func TestUnmarshalMergesMessages(t *testing.T) {
1696 // If a nested message occurs twice in the input,
1697 // the fields should be merged when decoding.
1698 a := &OtherMessage{
1699 Key: Int64(123),
1700 Inner: &InnerMessage{
1701 Host: String("polhode"),
1702 Port: Int32(1234),
1703 },
1704 }
1705 aData, err := Marshal(a)
1706 if err != nil {
1707 t.Fatalf("Marshal(a): %v", err)
1708 }
1709 b := &OtherMessage{
1710 Weight: Float32(1.2),
1711 Inner: &InnerMessage{
1712 Host: String("herpolhode"),
1713 Connected: Bool(true),
1714 },
1715 }
1716 bData, err := Marshal(b)
1717 if err != nil {
1718 t.Fatalf("Marshal(b): %v", err)
1719 }
1720 want := &OtherMessage{
1721 Key: Int64(123),
1722 Weight: Float32(1.2),
1723 Inner: &InnerMessage{
1724 Host: String("herpolhode"),
1725 Port: Int32(1234),
1726 Connected: Bool(true),
1727 },
1728 }
1729 got := new(OtherMessage)
1730 if err := Unmarshal(append(aData, bData...), got); err != nil {
1731 t.Fatalf("Unmarshal: %v", err)
1732 }
1733 if !Equal(got, want) {
1734 t.Errorf("\n got %v\nwant %v", got, want)
1735 }
1736}
1737
David Symondsc31645c2013-06-22 17:57:36 +10001738func TestEncodingSizes(t *testing.T) {
1739 tests := []struct {
1740 m Message
1741 n int
1742 }{
1743 {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
David Symondsf054e842014-07-22 14:06:27 +10001744 {&Defaults{F_Int32: Int32(math.MinInt32)}, 11},
1745 {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6},
David Symondsc31645c2013-06-22 17:57:36 +10001746 {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
1747 }
1748 for _, test := range tests {
1749 b, err := Marshal(test.m)
1750 if err != nil {
1751 t.Errorf("Marshal(%v): %v", test.m, err)
1752 continue
1753 }
1754 if len(b) != test.n {
1755 t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
1756 }
1757 }
1758}
1759
David Symondse583a5f2013-09-27 10:02:37 +10001760func TestRequiredNotSetError(t *testing.T) {
David Symonds4646c372013-09-09 13:18:58 +10001761 pb := initGoTest(false)
1762 pb.RequiredField.Label = nil
1763 pb.F_Int32Required = nil
1764 pb.F_Int64Required = nil
1765
1766 expected := "0807" + // field 1, encoding 0, value 7
1767 "2206" + "120474797065" + // field 4, encoding 2 (GoTestField)
1768 "5001" + // field 10, encoding 0, value 1
1769 "6d20000000" + // field 13, encoding 5, value 0x20
1770 "714000000000000000" + // field 14, encoding 1, value 0x40
1771 "78a019" + // field 15, encoding 0, value 0xca0 = 3232
1772 "8001c032" + // field 16, encoding 0, value 0x1940 = 6464
1773 "8d0100004a45" + // field 17, encoding 5, value 3232.0
1774 "9101000000000040b940" + // field 18, encoding 1, value 6464.0
1775 "9a0106" + "737472696e67" + // field 19, encoding 2, string "string"
1776 "b304" + // field 70, encoding 3, start group
1777 "ba0408" + "7265717569726564" + // field 71, encoding 2, string "required"
1778 "b404" + // field 70, encoding 4, end group
1779 "aa0605" + "6279746573" + // field 101, encoding 2, string "bytes"
1780 "b0063f" + // field 102, encoding 0, 0x3f zigzag32
1781 "b8067f" // field 103, encoding 0, 0x7f zigzag64
1782
1783 o := old()
1784 bytes, err := Marshal(pb)
David Symondse583a5f2013-09-27 10:02:37 +10001785 if _, ok := err.(*RequiredNotSetError); !ok {
1786 fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001787 o.DebugPrint("", bytes)
1788 t.Fatalf("expected = %s", expected)
1789 }
1790 if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1791 t.Errorf("marshal-1 wrong err msg: %v", err)
1792 }
1793 if !equal(bytes, expected, t) {
1794 o.DebugPrint("neq 1", bytes)
1795 t.Fatalf("expected = %s", expected)
1796 }
1797
1798 // Now test Unmarshal by recreating the original buffer.
1799 pbd := new(GoTest)
1800 err = Unmarshal(bytes, pbd)
David Symondse583a5f2013-09-27 10:02:37 +10001801 if _, ok := err.(*RequiredNotSetError); !ok {
1802 t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001803 o.DebugPrint("", bytes)
1804 t.Fatalf("string = %s", expected)
1805 }
1806 if strings.Index(err.Error(), "RequiredField.{Unknown}") < 0 {
1807 t.Errorf("unmarshal wrong err msg: %v", err)
1808 }
1809 bytes, err = Marshal(pbd)
David Symondse583a5f2013-09-27 10:02:37 +10001810 if _, ok := err.(*RequiredNotSetError); !ok {
1811 t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
David Symonds4646c372013-09-09 13:18:58 +10001812 o.DebugPrint("", bytes)
1813 t.Fatalf("string = %s", expected)
1814 }
1815 if strings.Index(err.Error(), "RequiredField.Label") < 0 {
1816 t.Errorf("marshal-2 wrong err msg: %v", err)
1817 }
1818 if !equal(bytes, expected, t) {
1819 o.DebugPrint("neq 2", bytes)
1820 t.Fatalf("string = %s", expected)
1821 }
1822}
1823
David Symonds22ac1502012-01-18 12:37:12 +11001824func fuzzUnmarshal(t *testing.T, data []byte) {
1825 defer func() {
1826 if e := recover(); e != nil {
1827 t.Errorf("These bytes caused a panic: %+v", data)
1828 t.Logf("Stack:\n%s", debug.Stack())
1829 t.FailNow()
1830 }
1831 }()
1832
1833 pb := new(MyMessage)
1834 Unmarshal(data, pb)
1835}
1836
David Symonds3ea3e052014-12-22 16:15:28 +11001837func TestMapFieldMarshal(t *testing.T) {
1838 m := &MessageWithMap{
1839 NameMapping: map[int32]string{
1840 1: "Rob",
1841 4: "Ian",
1842 8: "Dave",
1843 },
1844 }
1845 b, err := Marshal(m)
1846 if err != nil {
1847 t.Fatalf("Marshal: %v", err)
1848 }
1849
1850 // b should be the concatenation of these three byte sequences in some order.
1851 parts := []string{
1852 "\n\a\b\x01\x12\x03Rob",
1853 "\n\a\b\x04\x12\x03Ian",
1854 "\n\b\b\x08\x12\x04Dave",
1855 }
1856 ok := false
1857 for i := range parts {
1858 for j := range parts {
1859 if j == i {
1860 continue
1861 }
1862 for k := range parts {
1863 if k == i || k == j {
1864 continue
1865 }
1866 try := parts[i] + parts[j] + parts[k]
1867 if bytes.Equal(b, []byte(try)) {
1868 ok = true
1869 break
1870 }
1871 }
1872 }
1873 }
1874 if !ok {
1875 t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
1876 }
1877 t.Logf("FYI b: %q", b)
1878
1879 (new(Buffer)).DebugPrint("Dump of b", b)
1880}
1881
1882func TestMapFieldRoundTrips(t *testing.T) {
1883 m := &MessageWithMap{
1884 NameMapping: map[int32]string{
1885 1: "Rob",
1886 4: "Ian",
1887 8: "Dave",
1888 },
1889 MsgMapping: map[int64]*FloatingPoint{
1890 0x7001: &FloatingPoint{F: Float64(2.0)},
1891 },
1892 ByteMapping: map[bool][]byte{
1893 false: []byte("that's not right!"),
1894 true: []byte("aye, 'tis true!"),
1895 },
1896 }
1897 b, err := Marshal(m)
1898 if err != nil {
1899 t.Fatalf("Marshal: %v", err)
1900 }
1901 t.Logf("FYI b: %q", b)
1902 m2 := new(MessageWithMap)
1903 if err := Unmarshal(b, m2); err != nil {
1904 t.Fatalf("Unmarshal: %v", err)
1905 }
1906 for _, pair := range [][2]interface{}{
1907 {m.NameMapping, m2.NameMapping},
1908 {m.MsgMapping, m2.MsgMapping},
1909 {m.ByteMapping, m2.ByteMapping},
1910 } {
1911 if !reflect.DeepEqual(pair[0], pair[1]) {
1912 t.Errorf("Map did not survive a round trip.\ninitial: %v\n final: %v", pair[0], pair[1])
1913 }
1914 }
1915}
1916
David Symonds0bf1ad52013-10-11 09:07:50 +11001917// Benchmarks
1918
1919func testMsg() *GoTest {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001920 pb := initGoTest(true)
David Symonds0bf1ad52013-10-11 09:07:50 +11001921 const N = 1000 // Internally the library starts much smaller.
1922 pb.F_Int32Repeated = make([]int32, N)
1923 pb.F_DoubleRepeated = make([]float64, N)
1924 for i := 0; i < N; i++ {
1925 pb.F_Int32Repeated[i] = int32(i)
1926 pb.F_DoubleRepeated[i] = float64(i)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001927 }
Russ Coxd4ce3f12012-09-12 10:36:26 +10001928 return pb
1929}
David Symondsd9da6ba2011-08-30 14:41:30 +10001930
David Symonds0bf1ad52013-10-11 09:07:50 +11001931func bytesMsg() *GoTest {
1932 pb := initGoTest(true)
1933 buf := make([]byte, 4000)
1934 for i := range buf {
1935 buf[i] = byte(i)
1936 }
1937 pb.F_BytesDefaulted = buf
1938 return pb
1939}
1940
1941func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) {
1942 d, _ := marshal(pb)
1943 b.SetBytes(int64(len(d)))
1944 b.ResetTimer()
1945 for i := 0; i < b.N; i++ {
1946 marshal(pb)
1947 }
1948}
1949
1950func benchmarkBufferMarshal(b *testing.B, pb Message) {
Rob Pikeaaa3a622010-03-20 22:32:34 -07001951 p := NewBuffer(nil)
David Symonds0bf1ad52013-10-11 09:07:50 +11001952 benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1953 p.Reset()
1954 err := p.Marshal(pb0)
1955 return p.Bytes(), err
1956 })
1957}
1958
1959func benchmarkSize(b *testing.B, pb Message) {
1960 benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
1961 Size(pb)
1962 return nil, nil
1963 })
1964}
1965
1966func newOf(pb Message) Message {
1967 in := reflect.ValueOf(pb)
1968 if in.IsNil() {
1969 return pb
1970 }
1971 return reflect.New(in.Type().Elem()).Interface().(Message)
1972}
1973
1974func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) {
1975 d, _ := Marshal(pb)
1976 b.SetBytes(int64(len(d)))
1977 pbd := newOf(pb)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001978
Russ Coxd4ce3f12012-09-12 10:36:26 +10001979 b.ResetTimer()
Rob Pikeaaa3a622010-03-20 22:32:34 -07001980 for i := 0; i < b.N; i++ {
David Symonds0bf1ad52013-10-11 09:07:50 +11001981 unmarshal(d, pbd)
Rob Pikeaaa3a622010-03-20 22:32:34 -07001982 }
David Symonds0bf1ad52013-10-11 09:07:50 +11001983}
1984
1985func benchmarkBufferUnmarshal(b *testing.B, pb Message) {
1986 p := NewBuffer(nil)
1987 benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error {
1988 p.SetBuf(d)
1989 return p.Unmarshal(pb0)
1990 })
1991}
1992
1993// Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes}
1994
1995func BenchmarkMarshal(b *testing.B) {
1996 benchmarkMarshal(b, testMsg(), Marshal)
1997}
1998
1999func BenchmarkBufferMarshal(b *testing.B) {
2000 benchmarkBufferMarshal(b, testMsg())
2001}
2002
2003func BenchmarkSize(b *testing.B) {
2004 benchmarkSize(b, testMsg())
Rob Pikeaaa3a622010-03-20 22:32:34 -07002005}
2006
2007func BenchmarkUnmarshal(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11002008 benchmarkUnmarshal(b, testMsg(), Unmarshal)
2009}
Rob Pikeaaa3a622010-03-20 22:32:34 -07002010
David Symonds0bf1ad52013-10-11 09:07:50 +11002011func BenchmarkBufferUnmarshal(b *testing.B) {
2012 benchmarkBufferUnmarshal(b, testMsg())
Russ Coxd4ce3f12012-09-12 10:36:26 +10002013}
2014
2015func BenchmarkMarshalBytes(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11002016 benchmarkMarshal(b, bytesMsg(), Marshal)
2017}
Russ Coxd4ce3f12012-09-12 10:36:26 +10002018
David Symonds0bf1ad52013-10-11 09:07:50 +11002019func BenchmarkBufferMarshalBytes(b *testing.B) {
2020 benchmarkBufferMarshal(b, bytesMsg())
2021}
2022
2023func BenchmarkSizeBytes(b *testing.B) {
2024 benchmarkSize(b, bytesMsg())
Russ Coxd4ce3f12012-09-12 10:36:26 +10002025}
2026
2027func BenchmarkUnmarshalBytes(b *testing.B) {
David Symonds0bf1ad52013-10-11 09:07:50 +11002028 benchmarkUnmarshal(b, bytesMsg(), Unmarshal)
2029}
Russ Coxd4ce3f12012-09-12 10:36:26 +10002030
David Symonds0bf1ad52013-10-11 09:07:50 +11002031func BenchmarkBufferUnmarshalBytes(b *testing.B) {
2032 benchmarkBufferUnmarshal(b, bytesMsg())
Rob Pikeaaa3a622010-03-20 22:32:34 -07002033}
David Symondsc0287172012-08-15 11:10:30 +10002034
2035func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
2036 b.StopTimer()
2037 pb := initGoTestField()
2038 skip := &GoSkipTest{
2039 SkipInt32: Int32(32),
2040 SkipFixed32: Uint32(3232),
2041 SkipFixed64: Uint64(6464),
2042 SkipString: String("skipper"),
2043 Skipgroup: &GoSkipTest_SkipGroup{
2044 GroupInt32: Int32(75),
2045 GroupString: String("wxyz"),
2046 },
2047 }
2048
2049 pbd := new(GoTestField)
2050 p := NewBuffer(nil)
2051 p.Marshal(pb)
2052 p.Marshal(skip)
2053 p2 := NewBuffer(nil)
2054
2055 b.StartTimer()
2056 for i := 0; i < b.N; i++ {
2057 p2.SetBuf(p.Bytes())
2058 p2.Unmarshal(pbd)
2059 }
2060}