all_test.go 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto_test
  32. import (
  33. "bytes"
  34. "encoding/json"
  35. "fmt"
  36. "math"
  37. "math/rand"
  38. "reflect"
  39. "runtime/debug"
  40. "strings"
  41. "testing"
  42. "time"
  43. . "./testdata"
  44. . "code.google.com/p/goprotobuf/proto"
  45. )
  46. var globalO *Buffer
  47. func old() *Buffer {
  48. if globalO == nil {
  49. globalO = NewBuffer(nil)
  50. }
  51. globalO.Reset()
  52. return globalO
  53. }
  54. func equalbytes(b1, b2 []byte, t *testing.T) {
  55. if len(b1) != len(b2) {
  56. t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
  57. return
  58. }
  59. for i := 0; i < len(b1); i++ {
  60. if b1[i] != b2[i] {
  61. t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
  62. }
  63. }
  64. }
  65. func initGoTestField() *GoTestField {
  66. f := new(GoTestField)
  67. f.Label = String("label")
  68. f.Type = String("type")
  69. return f
  70. }
  71. // These are all structurally equivalent but the tag numbers differ.
  72. // (It's remarkable that required, optional, and repeated all have
  73. // 8 letters.)
  74. func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
  75. return &GoTest_RequiredGroup{
  76. RequiredField: String("required"),
  77. }
  78. }
  79. func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
  80. return &GoTest_OptionalGroup{
  81. RequiredField: String("optional"),
  82. }
  83. }
  84. func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
  85. return &GoTest_RepeatedGroup{
  86. RequiredField: String("repeated"),
  87. }
  88. }
  89. func initGoTest(setdefaults bool) *GoTest {
  90. pb := new(GoTest)
  91. if setdefaults {
  92. pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
  93. pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
  94. pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
  95. pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
  96. pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
  97. pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
  98. pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
  99. pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
  100. pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
  101. pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
  102. pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
  103. pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
  104. pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
  105. }
  106. pb.Kind = GoTest_TIME.Enum()
  107. pb.RequiredField = initGoTestField()
  108. pb.F_BoolRequired = Bool(true)
  109. pb.F_Int32Required = Int32(3)
  110. pb.F_Int64Required = Int64(6)
  111. pb.F_Fixed32Required = Uint32(32)
  112. pb.F_Fixed64Required = Uint64(64)
  113. pb.F_Uint32Required = Uint32(3232)
  114. pb.F_Uint64Required = Uint64(6464)
  115. pb.F_FloatRequired = Float32(3232)
  116. pb.F_DoubleRequired = Float64(6464)
  117. pb.F_StringRequired = String("string")
  118. pb.F_BytesRequired = []byte("bytes")
  119. pb.F_Sint32Required = Int32(-32)
  120. pb.F_Sint64Required = Int64(-64)
  121. pb.Requiredgroup = initGoTest_RequiredGroup()
  122. return pb
  123. }
  124. func fail(msg string, b *bytes.Buffer, s string, t *testing.T) {
  125. data := b.Bytes()
  126. ld := len(data)
  127. ls := len(s) / 2
  128. fmt.Printf("fail %s ld=%d ls=%d\n", msg, ld, ls)
  129. // find the interesting spot - n
  130. n := ls
  131. if ld < ls {
  132. n = ld
  133. }
  134. j := 0
  135. for i := 0; i < n; i++ {
  136. bs := hex(s[j])*16 + hex(s[j+1])
  137. j += 2
  138. if data[i] == bs {
  139. continue
  140. }
  141. n = i
  142. break
  143. }
  144. l := n - 10
  145. if l < 0 {
  146. l = 0
  147. }
  148. h := n + 10
  149. // find the interesting spot - n
  150. fmt.Printf("is[%d]:", l)
  151. for i := l; i < h; i++ {
  152. if i >= ld {
  153. fmt.Printf(" --")
  154. continue
  155. }
  156. fmt.Printf(" %.2x", data[i])
  157. }
  158. fmt.Printf("\n")
  159. fmt.Printf("sb[%d]:", l)
  160. for i := l; i < h; i++ {
  161. if i >= ls {
  162. fmt.Printf(" --")
  163. continue
  164. }
  165. bs := hex(s[j])*16 + hex(s[j+1])
  166. j += 2
  167. fmt.Printf(" %.2x", bs)
  168. }
  169. fmt.Printf("\n")
  170. t.Fail()
  171. // t.Errorf("%s: \ngood: %s\nbad: %x", msg, s, b.Bytes())
  172. // Print the output in a partially-decoded format; can
  173. // be helpful when updating the test. It produces the output
  174. // that is pasted, with minor edits, into the argument to verify().
  175. // data := b.Bytes()
  176. // nesting := 0
  177. // for b.Len() > 0 {
  178. // start := len(data) - b.Len()
  179. // var u uint64
  180. // u, err := DecodeVarint(b)
  181. // if err != nil {
  182. // fmt.Printf("decode error on varint:", err)
  183. // return
  184. // }
  185. // wire := u & 0x7
  186. // tag := u >> 3
  187. // switch wire {
  188. // case WireVarint:
  189. // v, err := DecodeVarint(b)
  190. // if err != nil {
  191. // fmt.Printf("decode error on varint:", err)
  192. // return
  193. // }
  194. // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
  195. // data[start:len(data)-b.Len()], tag, wire, v)
  196. // case WireFixed32:
  197. // v, err := DecodeFixed32(b)
  198. // if err != nil {
  199. // fmt.Printf("decode error on fixed32:", err)
  200. // return
  201. // }
  202. // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
  203. // data[start:len(data)-b.Len()], tag, wire, v)
  204. // case WireFixed64:
  205. // v, err := DecodeFixed64(b)
  206. // if err != nil {
  207. // fmt.Printf("decode error on fixed64:", err)
  208. // return
  209. // }
  210. // fmt.Printf("\t\t\"%x\" // field %d, encoding %d, value %d\n",
  211. // data[start:len(data)-b.Len()], tag, wire, v)
  212. // case WireBytes:
  213. // nb, err := DecodeVarint(b)
  214. // if err != nil {
  215. // fmt.Printf("decode error on bytes:", err)
  216. // return
  217. // }
  218. // after_tag := len(data) - b.Len()
  219. // str := make([]byte, nb)
  220. // _, err = b.Read(str)
  221. // if err != nil {
  222. // fmt.Printf("decode error on bytes:", err)
  223. // return
  224. // }
  225. // fmt.Printf("\t\t\"%x\" \"%x\" // field %d, encoding %d (FIELD)\n",
  226. // data[start:after_tag], str, tag, wire)
  227. // case WireStartGroup:
  228. // nesting++
  229. // fmt.Printf("\t\t\"%x\"\t\t// start group field %d level %d\n",
  230. // data[start:len(data)-b.Len()], tag, nesting)
  231. // case WireEndGroup:
  232. // fmt.Printf("\t\t\"%x\"\t\t// end group field %d level %d\n",
  233. // data[start:len(data)-b.Len()], tag, nesting)
  234. // nesting--
  235. // default:
  236. // fmt.Printf("unrecognized wire type %d\n", wire)
  237. // return
  238. // }
  239. // }
  240. }
  241. func hex(c uint8) uint8 {
  242. if '0' <= c && c <= '9' {
  243. return c - '0'
  244. }
  245. if 'a' <= c && c <= 'f' {
  246. return 10 + c - 'a'
  247. }
  248. if 'A' <= c && c <= 'F' {
  249. return 10 + c - 'A'
  250. }
  251. return 0
  252. }
  253. func equal(b []byte, s string, t *testing.T) bool {
  254. if 2*len(b) != len(s) {
  255. // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
  256. fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
  257. return false
  258. }
  259. for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
  260. x := hex(s[j])*16 + hex(s[j+1])
  261. if b[i] != x {
  262. // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
  263. fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
  264. return false
  265. }
  266. }
  267. return true
  268. }
  269. func overify(t *testing.T, pb *GoTest, expected string) {
  270. o := old()
  271. err := o.Marshal(pb)
  272. if err != nil {
  273. fmt.Printf("overify marshal-1 err = %v", err)
  274. o.DebugPrint("", o.Bytes())
  275. t.Fatalf("expected = %s", expected)
  276. }
  277. if !equal(o.Bytes(), expected, t) {
  278. o.DebugPrint("overify neq 1", o.Bytes())
  279. t.Fatalf("expected = %s", expected)
  280. }
  281. // Now test Unmarshal by recreating the original buffer.
  282. pbd := new(GoTest)
  283. err = o.Unmarshal(pbd)
  284. if err != nil {
  285. t.Fatalf("overify unmarshal err = %v", err)
  286. o.DebugPrint("", o.Bytes())
  287. t.Fatalf("string = %s", expected)
  288. }
  289. o.Reset()
  290. err = o.Marshal(pbd)
  291. if err != nil {
  292. t.Errorf("overify marshal-2 err = %v", err)
  293. o.DebugPrint("", o.Bytes())
  294. t.Fatalf("string = %s", expected)
  295. }
  296. if !equal(o.Bytes(), expected, t) {
  297. o.DebugPrint("overify neq 2", o.Bytes())
  298. t.Fatalf("string = %s", expected)
  299. }
  300. }
  301. // Simple tests for numeric encode/decode primitives (varint, etc.)
  302. func TestNumericPrimitives(t *testing.T) {
  303. for i := uint64(0); i < 1e6; i += 111 {
  304. o := old()
  305. if o.EncodeVarint(i) != nil {
  306. t.Error("EncodeVarint")
  307. break
  308. }
  309. x, e := o.DecodeVarint()
  310. if e != nil {
  311. t.Fatal("DecodeVarint")
  312. }
  313. if x != i {
  314. t.Fatal("varint decode fail:", i, x)
  315. }
  316. o = old()
  317. if o.EncodeFixed32(i) != nil {
  318. t.Fatal("encFixed32")
  319. }
  320. x, e = o.DecodeFixed32()
  321. if e != nil {
  322. t.Fatal("decFixed32")
  323. }
  324. if x != i {
  325. t.Fatal("fixed32 decode fail:", i, x)
  326. }
  327. o = old()
  328. if o.EncodeFixed64(i*1234567) != nil {
  329. t.Error("encFixed64")
  330. break
  331. }
  332. x, e = o.DecodeFixed64()
  333. if e != nil {
  334. t.Error("decFixed64")
  335. break
  336. }
  337. if x != i*1234567 {
  338. t.Error("fixed64 decode fail:", i*1234567, x)
  339. break
  340. }
  341. o = old()
  342. i32 := int32(i - 12345)
  343. if o.EncodeZigzag32(uint64(i32)) != nil {
  344. t.Fatal("EncodeZigzag32")
  345. }
  346. x, e = o.DecodeZigzag32()
  347. if e != nil {
  348. t.Fatal("DecodeZigzag32")
  349. }
  350. if x != uint64(uint32(i32)) {
  351. t.Fatal("zigzag32 decode fail:", i32, x)
  352. }
  353. o = old()
  354. i64 := int64(i - 12345)
  355. if o.EncodeZigzag64(uint64(i64)) != nil {
  356. t.Fatal("EncodeZigzag64")
  357. }
  358. x, e = o.DecodeZigzag64()
  359. if e != nil {
  360. t.Fatal("DecodeZigzag64")
  361. }
  362. if x != uint64(i64) {
  363. t.Fatal("zigzag64 decode fail:", i64, x)
  364. }
  365. }
  366. }
  367. // Simple tests for bytes
  368. func TestBytesPrimitives(t *testing.T) {
  369. o := old()
  370. bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
  371. if o.EncodeRawBytes(bytes) != nil {
  372. t.Error("EncodeRawBytes")
  373. }
  374. decb, e := o.DecodeRawBytes(false)
  375. if e != nil {
  376. t.Error("DecodeRawBytes")
  377. }
  378. equalbytes(bytes, decb, t)
  379. }
  380. // Simple tests for strings
  381. func TestStringPrimitives(t *testing.T) {
  382. o := old()
  383. s := "now is the time"
  384. if o.EncodeStringBytes(s) != nil {
  385. t.Error("enc_string")
  386. }
  387. decs, e := o.DecodeStringBytes()
  388. if e != nil {
  389. t.Error("dec_string")
  390. }
  391. if s != decs {
  392. t.Error("string encode/decode fail:", s, decs)
  393. }
  394. }
  395. // Do we catch the "required bit not set" case?
  396. func TestRequiredBit(t *testing.T) {
  397. o := old()
  398. pb := new(GoTest)
  399. err := o.Marshal(pb)
  400. if err == nil {
  401. t.Error("did not catch missing required fields")
  402. } else if strings.Index(err.Error(), "GoTest") < 0 {
  403. t.Error("wrong error type:", err)
  404. }
  405. }
  406. // Check that all fields are nil.
  407. // Clearly silly, and a residue from a more interesting test with an earlier,
  408. // different initialization property, but it once caught a compiler bug so
  409. // it lives.
  410. func checkInitialized(pb *GoTest, t *testing.T) {
  411. if pb.F_BoolDefaulted != nil {
  412. t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
  413. }
  414. if pb.F_Int32Defaulted != nil {
  415. t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
  416. }
  417. if pb.F_Int64Defaulted != nil {
  418. t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
  419. }
  420. if pb.F_Fixed32Defaulted != nil {
  421. t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
  422. }
  423. if pb.F_Fixed64Defaulted != nil {
  424. t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
  425. }
  426. if pb.F_Uint32Defaulted != nil {
  427. t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
  428. }
  429. if pb.F_Uint64Defaulted != nil {
  430. t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
  431. }
  432. if pb.F_FloatDefaulted != nil {
  433. t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
  434. }
  435. if pb.F_DoubleDefaulted != nil {
  436. t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
  437. }
  438. if pb.F_StringDefaulted != nil {
  439. t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
  440. }
  441. if pb.F_BytesDefaulted != nil {
  442. t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
  443. }
  444. if pb.F_Sint32Defaulted != nil {
  445. t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
  446. }
  447. if pb.F_Sint64Defaulted != nil {
  448. t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
  449. }
  450. }
  451. // Does Reset() reset?
  452. func TestReset(t *testing.T) {
  453. pb := initGoTest(true)
  454. // muck with some values
  455. pb.F_BoolDefaulted = Bool(false)
  456. pb.F_Int32Defaulted = Int32(237)
  457. pb.F_Int64Defaulted = Int64(12346)
  458. pb.F_Fixed32Defaulted = Uint32(32000)
  459. pb.F_Fixed64Defaulted = Uint64(666)
  460. pb.F_Uint32Defaulted = Uint32(323232)
  461. pb.F_Uint64Defaulted = nil
  462. pb.F_FloatDefaulted = nil
  463. pb.F_DoubleDefaulted = Float64(0)
  464. pb.F_StringDefaulted = String("gotcha")
  465. pb.F_BytesDefaulted = []byte("asdfasdf")
  466. pb.F_Sint32Defaulted = Int32(123)
  467. pb.F_Sint64Defaulted = Int64(789)
  468. pb.Reset()
  469. checkInitialized(pb, t)
  470. }
  471. // All required fields set, no defaults provided.
  472. func TestEncodeDecode1(t *testing.T) {
  473. pb := initGoTest(false)
  474. overify(t, pb,
  475. "0807"+ // field 1, encoding 0, value 7
  476. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  477. "5001"+ // field 10, encoding 0, value 1
  478. "5803"+ // field 11, encoding 0, value 3
  479. "6006"+ // field 12, encoding 0, value 6
  480. "6d20000000"+ // field 13, encoding 5, value 0x20
  481. "714000000000000000"+ // field 14, encoding 1, value 0x40
  482. "78a019"+ // field 15, encoding 0, value 0xca0 = 3232
  483. "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
  484. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  485. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  486. "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
  487. "b304"+ // field 70, encoding 3, start group
  488. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  489. "b404"+ // field 70, encoding 4, end group
  490. "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
  491. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  492. "b8067f") // field 103, encoding 0, 0x7f zigzag64
  493. }
  494. // All required fields set, defaults provided.
  495. func TestEncodeDecode2(t *testing.T) {
  496. pb := initGoTest(true)
  497. overify(t, pb,
  498. "0807"+ // field 1, encoding 0, value 7
  499. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  500. "5001"+ // field 10, encoding 0, value 1
  501. "5803"+ // field 11, encoding 0, value 3
  502. "6006"+ // field 12, encoding 0, value 6
  503. "6d20000000"+ // field 13, encoding 5, value 32
  504. "714000000000000000"+ // field 14, encoding 1, value 64
  505. "78a019"+ // field 15, encoding 0, value 3232
  506. "8001c032"+ // field 16, encoding 0, value 6464
  507. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  508. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  509. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  510. "c00201"+ // field 40, encoding 0, value 1
  511. "c80220"+ // field 41, encoding 0, value 32
  512. "d00240"+ // field 42, encoding 0, value 64
  513. "dd0240010000"+ // field 43, encoding 5, value 320
  514. "e1028002000000000000"+ // field 44, encoding 1, value 640
  515. "e8028019"+ // field 45, encoding 0, value 3200
  516. "f0028032"+ // field 46, encoding 0, value 6400
  517. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  518. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  519. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  520. "b304"+ // start group field 70 level 1
  521. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  522. "b404"+ // end group field 70 level 1
  523. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  524. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  525. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  526. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  527. "90193f"+ // field 402, encoding 0, value 63
  528. "98197f") // field 403, encoding 0, value 127
  529. }
  530. // All default fields set to their default value by hand
  531. func TestEncodeDecode3(t *testing.T) {
  532. pb := initGoTest(false)
  533. pb.F_BoolDefaulted = Bool(true)
  534. pb.F_Int32Defaulted = Int32(32)
  535. pb.F_Int64Defaulted = Int64(64)
  536. pb.F_Fixed32Defaulted = Uint32(320)
  537. pb.F_Fixed64Defaulted = Uint64(640)
  538. pb.F_Uint32Defaulted = Uint32(3200)
  539. pb.F_Uint64Defaulted = Uint64(6400)
  540. pb.F_FloatDefaulted = Float32(314159)
  541. pb.F_DoubleDefaulted = Float64(271828)
  542. pb.F_StringDefaulted = String("hello, \"world!\"\n")
  543. pb.F_BytesDefaulted = []byte("Bignose")
  544. pb.F_Sint32Defaulted = Int32(-32)
  545. pb.F_Sint64Defaulted = Int64(-64)
  546. overify(t, pb,
  547. "0807"+ // field 1, encoding 0, value 7
  548. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  549. "5001"+ // field 10, encoding 0, value 1
  550. "5803"+ // field 11, encoding 0, value 3
  551. "6006"+ // field 12, encoding 0, value 6
  552. "6d20000000"+ // field 13, encoding 5, value 32
  553. "714000000000000000"+ // field 14, encoding 1, value 64
  554. "78a019"+ // field 15, encoding 0, value 3232
  555. "8001c032"+ // field 16, encoding 0, value 6464
  556. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  557. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  558. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  559. "c00201"+ // field 40, encoding 0, value 1
  560. "c80220"+ // field 41, encoding 0, value 32
  561. "d00240"+ // field 42, encoding 0, value 64
  562. "dd0240010000"+ // field 43, encoding 5, value 320
  563. "e1028002000000000000"+ // field 44, encoding 1, value 640
  564. "e8028019"+ // field 45, encoding 0, value 3200
  565. "f0028032"+ // field 46, encoding 0, value 6400
  566. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  567. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  568. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  569. "b304"+ // start group field 70 level 1
  570. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  571. "b404"+ // end group field 70 level 1
  572. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  573. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  574. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  575. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  576. "90193f"+ // field 402, encoding 0, value 63
  577. "98197f") // field 403, encoding 0, value 127
  578. }
  579. // All required fields set, defaults provided, all non-defaulted optional fields have values.
  580. func TestEncodeDecode4(t *testing.T) {
  581. pb := initGoTest(true)
  582. pb.Table = String("hello")
  583. pb.Param = Int32(7)
  584. pb.OptionalField = initGoTestField()
  585. pb.F_BoolOptional = Bool(true)
  586. pb.F_Int32Optional = Int32(32)
  587. pb.F_Int64Optional = Int64(64)
  588. pb.F_Fixed32Optional = Uint32(3232)
  589. pb.F_Fixed64Optional = Uint64(6464)
  590. pb.F_Uint32Optional = Uint32(323232)
  591. pb.F_Uint64Optional = Uint64(646464)
  592. pb.F_FloatOptional = Float32(32.)
  593. pb.F_DoubleOptional = Float64(64.)
  594. pb.F_StringOptional = String("hello")
  595. pb.F_BytesOptional = []byte("Bignose")
  596. pb.F_Sint32Optional = Int32(-32)
  597. pb.F_Sint64Optional = Int64(-64)
  598. pb.Optionalgroup = initGoTest_OptionalGroup()
  599. overify(t, pb,
  600. "0807"+ // field 1, encoding 0, value 7
  601. "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
  602. "1807"+ // field 3, encoding 0, value 7
  603. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  604. "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
  605. "5001"+ // field 10, encoding 0, value 1
  606. "5803"+ // field 11, encoding 0, value 3
  607. "6006"+ // field 12, encoding 0, value 6
  608. "6d20000000"+ // field 13, encoding 5, value 32
  609. "714000000000000000"+ // field 14, encoding 1, value 64
  610. "78a019"+ // field 15, encoding 0, value 3232
  611. "8001c032"+ // field 16, encoding 0, value 6464
  612. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  613. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  614. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  615. "f00101"+ // field 30, encoding 0, value 1
  616. "f80120"+ // field 31, encoding 0, value 32
  617. "800240"+ // field 32, encoding 0, value 64
  618. "8d02a00c0000"+ // field 33, encoding 5, value 3232
  619. "91024019000000000000"+ // field 34, encoding 1, value 6464
  620. "9802a0dd13"+ // field 35, encoding 0, value 323232
  621. "a002c0ba27"+ // field 36, encoding 0, value 646464
  622. "ad0200000042"+ // field 37, encoding 5, value 32.0
  623. "b1020000000000005040"+ // field 38, encoding 1, value 64.0
  624. "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
  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"
  635. "b304"+ // start group field 70 level 1
  636. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  637. "b404"+ // end group field 70 level 1
  638. "d305"+ // start group field 90 level 1
  639. "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
  640. "d405"+ // end group field 90 level 1
  641. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  642. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  643. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  644. "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
  645. "f0123f"+ // field 302, encoding 0, value 63
  646. "f8127f"+ // field 303, encoding 0, value 127
  647. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  648. "90193f"+ // field 402, encoding 0, value 63
  649. "98197f") // field 403, encoding 0, value 127
  650. }
  651. // All required fields set, defaults provided, all repeated fields given two values.
  652. func TestEncodeDecode5(t *testing.T) {
  653. pb := initGoTest(true)
  654. pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
  655. pb.F_BoolRepeated = []bool{false, true}
  656. pb.F_Int32Repeated = []int32{32, 33}
  657. pb.F_Int64Repeated = []int64{64, 65}
  658. pb.F_Fixed32Repeated = []uint32{3232, 3333}
  659. pb.F_Fixed64Repeated = []uint64{6464, 6565}
  660. pb.F_Uint32Repeated = []uint32{323232, 333333}
  661. pb.F_Uint64Repeated = []uint64{646464, 656565}
  662. pb.F_FloatRepeated = []float32{32., 33.}
  663. pb.F_DoubleRepeated = []float64{64., 65.}
  664. pb.F_StringRepeated = []string{"hello", "sailor"}
  665. pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
  666. pb.F_Sint32Repeated = []int32{32, -32}
  667. pb.F_Sint64Repeated = []int64{64, -64}
  668. pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
  669. overify(t, pb,
  670. "0807"+ // field 1, encoding 0, value 7
  671. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  672. "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
  673. "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
  674. "5001"+ // field 10, encoding 0, value 1
  675. "5803"+ // field 11, encoding 0, value 3
  676. "6006"+ // field 12, encoding 0, value 6
  677. "6d20000000"+ // field 13, encoding 5, value 32
  678. "714000000000000000"+ // field 14, encoding 1, value 64
  679. "78a019"+ // field 15, encoding 0, value 3232
  680. "8001c032"+ // field 16, encoding 0, value 6464
  681. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  682. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  683. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  684. "a00100"+ // field 20, encoding 0, value 0
  685. "a00101"+ // field 20, encoding 0, value 1
  686. "a80120"+ // field 21, encoding 0, value 32
  687. "a80121"+ // field 21, encoding 0, value 33
  688. "b00140"+ // field 22, encoding 0, value 64
  689. "b00141"+ // field 22, encoding 0, value 65
  690. "bd01a00c0000"+ // field 23, encoding 5, value 3232
  691. "bd01050d0000"+ // field 23, encoding 5, value 3333
  692. "c1014019000000000000"+ // field 24, encoding 1, value 6464
  693. "c101a519000000000000"+ // field 24, encoding 1, value 6565
  694. "c801a0dd13"+ // field 25, encoding 0, value 323232
  695. "c80195ac14"+ // field 25, encoding 0, value 333333
  696. "d001c0ba27"+ // field 26, encoding 0, value 646464
  697. "d001b58928"+ // field 26, encoding 0, value 656565
  698. "dd0100000042"+ // field 27, encoding 5, value 32.0
  699. "dd0100000442"+ // field 27, encoding 5, value 33.0
  700. "e1010000000000005040"+ // field 28, encoding 1, value 64.0
  701. "e1010000000000405040"+ // field 28, encoding 1, value 65.0
  702. "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
  703. "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
  704. "c00201"+ // field 40, encoding 0, value 1
  705. "c80220"+ // field 41, encoding 0, value 32
  706. "d00240"+ // field 42, encoding 0, value 64
  707. "dd0240010000"+ // field 43, encoding 5, value 320
  708. "e1028002000000000000"+ // field 44, encoding 1, value 640
  709. "e8028019"+ // field 45, encoding 0, value 3200
  710. "f0028032"+ // field 46, encoding 0, value 6400
  711. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  712. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  713. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  714. "b304"+ // start group field 70 level 1
  715. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  716. "b404"+ // end group field 70 level 1
  717. "8305"+ // start group field 80 level 1
  718. "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
  719. "8405"+ // end group field 80 level 1
  720. "8305"+ // start group field 80 level 1
  721. "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
  722. "8405"+ // end group field 80 level 1
  723. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  724. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  725. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  726. "ca0c03"+"626967"+ // field 201, encoding 2, string "big"
  727. "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
  728. "d00c40"+ // field 202, encoding 0, value 32
  729. "d00c3f"+ // field 202, encoding 0, value -32
  730. "d80c8001"+ // field 203, encoding 0, value 64
  731. "d80c7f"+ // field 203, encoding 0, value -64
  732. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  733. "90193f"+ // field 402, encoding 0, value 63
  734. "98197f") // field 403, encoding 0, value 127
  735. }
  736. // All required fields set, all packed repeated fields given two values.
  737. func TestEncodeDecode6(t *testing.T) {
  738. pb := initGoTest(false)
  739. pb.F_BoolRepeatedPacked = []bool{false, true}
  740. pb.F_Int32RepeatedPacked = []int32{32, 33}
  741. pb.F_Int64RepeatedPacked = []int64{64, 65}
  742. pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
  743. pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
  744. pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
  745. pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
  746. pb.F_FloatRepeatedPacked = []float32{32., 33.}
  747. pb.F_DoubleRepeatedPacked = []float64{64., 65.}
  748. pb.F_Sint32RepeatedPacked = []int32{32, -32}
  749. pb.F_Sint64RepeatedPacked = []int64{64, -64}
  750. overify(t, pb,
  751. "0807"+ // field 1, encoding 0, value 7
  752. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  753. "5001"+ // field 10, encoding 0, value 1
  754. "5803"+ // field 11, encoding 0, value 3
  755. "6006"+ // field 12, encoding 0, value 6
  756. "6d20000000"+ // field 13, encoding 5, value 32
  757. "714000000000000000"+ // field 14, encoding 1, value 64
  758. "78a019"+ // field 15, encoding 0, value 3232
  759. "8001c032"+ // field 16, encoding 0, value 6464
  760. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  761. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  762. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  763. "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
  764. "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
  765. "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
  766. "aa0308"+ // field 53, encoding 2, 8 bytes
  767. "a00c0000050d0000"+ // value 3232, value 3333
  768. "b20310"+ // field 54, encoding 2, 16 bytes
  769. "4019000000000000a519000000000000"+ // value 6464, value 6565
  770. "ba0306"+ // field 55, encoding 2, 6 bytes
  771. "a0dd1395ac14"+ // value 323232, value 333333
  772. "c20306"+ // field 56, encoding 2, 6 bytes
  773. "c0ba27b58928"+ // value 646464, value 656565
  774. "ca0308"+ // field 57, encoding 2, 8 bytes
  775. "0000004200000442"+ // value 32.0, value 33.0
  776. "d20310"+ // field 58, encoding 2, 16 bytes
  777. "00000000000050400000000000405040"+ // value 64.0, value 65.0
  778. "b304"+ // start group field 70 level 1
  779. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  780. "b404"+ // end group field 70 level 1
  781. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  782. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  783. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  784. "b21f02"+ // field 502, encoding 2, 2 bytes
  785. "403f"+ // value 32, value -32
  786. "ba1f03"+ // field 503, encoding 2, 3 bytes
  787. "80017f") // value 64, value -64
  788. }
  789. // Test that we can encode empty bytes fields.
  790. func TestEncodeDecodeBytes1(t *testing.T) {
  791. pb := initGoTest(false)
  792. // Create our bytes
  793. pb.F_BytesRequired = []byte{}
  794. pb.F_BytesRepeated = [][]byte{{}}
  795. pb.F_BytesOptional = []byte{}
  796. d, err := Marshal(pb)
  797. if err != nil {
  798. t.Error(err)
  799. }
  800. pbd := new(GoTest)
  801. if err := Unmarshal(d, pbd); err != nil {
  802. t.Error(err)
  803. }
  804. if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
  805. t.Error("required empty bytes field is incorrect")
  806. }
  807. if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
  808. t.Error("repeated empty bytes field is incorrect")
  809. }
  810. if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
  811. t.Error("optional empty bytes field is incorrect")
  812. }
  813. }
  814. // Test that we encode nil-valued fields of a repeated bytes field correctly.
  815. // Since entries in a repeated field cannot be nil, nil must mean empty value.
  816. func TestEncodeDecodeBytes2(t *testing.T) {
  817. pb := initGoTest(false)
  818. // Create our bytes
  819. pb.F_BytesRepeated = [][]byte{nil}
  820. d, err := Marshal(pb)
  821. if err != nil {
  822. t.Error(err)
  823. }
  824. pbd := new(GoTest)
  825. if err := Unmarshal(d, pbd); err != nil {
  826. t.Error(err)
  827. }
  828. if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
  829. t.Error("Unexpected value for repeated bytes field")
  830. }
  831. }
  832. // All required fields set, defaults provided, all repeated fields given two values.
  833. func TestSkippingUnrecognizedFields(t *testing.T) {
  834. o := old()
  835. pb := initGoTestField()
  836. // Marshal it normally.
  837. o.Marshal(pb)
  838. // Now new a GoSkipTest record.
  839. skip := &GoSkipTest{
  840. SkipInt32: Int32(32),
  841. SkipFixed32: Uint32(3232),
  842. SkipFixed64: Uint64(6464),
  843. SkipString: String("skipper"),
  844. Skipgroup: &GoSkipTest_SkipGroup{
  845. GroupInt32: Int32(75),
  846. GroupString: String("wxyz"),
  847. },
  848. }
  849. // Marshal it into same buffer.
  850. o.Marshal(skip)
  851. pbd := new(GoTestField)
  852. o.Unmarshal(pbd)
  853. // The __unrecognized field should be a marshaling of GoSkipTest
  854. skipd := new(GoSkipTest)
  855. o.SetBuf(pbd.XXX_unrecognized)
  856. o.Unmarshal(skipd)
  857. if *skipd.SkipInt32 != *skip.SkipInt32 {
  858. t.Error("skip int32", skipd.SkipInt32)
  859. }
  860. if *skipd.SkipFixed32 != *skip.SkipFixed32 {
  861. t.Error("skip fixed32", skipd.SkipFixed32)
  862. }
  863. if *skipd.SkipFixed64 != *skip.SkipFixed64 {
  864. t.Error("skip fixed64", skipd.SkipFixed64)
  865. }
  866. if *skipd.SkipString != *skip.SkipString {
  867. t.Error("skip string", *skipd.SkipString)
  868. }
  869. if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
  870. t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
  871. }
  872. if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
  873. t.Error("skip group string", *skipd.Skipgroup.GroupString)
  874. }
  875. }
  876. // Check that unrecognized fields of a submessage are preserved.
  877. func TestSubmessageUnrecognizedFields(t *testing.T) {
  878. nm := &NewMessage{
  879. Nested: &NewMessage_Nested{
  880. Name: String("Nigel"),
  881. FoodGroup: String("carbs"),
  882. },
  883. }
  884. b, err := Marshal(nm)
  885. if err != nil {
  886. t.Fatalf("Marshal of NewMessage: %v", err)
  887. }
  888. // Unmarshal into an OldMessage.
  889. om := new(OldMessage)
  890. if err := Unmarshal(b, om); err != nil {
  891. t.Fatalf("Unmarshal to OldMessage: %v", err)
  892. }
  893. exp := &OldMessage{
  894. Nested: &OldMessage_Nested{
  895. Name: String("Nigel"),
  896. // normal protocol buffer users should not do this
  897. XXX_unrecognized: []byte("\x12\x05carbs"),
  898. },
  899. }
  900. if !Equal(om, exp) {
  901. t.Errorf("om = %v, want %v", om, exp)
  902. }
  903. // Clone the OldMessage.
  904. om = Clone(om).(*OldMessage)
  905. if !Equal(om, exp) {
  906. t.Errorf("Clone(om) = %v, want %v", om, exp)
  907. }
  908. // Marshal the OldMessage, then unmarshal it into an empty NewMessage.
  909. if b, err = Marshal(om); err != nil {
  910. t.Fatalf("Marshal of OldMessage: %v", err)
  911. }
  912. t.Logf("Marshal(%v) -> %q", om, b)
  913. nm2 := new(NewMessage)
  914. if err := Unmarshal(b, nm2); err != nil {
  915. t.Fatalf("Unmarshal to NewMessage: %v", err)
  916. }
  917. if !Equal(nm, nm2) {
  918. t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
  919. }
  920. }
  921. // Check that we can grow an array (repeated field) to have many elements.
  922. // This test doesn't depend only on our encoding; for variety, it makes sure
  923. // we create, encode, and decode the correct contents explicitly. It's therefore
  924. // a bit messier.
  925. // This test also uses (and hence tests) the Marshal/Unmarshal functions
  926. // instead of the methods.
  927. func TestBigRepeated(t *testing.T) {
  928. pb := initGoTest(true)
  929. // Create the arrays
  930. const N = 50 // Internally the library starts much smaller.
  931. pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
  932. pb.F_Sint64Repeated = make([]int64, N)
  933. pb.F_Sint32Repeated = make([]int32, N)
  934. pb.F_BytesRepeated = make([][]byte, N)
  935. pb.F_StringRepeated = make([]string, N)
  936. pb.F_DoubleRepeated = make([]float64, N)
  937. pb.F_FloatRepeated = make([]float32, N)
  938. pb.F_Uint64Repeated = make([]uint64, N)
  939. pb.F_Uint32Repeated = make([]uint32, N)
  940. pb.F_Fixed64Repeated = make([]uint64, N)
  941. pb.F_Fixed32Repeated = make([]uint32, N)
  942. pb.F_Int64Repeated = make([]int64, N)
  943. pb.F_Int32Repeated = make([]int32, N)
  944. pb.F_BoolRepeated = make([]bool, N)
  945. pb.RepeatedField = make([]*GoTestField, N)
  946. // Fill in the arrays with checkable values.
  947. igtf := initGoTestField()
  948. igtrg := initGoTest_RepeatedGroup()
  949. for i := 0; i < N; i++ {
  950. pb.Repeatedgroup[i] = igtrg
  951. pb.F_Sint64Repeated[i] = int64(i)
  952. pb.F_Sint32Repeated[i] = int32(i)
  953. s := fmt.Sprint(i)
  954. pb.F_BytesRepeated[i] = []byte(s)
  955. pb.F_StringRepeated[i] = s
  956. pb.F_DoubleRepeated[i] = float64(i)
  957. pb.F_FloatRepeated[i] = float32(i)
  958. pb.F_Uint64Repeated[i] = uint64(i)
  959. pb.F_Uint32Repeated[i] = uint32(i)
  960. pb.F_Fixed64Repeated[i] = uint64(i)
  961. pb.F_Fixed32Repeated[i] = uint32(i)
  962. pb.F_Int64Repeated[i] = int64(i)
  963. pb.F_Int32Repeated[i] = int32(i)
  964. pb.F_BoolRepeated[i] = i%2 == 0
  965. pb.RepeatedField[i] = igtf
  966. }
  967. // Marshal.
  968. buf, _ := Marshal(pb)
  969. // Now test Unmarshal by recreating the original buffer.
  970. pbd := new(GoTest)
  971. Unmarshal(buf, pbd)
  972. // Check the checkable values
  973. for i := uint64(0); i < N; i++ {
  974. if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
  975. t.Error("pbd.Repeatedgroup bad")
  976. }
  977. var x uint64
  978. x = uint64(pbd.F_Sint64Repeated[i])
  979. if x != i {
  980. t.Error("pbd.F_Sint64Repeated bad", x, i)
  981. }
  982. x = uint64(pbd.F_Sint32Repeated[i])
  983. if x != i {
  984. t.Error("pbd.F_Sint32Repeated bad", x, i)
  985. }
  986. s := fmt.Sprint(i)
  987. equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
  988. if pbd.F_StringRepeated[i] != s {
  989. t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
  990. }
  991. x = uint64(pbd.F_DoubleRepeated[i])
  992. if x != i {
  993. t.Error("pbd.F_DoubleRepeated bad", x, i)
  994. }
  995. x = uint64(pbd.F_FloatRepeated[i])
  996. if x != i {
  997. t.Error("pbd.F_FloatRepeated bad", x, i)
  998. }
  999. x = pbd.F_Uint64Repeated[i]
  1000. if x != i {
  1001. t.Error("pbd.F_Uint64Repeated bad", x, i)
  1002. }
  1003. x = uint64(pbd.F_Uint32Repeated[i])
  1004. if x != i {
  1005. t.Error("pbd.F_Uint32Repeated bad", x, i)
  1006. }
  1007. x = pbd.F_Fixed64Repeated[i]
  1008. if x != i {
  1009. t.Error("pbd.F_Fixed64Repeated bad", x, i)
  1010. }
  1011. x = uint64(pbd.F_Fixed32Repeated[i])
  1012. if x != i {
  1013. t.Error("pbd.F_Fixed32Repeated bad", x, i)
  1014. }
  1015. x = uint64(pbd.F_Int64Repeated[i])
  1016. if x != i {
  1017. t.Error("pbd.F_Int64Repeated bad", x, i)
  1018. }
  1019. x = uint64(pbd.F_Int32Repeated[i])
  1020. if x != i {
  1021. t.Error("pbd.F_Int32Repeated bad", x, i)
  1022. }
  1023. if pbd.F_BoolRepeated[i] != (i%2 == 0) {
  1024. t.Error("pbd.F_BoolRepeated bad", x, i)
  1025. }
  1026. if pbd.RepeatedField[i] == nil { // TODO: more checking?
  1027. t.Error("pbd.RepeatedField bad")
  1028. }
  1029. }
  1030. }
  1031. // Verify we give a useful message when decoding to the wrong structure type.
  1032. func TestTypeMismatch(t *testing.T) {
  1033. pb1 := initGoTest(true)
  1034. // Marshal
  1035. o := old()
  1036. o.Marshal(pb1)
  1037. // Now Unmarshal it to the wrong type.
  1038. pb2 := initGoTestField()
  1039. err := o.Unmarshal(pb2)
  1040. switch err {
  1041. case ErrWrongType:
  1042. // fine
  1043. case nil:
  1044. t.Error("expected wrong type error, got no error")
  1045. default:
  1046. t.Error("expected wrong type error, got", err)
  1047. }
  1048. }
  1049. func encodeDecode(t *testing.T, in, out Message, msg string) {
  1050. buf, err := Marshal(in)
  1051. if err != nil {
  1052. t.Fatalf("failed marshaling %v: %v", msg, err)
  1053. }
  1054. if err := Unmarshal(buf, out); err != nil {
  1055. t.Fatalf("failed unmarshaling %v: %v", msg, err)
  1056. }
  1057. }
  1058. func TestPackedNonPackedDecoderSwitching(t *testing.T) {
  1059. np, p := new(NonPackedTest), new(PackedTest)
  1060. // non-packed -> packed
  1061. np.A = []int32{0, 1, 1, 2, 3, 5}
  1062. encodeDecode(t, np, p, "non-packed -> packed")
  1063. if !reflect.DeepEqual(np.A, p.B) {
  1064. t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
  1065. }
  1066. // packed -> non-packed
  1067. np.Reset()
  1068. p.B = []int32{3, 1, 4, 1, 5, 9}
  1069. encodeDecode(t, p, np, "packed -> non-packed")
  1070. if !reflect.DeepEqual(p.B, np.A) {
  1071. t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
  1072. }
  1073. }
  1074. func TestProto1RepeatedGroup(t *testing.T) {
  1075. pb := &MessageList{
  1076. Message: []*MessageList_Message{
  1077. {
  1078. Name: String("blah"),
  1079. Count: Int32(7),
  1080. },
  1081. // NOTE: pb.Message[1] is a nil
  1082. nil,
  1083. },
  1084. }
  1085. o := old()
  1086. if err := o.Marshal(pb); err != ErrRepeatedHasNil {
  1087. t.Fatalf("unexpected or no error when marshaling: %v", err)
  1088. }
  1089. }
  1090. // Test that enums work. Checks for a bug introduced by making enums
  1091. // named types instead of int32: newInt32FromUint64 would crash with
  1092. // a type mismatch in reflect.PointTo.
  1093. func TestEnum(t *testing.T) {
  1094. pb := new(GoEnum)
  1095. pb.Foo = FOO_FOO1.Enum()
  1096. o := old()
  1097. if err := o.Marshal(pb); err != nil {
  1098. t.Fatal("error encoding enum:", err)
  1099. }
  1100. pb1 := new(GoEnum)
  1101. if err := o.Unmarshal(pb1); err != nil {
  1102. t.Fatal("error decoding enum:", err)
  1103. }
  1104. if *pb1.Foo != FOO_FOO1 {
  1105. t.Error("expected 7 but got ", *pb1.Foo)
  1106. }
  1107. }
  1108. // Enum types have String methods. Check that enum fields can be printed.
  1109. // We don't care what the value actually is, just as long as it doesn't crash.
  1110. func TestPrintingNilEnumFields(t *testing.T) {
  1111. pb := new(GoEnum)
  1112. fmt.Sprintf("%+v", pb)
  1113. }
  1114. // Verify that absent required fields cause Marshal/Unmarshal to return errors.
  1115. func TestRequiredFieldEnforcement(t *testing.T) {
  1116. pb := new(GoTestField)
  1117. _, err := Marshal(pb)
  1118. if err == nil {
  1119. t.Error("marshal: expected error, got nil")
  1120. } else if strings.Index(err.Error(), "GoTestField") < 0 {
  1121. t.Errorf("marshal: bad error type: %v", err)
  1122. }
  1123. // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
  1124. // so simply counting the required fields is insufficient.
  1125. // field 1, encoding 2, value "hi"
  1126. buf := []byte("\x0A\x02hi\x0A\x02hi")
  1127. err = Unmarshal(buf, pb)
  1128. if err == nil {
  1129. t.Error("unmarshal: expected error, got nil")
  1130. } else if strings.Index(err.Error(), "GoTestField") < 0 {
  1131. t.Errorf("unmarshal: bad error type: %v", err)
  1132. }
  1133. }
  1134. func TestTypedNilMarshal(t *testing.T) {
  1135. // A typed nil should return ErrNil and not crash.
  1136. _, err := Marshal((*GoEnum)(nil))
  1137. if err != ErrNil {
  1138. t.Errorf("Marshal: got err %v, want ErrNil", err)
  1139. }
  1140. }
  1141. // A type that implements the Marshaler interface, but is not nillable.
  1142. type nonNillableInt uint64
  1143. func (nni nonNillableInt) Marshal() ([]byte, error) {
  1144. return EncodeVarint(uint64(nni)), nil
  1145. }
  1146. type NNIMessage struct {
  1147. nni nonNillableInt
  1148. }
  1149. func (*NNIMessage) Reset() {}
  1150. func (*NNIMessage) String() string { return "" }
  1151. func (*NNIMessage) ProtoMessage() {}
  1152. // A type that implements the Marshaler interface and is nillable.
  1153. type nillableMessage struct {
  1154. x uint64
  1155. }
  1156. func (nm *nillableMessage) Marshal() ([]byte, error) {
  1157. return EncodeVarint(nm.x), nil
  1158. }
  1159. type NMMessage struct {
  1160. nm *nillableMessage
  1161. }
  1162. func (*NMMessage) Reset() {}
  1163. func (*NMMessage) String() string { return "" }
  1164. func (*NMMessage) ProtoMessage() {}
  1165. // Verify a type that uses the Marshaler interface, but has a nil pointer.
  1166. func TestNilMarshaler(t *testing.T) {
  1167. // Try a struct with a Marshaler field that is nil.
  1168. // It should be directly marshable.
  1169. nmm := new(NMMessage)
  1170. if _, err := Marshal(nmm); err != nil {
  1171. t.Error("unexpected error marshaling nmm: ", err)
  1172. }
  1173. // Try a struct with a Marshaler field that is not nillable.
  1174. nnim := new(NNIMessage)
  1175. nnim.nni = 7
  1176. var _ Marshaler = nnim.nni // verify it is truly a Marshaler
  1177. if _, err := Marshal(nnim); err != nil {
  1178. t.Error("unexpected error marshaling nnim: ", err)
  1179. }
  1180. }
  1181. func TestAllSetDefaults(t *testing.T) {
  1182. // Exercise SetDefaults with all scalar field types.
  1183. m := &Defaults{
  1184. // NaN != NaN, so override that here.
  1185. F_Nan: Float32(1.7),
  1186. }
  1187. expected := &Defaults{
  1188. F_Bool: Bool(true),
  1189. F_Int32: Int32(32),
  1190. F_Int64: Int64(64),
  1191. F_Fixed32: Uint32(320),
  1192. F_Fixed64: Uint64(640),
  1193. F_Uint32: Uint32(3200),
  1194. F_Uint64: Uint64(6400),
  1195. F_Float: Float32(314159),
  1196. F_Double: Float64(271828),
  1197. F_String: String(`hello, "world!"` + "\n"),
  1198. F_Bytes: []byte("Bignose"),
  1199. F_Sint32: Int32(-32),
  1200. F_Sint64: Int64(-64),
  1201. F_Enum: Defaults_GREEN.Enum(),
  1202. F_Pinf: Float32(float32(math.Inf(1))),
  1203. F_Ninf: Float32(float32(math.Inf(-1))),
  1204. F_Nan: Float32(1.7),
  1205. }
  1206. SetDefaults(m)
  1207. if !Equal(m, expected) {
  1208. t.Errorf(" got %v\nwant %v", m, expected)
  1209. }
  1210. }
  1211. func TestSetDefaultsWithSetField(t *testing.T) {
  1212. // Check that a set value is not overridden.
  1213. m := &Defaults{
  1214. F_Int32: Int32(12),
  1215. }
  1216. SetDefaults(m)
  1217. if v := m.GetF_Int32(); v != 12 {
  1218. t.Errorf("m.FInt32 = %v, want 12", v)
  1219. }
  1220. }
  1221. func TestSetDefaultsWithSubMessage(t *testing.T) {
  1222. m := &OtherMessage{
  1223. Key: Int64(123),
  1224. Inner: &InnerMessage{
  1225. Host: String("gopher"),
  1226. },
  1227. }
  1228. expected := &OtherMessage{
  1229. Key: Int64(123),
  1230. Inner: &InnerMessage{
  1231. Host: String("gopher"),
  1232. Port: Int32(4000),
  1233. },
  1234. }
  1235. SetDefaults(m)
  1236. if !Equal(m, expected) {
  1237. t.Errorf("\n got %v\nwant %v", m, expected)
  1238. }
  1239. }
  1240. func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
  1241. m := &MyMessage{
  1242. RepInner: []*InnerMessage{{}},
  1243. }
  1244. expected := &MyMessage{
  1245. RepInner: []*InnerMessage{{
  1246. Port: Int32(4000),
  1247. }},
  1248. }
  1249. SetDefaults(m)
  1250. if !Equal(m, expected) {
  1251. t.Errorf("\n got %v\nwant %v", m, expected)
  1252. }
  1253. }
  1254. func TestMaximumTagNumber(t *testing.T) {
  1255. m := &MaxTag{
  1256. LastField: String("natural goat essence"),
  1257. }
  1258. buf, err := Marshal(m)
  1259. if err != nil {
  1260. t.Fatalf("proto.Marshal failed: %v", err)
  1261. }
  1262. m2 := new(MaxTag)
  1263. if err := Unmarshal(buf, m2); err != nil {
  1264. t.Fatalf("proto.Unmarshal failed: %v", err)
  1265. }
  1266. if got, want := m2.GetLastField(), *m.LastField; got != want {
  1267. t.Errorf("got %q, want %q", got, want)
  1268. }
  1269. }
  1270. func TestJSON(t *testing.T) {
  1271. m := &MyMessage{
  1272. Count: Int32(4),
  1273. Pet: []string{"bunny", "kitty"},
  1274. Inner: &InnerMessage{
  1275. Host: String("cauchy"),
  1276. },
  1277. Bikeshed: MyMessage_GREEN.Enum(),
  1278. }
  1279. const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
  1280. b, err := json.Marshal(m)
  1281. if err != nil {
  1282. t.Fatalf("json.Marshal failed: %v", err)
  1283. }
  1284. s := string(b)
  1285. if s != expected {
  1286. t.Errorf("got %s\nwant %s", s, expected)
  1287. }
  1288. received := new(MyMessage)
  1289. if err := json.Unmarshal(b, received); err != nil {
  1290. t.Fatalf("json.Unmarshal failed: %v", err)
  1291. }
  1292. if !Equal(received, m) {
  1293. t.Fatalf("got %s, want %s", received, m)
  1294. }
  1295. // Test unmarshalling of older json wire format.
  1296. const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
  1297. received.Reset()
  1298. if err := json.Unmarshal([]byte(old), received); err != nil {
  1299. t.Fatalf("json.Unmarshal failed: %v", err)
  1300. }
  1301. if !Equal(received, m) {
  1302. t.Fatalf("got %s, want %s", received, m)
  1303. }
  1304. }
  1305. func TestBadWireType(t *testing.T) {
  1306. b := []byte{7<<3 | 6} // field 7, wire type 6
  1307. pb := new(OtherMessage)
  1308. if err := Unmarshal(b, pb); err == nil {
  1309. t.Errorf("Unmarshal did not fail")
  1310. } else if !strings.Contains(err.Error(), "unknown wire type") {
  1311. t.Errorf("wrong error: %v", err)
  1312. }
  1313. }
  1314. func TestBytesWithInvalidLength(t *testing.T) {
  1315. // If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
  1316. b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
  1317. Unmarshal(b, new(MyMessage))
  1318. }
  1319. func TestLengthOverflow(t *testing.T) {
  1320. // Overflowing a length should not panic.
  1321. b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
  1322. Unmarshal(b, new(MyMessage))
  1323. }
  1324. func TestVarintOverflow(t *testing.T) {
  1325. // Overflowing a 64-bit length should not be allowed.
  1326. b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
  1327. if err := Unmarshal(b, new(MyMessage)); err == nil {
  1328. t.Fatalf("Overflowed uint64 length without error")
  1329. }
  1330. }
  1331. func TestUnmarshalFuzz(t *testing.T) {
  1332. const N = 1000
  1333. seed := time.Now().UnixNano()
  1334. t.Logf("RNG seed is %d", seed)
  1335. rng := rand.New(rand.NewSource(seed))
  1336. buf := make([]byte, 20)
  1337. for i := 0; i < N; i++ {
  1338. for j := range buf {
  1339. buf[j] = byte(rng.Intn(256))
  1340. }
  1341. fuzzUnmarshal(t, buf)
  1342. }
  1343. }
  1344. func TestMergeMessages(t *testing.T) {
  1345. pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
  1346. data, err := Marshal(pb)
  1347. if err != nil {
  1348. t.Fatalf("Marshal: %v", err)
  1349. }
  1350. pb1 := new(MessageList)
  1351. if err := Unmarshal(data, pb1); err != nil {
  1352. t.Fatalf("first Unmarshal: %v", err)
  1353. }
  1354. if err := Unmarshal(data, pb1); err != nil {
  1355. t.Fatalf("second Unmarshal: %v", err)
  1356. }
  1357. if len(pb1.Message) != 1 {
  1358. t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
  1359. }
  1360. pb2 := new(MessageList)
  1361. if err := UnmarshalMerge(data, pb2); err != nil {
  1362. t.Fatalf("first UnmarshalMerge: %v", err)
  1363. }
  1364. if err := UnmarshalMerge(data, pb2); err != nil {
  1365. t.Fatalf("second UnmarshalMerge: %v", err)
  1366. }
  1367. if len(pb2.Message) != 2 {
  1368. t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
  1369. }
  1370. }
  1371. func TestExtensionMarshalOrder(t *testing.T) {
  1372. m := &MyMessage{Count: Int(123)}
  1373. if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
  1374. t.Fatalf("SetExtension: %v", err)
  1375. }
  1376. if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
  1377. t.Fatalf("SetExtension: %v", err)
  1378. }
  1379. if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
  1380. t.Fatalf("SetExtension: %v", err)
  1381. }
  1382. // Serialize m several times, and check we get the same bytes each time.
  1383. var orig []byte
  1384. for i := 0; i < 100; i++ {
  1385. b, err := Marshal(m)
  1386. if err != nil {
  1387. t.Fatalf("Marshal: %v", err)
  1388. }
  1389. if i == 0 {
  1390. orig = b
  1391. continue
  1392. }
  1393. if !bytes.Equal(b, orig) {
  1394. t.Errorf("Bytes differ on attempt #%d", i)
  1395. }
  1396. }
  1397. }
  1398. // Many extensions, because small maps might not iterate differently on each iteration.
  1399. var exts = []*ExtensionDesc{
  1400. E_X201,
  1401. E_X202,
  1402. E_X203,
  1403. E_X204,
  1404. E_X205,
  1405. E_X206,
  1406. E_X207,
  1407. E_X208,
  1408. E_X209,
  1409. E_X210,
  1410. E_X211,
  1411. E_X212,
  1412. E_X213,
  1413. E_X214,
  1414. E_X215,
  1415. E_X216,
  1416. E_X217,
  1417. E_X218,
  1418. E_X219,
  1419. E_X220,
  1420. E_X221,
  1421. E_X222,
  1422. E_X223,
  1423. E_X224,
  1424. E_X225,
  1425. E_X226,
  1426. E_X227,
  1427. E_X228,
  1428. E_X229,
  1429. E_X230,
  1430. E_X231,
  1431. E_X232,
  1432. E_X233,
  1433. E_X234,
  1434. E_X235,
  1435. E_X236,
  1436. E_X237,
  1437. E_X238,
  1438. E_X239,
  1439. E_X240,
  1440. E_X241,
  1441. E_X242,
  1442. E_X243,
  1443. E_X244,
  1444. E_X245,
  1445. E_X246,
  1446. E_X247,
  1447. E_X248,
  1448. E_X249,
  1449. E_X250,
  1450. }
  1451. func TestMessageSetMarshalOrder(t *testing.T) {
  1452. m := &MyMessageSet{}
  1453. for _, x := range exts {
  1454. if err := SetExtension(m, x, &Empty{}); err != nil {
  1455. t.Fatalf("SetExtension: %v", err)
  1456. }
  1457. }
  1458. buf, err := Marshal(m)
  1459. if err != nil {
  1460. t.Fatalf("Marshal: %v", err)
  1461. }
  1462. // Serialize m several times, and check we get the same bytes each time.
  1463. for i := 0; i < 10; i++ {
  1464. b1, err := Marshal(m)
  1465. if err != nil {
  1466. t.Fatalf("Marshal: %v", err)
  1467. }
  1468. if !bytes.Equal(b1, buf) {
  1469. t.Errorf("Bytes differ on re-Marshal #%d", i)
  1470. }
  1471. m2 := &MyMessageSet{}
  1472. if err := Unmarshal(buf, m2); err != nil {
  1473. t.Errorf("Unmarshal: %v", err)
  1474. }
  1475. b2, err := Marshal(m2)
  1476. if err != nil {
  1477. t.Errorf("re-Marshal: %v", err)
  1478. }
  1479. if !bytes.Equal(b2, buf) {
  1480. t.Errorf("Bytes differ on round-trip #%d", i)
  1481. }
  1482. }
  1483. }
  1484. func TestUnmarshalMergesMessages(t *testing.T) {
  1485. // If a nested message occurs twice in the input,
  1486. // the fields should be merged when decoding.
  1487. a := &OtherMessage{
  1488. Key: Int64(123),
  1489. Inner: &InnerMessage{
  1490. Host: String("polhode"),
  1491. Port: Int32(1234),
  1492. },
  1493. }
  1494. aData, err := Marshal(a)
  1495. if err != nil {
  1496. t.Fatalf("Marshal(a): %v", err)
  1497. }
  1498. b := &OtherMessage{
  1499. Weight: Float32(1.2),
  1500. Inner: &InnerMessage{
  1501. Host: String("herpolhode"),
  1502. Connected: Bool(true),
  1503. },
  1504. }
  1505. bData, err := Marshal(b)
  1506. if err != nil {
  1507. t.Fatalf("Marshal(b): %v", err)
  1508. }
  1509. want := &OtherMessage{
  1510. Key: Int64(123),
  1511. Weight: Float32(1.2),
  1512. Inner: &InnerMessage{
  1513. Host: String("herpolhode"),
  1514. Port: Int32(1234),
  1515. Connected: Bool(true),
  1516. },
  1517. }
  1518. got := new(OtherMessage)
  1519. if err := Unmarshal(append(aData, bData...), got); err != nil {
  1520. t.Fatalf("Unmarshal: %v", err)
  1521. }
  1522. if !Equal(got, want) {
  1523. t.Errorf("\n got %v\nwant %v", got, want)
  1524. }
  1525. }
  1526. func TestEncodingSizes(t *testing.T) {
  1527. tests := []struct {
  1528. m Message
  1529. n int
  1530. }{
  1531. {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
  1532. {&Defaults{F_Int32: Int32(math.MinInt32)}, 6},
  1533. {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
  1534. }
  1535. for _, test := range tests {
  1536. b, err := Marshal(test.m)
  1537. if err != nil {
  1538. t.Errorf("Marshal(%v): %v", test.m, err)
  1539. continue
  1540. }
  1541. if len(b) != test.n {
  1542. t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
  1543. }
  1544. }
  1545. }
  1546. func fuzzUnmarshal(t *testing.T, data []byte) {
  1547. defer func() {
  1548. if e := recover(); e != nil {
  1549. t.Errorf("These bytes caused a panic: %+v", data)
  1550. t.Logf("Stack:\n%s", debug.Stack())
  1551. t.FailNow()
  1552. }
  1553. }()
  1554. pb := new(MyMessage)
  1555. Unmarshal(data, pb)
  1556. }
  1557. func benchmarkMsg(bytes bool) *GoTest {
  1558. pb := initGoTest(true)
  1559. if bytes {
  1560. buf := make([]byte, 4000)
  1561. for i := range buf {
  1562. buf[i] = byte(i)
  1563. }
  1564. pb.F_BytesDefaulted = buf
  1565. } else {
  1566. const N = 1000 // Internally the library starts much smaller.
  1567. pb.F_Int32Repeated = make([]int32, N)
  1568. pb.F_DoubleRepeated = make([]float64, N)
  1569. for i := 0; i < N; i++ {
  1570. pb.F_Int32Repeated[i] = int32(i)
  1571. pb.F_DoubleRepeated[i] = float64(i)
  1572. }
  1573. }
  1574. return pb
  1575. }
  1576. func BenchmarkMarshal(b *testing.B) {
  1577. pb := benchmarkMsg(false)
  1578. p := NewBuffer(nil)
  1579. b.ResetTimer()
  1580. for i := 0; i < b.N; i++ {
  1581. p.Reset()
  1582. p.Marshal(pb)
  1583. }
  1584. b.SetBytes(int64(len(p.Bytes())))
  1585. }
  1586. func BenchmarkUnmarshal(b *testing.B) {
  1587. pb := benchmarkMsg(false)
  1588. p := NewBuffer(nil)
  1589. p.Marshal(pb)
  1590. b.SetBytes(int64(len(p.Bytes())))
  1591. p2 := NewBuffer(nil)
  1592. pbd := new(GoTest)
  1593. b.ResetTimer()
  1594. for i := 0; i < b.N; i++ {
  1595. p2.SetBuf(p.Bytes())
  1596. p2.Unmarshal(pbd)
  1597. }
  1598. }
  1599. func BenchmarkMarshalBytes(b *testing.B) {
  1600. pb := benchmarkMsg(true)
  1601. p := NewBuffer(nil)
  1602. b.ResetTimer()
  1603. for i := 0; i < b.N; i++ {
  1604. p.Reset()
  1605. p.Marshal(pb)
  1606. }
  1607. b.SetBytes(int64(len(p.Bytes())))
  1608. }
  1609. func BenchmarkUnmarshalBytes(b *testing.B) {
  1610. pb := benchmarkMsg(true)
  1611. p := NewBuffer(nil)
  1612. p.Marshal(pb)
  1613. b.SetBytes(int64(len(p.Bytes())))
  1614. p2 := NewBuffer(nil)
  1615. pbd := new(GoTest)
  1616. b.ResetTimer()
  1617. for i := 0; i < b.N; i++ {
  1618. p2.SetBuf(p.Bytes())
  1619. p2.Unmarshal(pbd)
  1620. }
  1621. }
  1622. func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
  1623. b.StopTimer()
  1624. pb := initGoTestField()
  1625. skip := &GoSkipTest{
  1626. SkipInt32: Int32(32),
  1627. SkipFixed32: Uint32(3232),
  1628. SkipFixed64: Uint64(6464),
  1629. SkipString: String("skipper"),
  1630. Skipgroup: &GoSkipTest_SkipGroup{
  1631. GroupInt32: Int32(75),
  1632. GroupString: String("wxyz"),
  1633. },
  1634. }
  1635. pbd := new(GoTestField)
  1636. p := NewBuffer(nil)
  1637. p.Marshal(pb)
  1638. p.Marshal(skip)
  1639. p2 := NewBuffer(nil)
  1640. b.StartTimer()
  1641. for i := 0; i < b.N; i++ {
  1642. p2.SetBuf(p.Bytes())
  1643. p2.Unmarshal(pbd)
  1644. }
  1645. }