| 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 | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 47 | . "github.com/golang/protobuf/proto" |
| David Symonds | a8de284 | 2015-03-20 16:24:29 +1100 | [diff] [blame] | 48 | . "github.com/golang/protobuf/proto/testdata" |
| 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 | |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 404 | func (f *fakeMarshaler) Marshal() ([]byte, error) { return f.b, f.err } |
| 405 | func (f *fakeMarshaler) String() string { return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) } |
| 406 | func (f *fakeMarshaler) ProtoMessage() {} |
| 407 | func (f *fakeMarshaler) Reset() {} |
| 408 | |
| 409 | type msgWithFakeMarshaler struct { |
| 410 | M *fakeMarshaler `protobuf:"bytes,1,opt,name=fake"` |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 411 | } |
| 412 | |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 413 | func (m *msgWithFakeMarshaler) String() string { return CompactTextString(m) } |
| 414 | func (m *msgWithFakeMarshaler) ProtoMessage() {} |
| 415 | func (m *msgWithFakeMarshaler) Reset() {} |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 416 | |
| 417 | // Simple tests for proto messages that implement the Marshaler interface. |
| 418 | func TestMarshalerEncoding(t *testing.T) { |
| 419 | tests := []struct { |
| 420 | name string |
| 421 | m Message |
| 422 | want []byte |
| 423 | wantErr error |
| 424 | }{ |
| 425 | { |
| 426 | name: "Marshaler that fails", |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 427 | m: &fakeMarshaler{ |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 428 | err: errors.New("some marshal err"), |
| 429 | b: []byte{5, 6, 7}, |
| 430 | }, |
| 431 | // Since there's an error, nothing should be written to buffer. |
| 432 | want: nil, |
| 433 | wantErr: errors.New("some marshal err"), |
| 434 | }, |
| 435 | { |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 436 | name: "Marshaler that fails with RequiredNotSetError", |
| 437 | m: &msgWithFakeMarshaler{ |
| 438 | M: &fakeMarshaler{ |
| 439 | err: &RequiredNotSetError{}, |
| 440 | b: []byte{5, 6, 7}, |
| 441 | }, |
| 442 | }, |
| 443 | // Since there's an error that can be continued after, |
| 444 | // the buffer should be written. |
| 445 | want: []byte{ |
| 446 | 10, 3, // for &msgWithFakeMarshaler |
| 447 | 5, 6, 7, // for &fakeMarshaler |
| 448 | }, |
| 449 | wantErr: &RequiredNotSetError{}, |
| 450 | }, |
| 451 | { |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 452 | name: "Marshaler that succeeds", |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 453 | m: &fakeMarshaler{ |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 454 | b: []byte{0, 1, 2, 3, 4, 127, 255}, |
| 455 | }, |
| 456 | want: []byte{0, 1, 2, 3, 4, 127, 255}, |
| 457 | wantErr: nil, |
| 458 | }, |
| 459 | } |
| 460 | for _, test := range tests { |
| 461 | b := NewBuffer(nil) |
| 462 | err := b.Marshal(test.m) |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 463 | if _, ok := err.(*RequiredNotSetError); ok { |
| 464 | // We're not in package proto, so we can only assert the type in this case. |
| 465 | err = &RequiredNotSetError{} |
| 466 | } |
| David Symonds | 29bcc89 | 2014-04-15 14:01:13 +1000 | [diff] [blame] | 467 | if !reflect.DeepEqual(test.wantErr, err) { |
| 468 | t.Errorf("%s: got err %v wanted %v", test.name, err, test.wantErr) |
| 469 | } |
| 470 | if !reflect.DeepEqual(test.want, b.Bytes()) { |
| 471 | t.Errorf("%s: got bytes %v wanted %v", test.name, b.Bytes(), test.want) |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 476 | // Simple tests for bytes |
| 477 | func TestBytesPrimitives(t *testing.T) { |
| 478 | o := old() |
| 479 | bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'} |
| 480 | if o.EncodeRawBytes(bytes) != nil { |
| 481 | t.Error("EncodeRawBytes") |
| 482 | } |
| 483 | decb, e := o.DecodeRawBytes(false) |
| 484 | if e != nil { |
| 485 | t.Error("DecodeRawBytes") |
| 486 | } |
| 487 | equalbytes(bytes, decb, t) |
| 488 | } |
| 489 | |
| 490 | // Simple tests for strings |
| 491 | func TestStringPrimitives(t *testing.T) { |
| 492 | o := old() |
| 493 | s := "now is the time" |
| 494 | if o.EncodeStringBytes(s) != nil { |
| 495 | t.Error("enc_string") |
| 496 | } |
| 497 | decs, e := o.DecodeStringBytes() |
| 498 | if e != nil { |
| 499 | t.Error("dec_string") |
| 500 | } |
| 501 | if s != decs { |
| 502 | t.Error("string encode/decode fail:", s, decs) |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | // Do we catch the "required bit not set" case? |
| 507 | func TestRequiredBit(t *testing.T) { |
| 508 | o := old() |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 509 | pb := new(GoTest) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 510 | err := o.Marshal(pb) |
| 511 | if err == nil { |
| 512 | t.Error("did not catch missing required fields") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 513 | } else if strings.Index(err.Error(), "Kind") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 514 | t.Error("wrong error type:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | // Check that all fields are nil. |
| 519 | // Clearly silly, and a residue from a more interesting test with an earlier, |
| 520 | // different initialization property, but it once caught a compiler bug so |
| 521 | // it lives. |
| 522 | func checkInitialized(pb *GoTest, t *testing.T) { |
| 523 | if pb.F_BoolDefaulted != nil { |
| 524 | t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted) |
| 525 | } |
| 526 | if pb.F_Int32Defaulted != nil { |
| 527 | t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted) |
| 528 | } |
| 529 | if pb.F_Int64Defaulted != nil { |
| 530 | t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted) |
| 531 | } |
| 532 | if pb.F_Fixed32Defaulted != nil { |
| 533 | t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted) |
| 534 | } |
| 535 | if pb.F_Fixed64Defaulted != nil { |
| 536 | t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted) |
| 537 | } |
| 538 | if pb.F_Uint32Defaulted != nil { |
| 539 | t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted) |
| 540 | } |
| 541 | if pb.F_Uint64Defaulted != nil { |
| 542 | t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted) |
| 543 | } |
| 544 | if pb.F_FloatDefaulted != nil { |
| 545 | t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted) |
| 546 | } |
| 547 | if pb.F_DoubleDefaulted != nil { |
| 548 | t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted) |
| 549 | } |
| 550 | if pb.F_StringDefaulted != nil { |
| 551 | t.Error("New or Reset did not set string:", *pb.F_StringDefaulted) |
| 552 | } |
| 553 | if pb.F_BytesDefaulted != nil { |
| 554 | t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted)) |
| 555 | } |
| 556 | if pb.F_Sint32Defaulted != nil { |
| 557 | t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted) |
| 558 | } |
| 559 | if pb.F_Sint64Defaulted != nil { |
| 560 | t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted) |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Does Reset() reset? |
| 565 | func TestReset(t *testing.T) { |
| 566 | pb := initGoTest(true) |
| 567 | // muck with some values |
| 568 | pb.F_BoolDefaulted = Bool(false) |
| 569 | pb.F_Int32Defaulted = Int32(237) |
| 570 | pb.F_Int64Defaulted = Int64(12346) |
| 571 | pb.F_Fixed32Defaulted = Uint32(32000) |
| 572 | pb.F_Fixed64Defaulted = Uint64(666) |
| 573 | pb.F_Uint32Defaulted = Uint32(323232) |
| 574 | pb.F_Uint64Defaulted = nil |
| 575 | pb.F_FloatDefaulted = nil |
| 576 | pb.F_DoubleDefaulted = Float64(0) |
| 577 | pb.F_StringDefaulted = String("gotcha") |
| 578 | pb.F_BytesDefaulted = []byte("asdfasdf") |
| 579 | pb.F_Sint32Defaulted = Int32(123) |
| 580 | pb.F_Sint64Defaulted = Int64(789) |
| 581 | pb.Reset() |
| 582 | checkInitialized(pb, t) |
| 583 | } |
| 584 | |
| 585 | // All required fields set, no defaults provided. |
| 586 | func TestEncodeDecode1(t *testing.T) { |
| 587 | pb := initGoTest(false) |
| 588 | overify(t, pb, |
| 589 | "0807"+ // field 1, encoding 0, value 7 |
| 590 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 591 | "5001"+ // field 10, encoding 0, value 1 |
| 592 | "5803"+ // field 11, encoding 0, value 3 |
| 593 | "6006"+ // field 12, encoding 0, value 6 |
| 594 | "6d20000000"+ // field 13, encoding 5, value 0x20 |
| 595 | "714000000000000000"+ // field 14, encoding 1, value 0x40 |
| 596 | "78a019"+ // field 15, encoding 0, value 0xca0 = 3232 |
| 597 | "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464 |
| 598 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 599 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 600 | "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 601 | "b304"+ // field 70, encoding 3, start group |
| 602 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 603 | "b404"+ // field 70, encoding 4, end group |
| 604 | "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes" |
| 605 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 606 | "b8067f") // field 103, encoding 0, 0x7f zigzag64 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // All required fields set, defaults provided. |
| 610 | func TestEncodeDecode2(t *testing.T) { |
| 611 | pb := initGoTest(true) |
| 612 | overify(t, pb, |
| 613 | "0807"+ // field 1, encoding 0, value 7 |
| 614 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 615 | "5001"+ // field 10, encoding 0, value 1 |
| 616 | "5803"+ // field 11, encoding 0, value 3 |
| 617 | "6006"+ // field 12, encoding 0, value 6 |
| 618 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 619 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 620 | "78a019"+ // field 15, encoding 0, value 3232 |
| 621 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 622 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 623 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 624 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 625 | "c00201"+ // field 40, encoding 0, value 1 |
| 626 | "c80220"+ // field 41, encoding 0, value 32 |
| 627 | "d00240"+ // field 42, encoding 0, value 64 |
| 628 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 629 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 630 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 631 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 632 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 633 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 634 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 635 | "b304"+ // start group field 70 level 1 |
| 636 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 637 | "b404"+ // end group field 70 level 1 |
| 638 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 639 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 640 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 641 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| 642 | "90193f"+ // field 402, encoding 0, value 63 |
| 643 | "98197f") // field 403, encoding 0, value 127 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 644 | |
| 645 | } |
| 646 | |
| 647 | // All default fields set to their default value by hand |
| 648 | func TestEncodeDecode3(t *testing.T) { |
| 649 | pb := initGoTest(false) |
| 650 | pb.F_BoolDefaulted = Bool(true) |
| 651 | pb.F_Int32Defaulted = Int32(32) |
| 652 | pb.F_Int64Defaulted = Int64(64) |
| 653 | pb.F_Fixed32Defaulted = Uint32(320) |
| 654 | pb.F_Fixed64Defaulted = Uint64(640) |
| 655 | pb.F_Uint32Defaulted = Uint32(3200) |
| 656 | pb.F_Uint64Defaulted = Uint64(6400) |
| 657 | pb.F_FloatDefaulted = Float32(314159) |
| 658 | pb.F_DoubleDefaulted = Float64(271828) |
| 659 | pb.F_StringDefaulted = String("hello, \"world!\"\n") |
| 660 | pb.F_BytesDefaulted = []byte("Bignose") |
| 661 | pb.F_Sint32Defaulted = Int32(-32) |
| 662 | pb.F_Sint64Defaulted = Int64(-64) |
| 663 | |
| 664 | overify(t, pb, |
| 665 | "0807"+ // field 1, encoding 0, value 7 |
| 666 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 667 | "5001"+ // field 10, encoding 0, value 1 |
| 668 | "5803"+ // field 11, encoding 0, value 3 |
| 669 | "6006"+ // field 12, encoding 0, value 6 |
| 670 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 671 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 672 | "78a019"+ // field 15, encoding 0, value 3232 |
| 673 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 674 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 675 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 676 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 677 | "c00201"+ // field 40, encoding 0, value 1 |
| 678 | "c80220"+ // field 41, encoding 0, value 32 |
| 679 | "d00240"+ // field 42, encoding 0, value 64 |
| 680 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 681 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 682 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 683 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 684 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 685 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 686 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 687 | "b304"+ // start group field 70 level 1 |
| 688 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 689 | "b404"+ // end group field 70 level 1 |
| 690 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 691 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 692 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 693 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| 694 | "90193f"+ // field 402, encoding 0, value 63 |
| 695 | "98197f") // field 403, encoding 0, value 127 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 696 | |
| 697 | } |
| 698 | |
| 699 | // All required fields set, defaults provided, all non-defaulted optional fields have values. |
| 700 | func TestEncodeDecode4(t *testing.T) { |
| 701 | pb := initGoTest(true) |
| 702 | pb.Table = String("hello") |
| 703 | pb.Param = Int32(7) |
| 704 | pb.OptionalField = initGoTestField() |
| 705 | pb.F_BoolOptional = Bool(true) |
| 706 | pb.F_Int32Optional = Int32(32) |
| 707 | pb.F_Int64Optional = Int64(64) |
| 708 | pb.F_Fixed32Optional = Uint32(3232) |
| 709 | pb.F_Fixed64Optional = Uint64(6464) |
| 710 | pb.F_Uint32Optional = Uint32(323232) |
| 711 | pb.F_Uint64Optional = Uint64(646464) |
| 712 | pb.F_FloatOptional = Float32(32.) |
| 713 | pb.F_DoubleOptional = Float64(64.) |
| 714 | pb.F_StringOptional = String("hello") |
| 715 | pb.F_BytesOptional = []byte("Bignose") |
| 716 | pb.F_Sint32Optional = Int32(-32) |
| 717 | pb.F_Sint64Optional = Int64(-64) |
| 718 | pb.Optionalgroup = initGoTest_OptionalGroup() |
| 719 | |
| 720 | overify(t, pb, |
| 721 | "0807"+ // field 1, encoding 0, value 7 |
| 722 | "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello" |
| 723 | "1807"+ // field 3, encoding 0, value 7 |
| 724 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 725 | "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField) |
| 726 | "5001"+ // field 10, encoding 0, value 1 |
| 727 | "5803"+ // field 11, encoding 0, value 3 |
| 728 | "6006"+ // field 12, encoding 0, value 6 |
| 729 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 730 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 731 | "78a019"+ // field 15, encoding 0, value 3232 |
| 732 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 733 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 734 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 735 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 736 | "f00101"+ // field 30, encoding 0, value 1 |
| 737 | "f80120"+ // field 31, encoding 0, value 32 |
| 738 | "800240"+ // field 32, encoding 0, value 64 |
| 739 | "8d02a00c0000"+ // field 33, encoding 5, value 3232 |
| 740 | "91024019000000000000"+ // field 34, encoding 1, value 6464 |
| 741 | "9802a0dd13"+ // field 35, encoding 0, value 323232 |
| 742 | "a002c0ba27"+ // field 36, encoding 0, value 646464 |
| 743 | "ad0200000042"+ // field 37, encoding 5, value 32.0 |
| 744 | "b1020000000000005040"+ // field 38, encoding 1, value 64.0 |
| 745 | "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 746 | "c00201"+ // field 40, encoding 0, value 1 |
| 747 | "c80220"+ // field 41, encoding 0, value 32 |
| 748 | "d00240"+ // field 42, encoding 0, value 64 |
| 749 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 750 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 751 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 752 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 753 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 754 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 755 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 756 | "b304"+ // start group field 70 level 1 |
| 757 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 758 | "b404"+ // end group field 70 level 1 |
| 759 | "d305"+ // start group field 90 level 1 |
| 760 | "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 761 | "d405"+ // end group field 90 level 1 |
| 762 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 763 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 764 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 765 | "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose" |
| 766 | "f0123f"+ // field 302, encoding 0, value 63 |
| 767 | "f8127f"+ // field 303, encoding 0, value 127 |
| 768 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| 769 | "90193f"+ // field 402, encoding 0, value 63 |
| 770 | "98197f") // field 403, encoding 0, value 127 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 771 | |
| 772 | } |
| 773 | |
| 774 | // All required fields set, defaults provided, all repeated fields given two values. |
| 775 | func TestEncodeDecode5(t *testing.T) { |
| 776 | pb := initGoTest(true) |
| 777 | pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()} |
| 778 | pb.F_BoolRepeated = []bool{false, true} |
| 779 | pb.F_Int32Repeated = []int32{32, 33} |
| 780 | pb.F_Int64Repeated = []int64{64, 65} |
| 781 | pb.F_Fixed32Repeated = []uint32{3232, 3333} |
| 782 | pb.F_Fixed64Repeated = []uint64{6464, 6565} |
| 783 | pb.F_Uint32Repeated = []uint32{323232, 333333} |
| 784 | pb.F_Uint64Repeated = []uint64{646464, 656565} |
| 785 | pb.F_FloatRepeated = []float32{32., 33.} |
| 786 | pb.F_DoubleRepeated = []float64{64., 65.} |
| 787 | pb.F_StringRepeated = []string{"hello", "sailor"} |
| 788 | pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")} |
| 789 | pb.F_Sint32Repeated = []int32{32, -32} |
| 790 | pb.F_Sint64Repeated = []int64{64, -64} |
| 791 | pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()} |
| 792 | |
| 793 | overify(t, pb, |
| 794 | "0807"+ // field 1, encoding 0, value 7 |
| 795 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 796 | "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField) |
| 797 | "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField) |
| 798 | "5001"+ // field 10, encoding 0, value 1 |
| 799 | "5803"+ // field 11, encoding 0, value 3 |
| 800 | "6006"+ // field 12, encoding 0, value 6 |
| 801 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 802 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 803 | "78a019"+ // field 15, encoding 0, value 3232 |
| 804 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 805 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 806 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 807 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 808 | "a00100"+ // field 20, encoding 0, value 0 |
| 809 | "a00101"+ // field 20, encoding 0, value 1 |
| 810 | "a80120"+ // field 21, encoding 0, value 32 |
| 811 | "a80121"+ // field 21, encoding 0, value 33 |
| 812 | "b00140"+ // field 22, encoding 0, value 64 |
| 813 | "b00141"+ // field 22, encoding 0, value 65 |
| 814 | "bd01a00c0000"+ // field 23, encoding 5, value 3232 |
| 815 | "bd01050d0000"+ // field 23, encoding 5, value 3333 |
| 816 | "c1014019000000000000"+ // field 24, encoding 1, value 6464 |
| 817 | "c101a519000000000000"+ // field 24, encoding 1, value 6565 |
| 818 | "c801a0dd13"+ // field 25, encoding 0, value 323232 |
| 819 | "c80195ac14"+ // field 25, encoding 0, value 333333 |
| 820 | "d001c0ba27"+ // field 26, encoding 0, value 646464 |
| 821 | "d001b58928"+ // field 26, encoding 0, value 656565 |
| 822 | "dd0100000042"+ // field 27, encoding 5, value 32.0 |
| 823 | "dd0100000442"+ // field 27, encoding 5, value 33.0 |
| 824 | "e1010000000000005040"+ // field 28, encoding 1, value 64.0 |
| 825 | "e1010000000000405040"+ // field 28, encoding 1, value 65.0 |
| 826 | "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello" |
| 827 | "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 828 | "c00201"+ // field 40, encoding 0, value 1 |
| 829 | "c80220"+ // field 41, encoding 0, value 32 |
| 830 | "d00240"+ // field 42, encoding 0, value 64 |
| 831 | "dd0240010000"+ // field 43, encoding 5, value 320 |
| 832 | "e1028002000000000000"+ // field 44, encoding 1, value 640 |
| 833 | "e8028019"+ // field 45, encoding 0, value 3200 |
| 834 | "f0028032"+ // field 46, encoding 0, value 6400 |
| 835 | "fd02e0659948"+ // field 47, encoding 5, value 314159.0 |
| 836 | "81030000000050971041"+ // field 48, encoding 1, value 271828.0 |
| 837 | "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 838 | "b304"+ // start group field 70 level 1 |
| 839 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 840 | "b404"+ // end group field 70 level 1 |
| 841 | "8305"+ // start group field 80 level 1 |
| 842 | "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated" |
| 843 | "8405"+ // end group field 80 level 1 |
| 844 | "8305"+ // start group field 80 level 1 |
| 845 | "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated" |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 846 | "8405"+ // end group field 80 level 1 |
| 847 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 848 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 849 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| 850 | "ca0c03"+"626967"+ // field 201, encoding 2, string "big" |
| 851 | "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose" |
| 852 | "d00c40"+ // field 202, encoding 0, value 32 |
| 853 | "d00c3f"+ // field 202, encoding 0, value -32 |
| 854 | "d80c8001"+ // field 203, encoding 0, value 64 |
| 855 | "d80c7f"+ // field 203, encoding 0, value -64 |
| 856 | "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose" |
| 857 | "90193f"+ // field 402, encoding 0, value 63 |
| 858 | "98197f") // field 403, encoding 0, value 127 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 859 | |
| 860 | } |
| 861 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 862 | // All required fields set, all packed repeated fields given two values. |
| 863 | func TestEncodeDecode6(t *testing.T) { |
| 864 | pb := initGoTest(false) |
| 865 | pb.F_BoolRepeatedPacked = []bool{false, true} |
| 866 | pb.F_Int32RepeatedPacked = []int32{32, 33} |
| 867 | pb.F_Int64RepeatedPacked = []int64{64, 65} |
| 868 | pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333} |
| 869 | pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565} |
| 870 | pb.F_Uint32RepeatedPacked = []uint32{323232, 333333} |
| 871 | pb.F_Uint64RepeatedPacked = []uint64{646464, 656565} |
| 872 | pb.F_FloatRepeatedPacked = []float32{32., 33.} |
| 873 | pb.F_DoubleRepeatedPacked = []float64{64., 65.} |
| 874 | pb.F_Sint32RepeatedPacked = []int32{32, -32} |
| 875 | pb.F_Sint64RepeatedPacked = []int64{64, -64} |
| 876 | |
| 877 | overify(t, pb, |
| 878 | "0807"+ // field 1, encoding 0, value 7 |
| 879 | "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField) |
| 880 | "5001"+ // field 10, encoding 0, value 1 |
| 881 | "5803"+ // field 11, encoding 0, value 3 |
| 882 | "6006"+ // field 12, encoding 0, value 6 |
| 883 | "6d20000000"+ // field 13, encoding 5, value 32 |
| 884 | "714000000000000000"+ // field 14, encoding 1, value 64 |
| 885 | "78a019"+ // field 15, encoding 0, value 3232 |
| 886 | "8001c032"+ // field 16, encoding 0, value 6464 |
| 887 | "8d0100004a45"+ // field 17, encoding 5, value 3232.0 |
| 888 | "9101000000000040b940"+ // field 18, encoding 1, value 6464.0 |
| 889 | "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string" |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 890 | "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1 |
| 891 | "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33 |
| 892 | "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65 |
| 893 | "aa0308"+ // field 53, encoding 2, 8 bytes |
| 894 | "a00c0000050d0000"+ // value 3232, value 3333 |
| 895 | "b20310"+ // field 54, encoding 2, 16 bytes |
| 896 | "4019000000000000a519000000000000"+ // value 6464, value 6565 |
| 897 | "ba0306"+ // field 55, encoding 2, 6 bytes |
| 898 | "a0dd1395ac14"+ // value 323232, value 333333 |
| 899 | "c20306"+ // field 56, encoding 2, 6 bytes |
| 900 | "c0ba27b58928"+ // value 646464, value 656565 |
| 901 | "ca0308"+ // field 57, encoding 2, 8 bytes |
| 902 | "0000004200000442"+ // value 32.0, value 33.0 |
| 903 | "d20310"+ // field 58, encoding 2, 16 bytes |
| 904 | "00000000000050400000000000405040"+ // value 64.0, value 65.0 |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 905 | "b304"+ // start group field 70 level 1 |
| 906 | "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required" |
| 907 | "b404"+ // end group field 70 level 1 |
| 908 | "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes" |
| 909 | "b0063f"+ // field 102, encoding 0, 0x3f zigzag32 |
| 910 | "b8067f"+ // field 103, encoding 0, 0x7f zigzag64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 911 | "b21f02"+ // field 502, encoding 2, 2 bytes |
| 912 | "403f"+ // value 32, value -32 |
| 913 | "ba1f03"+ // field 503, encoding 2, 3 bytes |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 914 | "80017f") // value 64, value -64 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 915 | } |
| 916 | |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 917 | // Test that we can encode empty bytes fields. |
| 918 | func TestEncodeDecodeBytes1(t *testing.T) { |
| 919 | pb := initGoTest(false) |
| 920 | |
| 921 | // Create our bytes |
| 922 | pb.F_BytesRequired = []byte{} |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 923 | pb.F_BytesRepeated = [][]byte{{}} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 924 | pb.F_BytesOptional = []byte{} |
| 925 | |
| 926 | d, err := Marshal(pb) |
| 927 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 928 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | pbd := new(GoTest) |
| 932 | if err := Unmarshal(d, pbd); err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 933 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 937 | t.Error("required empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 938 | } |
| 939 | 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] | 940 | t.Error("repeated empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 941 | } |
| 942 | if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 943 | t.Error("optional empty bytes field is incorrect") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | // Test that we encode nil-valued fields of a repeated bytes field correctly. |
| 948 | // Since entries in a repeated field cannot be nil, nil must mean empty value. |
| 949 | func TestEncodeDecodeBytes2(t *testing.T) { |
| 950 | pb := initGoTest(false) |
| 951 | |
| 952 | // Create our bytes |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 953 | pb.F_BytesRepeated = [][]byte{nil} |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 954 | |
| 955 | d, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 956 | if err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 957 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | pbd := new(GoTest) |
| 961 | if err := Unmarshal(d, pbd); err != nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 962 | t.Error(err) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 966 | t.Error("Unexpected value for repeated bytes field") |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 970 | // All required fields set, defaults provided, all repeated fields given two values. |
| 971 | func TestSkippingUnrecognizedFields(t *testing.T) { |
| 972 | o := old() |
| 973 | pb := initGoTestField() |
| 974 | |
| 975 | // Marshal it normally. |
| 976 | o.Marshal(pb) |
| 977 | |
| 978 | // Now new a GoSkipTest record. |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 979 | skip := &GoSkipTest{ |
| 980 | SkipInt32: Int32(32), |
| 981 | SkipFixed32: Uint32(3232), |
| 982 | SkipFixed64: Uint64(6464), |
| 983 | SkipString: String("skipper"), |
| 984 | Skipgroup: &GoSkipTest_SkipGroup{ |
| 985 | GroupInt32: Int32(75), |
| 986 | GroupString: String("wxyz"), |
| 987 | }, |
| 988 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 989 | |
| 990 | // Marshal it into same buffer. |
| 991 | o.Marshal(skip) |
| 992 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 993 | pbd := new(GoTestField) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 994 | o.Unmarshal(pbd) |
| 995 | |
| 996 | // The __unrecognized field should be a marshaling of GoSkipTest |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 997 | skipd := new(GoSkipTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 998 | |
| 999 | o.SetBuf(pbd.XXX_unrecognized) |
| 1000 | o.Unmarshal(skipd) |
| 1001 | |
| 1002 | if *skipd.SkipInt32 != *skip.SkipInt32 { |
| 1003 | t.Error("skip int32", skipd.SkipInt32) |
| 1004 | } |
| 1005 | if *skipd.SkipFixed32 != *skip.SkipFixed32 { |
| 1006 | t.Error("skip fixed32", skipd.SkipFixed32) |
| 1007 | } |
| 1008 | if *skipd.SkipFixed64 != *skip.SkipFixed64 { |
| 1009 | t.Error("skip fixed64", skipd.SkipFixed64) |
| 1010 | } |
| 1011 | if *skipd.SkipString != *skip.SkipString { |
| 1012 | t.Error("skip string", *skipd.SkipString) |
| 1013 | } |
| 1014 | if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 { |
| 1015 | t.Error("skip group int32", skipd.Skipgroup.GroupInt32) |
| 1016 | } |
| 1017 | if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString { |
| 1018 | t.Error("skip group string", *skipd.Skipgroup.GroupString) |
| 1019 | } |
| 1020 | } |
| 1021 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1022 | // Check that unrecognized fields of a submessage are preserved. |
| 1023 | func TestSubmessageUnrecognizedFields(t *testing.T) { |
| 1024 | nm := &NewMessage{ |
| 1025 | Nested: &NewMessage_Nested{ |
| 1026 | Name: String("Nigel"), |
| 1027 | FoodGroup: String("carbs"), |
| 1028 | }, |
| 1029 | } |
| 1030 | b, err := Marshal(nm) |
| 1031 | if err != nil { |
| 1032 | t.Fatalf("Marshal of NewMessage: %v", err) |
| 1033 | } |
| 1034 | |
| 1035 | // Unmarshal into an OldMessage. |
| 1036 | om := new(OldMessage) |
| 1037 | if err := Unmarshal(b, om); err != nil { |
| 1038 | t.Fatalf("Unmarshal to OldMessage: %v", err) |
| 1039 | } |
| 1040 | exp := &OldMessage{ |
| 1041 | Nested: &OldMessage_Nested{ |
| 1042 | Name: String("Nigel"), |
| 1043 | // normal protocol buffer users should not do this |
| 1044 | XXX_unrecognized: []byte("\x12\x05carbs"), |
| 1045 | }, |
| 1046 | } |
| 1047 | if !Equal(om, exp) { |
| 1048 | t.Errorf("om = %v, want %v", om, exp) |
| 1049 | } |
| 1050 | |
| 1051 | // Clone the OldMessage. |
| 1052 | om = Clone(om).(*OldMessage) |
| 1053 | if !Equal(om, exp) { |
| 1054 | t.Errorf("Clone(om) = %v, want %v", om, exp) |
| 1055 | } |
| 1056 | |
| 1057 | // Marshal the OldMessage, then unmarshal it into an empty NewMessage. |
| 1058 | if b, err = Marshal(om); err != nil { |
| 1059 | t.Fatalf("Marshal of OldMessage: %v", err) |
| 1060 | } |
| 1061 | t.Logf("Marshal(%v) -> %q", om, b) |
| 1062 | nm2 := new(NewMessage) |
| 1063 | if err := Unmarshal(b, nm2); err != nil { |
| 1064 | t.Fatalf("Unmarshal to NewMessage: %v", err) |
| 1065 | } |
| 1066 | if !Equal(nm, nm2) { |
| 1067 | t.Errorf("NewMessage round-trip: %v => %v", nm, nm2) |
| 1068 | } |
| 1069 | } |
| 1070 | |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 1071 | // Check that an int32 field can be upgraded to an int64 field. |
| 1072 | func TestNegativeInt32(t *testing.T) { |
| 1073 | om := &OldMessage{ |
| 1074 | Num: Int32(-1), |
| 1075 | } |
| 1076 | b, err := Marshal(om) |
| 1077 | if err != nil { |
| 1078 | t.Fatalf("Marshal of OldMessage: %v", err) |
| 1079 | } |
| 1080 | |
| 1081 | // Check the size. It should be 11 bytes; |
| 1082 | // 1 for the field/wire type, and 10 for the negative number. |
| 1083 | if len(b) != 11 { |
| 1084 | t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b) |
| 1085 | } |
| 1086 | |
| 1087 | // Unmarshal into a NewMessage. |
| 1088 | nm := new(NewMessage) |
| 1089 | if err := Unmarshal(b, nm); err != nil { |
| 1090 | t.Fatalf("Unmarshal to NewMessage: %v", err) |
| 1091 | } |
| 1092 | want := &NewMessage{ |
| 1093 | Num: Int64(-1), |
| 1094 | } |
| 1095 | if !Equal(nm, want) { |
| 1096 | t.Errorf("nm = %v, want %v", nm, want) |
| 1097 | } |
| 1098 | } |
| 1099 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1100 | // Check that we can grow an array (repeated field) to have many elements. |
| 1101 | // This test doesn't depend only on our encoding; for variety, it makes sure |
| 1102 | // we create, encode, and decode the correct contents explicitly. It's therefore |
| 1103 | // a bit messier. |
| 1104 | // This test also uses (and hence tests) the Marshal/Unmarshal functions |
| 1105 | // instead of the methods. |
| 1106 | func TestBigRepeated(t *testing.T) { |
| 1107 | pb := initGoTest(true) |
| 1108 | |
| 1109 | // Create the arrays |
| 1110 | const N = 50 // Internally the library starts much smaller. |
| 1111 | pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N) |
| 1112 | pb.F_Sint64Repeated = make([]int64, N) |
| 1113 | pb.F_Sint32Repeated = make([]int32, N) |
| 1114 | pb.F_BytesRepeated = make([][]byte, N) |
| 1115 | pb.F_StringRepeated = make([]string, N) |
| 1116 | pb.F_DoubleRepeated = make([]float64, N) |
| 1117 | pb.F_FloatRepeated = make([]float32, N) |
| 1118 | pb.F_Uint64Repeated = make([]uint64, N) |
| 1119 | pb.F_Uint32Repeated = make([]uint32, N) |
| 1120 | pb.F_Fixed64Repeated = make([]uint64, N) |
| 1121 | pb.F_Fixed32Repeated = make([]uint32, N) |
| 1122 | pb.F_Int64Repeated = make([]int64, N) |
| 1123 | pb.F_Int32Repeated = make([]int32, N) |
| 1124 | pb.F_BoolRepeated = make([]bool, N) |
| 1125 | pb.RepeatedField = make([]*GoTestField, N) |
| 1126 | |
| 1127 | // Fill in the arrays with checkable values. |
| 1128 | igtf := initGoTestField() |
| 1129 | igtrg := initGoTest_RepeatedGroup() |
| 1130 | for i := 0; i < N; i++ { |
| 1131 | pb.Repeatedgroup[i] = igtrg |
| 1132 | pb.F_Sint64Repeated[i] = int64(i) |
| 1133 | pb.F_Sint32Repeated[i] = int32(i) |
| 1134 | s := fmt.Sprint(i) |
| 1135 | pb.F_BytesRepeated[i] = []byte(s) |
| 1136 | pb.F_StringRepeated[i] = s |
| 1137 | pb.F_DoubleRepeated[i] = float64(i) |
| 1138 | pb.F_FloatRepeated[i] = float32(i) |
| 1139 | pb.F_Uint64Repeated[i] = uint64(i) |
| 1140 | pb.F_Uint32Repeated[i] = uint32(i) |
| 1141 | pb.F_Fixed64Repeated[i] = uint64(i) |
| 1142 | pb.F_Fixed32Repeated[i] = uint32(i) |
| 1143 | pb.F_Int64Repeated[i] = int64(i) |
| 1144 | pb.F_Int32Repeated[i] = int32(i) |
| 1145 | pb.F_BoolRepeated[i] = i%2 == 0 |
| 1146 | pb.RepeatedField[i] = igtf |
| 1147 | } |
| 1148 | |
| 1149 | // Marshal. |
| 1150 | buf, _ := Marshal(pb) |
| 1151 | |
| 1152 | // Now test Unmarshal by recreating the original buffer. |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1153 | pbd := new(GoTest) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1154 | Unmarshal(buf, pbd) |
| 1155 | |
| 1156 | // Check the checkable values |
| 1157 | for i := uint64(0); i < N; i++ { |
| 1158 | if pbd.Repeatedgroup[i] == nil { // TODO: more checking? |
| 1159 | t.Error("pbd.Repeatedgroup bad") |
| 1160 | } |
| 1161 | var x uint64 |
| 1162 | x = uint64(pbd.F_Sint64Repeated[i]) |
| 1163 | if x != i { |
| 1164 | t.Error("pbd.F_Sint64Repeated bad", x, i) |
| 1165 | } |
| 1166 | x = uint64(pbd.F_Sint32Repeated[i]) |
| 1167 | if x != i { |
| 1168 | t.Error("pbd.F_Sint32Repeated bad", x, i) |
| 1169 | } |
| 1170 | s := fmt.Sprint(i) |
| 1171 | equalbytes(pbd.F_BytesRepeated[i], []byte(s), t) |
| 1172 | if pbd.F_StringRepeated[i] != s { |
| 1173 | t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i) |
| 1174 | } |
| 1175 | x = uint64(pbd.F_DoubleRepeated[i]) |
| 1176 | if x != i { |
| 1177 | t.Error("pbd.F_DoubleRepeated bad", x, i) |
| 1178 | } |
| 1179 | x = uint64(pbd.F_FloatRepeated[i]) |
| 1180 | if x != i { |
| 1181 | t.Error("pbd.F_FloatRepeated bad", x, i) |
| 1182 | } |
| 1183 | x = pbd.F_Uint64Repeated[i] |
| 1184 | if x != i { |
| 1185 | t.Error("pbd.F_Uint64Repeated bad", x, i) |
| 1186 | } |
| 1187 | x = uint64(pbd.F_Uint32Repeated[i]) |
| 1188 | if x != i { |
| 1189 | t.Error("pbd.F_Uint32Repeated bad", x, i) |
| 1190 | } |
| 1191 | x = pbd.F_Fixed64Repeated[i] |
| 1192 | if x != i { |
| 1193 | t.Error("pbd.F_Fixed64Repeated bad", x, i) |
| 1194 | } |
| 1195 | x = uint64(pbd.F_Fixed32Repeated[i]) |
| 1196 | if x != i { |
| 1197 | t.Error("pbd.F_Fixed32Repeated bad", x, i) |
| 1198 | } |
| 1199 | x = uint64(pbd.F_Int64Repeated[i]) |
| 1200 | if x != i { |
| 1201 | t.Error("pbd.F_Int64Repeated bad", x, i) |
| 1202 | } |
| 1203 | x = uint64(pbd.F_Int32Repeated[i]) |
| 1204 | if x != i { |
| 1205 | t.Error("pbd.F_Int32Repeated bad", x, i) |
| 1206 | } |
| 1207 | if pbd.F_BoolRepeated[i] != (i%2 == 0) { |
| 1208 | t.Error("pbd.F_BoolRepeated bad", x, i) |
| 1209 | } |
| 1210 | if pbd.RepeatedField[i] == nil { // TODO: more checking? |
| 1211 | t.Error("pbd.RepeatedField bad") |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // Verify we give a useful message when decoding to the wrong structure type. |
| 1217 | func TestTypeMismatch(t *testing.T) { |
| 1218 | pb1 := initGoTest(true) |
| 1219 | |
| 1220 | // Marshal |
| 1221 | o := old() |
| 1222 | o.Marshal(pb1) |
| 1223 | |
| 1224 | // Now Unmarshal it to the wrong type. |
| 1225 | pb2 := initGoTestField() |
| 1226 | err := o.Unmarshal(pb2) |
| David Symonds | baeae8b | 2014-06-23 09:41:27 +1000 | [diff] [blame] | 1227 | if err == nil { |
| 1228 | t.Error("expected error, got no error") |
| 1229 | } else if !strings.Contains(err.Error(), "bad wiretype") { |
| 1230 | t.Error("expected bad wiretype error, got", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1234 | func encodeDecode(t *testing.T, in, out Message, msg string) { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1235 | buf, err := Marshal(in) |
| 1236 | if err != nil { |
| 1237 | t.Fatalf("failed marshaling %v: %v", msg, err) |
| 1238 | } |
| 1239 | if err := Unmarshal(buf, out); err != nil { |
| 1240 | t.Fatalf("failed unmarshaling %v: %v", msg, err) |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | func TestPackedNonPackedDecoderSwitching(t *testing.T) { |
| 1245 | np, p := new(NonPackedTest), new(PackedTest) |
| 1246 | |
| 1247 | // non-packed -> packed |
| 1248 | np.A = []int32{0, 1, 1, 2, 3, 5} |
| 1249 | encodeDecode(t, np, p, "non-packed -> packed") |
| 1250 | if !reflect.DeepEqual(np.A, p.B) { |
| 1251 | t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B) |
| 1252 | } |
| 1253 | |
| 1254 | // packed -> non-packed |
| 1255 | np.Reset() |
| 1256 | p.B = []int32{3, 1, 4, 1, 5, 9} |
| 1257 | encodeDecode(t, p, np, "packed -> non-packed") |
| 1258 | if !reflect.DeepEqual(p.B, np.A) { |
| 1259 | t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A) |
| 1260 | } |
| 1261 | } |
| 1262 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1263 | func TestProto1RepeatedGroup(t *testing.T) { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1264 | pb := &MessageList{ |
| 1265 | Message: []*MessageList_Message{ |
| Albert Strasheim | 4676f6a | 2013-04-07 08:59:06 +1000 | [diff] [blame] | 1266 | { |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1267 | Name: String("blah"), |
| 1268 | Count: Int32(7), |
| 1269 | }, |
| 1270 | // NOTE: pb.Message[1] is a nil |
| 1271 | nil, |
| 1272 | }, |
| 1273 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1274 | |
| 1275 | o := old() |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1276 | err := o.Marshal(pb) |
| 1277 | if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1278 | t.Fatalf("unexpected or no error when marshaling: %v", err) |
| 1279 | } |
| 1280 | } |
| 1281 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1282 | // Test that enums work. Checks for a bug introduced by making enums |
| 1283 | // named types instead of int32: newInt32FromUint64 would crash with |
| 1284 | // a type mismatch in reflect.PointTo. |
| 1285 | func TestEnum(t *testing.T) { |
| 1286 | pb := new(GoEnum) |
| David Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 1287 | pb.Foo = FOO_FOO1.Enum() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1288 | o := old() |
| 1289 | if err := o.Marshal(pb); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1290 | t.Fatal("error encoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1291 | } |
| 1292 | pb1 := new(GoEnum) |
| 1293 | if err := o.Unmarshal(pb1); err != nil { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1294 | t.Fatal("error decoding enum:", err) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1295 | } |
| 1296 | if *pb1.Foo != FOO_FOO1 { |
| Rob Pike | 853f911 | 2010-12-17 13:56:46 -0800 | [diff] [blame] | 1297 | t.Error("expected 7 but got ", *pb1.Foo) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 1301 | // Enum types have String methods. Check that enum fields can be printed. |
| 1302 | // We don't care what the value actually is, just as long as it doesn't crash. |
| 1303 | func TestPrintingNilEnumFields(t *testing.T) { |
| 1304 | pb := new(GoEnum) |
| 1305 | fmt.Sprintf("%+v", pb) |
| 1306 | } |
| 1307 | |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1308 | // Verify that absent required fields cause Marshal/Unmarshal to return errors. |
| 1309 | func TestRequiredFieldEnforcement(t *testing.T) { |
| 1310 | pb := new(GoTestField) |
| 1311 | _, err := Marshal(pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1312 | if err == nil { |
| 1313 | t.Error("marshal: expected error, got nil") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1314 | } else if strings.Index(err.Error(), "Label") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1315 | t.Errorf("marshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | // A slightly sneaky, yet valid, proto. It encodes the same required field twice, |
| 1319 | // so simply counting the required fields is insufficient. |
| 1320 | // field 1, encoding 2, value "hi" |
| 1321 | buf := []byte("\x0A\x02hi\x0A\x02hi") |
| 1322 | err = Unmarshal(buf, pb) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1323 | if err == nil { |
| 1324 | t.Error("unmarshal: expected error, got nil") |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1325 | } else if strings.Index(err.Error(), "{Unknown}") < 0 { |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 1326 | t.Errorf("unmarshal: bad error type: %v", err) |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 1330 | func TestTypedNilMarshal(t *testing.T) { |
| 1331 | // A typed nil should return ErrNil and not crash. |
| Bryan Mills | 8d92cf5 | 2016-03-19 05:57:00 +1100 | [diff] [blame] | 1332 | { |
| 1333 | var m *GoEnum |
| 1334 | if _, err := Marshal(m); err != ErrNil { |
| 1335 | t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err) |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | { |
| 1340 | m := &Communique{Union: &Communique_Msg{nil}} |
| 1341 | if _, err := Marshal(m); err == nil || err == ErrNil { |
| 1342 | t.Errorf("Marshal(%#v): got %v, want errOneofHasNil", m, err) |
| 1343 | } |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1347 | // A type that implements the Marshaler interface, but is not nillable. |
| 1348 | type nonNillableInt uint64 |
| 1349 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 1350 | func (nni nonNillableInt) Marshal() ([]byte, error) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1351 | return EncodeVarint(uint64(nni)), nil |
| 1352 | } |
| 1353 | |
| 1354 | type NNIMessage struct { |
| 1355 | nni nonNillableInt |
| 1356 | } |
| 1357 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1358 | func (*NNIMessage) Reset() {} |
| 1359 | func (*NNIMessage) String() string { return "" } |
| 1360 | func (*NNIMessage) ProtoMessage() {} |
| 1361 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1362 | // A type that implements the Marshaler interface and is nillable. |
| 1363 | type nillableMessage struct { |
| 1364 | x uint64 |
| 1365 | } |
| 1366 | |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 1367 | func (nm *nillableMessage) Marshal() ([]byte, error) { |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1368 | return EncodeVarint(nm.x), nil |
| 1369 | } |
| 1370 | |
| 1371 | type NMMessage struct { |
| 1372 | nm *nillableMessage |
| 1373 | } |
| 1374 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 1375 | func (*NMMessage) Reset() {} |
| 1376 | func (*NMMessage) String() string { return "" } |
| 1377 | func (*NMMessage) ProtoMessage() {} |
| 1378 | |
| David Symonds | 03c9d41 | 2010-08-26 14:23:18 +1000 | [diff] [blame] | 1379 | // Verify a type that uses the Marshaler interface, but has a nil pointer. |
| 1380 | func TestNilMarshaler(t *testing.T) { |
| 1381 | // Try a struct with a Marshaler field that is nil. |
| 1382 | // It should be directly marshable. |
| 1383 | nmm := new(NMMessage) |
| 1384 | if _, err := Marshal(nmm); err != nil { |
| 1385 | t.Error("unexpected error marshaling nmm: ", err) |
| 1386 | } |
| 1387 | |
| 1388 | // Try a struct with a Marshaler field that is not nillable. |
| 1389 | nnim := new(NNIMessage) |
| 1390 | nnim.nni = 7 |
| 1391 | var _ Marshaler = nnim.nni // verify it is truly a Marshaler |
| 1392 | if _, err := Marshal(nnim); err != nil { |
| 1393 | t.Error("unexpected error marshaling nnim: ", err) |
| 1394 | } |
| 1395 | } |
| 1396 | |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1397 | func TestAllSetDefaults(t *testing.T) { |
| 1398 | // Exercise SetDefaults with all scalar field types. |
| 1399 | m := &Defaults{ |
| 1400 | // NaN != NaN, so override that here. |
| 1401 | F_Nan: Float32(1.7), |
| 1402 | } |
| 1403 | expected := &Defaults{ |
| 1404 | F_Bool: Bool(true), |
| 1405 | F_Int32: Int32(32), |
| 1406 | F_Int64: Int64(64), |
| 1407 | F_Fixed32: Uint32(320), |
| 1408 | F_Fixed64: Uint64(640), |
| 1409 | F_Uint32: Uint32(3200), |
| 1410 | F_Uint64: Uint64(6400), |
| 1411 | F_Float: Float32(314159), |
| 1412 | F_Double: Float64(271828), |
| 1413 | F_String: String(`hello, "world!"` + "\n"), |
| 1414 | F_Bytes: []byte("Bignose"), |
| 1415 | F_Sint32: Int32(-32), |
| 1416 | F_Sint64: Int64(-64), |
| David Symonds | efeca9a | 2012-05-08 10:36:04 +1000 | [diff] [blame] | 1417 | F_Enum: Defaults_GREEN.Enum(), |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1418 | F_Pinf: Float32(float32(math.Inf(1))), |
| 1419 | F_Ninf: Float32(float32(math.Inf(-1))), |
| 1420 | F_Nan: Float32(1.7), |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 1421 | StrZero: String(""), |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1422 | } |
| 1423 | SetDefaults(m) |
| 1424 | if !Equal(m, expected) { |
| David Symonds | 7e81098 | 2014-08-12 13:11:17 +1000 | [diff] [blame] | 1425 | t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected) |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | func TestSetDefaultsWithSetField(t *testing.T) { |
| 1430 | // Check that a set value is not overridden. |
| 1431 | m := &Defaults{ |
| 1432 | F_Int32: Int32(12), |
| 1433 | } |
| 1434 | SetDefaults(m) |
| David Symonds | 0e08492 | 2012-07-02 16:04:40 -0700 | [diff] [blame] | 1435 | if v := m.GetF_Int32(); v != 12 { |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1436 | t.Errorf("m.FInt32 = %v, want 12", v) |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | func TestSetDefaultsWithSubMessage(t *testing.T) { |
| 1441 | m := &OtherMessage{ |
| 1442 | Key: Int64(123), |
| 1443 | Inner: &InnerMessage{ |
| 1444 | Host: String("gopher"), |
| 1445 | }, |
| 1446 | } |
| 1447 | expected := &OtherMessage{ |
| 1448 | Key: Int64(123), |
| 1449 | Inner: &InnerMessage{ |
| 1450 | Host: String("gopher"), |
| 1451 | Port: Int32(4000), |
| 1452 | }, |
| 1453 | } |
| 1454 | SetDefaults(m) |
| 1455 | if !Equal(m, expected) { |
| David Symonds | 814b936 | 2011-12-13 09:10:47 +1100 | [diff] [blame] | 1456 | t.Errorf("\n got %v\nwant %v", m, expected) |
| David Symonds | b79d99b | 2011-08-29 16:38:49 +1000 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
| David Symonds | 472e259 | 2013-07-15 11:10:07 +1000 | [diff] [blame] | 1460 | func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) { |
| 1461 | m := &MyMessage{ |
| 1462 | RepInner: []*InnerMessage{{}}, |
| 1463 | } |
| 1464 | expected := &MyMessage{ |
| 1465 | RepInner: []*InnerMessage{{ |
| 1466 | Port: Int32(4000), |
| 1467 | }}, |
| 1468 | } |
| 1469 | SetDefaults(m) |
| 1470 | if !Equal(m, expected) { |
| 1471 | t.Errorf("\n got %v\nwant %v", m, expected) |
| 1472 | } |
| 1473 | } |
| 1474 | |
| David Symonds | de8c523 | 2015-03-20 16:29:30 +1100 | [diff] [blame] | 1475 | func TestSetDefaultWithRepeatedNonMessage(t *testing.T) { |
| 1476 | m := &MyMessage{ |
| 1477 | Pet: []string{"turtle", "wombat"}, |
| 1478 | } |
| 1479 | expected := Clone(m) |
| 1480 | SetDefaults(m) |
| 1481 | if !Equal(m, expected) { |
| 1482 | t.Errorf("\n got %v\nwant %v", m, expected) |
| 1483 | } |
| 1484 | } |
| 1485 | |
| David Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 1486 | func TestMaximumTagNumber(t *testing.T) { |
| 1487 | m := &MaxTag{ |
| 1488 | LastField: String("natural goat essence"), |
| 1489 | } |
| 1490 | buf, err := Marshal(m) |
| 1491 | if err != nil { |
| 1492 | t.Fatalf("proto.Marshal failed: %v", err) |
| 1493 | } |
| 1494 | m2 := new(MaxTag) |
| 1495 | if err := Unmarshal(buf, m2); err != nil { |
| 1496 | t.Fatalf("proto.Unmarshal failed: %v", err) |
| 1497 | } |
| David Symonds | 0e08492 | 2012-07-02 16:04:40 -0700 | [diff] [blame] | 1498 | if got, want := m2.GetLastField(), *m.LastField; got != want { |
| David Symonds | d73d7b1 | 2011-09-28 10:56:43 -0700 | [diff] [blame] | 1499 | t.Errorf("got %q, want %q", got, want) |
| 1500 | } |
| 1501 | } |
| 1502 | |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1503 | func TestJSON(t *testing.T) { |
| 1504 | m := &MyMessage{ |
| 1505 | Count: Int32(4), |
| 1506 | Pet: []string{"bunny", "kitty"}, |
| 1507 | Inner: &InnerMessage{ |
| 1508 | Host: String("cauchy"), |
| 1509 | }, |
| David Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1510 | Bikeshed: MyMessage_GREEN.Enum(), |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1511 | } |
| David Symonds | 4af5f1f | 2013-10-11 10:08:35 +1100 | [diff] [blame] | 1512 | const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}` |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1513 | |
| 1514 | b, err := json.Marshal(m) |
| 1515 | if err != nil { |
| 1516 | t.Fatalf("json.Marshal failed: %v", err) |
| 1517 | } |
| 1518 | s := string(b) |
| 1519 | if s != expected { |
| 1520 | t.Errorf("got %s\nwant %s", s, expected) |
| 1521 | } |
| David Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1522 | |
| 1523 | received := new(MyMessage) |
| 1524 | if err := json.Unmarshal(b, received); err != nil { |
| 1525 | t.Fatalf("json.Unmarshal failed: %v", err) |
| 1526 | } |
| 1527 | if !Equal(received, m) { |
| 1528 | t.Fatalf("got %s, want %s", received, m) |
| 1529 | } |
| 1530 | |
| David Symonds | 4af5f1f | 2013-10-11 10:08:35 +1100 | [diff] [blame] | 1531 | // Test unmarshalling of JSON with symbolic enum name. |
| 1532 | const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}` |
| David Symonds | 6253986 | 2012-08-04 10:06:55 +1000 | [diff] [blame] | 1533 | received.Reset() |
| 1534 | if err := json.Unmarshal([]byte(old), received); err != nil { |
| 1535 | t.Fatalf("json.Unmarshal failed: %v", err) |
| 1536 | } |
| 1537 | if !Equal(received, m) { |
| 1538 | t.Fatalf("got %s, want %s", received, m) |
| 1539 | } |
| David Symonds | 002ec40 | 2011-09-29 17:24:20 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1542 | func TestBadWireType(t *testing.T) { |
| 1543 | b := []byte{7<<3 | 6} // field 7, wire type 6 |
| 1544 | pb := new(OtherMessage) |
| 1545 | if err := Unmarshal(b, pb); err == nil { |
| 1546 | t.Errorf("Unmarshal did not fail") |
| 1547 | } else if !strings.Contains(err.Error(), "unknown wire type") { |
| 1548 | t.Errorf("wrong error: %v", err) |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | func TestBytesWithInvalidLength(t *testing.T) { |
| 1553 | // If a byte sequence has an invalid (negative) length, Unmarshal should not panic. |
| 1554 | b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0} |
| 1555 | Unmarshal(b, new(MyMessage)) |
| 1556 | } |
| 1557 | |
| Adam Langley | 28c83cb | 2013-07-13 14:54:54 +1000 | [diff] [blame] | 1558 | func TestLengthOverflow(t *testing.T) { |
| 1559 | // Overflowing a length should not panic. |
| 1560 | b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01} |
| 1561 | Unmarshal(b, new(MyMessage)) |
| 1562 | } |
| 1563 | |
| 1564 | func TestVarintOverflow(t *testing.T) { |
| 1565 | // Overflowing a 64-bit length should not be allowed. |
| 1566 | b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01} |
| 1567 | if err := Unmarshal(b, new(MyMessage)); err == nil { |
| 1568 | t.Fatalf("Overflowed uint64 length without error") |
| 1569 | } |
| 1570 | } |
| 1571 | |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1572 | func TestUnmarshalFuzz(t *testing.T) { |
| 1573 | const N = 1000 |
| 1574 | seed := time.Now().UnixNano() |
| 1575 | t.Logf("RNG seed is %d", seed) |
| 1576 | rng := rand.New(rand.NewSource(seed)) |
| 1577 | buf := make([]byte, 20) |
| 1578 | for i := 0; i < N; i++ { |
| 1579 | for j := range buf { |
| 1580 | buf[j] = byte(rng.Intn(256)) |
| 1581 | } |
| 1582 | fuzzUnmarshal(t, buf) |
| 1583 | } |
| 1584 | } |
| 1585 | |
| David Symonds | e182aaf | 2013-01-30 17:04:37 +1100 | [diff] [blame] | 1586 | func TestMergeMessages(t *testing.T) { |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1587 | pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}} |
| 1588 | data, err := Marshal(pb) |
| 1589 | if err != nil { |
| 1590 | t.Fatalf("Marshal: %v", err) |
| 1591 | } |
| 1592 | |
| 1593 | pb1 := new(MessageList) |
| 1594 | if err := Unmarshal(data, pb1); err != nil { |
| 1595 | t.Fatalf("first Unmarshal: %v", err) |
| 1596 | } |
| 1597 | if err := Unmarshal(data, pb1); err != nil { |
| 1598 | t.Fatalf("second Unmarshal: %v", err) |
| 1599 | } |
| 1600 | if len(pb1.Message) != 1 { |
| 1601 | t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message)) |
| 1602 | } |
| 1603 | |
| 1604 | pb2 := new(MessageList) |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1605 | if err := UnmarshalMerge(data, pb2); err != nil { |
| 1606 | t.Fatalf("first UnmarshalMerge: %v", err) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1607 | } |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1608 | if err := UnmarshalMerge(data, pb2); err != nil { |
| 1609 | t.Fatalf("second UnmarshalMerge: %v", err) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1610 | } |
| 1611 | if len(pb2.Message) != 2 { |
| David Symonds | d4b52d0 | 2013-01-15 14:30:26 +1100 | [diff] [blame] | 1612 | t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message)) |
| David Symonds | 525838c | 2012-07-20 15:42:49 +1000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1616 | func TestExtensionMarshalOrder(t *testing.T) { |
| David Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1617 | m := &MyMessage{Count: Int(123)} |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1618 | if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil { |
| 1619 | t.Fatalf("SetExtension: %v", err) |
| 1620 | } |
| 1621 | if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil { |
| 1622 | t.Fatalf("SetExtension: %v", err) |
| 1623 | } |
| 1624 | if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil { |
| 1625 | t.Fatalf("SetExtension: %v", err) |
| 1626 | } |
| 1627 | |
| 1628 | // Serialize m several times, and check we get the same bytes each time. |
| 1629 | var orig []byte |
| David Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1630 | for i := 0; i < 100; i++ { |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1631 | b, err := Marshal(m) |
| 1632 | if err != nil { |
| 1633 | t.Fatalf("Marshal: %v", err) |
| 1634 | } |
| 1635 | if i == 0 { |
| 1636 | orig = b |
| 1637 | continue |
| 1638 | } |
| 1639 | if !bytes.Equal(b, orig) { |
| 1640 | t.Errorf("Bytes differ on attempt #%d", i) |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
| David Symonds | 0c1184e | 2013-06-08 15:42:12 +1000 | [diff] [blame] | 1645 | // Many extensions, because small maps might not iterate differently on each iteration. |
| 1646 | var exts = []*ExtensionDesc{ |
| 1647 | E_X201, |
| 1648 | E_X202, |
| 1649 | E_X203, |
| 1650 | E_X204, |
| 1651 | E_X205, |
| 1652 | E_X206, |
| 1653 | E_X207, |
| 1654 | E_X208, |
| 1655 | E_X209, |
| 1656 | E_X210, |
| 1657 | E_X211, |
| 1658 | E_X212, |
| 1659 | E_X213, |
| 1660 | E_X214, |
| 1661 | E_X215, |
| 1662 | E_X216, |
| 1663 | E_X217, |
| 1664 | E_X218, |
| 1665 | E_X219, |
| 1666 | E_X220, |
| 1667 | E_X221, |
| 1668 | E_X222, |
| 1669 | E_X223, |
| 1670 | E_X224, |
| 1671 | E_X225, |
| 1672 | E_X226, |
| 1673 | E_X227, |
| 1674 | E_X228, |
| 1675 | E_X229, |
| 1676 | E_X230, |
| 1677 | E_X231, |
| 1678 | E_X232, |
| 1679 | E_X233, |
| 1680 | E_X234, |
| 1681 | E_X235, |
| 1682 | E_X236, |
| 1683 | E_X237, |
| 1684 | E_X238, |
| 1685 | E_X239, |
| 1686 | E_X240, |
| 1687 | E_X241, |
| 1688 | E_X242, |
| 1689 | E_X243, |
| 1690 | E_X244, |
| 1691 | E_X245, |
| 1692 | E_X246, |
| 1693 | E_X247, |
| 1694 | E_X248, |
| 1695 | E_X249, |
| 1696 | E_X250, |
| 1697 | } |
| 1698 | |
| 1699 | func TestMessageSetMarshalOrder(t *testing.T) { |
| 1700 | m := &MyMessageSet{} |
| 1701 | for _, x := range exts { |
| 1702 | if err := SetExtension(m, x, &Empty{}); err != nil { |
| 1703 | t.Fatalf("SetExtension: %v", err) |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | buf, err := Marshal(m) |
| 1708 | if err != nil { |
| 1709 | t.Fatalf("Marshal: %v", err) |
| 1710 | } |
| 1711 | |
| 1712 | // Serialize m several times, and check we get the same bytes each time. |
| 1713 | for i := 0; i < 10; i++ { |
| 1714 | b1, err := Marshal(m) |
| 1715 | if err != nil { |
| 1716 | t.Fatalf("Marshal: %v", err) |
| 1717 | } |
| 1718 | if !bytes.Equal(b1, buf) { |
| 1719 | t.Errorf("Bytes differ on re-Marshal #%d", i) |
| 1720 | } |
| 1721 | |
| 1722 | m2 := &MyMessageSet{} |
| 1723 | if err := Unmarshal(buf, m2); err != nil { |
| 1724 | t.Errorf("Unmarshal: %v", err) |
| 1725 | } |
| 1726 | b2, err := Marshal(m2) |
| 1727 | if err != nil { |
| 1728 | t.Errorf("re-Marshal: %v", err) |
| 1729 | } |
| 1730 | if !bytes.Equal(b2, buf) { |
| 1731 | t.Errorf("Bytes differ on round-trip #%d", i) |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
| David Symonds | 2ce8ed4 | 2013-06-20 13:22:17 +1000 | [diff] [blame] | 1736 | func TestUnmarshalMergesMessages(t *testing.T) { |
| 1737 | // If a nested message occurs twice in the input, |
| 1738 | // the fields should be merged when decoding. |
| 1739 | a := &OtherMessage{ |
| 1740 | Key: Int64(123), |
| 1741 | Inner: &InnerMessage{ |
| 1742 | Host: String("polhode"), |
| 1743 | Port: Int32(1234), |
| 1744 | }, |
| 1745 | } |
| 1746 | aData, err := Marshal(a) |
| 1747 | if err != nil { |
| 1748 | t.Fatalf("Marshal(a): %v", err) |
| 1749 | } |
| 1750 | b := &OtherMessage{ |
| 1751 | Weight: Float32(1.2), |
| 1752 | Inner: &InnerMessage{ |
| 1753 | Host: String("herpolhode"), |
| 1754 | Connected: Bool(true), |
| 1755 | }, |
| 1756 | } |
| 1757 | bData, err := Marshal(b) |
| 1758 | if err != nil { |
| 1759 | t.Fatalf("Marshal(b): %v", err) |
| 1760 | } |
| 1761 | want := &OtherMessage{ |
| 1762 | Key: Int64(123), |
| 1763 | Weight: Float32(1.2), |
| 1764 | Inner: &InnerMessage{ |
| 1765 | Host: String("herpolhode"), |
| 1766 | Port: Int32(1234), |
| 1767 | Connected: Bool(true), |
| 1768 | }, |
| 1769 | } |
| 1770 | got := new(OtherMessage) |
| 1771 | if err := Unmarshal(append(aData, bData...), got); err != nil { |
| 1772 | t.Fatalf("Unmarshal: %v", err) |
| 1773 | } |
| 1774 | if !Equal(got, want) { |
| 1775 | t.Errorf("\n got %v\nwant %v", got, want) |
| 1776 | } |
| 1777 | } |
| 1778 | |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 1779 | func TestEncodingSizes(t *testing.T) { |
| 1780 | tests := []struct { |
| 1781 | m Message |
| 1782 | n int |
| 1783 | }{ |
| 1784 | {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6}, |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 1785 | {&Defaults{F_Int32: Int32(math.MinInt32)}, 11}, |
| 1786 | {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6}, |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 1787 | {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6}, |
| 1788 | } |
| 1789 | for _, test := range tests { |
| 1790 | b, err := Marshal(test.m) |
| 1791 | if err != nil { |
| 1792 | t.Errorf("Marshal(%v): %v", test.m, err) |
| 1793 | continue |
| 1794 | } |
| 1795 | if len(b) != test.n { |
| 1796 | t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n) |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1801 | func TestRequiredNotSetError(t *testing.T) { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1802 | pb := initGoTest(false) |
| 1803 | pb.RequiredField.Label = nil |
| 1804 | pb.F_Int32Required = nil |
| 1805 | pb.F_Int64Required = nil |
| 1806 | |
| 1807 | expected := "0807" + // field 1, encoding 0, value 7 |
| 1808 | "2206" + "120474797065" + // field 4, encoding 2 (GoTestField) |
| 1809 | "5001" + // field 10, encoding 0, value 1 |
| 1810 | "6d20000000" + // field 13, encoding 5, value 0x20 |
| 1811 | "714000000000000000" + // field 14, encoding 1, value 0x40 |
| 1812 | "78a019" + // field 15, encoding 0, value 0xca0 = 3232 |
| 1813 | "8001c032" + // field 16, encoding 0, value 0x1940 = 6464 |
| 1814 | "8d0100004a45" + // field 17, encoding 5, value 3232.0 |
| 1815 | "9101000000000040b940" + // field 18, encoding 1, value 6464.0 |
| 1816 | "9a0106" + "737472696e67" + // field 19, encoding 2, string "string" |
| 1817 | "b304" + // field 70, encoding 3, start group |
| 1818 | "ba0408" + "7265717569726564" + // field 71, encoding 2, string "required" |
| 1819 | "b404" + // field 70, encoding 4, end group |
| 1820 | "aa0605" + "6279746573" + // field 101, encoding 2, string "bytes" |
| 1821 | "b0063f" + // field 102, encoding 0, 0x3f zigzag32 |
| 1822 | "b8067f" // field 103, encoding 0, 0x7f zigzag64 |
| 1823 | |
| 1824 | o := old() |
| 1825 | bytes, err := Marshal(pb) |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1826 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1827 | fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1828 | o.DebugPrint("", bytes) |
| 1829 | t.Fatalf("expected = %s", expected) |
| 1830 | } |
| 1831 | if strings.Index(err.Error(), "RequiredField.Label") < 0 { |
| 1832 | t.Errorf("marshal-1 wrong err msg: %v", err) |
| 1833 | } |
| 1834 | if !equal(bytes, expected, t) { |
| 1835 | o.DebugPrint("neq 1", bytes) |
| 1836 | t.Fatalf("expected = %s", expected) |
| 1837 | } |
| 1838 | |
| 1839 | // Now test Unmarshal by recreating the original buffer. |
| 1840 | pbd := new(GoTest) |
| 1841 | err = Unmarshal(bytes, pbd) |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1842 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1843 | t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1844 | o.DebugPrint("", bytes) |
| 1845 | t.Fatalf("string = %s", expected) |
| 1846 | } |
| 1847 | if strings.Index(err.Error(), "RequiredField.{Unknown}") < 0 { |
| 1848 | t.Errorf("unmarshal wrong err msg: %v", err) |
| 1849 | } |
| 1850 | bytes, err = Marshal(pbd) |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1851 | if _, ok := err.(*RequiredNotSetError); !ok { |
| 1852 | t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1853 | o.DebugPrint("", bytes) |
| 1854 | t.Fatalf("string = %s", expected) |
| 1855 | } |
| 1856 | if strings.Index(err.Error(), "RequiredField.Label") < 0 { |
| 1857 | t.Errorf("marshal-2 wrong err msg: %v", err) |
| 1858 | } |
| 1859 | if !equal(bytes, expected, t) { |
| 1860 | o.DebugPrint("neq 2", bytes) |
| 1861 | t.Fatalf("string = %s", expected) |
| 1862 | } |
| 1863 | } |
| 1864 | |
| David Symonds | 22ac150 | 2012-01-18 12:37:12 +1100 | [diff] [blame] | 1865 | func fuzzUnmarshal(t *testing.T, data []byte) { |
| 1866 | defer func() { |
| 1867 | if e := recover(); e != nil { |
| 1868 | t.Errorf("These bytes caused a panic: %+v", data) |
| 1869 | t.Logf("Stack:\n%s", debug.Stack()) |
| 1870 | t.FailNow() |
| 1871 | } |
| 1872 | }() |
| 1873 | |
| 1874 | pb := new(MyMessage) |
| 1875 | Unmarshal(data, pb) |
| 1876 | } |
| 1877 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1878 | func TestMapFieldMarshal(t *testing.T) { |
| 1879 | m := &MessageWithMap{ |
| 1880 | NameMapping: map[int32]string{ |
| 1881 | 1: "Rob", |
| 1882 | 4: "Ian", |
| 1883 | 8: "Dave", |
| 1884 | }, |
| 1885 | } |
| 1886 | b, err := Marshal(m) |
| 1887 | if err != nil { |
| 1888 | t.Fatalf("Marshal: %v", err) |
| 1889 | } |
| 1890 | |
| 1891 | // b should be the concatenation of these three byte sequences in some order. |
| 1892 | parts := []string{ |
| 1893 | "\n\a\b\x01\x12\x03Rob", |
| 1894 | "\n\a\b\x04\x12\x03Ian", |
| 1895 | "\n\b\b\x08\x12\x04Dave", |
| 1896 | } |
| 1897 | ok := false |
| 1898 | for i := range parts { |
| 1899 | for j := range parts { |
| 1900 | if j == i { |
| 1901 | continue |
| 1902 | } |
| 1903 | for k := range parts { |
| 1904 | if k == i || k == j { |
| 1905 | continue |
| 1906 | } |
| 1907 | try := parts[i] + parts[j] + parts[k] |
| 1908 | if bytes.Equal(b, []byte(try)) { |
| 1909 | ok = true |
| 1910 | break |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | if !ok { |
| 1916 | t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2]) |
| 1917 | } |
| 1918 | t.Logf("FYI b: %q", b) |
| 1919 | |
| 1920 | (new(Buffer)).DebugPrint("Dump of b", b) |
| 1921 | } |
| 1922 | |
| 1923 | func TestMapFieldRoundTrips(t *testing.T) { |
| 1924 | m := &MessageWithMap{ |
| 1925 | NameMapping: map[int32]string{ |
| 1926 | 1: "Rob", |
| 1927 | 4: "Ian", |
| 1928 | 8: "Dave", |
| 1929 | }, |
| 1930 | MsgMapping: map[int64]*FloatingPoint{ |
| 1931 | 0x7001: &FloatingPoint{F: Float64(2.0)}, |
| 1932 | }, |
| 1933 | ByteMapping: map[bool][]byte{ |
| 1934 | false: []byte("that's not right!"), |
| 1935 | true: []byte("aye, 'tis true!"), |
| 1936 | }, |
| 1937 | } |
| 1938 | b, err := Marshal(m) |
| 1939 | if err != nil { |
| 1940 | t.Fatalf("Marshal: %v", err) |
| 1941 | } |
| 1942 | t.Logf("FYI b: %q", b) |
| 1943 | m2 := new(MessageWithMap) |
| 1944 | if err := Unmarshal(b, m2); err != nil { |
| 1945 | t.Fatalf("Unmarshal: %v", err) |
| 1946 | } |
| 1947 | for _, pair := range [][2]interface{}{ |
| 1948 | {m.NameMapping, m2.NameMapping}, |
| 1949 | {m.MsgMapping, m2.MsgMapping}, |
| 1950 | {m.ByteMapping, m2.ByteMapping}, |
| 1951 | } { |
| 1952 | if !reflect.DeepEqual(pair[0], pair[1]) { |
| 1953 | t.Errorf("Map did not survive a round trip.\ninitial: %v\n final: %v", pair[0], pair[1]) |
| 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | |
| David Symonds | cab84a3 | 2015-05-19 09:29:00 +1000 | [diff] [blame] | 1958 | func TestMapFieldWithNil(t *testing.T) { |
| matloob@google.com | 9e6977f | 2016-05-24 15:56:25 -0400 | [diff] [blame^] | 1959 | m1 := &MessageWithMap{ |
| David Symonds | cab84a3 | 2015-05-19 09:29:00 +1000 | [diff] [blame] | 1960 | MsgMapping: map[int64]*FloatingPoint{ |
| 1961 | 1: nil, |
| 1962 | }, |
| 1963 | } |
| matloob@google.com | 9e6977f | 2016-05-24 15:56:25 -0400 | [diff] [blame^] | 1964 | b, err := Marshal(m1) |
| 1965 | if err != nil { |
| 1966 | t.Fatalf("Marshal: %v", err) |
| 1967 | } |
| 1968 | m2 := new(MessageWithMap) |
| 1969 | if err := Unmarshal(b, m2); err != nil { |
| 1970 | t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b) |
| 1971 | } |
| 1972 | if v, ok := m2.MsgMapping[1]; !ok { |
| 1973 | t.Error("msg_mapping[1] not present") |
| 1974 | } else if v != nil { |
| 1975 | t.Errorf("msg_mapping[1] not nil: %v", v) |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | func TestMapFieldWithNilBytes(t *testing.T) { |
| 1980 | m1 := &MessageWithMap{ |
| 1981 | ByteMapping: map[bool][]byte{ |
| 1982 | false: []byte{}, |
| 1983 | true: nil, |
| 1984 | }, |
| 1985 | } |
| 1986 | n := Size(m1) |
| 1987 | b, err := Marshal(m1) |
| 1988 | if err != nil { |
| 1989 | t.Fatalf("Marshal: %v", err) |
| 1990 | } |
| 1991 | if n != len(b) { |
| 1992 | t.Errorf("Size(m1) = %d; want len(Marshal(m1)) = %d", n, len(b)) |
| 1993 | } |
| 1994 | m2 := new(MessageWithMap) |
| 1995 | if err := Unmarshal(b, m2); err != nil { |
| 1996 | t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b) |
| 1997 | } |
| 1998 | if v, ok := m2.ByteMapping[false]; !ok { |
| 1999 | t.Error("byte_mapping[false] not present") |
| 2000 | } else if len(v) != 0 { |
| 2001 | t.Errorf("byte_mapping[false] not empty: %#v", v) |
| 2002 | } |
| 2003 | if v, ok := m2.ByteMapping[true]; !ok { |
| 2004 | t.Error("byte_mapping[true] not present") |
| 2005 | } else if len(v) != 0 { |
| 2006 | t.Errorf("byte_mapping[true] not empty: %#v", v) |
| David Symonds | cab84a3 | 2015-05-19 09:29:00 +1000 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
| Michael Matloob | cde632b | 2016-03-08 09:25:00 +1100 | [diff] [blame] | 2010 | func TestDecodeMapFieldMissingKey(t *testing.T) { |
| 2011 | b := []byte{ |
| 2012 | 0x0A, 0x03, // message, tag 1 (name_mapping), of length 3 bytes |
| 2013 | // no key |
| 2014 | 0x12, 0x01, 0x6D, // string value of length 1 byte, value "m" |
| 2015 | } |
| 2016 | got := &MessageWithMap{} |
| 2017 | err := Unmarshal(b, got) |
| 2018 | if err != nil { |
| 2019 | t.Fatalf("failed to marshal map with missing key: %v", err) |
| 2020 | } |
| 2021 | want := &MessageWithMap{NameMapping: map[int32]string{0: "m"}} |
| 2022 | if !Equal(got, want) { |
| 2023 | t.Errorf("Unmarshaled map with no key was not as expected. got: %v, want %v", got, want) |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | func TestDecodeMapFieldMissingValue(t *testing.T) { |
| 2028 | b := []byte{ |
| 2029 | 0x0A, 0x02, // message, tag 1 (name_mapping), of length 2 bytes |
| 2030 | 0x08, 0x01, // varint key, value 1 |
| 2031 | // no value |
| 2032 | } |
| 2033 | got := &MessageWithMap{} |
| 2034 | err := Unmarshal(b, got) |
| 2035 | if err != nil { |
| 2036 | t.Fatalf("failed to marshal map with missing value: %v", err) |
| 2037 | } |
| 2038 | want := &MessageWithMap{NameMapping: map[int32]string{1: ""}} |
| 2039 | if !Equal(got, want) { |
| 2040 | t.Errorf("Unmarshaled map with no value was not as expected. got: %v, want %v", got, want) |
| 2041 | } |
| 2042 | } |
| 2043 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 2044 | func TestOneof(t *testing.T) { |
| 2045 | m := &Communique{} |
| 2046 | b, err := Marshal(m) |
| 2047 | if err != nil { |
| 2048 | t.Fatalf("Marshal of empty message with oneof: %v", err) |
| 2049 | } |
| 2050 | if len(b) != 0 { |
| 2051 | t.Errorf("Marshal of empty message yielded too many bytes: %v", b) |
| 2052 | } |
| 2053 | |
| 2054 | m = &Communique{ |
| 2055 | Union: &Communique_Name{"Barry"}, |
| 2056 | } |
| 2057 | |
| 2058 | // Round-trip. |
| 2059 | b, err = Marshal(m) |
| 2060 | if err != nil { |
| 2061 | t.Fatalf("Marshal of message with oneof: %v", err) |
| 2062 | } |
| 2063 | if len(b) != 7 { // name tag/wire (1) + name len (1) + name (5) |
| 2064 | t.Errorf("Incorrect marshal of message with oneof: %v", b) |
| 2065 | } |
| 2066 | m.Reset() |
| 2067 | if err := Unmarshal(b, m); err != nil { |
| 2068 | t.Fatalf("Unmarshal of message with oneof: %v", err) |
| 2069 | } |
| 2070 | if x, ok := m.Union.(*Communique_Name); !ok || x.Name != "Barry" { |
| 2071 | t.Errorf("After round trip, Union = %+v", m.Union) |
| 2072 | } |
| 2073 | if name := m.GetName(); name != "Barry" { |
| 2074 | t.Errorf("After round trip, GetName = %q, want %q", name, "Barry") |
| 2075 | } |
| 2076 | |
| 2077 | // Let's try with a message in the oneof. |
| 2078 | m.Union = &Communique_Msg{&Strings{StringField: String("deep deep string")}} |
| 2079 | b, err = Marshal(m) |
| 2080 | if err != nil { |
| 2081 | t.Fatalf("Marshal of message with oneof set to message: %v", err) |
| 2082 | } |
| 2083 | if len(b) != 20 { // msg tag/wire (1) + msg len (1) + msg (1 + 1 + 16) |
| 2084 | t.Errorf("Incorrect marshal of message with oneof set to message: %v", b) |
| 2085 | } |
| 2086 | m.Reset() |
| 2087 | if err := Unmarshal(b, m); err != nil { |
| 2088 | t.Fatalf("Unmarshal of message with oneof set to message: %v", err) |
| 2089 | } |
| 2090 | ss, ok := m.Union.(*Communique_Msg) |
| 2091 | if !ok || ss.Msg.GetStringField() != "deep deep string" { |
| 2092 | t.Errorf("After round trip with oneof set to message, Union = %+v", m.Union) |
| 2093 | } |
| 2094 | } |
| 2095 | |
| David Symonds | 0c959e8 | 2015-09-28 15:17:00 +1000 | [diff] [blame] | 2096 | func TestInefficientPackedBool(t *testing.T) { |
| 2097 | // https://github.com/golang/protobuf/issues/76 |
| 2098 | inp := []byte{ |
| 2099 | 0x12, 0x02, // 0x12 = 2<<3|2; 2 bytes |
| 2100 | // Usually a bool should take a single byte, |
| 2101 | // but it is permitted to be any varint. |
| 2102 | 0xb9, 0x30, |
| 2103 | } |
| 2104 | if err := Unmarshal(inp, new(MoreRepeated)); err != nil { |
| 2105 | t.Error(err) |
| 2106 | } |
| 2107 | } |
| 2108 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2109 | // Benchmarks |
| 2110 | |
| 2111 | func testMsg() *GoTest { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2112 | pb := initGoTest(true) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2113 | const N = 1000 // Internally the library starts much smaller. |
| 2114 | pb.F_Int32Repeated = make([]int32, N) |
| 2115 | pb.F_DoubleRepeated = make([]float64, N) |
| 2116 | for i := 0; i < N; i++ { |
| 2117 | pb.F_Int32Repeated[i] = int32(i) |
| 2118 | pb.F_DoubleRepeated[i] = float64(i) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2119 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2120 | return pb |
| 2121 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 2122 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2123 | func bytesMsg() *GoTest { |
| 2124 | pb := initGoTest(true) |
| 2125 | buf := make([]byte, 4000) |
| 2126 | for i := range buf { |
| 2127 | buf[i] = byte(i) |
| 2128 | } |
| 2129 | pb.F_BytesDefaulted = buf |
| 2130 | return pb |
| 2131 | } |
| 2132 | |
| 2133 | func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) { |
| 2134 | d, _ := marshal(pb) |
| 2135 | b.SetBytes(int64(len(d))) |
| 2136 | b.ResetTimer() |
| 2137 | for i := 0; i < b.N; i++ { |
| 2138 | marshal(pb) |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | func benchmarkBufferMarshal(b *testing.B, pb Message) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2143 | p := NewBuffer(nil) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2144 | benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) { |
| 2145 | p.Reset() |
| 2146 | err := p.Marshal(pb0) |
| 2147 | return p.Bytes(), err |
| 2148 | }) |
| 2149 | } |
| 2150 | |
| 2151 | func benchmarkSize(b *testing.B, pb Message) { |
| 2152 | benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) { |
| 2153 | Size(pb) |
| 2154 | return nil, nil |
| 2155 | }) |
| 2156 | } |
| 2157 | |
| 2158 | func newOf(pb Message) Message { |
| 2159 | in := reflect.ValueOf(pb) |
| 2160 | if in.IsNil() { |
| 2161 | return pb |
| 2162 | } |
| 2163 | return reflect.New(in.Type().Elem()).Interface().(Message) |
| 2164 | } |
| 2165 | |
| 2166 | func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) { |
| 2167 | d, _ := Marshal(pb) |
| 2168 | b.SetBytes(int64(len(d))) |
| 2169 | pbd := newOf(pb) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2170 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2171 | b.ResetTimer() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2172 | for i := 0; i < b.N; i++ { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2173 | unmarshal(d, pbd) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2174 | } |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | func benchmarkBufferUnmarshal(b *testing.B, pb Message) { |
| 2178 | p := NewBuffer(nil) |
| 2179 | benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error { |
| 2180 | p.SetBuf(d) |
| 2181 | return p.Unmarshal(pb0) |
| 2182 | }) |
| 2183 | } |
| 2184 | |
| 2185 | // Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes} |
| 2186 | |
| 2187 | func BenchmarkMarshal(b *testing.B) { |
| 2188 | benchmarkMarshal(b, testMsg(), Marshal) |
| 2189 | } |
| 2190 | |
| 2191 | func BenchmarkBufferMarshal(b *testing.B) { |
| 2192 | benchmarkBufferMarshal(b, testMsg()) |
| 2193 | } |
| 2194 | |
| 2195 | func BenchmarkSize(b *testing.B) { |
| 2196 | benchmarkSize(b, testMsg()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | func BenchmarkUnmarshal(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2200 | benchmarkUnmarshal(b, testMsg(), Unmarshal) |
| 2201 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2202 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2203 | func BenchmarkBufferUnmarshal(b *testing.B) { |
| 2204 | benchmarkBufferUnmarshal(b, testMsg()) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | func BenchmarkMarshalBytes(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2208 | benchmarkMarshal(b, bytesMsg(), Marshal) |
| 2209 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2210 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2211 | func BenchmarkBufferMarshalBytes(b *testing.B) { |
| 2212 | benchmarkBufferMarshal(b, bytesMsg()) |
| 2213 | } |
| 2214 | |
| 2215 | func BenchmarkSizeBytes(b *testing.B) { |
| 2216 | benchmarkSize(b, bytesMsg()) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | func BenchmarkUnmarshalBytes(b *testing.B) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2220 | benchmarkUnmarshal(b, bytesMsg(), Unmarshal) |
| 2221 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 2222 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 2223 | func BenchmarkBufferUnmarshalBytes(b *testing.B) { |
| 2224 | benchmarkBufferUnmarshal(b, bytesMsg()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 2225 | } |
| David Symonds | c028717 | 2012-08-15 11:10:30 +1000 | [diff] [blame] | 2226 | |
| 2227 | func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) { |
| 2228 | b.StopTimer() |
| 2229 | pb := initGoTestField() |
| 2230 | skip := &GoSkipTest{ |
| 2231 | SkipInt32: Int32(32), |
| 2232 | SkipFixed32: Uint32(3232), |
| 2233 | SkipFixed64: Uint64(6464), |
| 2234 | SkipString: String("skipper"), |
| 2235 | Skipgroup: &GoSkipTest_SkipGroup{ |
| 2236 | GroupInt32: Int32(75), |
| 2237 | GroupString: String("wxyz"), |
| 2238 | }, |
| 2239 | } |
| 2240 | |
| 2241 | pbd := new(GoTestField) |
| 2242 | p := NewBuffer(nil) |
| 2243 | p.Marshal(pb) |
| 2244 | p.Marshal(skip) |
| 2245 | p2 := NewBuffer(nil) |
| 2246 | |
| 2247 | b.StartTimer() |
| 2248 | for i := 0; i < b.N; i++ { |
| 2249 | p2.SetBuf(p.Bytes()) |
| 2250 | p2.Unmarshal(pbd) |
| 2251 | } |
| 2252 | } |