| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| David Symonds | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 4 | // https://github.com/golang/protobuf |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 32 | package proto_test |
| 33 | |
| 34 | import ( |
| 35 | "bytes" |
| Rob Pike | 022b101 | 2011-11-08 16:33:51 -0800 | [diff] [blame] | 36 | "encoding/json" |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 37 | "errors" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 38 | "fmt" |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 39 | "math" |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 40 | "math/rand" |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 41 | "reflect" |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 42 | "runtime/debug" |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 43 | "strings" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 44 | "testing" |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 45 | "time" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | |
| David Symonds | 704096f | 2012-03-15 15:10:26 +1100 | [diff] [blame] | 47 | . "./testdata" |
| David Symonds | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 48 | . "github.com/golang/protobuf/proto" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | var globalO *Buffer |
| 52 | |
| 53 | func old() *Buffer { |
| 54 | if globalO == nil { |
| 55 | globalO = NewBuffer(nil) |
| 56 | } |
| 57 | globalO.Reset() |
| 58 | return globalO |
| 59 | } |
| 60 | |
| 61 | func 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 | |
| 73 | func initGoTestField() *GoTestField { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 74 | f := new(GoTestField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 75 | 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.) |
| 83 | func initGoTest_RequiredGroup() *GoTest_RequiredGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 84 | return &GoTest_RequiredGroup{ |
| 85 | RequiredField: String("required"), |
| 86 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | func initGoTest_OptionalGroup() *GoTest_OptionalGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 90 | return &GoTest_OptionalGroup{ |
| 91 | RequiredField: String("optional"), |
| 92 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 96 | return &GoTest_RepeatedGroup{ |
| 97 | RequiredField: String("repeated"), |
| 98 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func initGoTest(setdefaults bool) *GoTest { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 102 | pb := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 103 | 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 Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 119 | pb.Kind = GoTest_TIME.Enum() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 120 | 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 | |
| 139 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 192 | // 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 | |
| 263 | func 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 | |
| 276 | func 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 | |
| 293 | func 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 Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 307 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 308 | 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.) |
| 328 | func 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 Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 398 | // fakeMarshaler is a simple struct implementing Marshaler and Message interfaces. |
| 399 | type fakeMarshaler struct { |
| 400 | b []byte |
| 401 | err error |
| 402 | } |
| 403 | |
| 404 | func (f fakeMarshaler) Marshal() ([]byte, error) { |
| 405 | return f.b, f.err |
| 406 | } |
| 407 | |
| 408 | func (f fakeMarshaler) String() string { |
| 409 | return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) |
| 410 | } |
| 411 | |
| 412 | func (f fakeMarshaler) ProtoMessage() {} |
| 413 | |
| 414 | func (f fakeMarshaler) Reset() {} |
| 415 | |
| 416 | // Simple tests for proto messages that implement the Marshaler interface. |
| 417 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 455 | // Simple tests for bytes |
| 456 | func 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 |
| 470 | func 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? |
| 486 | func TestRequiredBit(t *testing.T) { |
| 487 | o := old() |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 488 | pb := new(GoTest) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 489 | err := o.Marshal(pb) |
| 490 | if err == nil { |
| 491 | t.Error("did not catch missing required fields") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 492 | } else if strings.Index(err.Error(), "Kind") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 493 | t.Error("wrong error type:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 494 | } |
| 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. |
| 501 | func 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? |
| 544 | func 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. |
| 565 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 580 | "b304"+ // field 70, encoding 3, start group |
| 581 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 582 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // All required fields set, defaults provided. |
| 589 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 604 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 614 | "b304"+ // start group field 70 level 1 |
| 615 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 616 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 623 | |
| 624 | } |
| 625 | |
| 626 | // All default fields set to their default value by hand |
| 627 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 656 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 666 | "b304"+ // start group field 70 level 1 |
| 667 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 668 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 675 | |
| 676 | } |
| 677 | |
| 678 | // All required fields set, defaults provided, all non-defaulted optional fields have values. |
| 679 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 715 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 725 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 735 | "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 Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 740 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 750 | |
| 751 | } |
| 752 | |
| 753 | // All required fields set, defaults provided, all repeated fields given two values. |
| 754 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 787 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 807 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 817 | "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 Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 825 | "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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 838 | |
| 839 | } |
| 840 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 841 | // All required fields set, all packed repeated fields given two values. |
| 842 | func 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 Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 869 | "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 Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 884 | "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 Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 890 | "b21f02"+ // field 502, encoding 2, 2 bytes |
| 891 | "403f"+ // value 32, value -32 |
| 892 | "ba1f03"+ // field 503, encoding 2, 3 bytes |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 893 | "80017f") // value 64, value -64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 894 | } |
| 895 | |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 896 | // Test that we can encode empty bytes fields. |
| 897 | func TestEncodeDecodeBytes1(t *testing.T) { |
| 898 | pb := initGoTest(false) |
| 899 | |
| 900 | // Create our bytes |
| 901 | pb.F_BytesRequired = []byte{} |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 902 | pb.F_BytesRepeated = [][]byte{{}} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 903 | pb.F_BytesOptional = []byte{} |
| 904 | |
| 905 | d, err := Marshal(pb) |
| 906 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 907 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | pbd := new(GoTest) |
| 911 | if err := Unmarshal(d, pbd); err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 912 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 916 | t.Error("required empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 917 | } |
| 918 | if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 919 | t.Error("repeated empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 920 | } |
| 921 | if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 922 | t.Error("optional empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 923 | } |
| 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. |
| 928 | func TestEncodeDecodeBytes2(t *testing.T) { |
| 929 | pb := initGoTest(false) |
| 930 | |
| 931 | // Create our bytes |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 932 | pb.F_BytesRepeated = [][]byte{nil} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 933 | |
| 934 | d, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 935 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 936 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | pbd := new(GoTest) |
| 940 | if err := Unmarshal(d, pbd); err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 941 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 945 | t.Error("Unexpected value for repeated bytes field") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 949 | // All required fields set, defaults provided, all repeated fields given two values. |
| 950 | func 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 Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 958 | 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 968 | |
| 969 | // Marshal it into same buffer. |
| 970 | o.Marshal(skip) |
| 971 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 972 | pbd := new(GoTestField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 973 | o.Unmarshal(pbd) |
| 974 | |
| 975 | // The __unrecognized field should be a marshaling of GoSkipTest |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 976 | skipd := new(GoSkipTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 977 | |
| 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 Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1001 | // Check that unrecognized fields of a submessage are preserved. |
| 1002 | func 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 Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 1050 | // Check that an int32 field can be upgraded to an int64 field. |
| 1051 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1079 | // 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. |
| 1085 | func 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 Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1132 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1133 | 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. |
| 1196 | func 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 Symonds | baeae8b | 2014-06-23 09:41:27 +1000 | [diff] [blame] | 1206 | 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1213 | func encodeDecode(t *testing.T, in, out Message, msg string) { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1214 | 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 | |
| 1223 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1242 | func TestProto1RepeatedGroup(t *testing.T) { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1243 | pb := &MessageList{ |
| 1244 | Message: []*MessageList_Message{ |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 1245 | { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1246 | Name: String("blah"), |
| 1247 | Count: Int32(7), |
| 1248 | }, |
| 1249 | // NOTE: pb.Message[1] is a nil |
| 1250 | nil, |
| 1251 | }, |
| 1252 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1253 | |
| 1254 | o := old() |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame^] | 1255 | err := o.Marshal(pb) |
| 1256 | if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1257 | t.Fatalf("unexpected or no error when marshaling: %v", err) |
| 1258 | } |
| 1259 | } |
| 1260 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1261 | // 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. |
| 1264 | func TestEnum(t *testing.T) { |
| 1265 | pb := new(GoEnum) |
| David Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 1266 | pb.Foo = FOO_FOO1.Enum() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1267 | o := old() |
| 1268 | if err := o.Marshal(pb); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1269 | t.Fatal("error encoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1270 | } |
| 1271 | pb1 := new(GoEnum) |
| 1272 | if err := o.Unmarshal(pb1); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1273 | t.Fatal("error decoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1274 | } |
| 1275 | if *pb1.Foo != FOO_FOO1 { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1276 | t.Error("expected 7 but got ", *pb1.Foo) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1280 | // 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. |
| 1282 | func TestPrintingNilEnumFields(t *testing.T) { |
| 1283 | pb := new(GoEnum) |
| 1284 | fmt.Sprintf("%+v", pb) |
| 1285 | } |
| 1286 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1287 | // Verify that absent required fields cause Marshal/Unmarshal to return errors. |
| 1288 | func TestRequiredFieldEnforcement(t *testing.T) { |
| 1289 | pb := new(GoTestField) |
| 1290 | _, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1291 | if err == nil { |
| 1292 | t.Error("marshal: expected error, got nil") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1293 | } else if strings.Index(err.Error(), "Label") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1294 | t.Errorf("marshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1295 | } |
| 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 Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1302 | if err == nil { |
| 1303 | t.Error("unmarshal: expected error, got nil") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1304 | } else if strings.Index(err.Error(), "{Unknown}") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1305 | t.Errorf("unmarshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 1309 | func 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 Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1317 | // A type that implements the Marshaler interface, but is not nillable. |
| 1318 | type nonNillableInt uint64 |
| 1319 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 1320 | func (nni nonNillableInt) Marshal() ([]byte, error) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1321 | return EncodeVarint(uint64(nni)), nil |
| 1322 | } |
| 1323 | |
| 1324 | type NNIMessage struct { |
| 1325 | nni nonNillableInt |
| 1326 | } |
| 1327 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1328 | func (*NNIMessage) Reset() {} |
| 1329 | func (*NNIMessage) String() string { return "" } |
| 1330 | func (*NNIMessage) ProtoMessage() {} |
| 1331 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1332 | // A type that implements the Marshaler interface and is nillable. |
| 1333 | type nillableMessage struct { |
| 1334 | x uint64 |
| 1335 | } |
| 1336 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 1337 | func (nm *nillableMessage) Marshal() ([]byte, error) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1338 | return EncodeVarint(nm.x), nil |
| 1339 | } |
| 1340 | |
| 1341 | type NMMessage struct { |
| 1342 | nm *nillableMessage |
| 1343 | } |
| 1344 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1345 | func (*NMMessage) Reset() {} |
| 1346 | func (*NMMessage) String() string { return "" } |
| 1347 | func (*NMMessage) ProtoMessage() {} |
| 1348 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1349 | // Verify a type that uses the Marshaler interface, but has a nil pointer. |
| 1350 | func 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 Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1367 | func 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 Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 1387 | F_Enum: Defaults_GREEN.Enum(), |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1388 | F_Pinf: Float32(float32(math.Inf(1))), |
| 1389 | F_Ninf: Float32(float32(math.Inf(-1))), |
| 1390 | F_Nan: Float32(1.7), |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 1391 | StrZero: String(""), |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1392 | } |
| 1393 | SetDefaults(m) |
| 1394 | if !Equal(m, expected) { |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 1395 | t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected) |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | func 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 Symonds | 0e08492 | 2012-07-02 16:04:40 -0700 | [diff] [blame] | 1405 | if v := m.GetF_Int32(); v != 12 { |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1406 | t.Errorf("m.FInt32 = %v, want 12", v) |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | func 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 Symonds | 814b936 | 2011-12-13 09:10:47 +1100 | [diff] [blame] | 1426 | t.Errorf("\n got %v\nwant %v", m, expected) |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
| David Symonds | 472e259 | 2013-07-15 11:10:07 +1000 | [diff] [blame] | 1430 | func 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 Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 1445 | func 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 Symonds | 0e08492 | 2012-07-02 16:04:40 -0700 | [diff] [blame] | 1457 | if got, want := m2.GetLastField(), *m.LastField; got != want { |
| David Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 1458 | t.Errorf("got %q, want %q", got, want) |
| 1459 | } |
| 1460 | } |
| 1461 | |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1462 | func 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 Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1469 | Bikeshed: MyMessage_GREEN.Enum(), |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1470 | } |
| David Symonds | 4af5f1f | 2013-10-11 10:08:35 +1100 | [diff] [blame] | 1471 | const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}` |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1472 | |
| 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 Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1481 | |
| 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 Symonds | 4af5f1f | 2013-10-11 10:08:35 +1100 | [diff] [blame] | 1490 | // Test unmarshalling of JSON with symbolic enum name. |
| 1491 | const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}` |
| David Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1492 | 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 Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1501 | func 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 | |
| 1511 | func 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 Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 1517 | func 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 | |
| 1523 | func 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 Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1531 | func 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 Symonds | e182aaf | 2013-01-30 17:04:37 +1100 | [diff] [blame] | 1545 | func TestMergeMessages(t *testing.T) { |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1546 | 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 Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1564 | if err := UnmarshalMerge(data, pb2); err != nil { |
| 1565 | t.Fatalf("first UnmarshalMerge: %v", err) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1566 | } |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1567 | if err := UnmarshalMerge(data, pb2); err != nil { |
| 1568 | t.Fatalf("second UnmarshalMerge: %v", err) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1569 | } |
| 1570 | if len(pb2.Message) != 2 { |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1571 | t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message)) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1575 | func TestExtensionMarshalOrder(t *testing.T) { |
| David Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1576 | m := &MyMessage{Count: Int(123)} |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1577 | 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 Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1589 | for i := 0; i < 100; i++ { |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1590 | 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 Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1604 | // Many extensions, because small maps might not iterate differently on each iteration. |
| 1605 | var 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 | |
| 1658 | func 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 Symonds | 2ce8ed4 | 2013-06-20 13:22:17 +1000 | [diff] [blame] | 1695 | func 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 Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 1738 | func TestEncodingSizes(t *testing.T) { |
| 1739 | tests := []struct { |
| 1740 | m Message |
| 1741 | n int |
| 1742 | }{ |
| 1743 | {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6}, |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 1744 | {&Defaults{F_Int32: Int32(math.MinInt32)}, 11}, |
| 1745 | {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6}, |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 1746 | {&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 Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1760 | func TestRequiredNotSetError(t *testing.T) { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1761 | 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 Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1785 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1786 | fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1787 | 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 Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1801 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1802 | t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1803 | 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 Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1810 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1811 | t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1812 | 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 Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1824 | func 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 Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1837 | func 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 | |
| 1882 | func 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 Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1917 | // Benchmarks |
| 1918 | |
| 1919 | func testMsg() *GoTest { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1920 | pb := initGoTest(true) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1921 | 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1927 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1928 | return pb |
| 1929 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 1930 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1931 | func 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 | |
| 1941 | func 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 | |
| 1950 | func benchmarkBufferMarshal(b *testing.B, pb Message) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1951 | p := NewBuffer(nil) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1952 | benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) { |
| 1953 | p.Reset() |
| 1954 | err := p.Marshal(pb0) |
| 1955 | return p.Bytes(), err |
| 1956 | }) |
| 1957 | } |
| 1958 | |
| 1959 | func 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 | |
| 1966 | func 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 | |
| 1974 | func 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 Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1978 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1979 | b.ResetTimer() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1980 | for i := 0; i < b.N; i++ { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1981 | unmarshal(d, pbd) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1982 | } |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | func 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 | |
| 1995 | func BenchmarkMarshal(b *testing.B) { |
| 1996 | benchmarkMarshal(b, testMsg(), Marshal) |
| 1997 | } |
| 1998 | |
| 1999 | func BenchmarkBufferMarshal(b *testing.B) { |
| 2000 | benchmarkBufferMarshal(b, testMsg()) |
| 2001 | } |
| 2002 | |
| 2003 | func BenchmarkSize(b *testing.B) { |
| 2004 | benchmarkSize(b, testMsg()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | func BenchmarkUnmarshal(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2008 | benchmarkUnmarshal(b, testMsg(), Unmarshal) |
| 2009 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2010 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2011 | func BenchmarkBufferUnmarshal(b *testing.B) { |
| 2012 | benchmarkBufferUnmarshal(b, testMsg()) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | func BenchmarkMarshalBytes(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2016 | benchmarkMarshal(b, bytesMsg(), Marshal) |
| 2017 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2018 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2019 | func BenchmarkBufferMarshalBytes(b *testing.B) { |
| 2020 | benchmarkBufferMarshal(b, bytesMsg()) |
| 2021 | } |
| 2022 | |
| 2023 | func BenchmarkSizeBytes(b *testing.B) { |
| 2024 | benchmarkSize(b, bytesMsg()) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | func BenchmarkUnmarshalBytes(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2028 | benchmarkUnmarshal(b, bytesMsg(), Unmarshal) |
| 2029 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2030 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2031 | func BenchmarkBufferUnmarshalBytes(b *testing.B) { |
| 2032 | benchmarkBufferUnmarshal(b, bytesMsg()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2033 | } |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 2034 | |
| 2035 | func 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 | } |