all_test.go 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. "errors"
  36. "fmt"
  37. "math"
  38. "math/rand"
  39. "reflect"
  40. "runtime/debug"
  41. "strings"
  42. "sync"
  43. "testing"
  44. "time"
  45. "github.com/golang/protobuf/jsonpb"
  46. . "github.com/golang/protobuf/proto"
  47. pb3 "github.com/golang/protobuf/proto/proto3_proto"
  48. . "github.com/golang/protobuf/proto/test_proto"
  49. descriptorpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
  50. )
  51. var globalO *Buffer
  52. func old() *Buffer {
  53. if globalO == nil {
  54. globalO = NewBuffer(nil)
  55. }
  56. globalO.Reset()
  57. return globalO
  58. }
  59. func equalbytes(b1, b2 []byte, t *testing.T) {
  60. if len(b1) != len(b2) {
  61. t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
  62. return
  63. }
  64. for i := 0; i < len(b1); i++ {
  65. if b1[i] != b2[i] {
  66. t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
  67. }
  68. }
  69. }
  70. func initGoTestField() *GoTestField {
  71. f := new(GoTestField)
  72. f.Label = String("label")
  73. f.Type = String("type")
  74. return f
  75. }
  76. // These are all structurally equivalent but the tag numbers differ.
  77. // (It's remarkable that required, optional, and repeated all have
  78. // 8 letters.)
  79. func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
  80. return &GoTest_RequiredGroup{
  81. RequiredField: String("required"),
  82. }
  83. }
  84. func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
  85. return &GoTest_OptionalGroup{
  86. RequiredField: String("optional"),
  87. }
  88. }
  89. func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
  90. return &GoTest_RepeatedGroup{
  91. RequiredField: String("repeated"),
  92. }
  93. }
  94. func initGoTest(setdefaults bool) *GoTest {
  95. pb := new(GoTest)
  96. if setdefaults {
  97. pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
  98. pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
  99. pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
  100. pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
  101. pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
  102. pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
  103. pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
  104. pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
  105. pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
  106. pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
  107. pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
  108. pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
  109. pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
  110. pb.F_Sfixed32Defaulted = Int32(Default_GoTest_F_Sfixed32Defaulted)
  111. pb.F_Sfixed64Defaulted = Int64(Default_GoTest_F_Sfixed64Defaulted)
  112. }
  113. pb.Kind = GoTest_TIME.Enum()
  114. pb.RequiredField = initGoTestField()
  115. pb.F_BoolRequired = Bool(true)
  116. pb.F_Int32Required = Int32(3)
  117. pb.F_Int64Required = Int64(6)
  118. pb.F_Fixed32Required = Uint32(32)
  119. pb.F_Fixed64Required = Uint64(64)
  120. pb.F_Uint32Required = Uint32(3232)
  121. pb.F_Uint64Required = Uint64(6464)
  122. pb.F_FloatRequired = Float32(3232)
  123. pb.F_DoubleRequired = Float64(6464)
  124. pb.F_StringRequired = String("string")
  125. pb.F_BytesRequired = []byte("bytes")
  126. pb.F_Sint32Required = Int32(-32)
  127. pb.F_Sint64Required = Int64(-64)
  128. pb.F_Sfixed32Required = Int32(-32)
  129. pb.F_Sfixed64Required = Int64(-64)
  130. pb.Requiredgroup = initGoTest_RequiredGroup()
  131. return pb
  132. }
  133. func hex(c uint8) uint8 {
  134. if '0' <= c && c <= '9' {
  135. return c - '0'
  136. }
  137. if 'a' <= c && c <= 'f' {
  138. return 10 + c - 'a'
  139. }
  140. if 'A' <= c && c <= 'F' {
  141. return 10 + c - 'A'
  142. }
  143. return 0
  144. }
  145. func equal(b []byte, s string, t *testing.T) bool {
  146. if 2*len(b) != len(s) {
  147. // fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
  148. fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
  149. return false
  150. }
  151. for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
  152. x := hex(s[j])*16 + hex(s[j+1])
  153. if b[i] != x {
  154. // fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
  155. fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
  156. return false
  157. }
  158. }
  159. return true
  160. }
  161. func overify(t *testing.T, pb *GoTest, expected string) {
  162. o := old()
  163. err := o.Marshal(pb)
  164. if err != nil {
  165. fmt.Printf("overify marshal-1 err = %v", err)
  166. o.DebugPrint("", o.Bytes())
  167. t.Fatalf("expected = %s", expected)
  168. }
  169. if !equal(o.Bytes(), expected, t) {
  170. o.DebugPrint("overify neq 1", o.Bytes())
  171. t.Fatalf("expected = %s", expected)
  172. }
  173. // Now test Unmarshal by recreating the original buffer.
  174. pbd := new(GoTest)
  175. err = o.Unmarshal(pbd)
  176. if err != nil {
  177. t.Fatalf("overify unmarshal err = %v", err)
  178. o.DebugPrint("", o.Bytes())
  179. t.Fatalf("string = %s", expected)
  180. }
  181. o.Reset()
  182. err = o.Marshal(pbd)
  183. if err != nil {
  184. t.Errorf("overify marshal-2 err = %v", err)
  185. o.DebugPrint("", o.Bytes())
  186. t.Fatalf("string = %s", expected)
  187. }
  188. if !equal(o.Bytes(), expected, t) {
  189. o.DebugPrint("overify neq 2", o.Bytes())
  190. t.Fatalf("string = %s", expected)
  191. }
  192. }
  193. // Simple tests for numeric encode/decode primitives (varint, etc.)
  194. func TestNumericPrimitives(t *testing.T) {
  195. for i := uint64(0); i < 1e6; i += 111 {
  196. o := old()
  197. if o.EncodeVarint(i) != nil {
  198. t.Error("EncodeVarint")
  199. break
  200. }
  201. x, e := o.DecodeVarint()
  202. if e != nil {
  203. t.Fatal("DecodeVarint")
  204. }
  205. if x != i {
  206. t.Fatal("varint decode fail:", i, x)
  207. }
  208. o = old()
  209. if o.EncodeFixed32(i) != nil {
  210. t.Fatal("encFixed32")
  211. }
  212. x, e = o.DecodeFixed32()
  213. if e != nil {
  214. t.Fatal("decFixed32")
  215. }
  216. if x != i {
  217. t.Fatal("fixed32 decode fail:", i, x)
  218. }
  219. o = old()
  220. if o.EncodeFixed64(i*1234567) != nil {
  221. t.Error("encFixed64")
  222. break
  223. }
  224. x, e = o.DecodeFixed64()
  225. if e != nil {
  226. t.Error("decFixed64")
  227. break
  228. }
  229. if x != i*1234567 {
  230. t.Error("fixed64 decode fail:", i*1234567, x)
  231. break
  232. }
  233. o = old()
  234. i32 := int32(i - 12345)
  235. if o.EncodeZigzag32(uint64(i32)) != nil {
  236. t.Fatal("EncodeZigzag32")
  237. }
  238. x, e = o.DecodeZigzag32()
  239. if e != nil {
  240. t.Fatal("DecodeZigzag32")
  241. }
  242. if x != uint64(uint32(i32)) {
  243. t.Fatal("zigzag32 decode fail:", i32, x)
  244. }
  245. o = old()
  246. i64 := int64(i - 12345)
  247. if o.EncodeZigzag64(uint64(i64)) != nil {
  248. t.Fatal("EncodeZigzag64")
  249. }
  250. x, e = o.DecodeZigzag64()
  251. if e != nil {
  252. t.Fatal("DecodeZigzag64")
  253. }
  254. if x != uint64(i64) {
  255. t.Fatal("zigzag64 decode fail:", i64, x)
  256. }
  257. }
  258. }
  259. // fakeMarshaler is a simple struct implementing Marshaler and Message interfaces.
  260. type fakeMarshaler struct {
  261. b []byte
  262. err error
  263. }
  264. func (f *fakeMarshaler) Marshal() ([]byte, error) { return f.b, f.err }
  265. func (f *fakeMarshaler) String() string { return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) }
  266. func (f *fakeMarshaler) ProtoMessage() {}
  267. func (f *fakeMarshaler) Reset() {}
  268. type msgWithFakeMarshaler struct {
  269. M *fakeMarshaler `protobuf:"bytes,1,opt,name=fake"`
  270. }
  271. func (m *msgWithFakeMarshaler) String() string { return CompactTextString(m) }
  272. func (m *msgWithFakeMarshaler) ProtoMessage() {}
  273. func (m *msgWithFakeMarshaler) Reset() {}
  274. // Simple tests for proto messages that implement the Marshaler interface.
  275. func TestMarshalerEncoding(t *testing.T) {
  276. tests := []struct {
  277. name string
  278. m Message
  279. want []byte
  280. errType reflect.Type
  281. }{
  282. {
  283. name: "Marshaler that fails",
  284. m: &fakeMarshaler{
  285. err: errors.New("some marshal err"),
  286. b: []byte{5, 6, 7},
  287. },
  288. // Since the Marshal method returned bytes, they should be written to the
  289. // buffer. (For efficiency, we assume that Marshal implementations are
  290. // always correct w.r.t. RequiredNotSetError and output.)
  291. want: []byte{5, 6, 7},
  292. errType: reflect.TypeOf(errors.New("some marshal err")),
  293. },
  294. {
  295. name: "Marshaler that fails with RequiredNotSetError",
  296. m: &msgWithFakeMarshaler{
  297. M: &fakeMarshaler{
  298. err: &RequiredNotSetError{},
  299. b: []byte{5, 6, 7},
  300. },
  301. },
  302. // Since there's an error that can be continued after,
  303. // the buffer should be written.
  304. want: []byte{
  305. 10, 3, // for &msgWithFakeMarshaler
  306. 5, 6, 7, // for &fakeMarshaler
  307. },
  308. errType: reflect.TypeOf(&RequiredNotSetError{}),
  309. },
  310. {
  311. name: "Marshaler that succeeds",
  312. m: &fakeMarshaler{
  313. b: []byte{0, 1, 2, 3, 4, 127, 255},
  314. },
  315. want: []byte{0, 1, 2, 3, 4, 127, 255},
  316. },
  317. }
  318. for _, test := range tests {
  319. b := NewBuffer(nil)
  320. err := b.Marshal(test.m)
  321. if reflect.TypeOf(err) != test.errType {
  322. t.Errorf("%s: got err %T(%v) wanted %T", test.name, err, err, test.errType)
  323. }
  324. if !reflect.DeepEqual(test.want, b.Bytes()) {
  325. t.Errorf("%s: got bytes %v wanted %v", test.name, b.Bytes(), test.want)
  326. }
  327. if size := Size(test.m); size != len(b.Bytes()) {
  328. t.Errorf("%s: Size(_) = %v, but marshaled to %v bytes", test.name, size, len(b.Bytes()))
  329. }
  330. m, mErr := Marshal(test.m)
  331. if !bytes.Equal(b.Bytes(), m) {
  332. t.Errorf("%s: Marshal returned %v, but (*Buffer).Marshal wrote %v", test.name, m, b.Bytes())
  333. }
  334. if !reflect.DeepEqual(err, mErr) {
  335. t.Errorf("%s: Marshal err = %q, but (*Buffer).Marshal returned %q",
  336. test.name, fmt.Sprint(mErr), fmt.Sprint(err))
  337. }
  338. }
  339. }
  340. // Ensure that Buffer.Marshal uses O(N) memory for N messages
  341. func TestBufferMarshalAllocs(t *testing.T) {
  342. value := &OtherMessage{Key: Int64(1)}
  343. msg := &MyMessage{Count: Int32(1), Others: []*OtherMessage{value}}
  344. reallocSize := func(t *testing.T, items int, prealloc int) (int64, int64) {
  345. var b Buffer
  346. b.SetBuf(make([]byte, 0, prealloc))
  347. var allocSpace int64
  348. prevCap := cap(b.Bytes())
  349. for i := 0; i < items; i++ {
  350. err := b.Marshal(msg)
  351. if err != nil {
  352. t.Errorf("Marshal err = %q", err)
  353. break
  354. }
  355. if c := cap(b.Bytes()); prevCap != c {
  356. allocSpace += int64(c)
  357. prevCap = c
  358. }
  359. }
  360. needSpace := int64(len(b.Bytes()))
  361. return allocSpace, needSpace
  362. }
  363. for _, prealloc := range []int{0, 100, 10000} {
  364. for _, items := range []int{1, 2, 5, 10, 20, 50, 100, 200, 500, 1000} {
  365. runtimeSpace, need := reallocSize(t, items, prealloc)
  366. totalSpace := int64(prealloc) + runtimeSpace
  367. runtimeRatio := float64(runtimeSpace) / float64(need)
  368. totalRatio := float64(totalSpace) / float64(need)
  369. if totalRatio < 1 || runtimeRatio > 4 {
  370. t.Errorf("needed %dB, allocated %dB total (ratio %.1f), allocated %dB at runtime (ratio %.1f)",
  371. need, totalSpace, totalRatio, runtimeSpace, runtimeRatio)
  372. }
  373. }
  374. }
  375. }
  376. // Simple tests for bytes
  377. func TestBytesPrimitives(t *testing.T) {
  378. o := old()
  379. bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
  380. if o.EncodeRawBytes(bytes) != nil {
  381. t.Error("EncodeRawBytes")
  382. }
  383. decb, e := o.DecodeRawBytes(false)
  384. if e != nil {
  385. t.Error("DecodeRawBytes")
  386. }
  387. equalbytes(bytes, decb, t)
  388. }
  389. // Simple tests for strings
  390. func TestStringPrimitives(t *testing.T) {
  391. o := old()
  392. s := "now is the time"
  393. if o.EncodeStringBytes(s) != nil {
  394. t.Error("enc_string")
  395. }
  396. decs, e := o.DecodeStringBytes()
  397. if e != nil {
  398. t.Error("dec_string")
  399. }
  400. if s != decs {
  401. t.Error("string encode/decode fail:", s, decs)
  402. }
  403. }
  404. // Do we catch the "required bit not set" case?
  405. func TestRequiredBit(t *testing.T) {
  406. o := old()
  407. pb := new(GoTest)
  408. err := o.Marshal(pb)
  409. if err == nil {
  410. t.Error("did not catch missing required fields")
  411. } else if !strings.Contains(err.Error(), "Kind") {
  412. t.Error("wrong error type:", err)
  413. }
  414. }
  415. // Check that all fields are nil.
  416. // Clearly silly, and a residue from a more interesting test with an earlier,
  417. // different initialization property, but it once caught a compiler bug so
  418. // it lives.
  419. func checkInitialized(pb *GoTest, t *testing.T) {
  420. if pb.F_BoolDefaulted != nil {
  421. t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
  422. }
  423. if pb.F_Int32Defaulted != nil {
  424. t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
  425. }
  426. if pb.F_Int64Defaulted != nil {
  427. t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
  428. }
  429. if pb.F_Fixed32Defaulted != nil {
  430. t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
  431. }
  432. if pb.F_Fixed64Defaulted != nil {
  433. t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
  434. }
  435. if pb.F_Uint32Defaulted != nil {
  436. t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
  437. }
  438. if pb.F_Uint64Defaulted != nil {
  439. t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
  440. }
  441. if pb.F_FloatDefaulted != nil {
  442. t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
  443. }
  444. if pb.F_DoubleDefaulted != nil {
  445. t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
  446. }
  447. if pb.F_StringDefaulted != nil {
  448. t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
  449. }
  450. if pb.F_BytesDefaulted != nil {
  451. t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
  452. }
  453. if pb.F_Sint32Defaulted != nil {
  454. t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
  455. }
  456. if pb.F_Sint64Defaulted != nil {
  457. t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
  458. }
  459. }
  460. // Does Reset() reset?
  461. func TestReset(t *testing.T) {
  462. pb := initGoTest(true)
  463. // muck with some values
  464. pb.F_BoolDefaulted = Bool(false)
  465. pb.F_Int32Defaulted = Int32(237)
  466. pb.F_Int64Defaulted = Int64(12346)
  467. pb.F_Fixed32Defaulted = Uint32(32000)
  468. pb.F_Fixed64Defaulted = Uint64(666)
  469. pb.F_Uint32Defaulted = Uint32(323232)
  470. pb.F_Uint64Defaulted = nil
  471. pb.F_FloatDefaulted = nil
  472. pb.F_DoubleDefaulted = Float64(0)
  473. pb.F_StringDefaulted = String("gotcha")
  474. pb.F_BytesDefaulted = []byte("asdfasdf")
  475. pb.F_Sint32Defaulted = Int32(123)
  476. pb.F_Sint64Defaulted = Int64(789)
  477. pb.Reset()
  478. checkInitialized(pb, t)
  479. }
  480. // All required fields set, no defaults provided.
  481. func TestEncodeDecode1(t *testing.T) {
  482. pb := initGoTest(false)
  483. overify(t, pb,
  484. "0807"+ // field 1, encoding 0, value 7
  485. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  486. "5001"+ // field 10, encoding 0, value 1
  487. "5803"+ // field 11, encoding 0, value 3
  488. "6006"+ // field 12, encoding 0, value 6
  489. "6d20000000"+ // field 13, encoding 5, value 0x20
  490. "714000000000000000"+ // field 14, encoding 1, value 0x40
  491. "78a019"+ // field 15, encoding 0, value 0xca0 = 3232
  492. "8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
  493. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  494. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  495. "9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
  496. "b304"+ // field 70, encoding 3, start group
  497. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  498. "b404"+ // field 70, encoding 4, end group
  499. "aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
  500. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  501. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  502. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  503. "c906c0ffffffffffffff") // field 105, encoding 1, -64 fixed64
  504. }
  505. // All required fields set, defaults provided.
  506. func TestEncodeDecode2(t *testing.T) {
  507. pb := initGoTest(true)
  508. overify(t, pb,
  509. "0807"+ // field 1, encoding 0, value 7
  510. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  511. "5001"+ // field 10, encoding 0, value 1
  512. "5803"+ // field 11, encoding 0, value 3
  513. "6006"+ // field 12, encoding 0, value 6
  514. "6d20000000"+ // field 13, encoding 5, value 32
  515. "714000000000000000"+ // field 14, encoding 1, value 64
  516. "78a019"+ // field 15, encoding 0, value 3232
  517. "8001c032"+ // field 16, encoding 0, value 6464
  518. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  519. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  520. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  521. "c00201"+ // field 40, encoding 0, value 1
  522. "c80220"+ // field 41, encoding 0, value 32
  523. "d00240"+ // field 42, encoding 0, value 64
  524. "dd0240010000"+ // field 43, encoding 5, value 320
  525. "e1028002000000000000"+ // field 44, encoding 1, value 640
  526. "e8028019"+ // field 45, encoding 0, value 3200
  527. "f0028032"+ // field 46, encoding 0, value 6400
  528. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  529. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  530. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  531. "b304"+ // start group field 70 level 1
  532. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  533. "b404"+ // end group field 70 level 1
  534. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  535. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  536. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  537. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  538. "c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
  539. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  540. "90193f"+ // field 402, encoding 0, value 63
  541. "98197f"+ // field 403, encoding 0, value 127
  542. "a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
  543. "a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
  544. }
  545. // All default fields set to their default value by hand
  546. func TestEncodeDecode3(t *testing.T) {
  547. pb := initGoTest(false)
  548. pb.F_BoolDefaulted = Bool(true)
  549. pb.F_Int32Defaulted = Int32(32)
  550. pb.F_Int64Defaulted = Int64(64)
  551. pb.F_Fixed32Defaulted = Uint32(320)
  552. pb.F_Fixed64Defaulted = Uint64(640)
  553. pb.F_Uint32Defaulted = Uint32(3200)
  554. pb.F_Uint64Defaulted = Uint64(6400)
  555. pb.F_FloatDefaulted = Float32(314159)
  556. pb.F_DoubleDefaulted = Float64(271828)
  557. pb.F_StringDefaulted = String("hello, \"world!\"\n")
  558. pb.F_BytesDefaulted = []byte("Bignose")
  559. pb.F_Sint32Defaulted = Int32(-32)
  560. pb.F_Sint64Defaulted = Int64(-64)
  561. pb.F_Sfixed32Defaulted = Int32(-32)
  562. pb.F_Sfixed64Defaulted = Int64(-64)
  563. overify(t, pb,
  564. "0807"+ // field 1, encoding 0, value 7
  565. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  566. "5001"+ // field 10, encoding 0, value 1
  567. "5803"+ // field 11, encoding 0, value 3
  568. "6006"+ // field 12, encoding 0, value 6
  569. "6d20000000"+ // field 13, encoding 5, value 32
  570. "714000000000000000"+ // field 14, encoding 1, value 64
  571. "78a019"+ // field 15, encoding 0, value 3232
  572. "8001c032"+ // field 16, encoding 0, value 6464
  573. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  574. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  575. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  576. "c00201"+ // field 40, encoding 0, value 1
  577. "c80220"+ // field 41, encoding 0, value 32
  578. "d00240"+ // field 42, encoding 0, value 64
  579. "dd0240010000"+ // field 43, encoding 5, value 320
  580. "e1028002000000000000"+ // field 44, encoding 1, value 640
  581. "e8028019"+ // field 45, encoding 0, value 3200
  582. "f0028032"+ // field 46, encoding 0, value 6400
  583. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  584. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  585. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  586. "b304"+ // start group field 70 level 1
  587. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  588. "b404"+ // end group field 70 level 1
  589. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  590. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  591. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  592. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  593. "c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
  594. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  595. "90193f"+ // field 402, encoding 0, value 63
  596. "98197f"+ // field 403, encoding 0, value 127
  597. "a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
  598. "a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
  599. }
  600. // All required fields set, defaults provided, all non-defaulted optional fields have values.
  601. func TestEncodeDecode4(t *testing.T) {
  602. pb := initGoTest(true)
  603. pb.Table = String("hello")
  604. pb.Param = Int32(7)
  605. pb.OptionalField = initGoTestField()
  606. pb.F_BoolOptional = Bool(true)
  607. pb.F_Int32Optional = Int32(32)
  608. pb.F_Int64Optional = Int64(64)
  609. pb.F_Fixed32Optional = Uint32(3232)
  610. pb.F_Fixed64Optional = Uint64(6464)
  611. pb.F_Uint32Optional = Uint32(323232)
  612. pb.F_Uint64Optional = Uint64(646464)
  613. pb.F_FloatOptional = Float32(32.)
  614. pb.F_DoubleOptional = Float64(64.)
  615. pb.F_StringOptional = String("hello")
  616. pb.F_BytesOptional = []byte("Bignose")
  617. pb.F_Sint32Optional = Int32(-32)
  618. pb.F_Sint64Optional = Int64(-64)
  619. pb.F_Sfixed32Optional = Int32(-32)
  620. pb.F_Sfixed64Optional = Int64(-64)
  621. pb.Optionalgroup = initGoTest_OptionalGroup()
  622. overify(t, pb,
  623. "0807"+ // field 1, encoding 0, value 7
  624. "1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
  625. "1807"+ // field 3, encoding 0, value 7
  626. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  627. "320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
  628. "5001"+ // field 10, encoding 0, value 1
  629. "5803"+ // field 11, encoding 0, value 3
  630. "6006"+ // field 12, encoding 0, value 6
  631. "6d20000000"+ // field 13, encoding 5, value 32
  632. "714000000000000000"+ // field 14, encoding 1, value 64
  633. "78a019"+ // field 15, encoding 0, value 3232
  634. "8001c032"+ // field 16, encoding 0, value 6464
  635. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  636. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  637. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  638. "f00101"+ // field 30, encoding 0, value 1
  639. "f80120"+ // field 31, encoding 0, value 32
  640. "800240"+ // field 32, encoding 0, value 64
  641. "8d02a00c0000"+ // field 33, encoding 5, value 3232
  642. "91024019000000000000"+ // field 34, encoding 1, value 6464
  643. "9802a0dd13"+ // field 35, encoding 0, value 323232
  644. "a002c0ba27"+ // field 36, encoding 0, value 646464
  645. "ad0200000042"+ // field 37, encoding 5, value 32.0
  646. "b1020000000000005040"+ // field 38, encoding 1, value 64.0
  647. "ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
  648. "c00201"+ // field 40, encoding 0, value 1
  649. "c80220"+ // field 41, encoding 0, value 32
  650. "d00240"+ // field 42, encoding 0, value 64
  651. "dd0240010000"+ // field 43, encoding 5, value 320
  652. "e1028002000000000000"+ // field 44, encoding 1, value 640
  653. "e8028019"+ // field 45, encoding 0, value 3200
  654. "f0028032"+ // field 46, encoding 0, value 6400
  655. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  656. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  657. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  658. "b304"+ // start group field 70 level 1
  659. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  660. "b404"+ // end group field 70 level 1
  661. "d305"+ // start group field 90 level 1
  662. "da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
  663. "d405"+ // end group field 90 level 1
  664. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  665. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  666. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  667. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  668. "c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
  669. "ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
  670. "f0123f"+ // field 302, encoding 0, value 63
  671. "f8127f"+ // field 303, encoding 0, value 127
  672. "8513e0ffffff"+ // field 304, encoding 5, -32 fixed32
  673. "8913c0ffffffffffffff"+ // field 305, encoding 1, -64 fixed64
  674. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  675. "90193f"+ // field 402, encoding 0, value 63
  676. "98197f"+ // field 403, encoding 0, value 127
  677. "a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
  678. "a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
  679. }
  680. // All required fields set, defaults provided, all repeated fields given two values.
  681. func TestEncodeDecode5(t *testing.T) {
  682. pb := initGoTest(true)
  683. pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
  684. pb.F_BoolRepeated = []bool{false, true}
  685. pb.F_Int32Repeated = []int32{32, 33}
  686. pb.F_Int64Repeated = []int64{64, 65}
  687. pb.F_Fixed32Repeated = []uint32{3232, 3333}
  688. pb.F_Fixed64Repeated = []uint64{6464, 6565}
  689. pb.F_Uint32Repeated = []uint32{323232, 333333}
  690. pb.F_Uint64Repeated = []uint64{646464, 656565}
  691. pb.F_FloatRepeated = []float32{32., 33.}
  692. pb.F_DoubleRepeated = []float64{64., 65.}
  693. pb.F_StringRepeated = []string{"hello", "sailor"}
  694. pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
  695. pb.F_Sint32Repeated = []int32{32, -32}
  696. pb.F_Sint64Repeated = []int64{64, -64}
  697. pb.F_Sfixed32Repeated = []int32{32, -32}
  698. pb.F_Sfixed64Repeated = []int64{64, -64}
  699. pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
  700. overify(t, pb,
  701. "0807"+ // field 1, encoding 0, value 7
  702. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  703. "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
  704. "2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
  705. "5001"+ // field 10, encoding 0, value 1
  706. "5803"+ // field 11, encoding 0, value 3
  707. "6006"+ // field 12, encoding 0, value 6
  708. "6d20000000"+ // field 13, encoding 5, value 32
  709. "714000000000000000"+ // field 14, encoding 1, value 64
  710. "78a019"+ // field 15, encoding 0, value 3232
  711. "8001c032"+ // field 16, encoding 0, value 6464
  712. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  713. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  714. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  715. "a00100"+ // field 20, encoding 0, value 0
  716. "a00101"+ // field 20, encoding 0, value 1
  717. "a80120"+ // field 21, encoding 0, value 32
  718. "a80121"+ // field 21, encoding 0, value 33
  719. "b00140"+ // field 22, encoding 0, value 64
  720. "b00141"+ // field 22, encoding 0, value 65
  721. "bd01a00c0000"+ // field 23, encoding 5, value 3232
  722. "bd01050d0000"+ // field 23, encoding 5, value 3333
  723. "c1014019000000000000"+ // field 24, encoding 1, value 6464
  724. "c101a519000000000000"+ // field 24, encoding 1, value 6565
  725. "c801a0dd13"+ // field 25, encoding 0, value 323232
  726. "c80195ac14"+ // field 25, encoding 0, value 333333
  727. "d001c0ba27"+ // field 26, encoding 0, value 646464
  728. "d001b58928"+ // field 26, encoding 0, value 656565
  729. "dd0100000042"+ // field 27, encoding 5, value 32.0
  730. "dd0100000442"+ // field 27, encoding 5, value 33.0
  731. "e1010000000000005040"+ // field 28, encoding 1, value 64.0
  732. "e1010000000000405040"+ // field 28, encoding 1, value 65.0
  733. "ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
  734. "ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
  735. "c00201"+ // field 40, encoding 0, value 1
  736. "c80220"+ // field 41, encoding 0, value 32
  737. "d00240"+ // field 42, encoding 0, value 64
  738. "dd0240010000"+ // field 43, encoding 5, value 320
  739. "e1028002000000000000"+ // field 44, encoding 1, value 640
  740. "e8028019"+ // field 45, encoding 0, value 3200
  741. "f0028032"+ // field 46, encoding 0, value 6400
  742. "fd02e0659948"+ // field 47, encoding 5, value 314159.0
  743. "81030000000050971041"+ // field 48, encoding 1, value 271828.0
  744. "8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
  745. "b304"+ // start group field 70 level 1
  746. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  747. "b404"+ // end group field 70 level 1
  748. "8305"+ // start group field 80 level 1
  749. "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
  750. "8405"+ // end group field 80 level 1
  751. "8305"+ // start group field 80 level 1
  752. "8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
  753. "8405"+ // end group field 80 level 1
  754. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  755. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  756. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  757. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  758. "c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
  759. "ca0c03"+"626967"+ // field 201, encoding 2, string "big"
  760. "ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
  761. "d00c40"+ // field 202, encoding 0, value 32
  762. "d00c3f"+ // field 202, encoding 0, value -32
  763. "d80c8001"+ // field 203, encoding 0, value 64
  764. "d80c7f"+ // field 203, encoding 0, value -64
  765. "e50c20000000"+ // field 204, encoding 5, 32 fixed32
  766. "e50ce0ffffff"+ // field 204, encoding 5, -32 fixed32
  767. "e90c4000000000000000"+ // field 205, encoding 1, 64 fixed64
  768. "e90cc0ffffffffffffff"+ // field 205, encoding 1, -64 fixed64
  769. "8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
  770. "90193f"+ // field 402, encoding 0, value 63
  771. "98197f"+ // field 403, encoding 0, value 127
  772. "a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
  773. "a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
  774. }
  775. // All required fields set, all packed repeated fields given two values.
  776. func TestEncodeDecode6(t *testing.T) {
  777. pb := initGoTest(false)
  778. pb.F_BoolRepeatedPacked = []bool{false, true}
  779. pb.F_Int32RepeatedPacked = []int32{32, 33}
  780. pb.F_Int64RepeatedPacked = []int64{64, 65}
  781. pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
  782. pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
  783. pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
  784. pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
  785. pb.F_FloatRepeatedPacked = []float32{32., 33.}
  786. pb.F_DoubleRepeatedPacked = []float64{64., 65.}
  787. pb.F_Sint32RepeatedPacked = []int32{32, -32}
  788. pb.F_Sint64RepeatedPacked = []int64{64, -64}
  789. pb.F_Sfixed32RepeatedPacked = []int32{32, -32}
  790. pb.F_Sfixed64RepeatedPacked = []int64{64, -64}
  791. overify(t, pb,
  792. "0807"+ // field 1, encoding 0, value 7
  793. "220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
  794. "5001"+ // field 10, encoding 0, value 1
  795. "5803"+ // field 11, encoding 0, value 3
  796. "6006"+ // field 12, encoding 0, value 6
  797. "6d20000000"+ // field 13, encoding 5, value 32
  798. "714000000000000000"+ // field 14, encoding 1, value 64
  799. "78a019"+ // field 15, encoding 0, value 3232
  800. "8001c032"+ // field 16, encoding 0, value 6464
  801. "8d0100004a45"+ // field 17, encoding 5, value 3232.0
  802. "9101000000000040b940"+ // field 18, encoding 1, value 6464.0
  803. "9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
  804. "9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
  805. "9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
  806. "a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
  807. "aa0308"+ // field 53, encoding 2, 8 bytes
  808. "a00c0000050d0000"+ // value 3232, value 3333
  809. "b20310"+ // field 54, encoding 2, 16 bytes
  810. "4019000000000000a519000000000000"+ // value 6464, value 6565
  811. "ba0306"+ // field 55, encoding 2, 6 bytes
  812. "a0dd1395ac14"+ // value 323232, value 333333
  813. "c20306"+ // field 56, encoding 2, 6 bytes
  814. "c0ba27b58928"+ // value 646464, value 656565
  815. "ca0308"+ // field 57, encoding 2, 8 bytes
  816. "0000004200000442"+ // value 32.0, value 33.0
  817. "d20310"+ // field 58, encoding 2, 16 bytes
  818. "00000000000050400000000000405040"+ // value 64.0, value 65.0
  819. "b304"+ // start group field 70 level 1
  820. "ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
  821. "b404"+ // end group field 70 level 1
  822. "aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
  823. "b0063f"+ // field 102, encoding 0, 0x3f zigzag32
  824. "b8067f"+ // field 103, encoding 0, 0x7f zigzag64
  825. "c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
  826. "c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
  827. "b21f02"+ // field 502, encoding 2, 2 bytes
  828. "403f"+ // value 32, value -32
  829. "ba1f03"+ // field 503, encoding 2, 3 bytes
  830. "80017f"+ // value 64, value -64
  831. "c21f08"+ // field 504, encoding 2, 8 bytes
  832. "20000000e0ffffff"+ // value 32, value -32
  833. "ca1f10"+ // field 505, encoding 2, 16 bytes
  834. "4000000000000000c0ffffffffffffff") // value 64, value -64
  835. }
  836. // Test that we can encode empty bytes fields.
  837. func TestEncodeDecodeBytes1(t *testing.T) {
  838. pb := initGoTest(false)
  839. // Create our bytes
  840. pb.F_BytesRequired = []byte{}
  841. pb.F_BytesRepeated = [][]byte{{}}
  842. pb.F_BytesOptional = []byte{}
  843. d, err := Marshal(pb)
  844. if err != nil {
  845. t.Error(err)
  846. }
  847. pbd := new(GoTest)
  848. if err := Unmarshal(d, pbd); err != nil {
  849. t.Error(err)
  850. }
  851. if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
  852. t.Error("required empty bytes field is incorrect")
  853. }
  854. if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
  855. t.Error("repeated empty bytes field is incorrect")
  856. }
  857. if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
  858. t.Error("optional empty bytes field is incorrect")
  859. }
  860. }
  861. // Test that we encode nil-valued fields of a repeated bytes field correctly.
  862. // Since entries in a repeated field cannot be nil, nil must mean empty value.
  863. func TestEncodeDecodeBytes2(t *testing.T) {
  864. pb := initGoTest(false)
  865. // Create our bytes
  866. pb.F_BytesRepeated = [][]byte{nil}
  867. d, err := Marshal(pb)
  868. if err != nil {
  869. t.Error(err)
  870. }
  871. pbd := new(GoTest)
  872. if err := Unmarshal(d, pbd); err != nil {
  873. t.Error(err)
  874. }
  875. if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
  876. t.Error("Unexpected value for repeated bytes field")
  877. }
  878. }
  879. // All required fields set, defaults provided, all repeated fields given two values.
  880. func TestSkippingUnrecognizedFields(t *testing.T) {
  881. o := old()
  882. pb := initGoTestField()
  883. // Marshal it normally.
  884. o.Marshal(pb)
  885. // Now new a GoSkipTest record.
  886. skip := &GoSkipTest{
  887. SkipInt32: Int32(32),
  888. SkipFixed32: Uint32(3232),
  889. SkipFixed64: Uint64(6464),
  890. SkipString: String("skipper"),
  891. Skipgroup: &GoSkipTest_SkipGroup{
  892. GroupInt32: Int32(75),
  893. GroupString: String("wxyz"),
  894. },
  895. }
  896. // Marshal it into same buffer.
  897. o.Marshal(skip)
  898. pbd := new(GoTestField)
  899. o.Unmarshal(pbd)
  900. // The __unrecognized field should be a marshaling of GoSkipTest
  901. skipd := new(GoSkipTest)
  902. o.SetBuf(pbd.XXX_unrecognized)
  903. o.Unmarshal(skipd)
  904. if *skipd.SkipInt32 != *skip.SkipInt32 {
  905. t.Error("skip int32", skipd.SkipInt32)
  906. }
  907. if *skipd.SkipFixed32 != *skip.SkipFixed32 {
  908. t.Error("skip fixed32", skipd.SkipFixed32)
  909. }
  910. if *skipd.SkipFixed64 != *skip.SkipFixed64 {
  911. t.Error("skip fixed64", skipd.SkipFixed64)
  912. }
  913. if *skipd.SkipString != *skip.SkipString {
  914. t.Error("skip string", *skipd.SkipString)
  915. }
  916. if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
  917. t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
  918. }
  919. if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
  920. t.Error("skip group string", *skipd.Skipgroup.GroupString)
  921. }
  922. }
  923. // Check that unrecognized fields of a submessage are preserved.
  924. func TestSubmessageUnrecognizedFields(t *testing.T) {
  925. nm := &NewMessage{
  926. Nested: &NewMessage_Nested{
  927. Name: String("Nigel"),
  928. FoodGroup: String("carbs"),
  929. },
  930. }
  931. b, err := Marshal(nm)
  932. if err != nil {
  933. t.Fatalf("Marshal of NewMessage: %v", err)
  934. }
  935. // Unmarshal into an OldMessage.
  936. om := new(OldMessage)
  937. if err := Unmarshal(b, om); err != nil {
  938. t.Fatalf("Unmarshal to OldMessage: %v", err)
  939. }
  940. exp := &OldMessage{
  941. Nested: &OldMessage_Nested{
  942. Name: String("Nigel"),
  943. // normal protocol buffer users should not do this
  944. XXX_unrecognized: []byte("\x12\x05carbs"),
  945. },
  946. }
  947. if !Equal(om, exp) {
  948. t.Errorf("om = %v, want %v", om, exp)
  949. }
  950. // Clone the OldMessage.
  951. om = Clone(om).(*OldMessage)
  952. if !Equal(om, exp) {
  953. t.Errorf("Clone(om) = %v, want %v", om, exp)
  954. }
  955. // Marshal the OldMessage, then unmarshal it into an empty NewMessage.
  956. if b, err = Marshal(om); err != nil {
  957. t.Fatalf("Marshal of OldMessage: %v", err)
  958. }
  959. t.Logf("Marshal(%v) -> %q", om, b)
  960. nm2 := new(NewMessage)
  961. if err := Unmarshal(b, nm2); err != nil {
  962. t.Fatalf("Unmarshal to NewMessage: %v", err)
  963. }
  964. if !Equal(nm, nm2) {
  965. t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
  966. }
  967. }
  968. // Check that an int32 field can be upgraded to an int64 field.
  969. func TestNegativeInt32(t *testing.T) {
  970. om := &OldMessage{
  971. Num: Int32(-1),
  972. }
  973. b, err := Marshal(om)
  974. if err != nil {
  975. t.Fatalf("Marshal of OldMessage: %v", err)
  976. }
  977. // Check the size. It should be 11 bytes;
  978. // 1 for the field/wire type, and 10 for the negative number.
  979. if len(b) != 11 {
  980. t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
  981. }
  982. // Unmarshal into a NewMessage.
  983. nm := new(NewMessage)
  984. if err := Unmarshal(b, nm); err != nil {
  985. t.Fatalf("Unmarshal to NewMessage: %v", err)
  986. }
  987. want := &NewMessage{
  988. Num: Int64(-1),
  989. }
  990. if !Equal(nm, want) {
  991. t.Errorf("nm = %v, want %v", nm, want)
  992. }
  993. }
  994. // Check that we can grow an array (repeated field) to have many elements.
  995. // This test doesn't depend only on our encoding; for variety, it makes sure
  996. // we create, encode, and decode the correct contents explicitly. It's therefore
  997. // a bit messier.
  998. // This test also uses (and hence tests) the Marshal/Unmarshal functions
  999. // instead of the methods.
  1000. func TestBigRepeated(t *testing.T) {
  1001. pb := initGoTest(true)
  1002. // Create the arrays
  1003. const N = 50 // Internally the library starts much smaller.
  1004. pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
  1005. pb.F_Sint64Repeated = make([]int64, N)
  1006. pb.F_Sint32Repeated = make([]int32, N)
  1007. pb.F_BytesRepeated = make([][]byte, N)
  1008. pb.F_StringRepeated = make([]string, N)
  1009. pb.F_DoubleRepeated = make([]float64, N)
  1010. pb.F_FloatRepeated = make([]float32, N)
  1011. pb.F_Uint64Repeated = make([]uint64, N)
  1012. pb.F_Uint32Repeated = make([]uint32, N)
  1013. pb.F_Fixed64Repeated = make([]uint64, N)
  1014. pb.F_Fixed32Repeated = make([]uint32, N)
  1015. pb.F_Int64Repeated = make([]int64, N)
  1016. pb.F_Int32Repeated = make([]int32, N)
  1017. pb.F_BoolRepeated = make([]bool, N)
  1018. pb.RepeatedField = make([]*GoTestField, N)
  1019. // Fill in the arrays with checkable values.
  1020. igtf := initGoTestField()
  1021. igtrg := initGoTest_RepeatedGroup()
  1022. for i := 0; i < N; i++ {
  1023. pb.Repeatedgroup[i] = igtrg
  1024. pb.F_Sint64Repeated[i] = int64(i)
  1025. pb.F_Sint32Repeated[i] = int32(i)
  1026. s := fmt.Sprint(i)
  1027. pb.F_BytesRepeated[i] = []byte(s)
  1028. pb.F_StringRepeated[i] = s
  1029. pb.F_DoubleRepeated[i] = float64(i)
  1030. pb.F_FloatRepeated[i] = float32(i)
  1031. pb.F_Uint64Repeated[i] = uint64(i)
  1032. pb.F_Uint32Repeated[i] = uint32(i)
  1033. pb.F_Fixed64Repeated[i] = uint64(i)
  1034. pb.F_Fixed32Repeated[i] = uint32(i)
  1035. pb.F_Int64Repeated[i] = int64(i)
  1036. pb.F_Int32Repeated[i] = int32(i)
  1037. pb.F_BoolRepeated[i] = i%2 == 0
  1038. pb.RepeatedField[i] = igtf
  1039. }
  1040. // Marshal.
  1041. buf, _ := Marshal(pb)
  1042. // Now test Unmarshal by recreating the original buffer.
  1043. pbd := new(GoTest)
  1044. Unmarshal(buf, pbd)
  1045. // Check the checkable values
  1046. for i := uint64(0); i < N; i++ {
  1047. if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
  1048. t.Error("pbd.Repeatedgroup bad")
  1049. }
  1050. if x := uint64(pbd.F_Sint64Repeated[i]); x != i {
  1051. t.Error("pbd.F_Sint64Repeated bad", x, i)
  1052. }
  1053. if x := uint64(pbd.F_Sint32Repeated[i]); x != i {
  1054. t.Error("pbd.F_Sint32Repeated bad", x, i)
  1055. }
  1056. s := fmt.Sprint(i)
  1057. equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
  1058. if pbd.F_StringRepeated[i] != s {
  1059. t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
  1060. }
  1061. if x := uint64(pbd.F_DoubleRepeated[i]); x != i {
  1062. t.Error("pbd.F_DoubleRepeated bad", x, i)
  1063. }
  1064. if x := uint64(pbd.F_FloatRepeated[i]); x != i {
  1065. t.Error("pbd.F_FloatRepeated bad", x, i)
  1066. }
  1067. if x := pbd.F_Uint64Repeated[i]; x != i {
  1068. t.Error("pbd.F_Uint64Repeated bad", x, i)
  1069. }
  1070. if x := uint64(pbd.F_Uint32Repeated[i]); x != i {
  1071. t.Error("pbd.F_Uint32Repeated bad", x, i)
  1072. }
  1073. if x := pbd.F_Fixed64Repeated[i]; x != i {
  1074. t.Error("pbd.F_Fixed64Repeated bad", x, i)
  1075. }
  1076. if x := uint64(pbd.F_Fixed32Repeated[i]); x != i {
  1077. t.Error("pbd.F_Fixed32Repeated bad", x, i)
  1078. }
  1079. if x := uint64(pbd.F_Int64Repeated[i]); x != i {
  1080. t.Error("pbd.F_Int64Repeated bad", x, i)
  1081. }
  1082. if x := uint64(pbd.F_Int32Repeated[i]); x != i {
  1083. t.Error("pbd.F_Int32Repeated bad", x, i)
  1084. }
  1085. if x := pbd.F_BoolRepeated[i]; x != (i%2 == 0) {
  1086. t.Error("pbd.F_BoolRepeated bad", x, i)
  1087. }
  1088. if pbd.RepeatedField[i] == nil { // TODO: more checking?
  1089. t.Error("pbd.RepeatedField bad")
  1090. }
  1091. }
  1092. }
  1093. func TestBadWireTypeUnknown(t *testing.T) {
  1094. var b []byte
  1095. fmt.Sscanf("0a01780d00000000080b101612036161611521000000202c220362626225370000002203636363214200000000000000584d5a036464645900000000000056405d63000000", "%x", &b)
  1096. m := new(MyMessage)
  1097. if err := Unmarshal(b, m); err != nil {
  1098. t.Errorf("unexpected Unmarshal error: %v", err)
  1099. }
  1100. var unknown []byte
  1101. fmt.Sscanf("0a01780d0000000010161521000000202c2537000000214200000000000000584d5a036464645d63000000", "%x", &unknown)
  1102. if !bytes.Equal(m.XXX_unrecognized, unknown) {
  1103. t.Errorf("unknown bytes mismatch:\ngot %x\nwant %x", m.XXX_unrecognized, unknown)
  1104. }
  1105. DiscardUnknown(m)
  1106. want := &MyMessage{Count: Int32(11), Name: String("aaa"), Pet: []string{"bbb", "ccc"}, Bigfloat: Float64(88)}
  1107. if !Equal(m, want) {
  1108. t.Errorf("message mismatch:\ngot %v\nwant %v", m, want)
  1109. }
  1110. }
  1111. func encodeDecode(t *testing.T, in, out Message, msg string) {
  1112. buf, err := Marshal(in)
  1113. if err != nil {
  1114. t.Fatalf("failed marshaling %v: %v", msg, err)
  1115. }
  1116. if err := Unmarshal(buf, out); err != nil {
  1117. t.Fatalf("failed unmarshaling %v: %v", msg, err)
  1118. }
  1119. }
  1120. func TestPackedNonPackedDecoderSwitching(t *testing.T) {
  1121. np, p := new(NonPackedTest), new(PackedTest)
  1122. // non-packed -> packed
  1123. np.A = []int32{0, 1, 1, 2, 3, 5}
  1124. encodeDecode(t, np, p, "non-packed -> packed")
  1125. if !reflect.DeepEqual(np.A, p.B) {
  1126. t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
  1127. }
  1128. // packed -> non-packed
  1129. np.Reset()
  1130. p.B = []int32{3, 1, 4, 1, 5, 9}
  1131. encodeDecode(t, p, np, "packed -> non-packed")
  1132. if !reflect.DeepEqual(p.B, np.A) {
  1133. t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
  1134. }
  1135. }
  1136. func TestProto1RepeatedGroup(t *testing.T) {
  1137. pb := &MessageList{
  1138. Message: []*MessageList_Message{
  1139. {
  1140. Name: String("blah"),
  1141. Count: Int32(7),
  1142. },
  1143. // NOTE: pb.Message[1] is a nil
  1144. nil,
  1145. },
  1146. }
  1147. o := old()
  1148. err := o.Marshal(pb)
  1149. if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") {
  1150. t.Fatalf("unexpected or no error when marshaling: %v", err)
  1151. }
  1152. }
  1153. // Test that enums work. Checks for a bug introduced by making enums
  1154. // named types instead of int32: newInt32FromUint64 would crash with
  1155. // a type mismatch in reflect.PointTo.
  1156. func TestEnum(t *testing.T) {
  1157. pb := new(GoEnum)
  1158. pb.Foo = FOO_FOO1.Enum()
  1159. o := old()
  1160. if err := o.Marshal(pb); err != nil {
  1161. t.Fatal("error encoding enum:", err)
  1162. }
  1163. pb1 := new(GoEnum)
  1164. if err := o.Unmarshal(pb1); err != nil {
  1165. t.Fatal("error decoding enum:", err)
  1166. }
  1167. if *pb1.Foo != FOO_FOO1 {
  1168. t.Error("expected 7 but got ", *pb1.Foo)
  1169. }
  1170. }
  1171. // Enum types have String methods. Check that enum fields can be printed.
  1172. // We don't care what the value actually is, just as long as it doesn't crash.
  1173. func TestPrintingNilEnumFields(t *testing.T) {
  1174. pb := new(GoEnum)
  1175. _ = fmt.Sprintf("%+v", pb)
  1176. }
  1177. // Verify that absent required fields cause Marshal/Unmarshal to return errors.
  1178. func TestRequiredFieldEnforcement(t *testing.T) {
  1179. pb := new(GoTestField)
  1180. _, err := Marshal(pb)
  1181. if err == nil {
  1182. t.Error("marshal: expected error, got nil")
  1183. } else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Label") {
  1184. t.Errorf("marshal: bad error type: %v", err)
  1185. }
  1186. // A slightly sneaky, yet valid, proto. It encodes the same required field twice,
  1187. // so simply counting the required fields is insufficient.
  1188. // field 1, encoding 2, value "hi"
  1189. buf := []byte("\x0A\x02hi\x0A\x02hi")
  1190. err = Unmarshal(buf, pb)
  1191. if err == nil {
  1192. t.Error("unmarshal: expected error, got nil")
  1193. } else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Type") && !strings.Contains(err.Error(), "{Unknown}") {
  1194. // TODO: remove unknown cases once we commit to the new unmarshaler.
  1195. t.Errorf("unmarshal: bad error type: %v", err)
  1196. }
  1197. }
  1198. // Verify that absent required fields in groups cause Marshal/Unmarshal to return errors.
  1199. func TestRequiredFieldEnforcementGroups(t *testing.T) {
  1200. pb := &GoTestRequiredGroupField{Group: &GoTestRequiredGroupField_Group{}}
  1201. if _, err := Marshal(pb); err == nil {
  1202. t.Error("marshal: expected error, got nil")
  1203. } else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Group.Field") {
  1204. t.Errorf("marshal: bad error type: %v", err)
  1205. }
  1206. buf := []byte{11, 12}
  1207. if err := Unmarshal(buf, pb); err == nil {
  1208. t.Error("unmarshal: expected error, got nil")
  1209. } else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Group.Field") && !strings.Contains(err.Error(), "Group.{Unknown}") {
  1210. t.Errorf("unmarshal: bad error type: %v", err)
  1211. }
  1212. }
  1213. func TestTypedNilMarshal(t *testing.T) {
  1214. // A typed nil should return ErrNil and not crash.
  1215. {
  1216. var m *GoEnum
  1217. if _, err := Marshal(m); err != ErrNil {
  1218. t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err)
  1219. }
  1220. }
  1221. {
  1222. m := &Communique{Union: &Communique_Msg{nil}}
  1223. if _, err := Marshal(m); err == nil || err == ErrNil {
  1224. t.Errorf("Marshal(%#v): got %v, want errOneofHasNil", m, err)
  1225. }
  1226. }
  1227. }
  1228. // A type that implements the Marshaler interface, but is not nillable.
  1229. type nonNillableInt uint64
  1230. func (nni nonNillableInt) Marshal() ([]byte, error) {
  1231. return EncodeVarint(uint64(nni)), nil
  1232. }
  1233. type NNIMessage struct {
  1234. nni nonNillableInt
  1235. }
  1236. func (*NNIMessage) Reset() {}
  1237. func (*NNIMessage) String() string { return "" }
  1238. func (*NNIMessage) ProtoMessage() {}
  1239. type NMMessage struct{}
  1240. func (*NMMessage) Reset() {}
  1241. func (*NMMessage) String() string { return "" }
  1242. func (*NMMessage) ProtoMessage() {}
  1243. // Verify a type that uses the Marshaler interface, but has a nil pointer.
  1244. func TestNilMarshaler(t *testing.T) {
  1245. // Try a struct with a Marshaler field that is nil.
  1246. // It should be directly marshable.
  1247. nmm := new(NMMessage)
  1248. if _, err := Marshal(nmm); err != nil {
  1249. t.Error("unexpected error marshaling nmm: ", err)
  1250. }
  1251. // Try a struct with a Marshaler field that is not nillable.
  1252. nnim := new(NNIMessage)
  1253. nnim.nni = 7
  1254. var _ Marshaler = nnim.nni // verify it is truly a Marshaler
  1255. if _, err := Marshal(nnim); err != nil {
  1256. t.Error("unexpected error marshaling nnim: ", err)
  1257. }
  1258. }
  1259. func TestAllSetDefaults(t *testing.T) {
  1260. // Exercise SetDefaults with all scalar field types.
  1261. m := &Defaults{
  1262. // NaN != NaN, so override that here.
  1263. F_Nan: Float32(1.7),
  1264. }
  1265. expected := &Defaults{
  1266. F_Bool: Bool(true),
  1267. F_Int32: Int32(32),
  1268. F_Int64: Int64(64),
  1269. F_Fixed32: Uint32(320),
  1270. F_Fixed64: Uint64(640),
  1271. F_Uint32: Uint32(3200),
  1272. F_Uint64: Uint64(6400),
  1273. F_Float: Float32(314159),
  1274. F_Double: Float64(271828),
  1275. F_String: String(`hello, "world!"` + "\n"),
  1276. F_Bytes: []byte("Bignose"),
  1277. F_Sint32: Int32(-32),
  1278. F_Sint64: Int64(-64),
  1279. F_Enum: Defaults_GREEN.Enum(),
  1280. F_Pinf: Float32(float32(math.Inf(1))),
  1281. F_Ninf: Float32(float32(math.Inf(-1))),
  1282. F_Nan: Float32(1.7),
  1283. StrZero: String(""),
  1284. }
  1285. SetDefaults(m)
  1286. if !Equal(m, expected) {
  1287. t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected)
  1288. }
  1289. }
  1290. func TestSetDefaultsWithSetField(t *testing.T) {
  1291. // Check that a set value is not overridden.
  1292. m := &Defaults{
  1293. F_Int32: Int32(12),
  1294. }
  1295. SetDefaults(m)
  1296. if v := m.GetF_Int32(); v != 12 {
  1297. t.Errorf("m.FInt32 = %v, want 12", v)
  1298. }
  1299. }
  1300. func TestSetDefaultsWithSubMessage(t *testing.T) {
  1301. m := &OtherMessage{
  1302. Key: Int64(123),
  1303. Inner: &InnerMessage{
  1304. Host: String("gopher"),
  1305. },
  1306. }
  1307. expected := &OtherMessage{
  1308. Key: Int64(123),
  1309. Inner: &InnerMessage{
  1310. Host: String("gopher"),
  1311. Port: Int32(4000),
  1312. },
  1313. }
  1314. SetDefaults(m)
  1315. if !Equal(m, expected) {
  1316. t.Errorf("\n got %v\nwant %v", m, expected)
  1317. }
  1318. }
  1319. func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
  1320. m := &MyMessage{
  1321. RepInner: []*InnerMessage{{}},
  1322. }
  1323. expected := &MyMessage{
  1324. RepInner: []*InnerMessage{{
  1325. Port: Int32(4000),
  1326. }},
  1327. }
  1328. SetDefaults(m)
  1329. if !Equal(m, expected) {
  1330. t.Errorf("\n got %v\nwant %v", m, expected)
  1331. }
  1332. }
  1333. func TestSetDefaultWithRepeatedNonMessage(t *testing.T) {
  1334. m := &MyMessage{
  1335. Pet: []string{"turtle", "wombat"},
  1336. }
  1337. expected := Clone(m)
  1338. SetDefaults(m)
  1339. if !Equal(m, expected) {
  1340. t.Errorf("\n got %v\nwant %v", m, expected)
  1341. }
  1342. }
  1343. func TestMaximumTagNumber(t *testing.T) {
  1344. m := &MaxTag{
  1345. LastField: String("natural goat essence"),
  1346. }
  1347. buf, err := Marshal(m)
  1348. if err != nil {
  1349. t.Fatalf("proto.Marshal failed: %v", err)
  1350. }
  1351. m2 := new(MaxTag)
  1352. if err := Unmarshal(buf, m2); err != nil {
  1353. t.Fatalf("proto.Unmarshal failed: %v", err)
  1354. }
  1355. if got, want := m2.GetLastField(), *m.LastField; got != want {
  1356. t.Errorf("got %q, want %q", got, want)
  1357. }
  1358. }
  1359. func TestJSON(t *testing.T) {
  1360. m := &MyMessage{
  1361. Count: Int32(4),
  1362. Pet: []string{"bunny", "kitty"},
  1363. Inner: &InnerMessage{
  1364. Host: String("cauchy"),
  1365. },
  1366. Bikeshed: MyMessage_GREEN.Enum(),
  1367. }
  1368. const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
  1369. b, err := json.Marshal(m)
  1370. if err != nil {
  1371. t.Fatalf("json.Marshal failed: %v", err)
  1372. }
  1373. s := string(b)
  1374. if s != expected {
  1375. t.Errorf("got %s\nwant %s", s, expected)
  1376. }
  1377. received := new(MyMessage)
  1378. if err := json.Unmarshal(b, received); err != nil {
  1379. t.Fatalf("json.Unmarshal failed: %v", err)
  1380. }
  1381. if !Equal(received, m) {
  1382. t.Fatalf("got %s, want %s", received, m)
  1383. }
  1384. // Test unmarshalling of JSON with symbolic enum name.
  1385. const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
  1386. received.Reset()
  1387. if err := json.Unmarshal([]byte(old), received); err != nil {
  1388. t.Fatalf("json.Unmarshal failed: %v", err)
  1389. }
  1390. if !Equal(received, m) {
  1391. t.Fatalf("got %s, want %s", received, m)
  1392. }
  1393. }
  1394. func TestBadWireType(t *testing.T) {
  1395. b := []byte{7<<3 | 6} // field 7, wire type 6
  1396. pb := new(OtherMessage)
  1397. if err := Unmarshal(b, pb); err == nil {
  1398. t.Errorf("Unmarshal did not fail")
  1399. } else if !strings.Contains(err.Error(), "unknown wire type") {
  1400. t.Errorf("wrong error: %v", err)
  1401. }
  1402. }
  1403. func TestBytesWithInvalidLength(t *testing.T) {
  1404. // If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
  1405. b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
  1406. Unmarshal(b, new(MyMessage))
  1407. }
  1408. func TestLengthOverflow(t *testing.T) {
  1409. // Overflowing a length should not panic.
  1410. b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
  1411. Unmarshal(b, new(MyMessage))
  1412. }
  1413. func TestVarintOverflow(t *testing.T) {
  1414. // Overflowing a 64-bit length should not be allowed.
  1415. b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
  1416. if err := Unmarshal(b, new(MyMessage)); err == nil {
  1417. t.Fatalf("Overflowed uint64 length without error")
  1418. }
  1419. }
  1420. func TestBytesWithInvalidLengthInGroup(t *testing.T) {
  1421. // Overflowing a 64-bit length should not be allowed.
  1422. b := []byte{0xbb, 0x30, 0xb2, 0x30, 0xb0, 0xb2, 0x83, 0xf1, 0xb0, 0xb2, 0xef, 0xbf, 0xbd, 0x01}
  1423. if err := Unmarshal(b, new(MyMessage)); err == nil {
  1424. t.Fatalf("Overflowed uint64 length without error")
  1425. }
  1426. }
  1427. func TestUnmarshalFuzz(t *testing.T) {
  1428. const N = 1000
  1429. seed := time.Now().UnixNano()
  1430. t.Logf("RNG seed is %d", seed)
  1431. rng := rand.New(rand.NewSource(seed))
  1432. buf := make([]byte, 20)
  1433. for i := 0; i < N; i++ {
  1434. for j := range buf {
  1435. buf[j] = byte(rng.Intn(256))
  1436. }
  1437. fuzzUnmarshal(t, buf)
  1438. }
  1439. }
  1440. func TestMergeMessages(t *testing.T) {
  1441. pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
  1442. data, err := Marshal(pb)
  1443. if err != nil {
  1444. t.Fatalf("Marshal: %v", err)
  1445. }
  1446. pb1 := new(MessageList)
  1447. if err := Unmarshal(data, pb1); err != nil {
  1448. t.Fatalf("first Unmarshal: %v", err)
  1449. }
  1450. if err := Unmarshal(data, pb1); err != nil {
  1451. t.Fatalf("second Unmarshal: %v", err)
  1452. }
  1453. if len(pb1.Message) != 1 {
  1454. t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
  1455. }
  1456. pb2 := new(MessageList)
  1457. if err := UnmarshalMerge(data, pb2); err != nil {
  1458. t.Fatalf("first UnmarshalMerge: %v", err)
  1459. }
  1460. if err := UnmarshalMerge(data, pb2); err != nil {
  1461. t.Fatalf("second UnmarshalMerge: %v", err)
  1462. }
  1463. if len(pb2.Message) != 2 {
  1464. t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
  1465. }
  1466. }
  1467. func TestExtensionMarshalOrder(t *testing.T) {
  1468. m := &MyMessage{Count: Int(123)}
  1469. if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
  1470. t.Fatalf("SetExtension: %v", err)
  1471. }
  1472. if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
  1473. t.Fatalf("SetExtension: %v", err)
  1474. }
  1475. if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
  1476. t.Fatalf("SetExtension: %v", err)
  1477. }
  1478. // Serialize m several times, and check we get the same bytes each time.
  1479. var orig []byte
  1480. for i := 0; i < 100; i++ {
  1481. b, err := Marshal(m)
  1482. if err != nil {
  1483. t.Fatalf("Marshal: %v", err)
  1484. }
  1485. if i == 0 {
  1486. orig = b
  1487. continue
  1488. }
  1489. if !bytes.Equal(b, orig) {
  1490. t.Errorf("Bytes differ on attempt #%d", i)
  1491. }
  1492. }
  1493. }
  1494. func TestExtensionMapFieldMarshalDeterministic(t *testing.T) {
  1495. m := &MyMessage{Count: Int(123)}
  1496. if err := SetExtension(m, E_Ext_More, &Ext{MapField: map[int32]int32{1: 1, 2: 2, 3: 3, 4: 4}}); err != nil {
  1497. t.Fatalf("SetExtension: %v", err)
  1498. }
  1499. marshal := func(m Message) []byte {
  1500. var b Buffer
  1501. b.SetDeterministic(true)
  1502. if err := b.Marshal(m); err != nil {
  1503. t.Fatalf("Marshal failed: %v", err)
  1504. }
  1505. return b.Bytes()
  1506. }
  1507. want := marshal(m)
  1508. for i := 0; i < 100; i++ {
  1509. if got := marshal(m); !bytes.Equal(got, want) {
  1510. t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
  1511. }
  1512. }
  1513. }
  1514. // Many extensions, because small maps might not iterate differently on each iteration.
  1515. var exts = []*ExtensionDesc{
  1516. E_X201,
  1517. E_X202,
  1518. E_X203,
  1519. E_X204,
  1520. E_X205,
  1521. E_X206,
  1522. E_X207,
  1523. E_X208,
  1524. E_X209,
  1525. E_X210,
  1526. E_X211,
  1527. E_X212,
  1528. E_X213,
  1529. E_X214,
  1530. E_X215,
  1531. E_X216,
  1532. E_X217,
  1533. E_X218,
  1534. E_X219,
  1535. E_X220,
  1536. E_X221,
  1537. E_X222,
  1538. E_X223,
  1539. E_X224,
  1540. E_X225,
  1541. E_X226,
  1542. E_X227,
  1543. E_X228,
  1544. E_X229,
  1545. E_X230,
  1546. E_X231,
  1547. E_X232,
  1548. E_X233,
  1549. E_X234,
  1550. E_X235,
  1551. E_X236,
  1552. E_X237,
  1553. E_X238,
  1554. E_X239,
  1555. E_X240,
  1556. E_X241,
  1557. E_X242,
  1558. E_X243,
  1559. E_X244,
  1560. E_X245,
  1561. E_X246,
  1562. E_X247,
  1563. E_X248,
  1564. E_X249,
  1565. E_X250,
  1566. }
  1567. func TestMessageSetMarshalOrder(t *testing.T) {
  1568. m := &MyMessageSet{}
  1569. for _, x := range exts {
  1570. if err := SetExtension(m, x, &Empty{}); err != nil {
  1571. t.Fatalf("SetExtension: %v", err)
  1572. }
  1573. }
  1574. buf, err := Marshal(m)
  1575. if err != nil {
  1576. t.Fatalf("Marshal: %v", err)
  1577. }
  1578. // Serialize m several times, and check we get the same bytes each time.
  1579. for i := 0; i < 10; i++ {
  1580. b1, err := Marshal(m)
  1581. if err != nil {
  1582. t.Fatalf("Marshal: %v", err)
  1583. }
  1584. if !bytes.Equal(b1, buf) {
  1585. t.Errorf("Bytes differ on re-Marshal #%d", i)
  1586. }
  1587. m2 := &MyMessageSet{}
  1588. if err := Unmarshal(buf, m2); err != nil {
  1589. t.Errorf("Unmarshal: %v", err)
  1590. }
  1591. b2, err := Marshal(m2)
  1592. if err != nil {
  1593. t.Errorf("re-Marshal: %v", err)
  1594. }
  1595. if !bytes.Equal(b2, buf) {
  1596. t.Errorf("Bytes differ on round-trip #%d", i)
  1597. }
  1598. }
  1599. }
  1600. func TestUnmarshalMergesMessages(t *testing.T) {
  1601. // If a nested message occurs twice in the input,
  1602. // the fields should be merged when decoding.
  1603. a := &OtherMessage{
  1604. Key: Int64(123),
  1605. Inner: &InnerMessage{
  1606. Host: String("polhode"),
  1607. Port: Int32(1234),
  1608. },
  1609. }
  1610. aData, err := Marshal(a)
  1611. if err != nil {
  1612. t.Fatalf("Marshal(a): %v", err)
  1613. }
  1614. b := &OtherMessage{
  1615. Weight: Float32(1.2),
  1616. Inner: &InnerMessage{
  1617. Host: String("herpolhode"),
  1618. Connected: Bool(true),
  1619. },
  1620. }
  1621. bData, err := Marshal(b)
  1622. if err != nil {
  1623. t.Fatalf("Marshal(b): %v", err)
  1624. }
  1625. want := &OtherMessage{
  1626. Key: Int64(123),
  1627. Weight: Float32(1.2),
  1628. Inner: &InnerMessage{
  1629. Host: String("herpolhode"),
  1630. Port: Int32(1234),
  1631. Connected: Bool(true),
  1632. },
  1633. }
  1634. got := new(OtherMessage)
  1635. if err := Unmarshal(append(aData, bData...), got); err != nil {
  1636. t.Fatalf("Unmarshal: %v", err)
  1637. }
  1638. if !Equal(got, want) {
  1639. t.Errorf("\n got %v\nwant %v", got, want)
  1640. }
  1641. }
  1642. func TestUnmarshalMergesGroups(t *testing.T) {
  1643. // If a nested group occurs twice in the input,
  1644. // the fields should be merged when decoding.
  1645. a := &GroupNew{
  1646. G: &GroupNew_G{
  1647. X: Int32(7),
  1648. Y: Int32(8),
  1649. },
  1650. }
  1651. aData, err := Marshal(a)
  1652. if err != nil {
  1653. t.Fatalf("Marshal(a): %v", err)
  1654. }
  1655. b := &GroupNew{
  1656. G: &GroupNew_G{
  1657. X: Int32(9),
  1658. },
  1659. }
  1660. bData, err := Marshal(b)
  1661. if err != nil {
  1662. t.Fatalf("Marshal(b): %v", err)
  1663. }
  1664. want := &GroupNew{
  1665. G: &GroupNew_G{
  1666. X: Int32(9),
  1667. Y: Int32(8),
  1668. },
  1669. }
  1670. got := new(GroupNew)
  1671. if err := Unmarshal(append(aData, bData...), got); err != nil {
  1672. t.Fatalf("Unmarshal: %v", err)
  1673. }
  1674. if !Equal(got, want) {
  1675. t.Errorf("\n got %v\nwant %v", got, want)
  1676. }
  1677. }
  1678. func TestEncodingSizes(t *testing.T) {
  1679. tests := []struct {
  1680. m Message
  1681. n int
  1682. }{
  1683. {&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
  1684. {&Defaults{F_Int32: Int32(math.MinInt32)}, 11},
  1685. {&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6},
  1686. {&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
  1687. }
  1688. for _, test := range tests {
  1689. b, err := Marshal(test.m)
  1690. if err != nil {
  1691. t.Errorf("Marshal(%v): %v", test.m, err)
  1692. continue
  1693. }
  1694. if len(b) != test.n {
  1695. t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
  1696. }
  1697. }
  1698. }
  1699. func TestRequiredNotSetError(t *testing.T) {
  1700. pb := initGoTest(false)
  1701. pb.RequiredField.Label = nil
  1702. pb.F_Int32Required = nil
  1703. pb.F_Int64Required = nil
  1704. expected := "0807" + // field 1, encoding 0, value 7
  1705. "2206" + "120474797065" + // field 4, encoding 2 (GoTestField)
  1706. "5001" + // field 10, encoding 0, value 1
  1707. "6d20000000" + // field 13, encoding 5, value 0x20
  1708. "714000000000000000" + // field 14, encoding 1, value 0x40
  1709. "78a019" + // field 15, encoding 0, value 0xca0 = 3232
  1710. "8001c032" + // field 16, encoding 0, value 0x1940 = 6464
  1711. "8d0100004a45" + // field 17, encoding 5, value 3232.0
  1712. "9101000000000040b940" + // field 18, encoding 1, value 6464.0
  1713. "9a0106" + "737472696e67" + // field 19, encoding 2, string "string"
  1714. "b304" + // field 70, encoding 3, start group
  1715. "ba0408" + "7265717569726564" + // field 71, encoding 2, string "required"
  1716. "b404" + // field 70, encoding 4, end group
  1717. "aa0605" + "6279746573" + // field 101, encoding 2, string "bytes"
  1718. "b0063f" + // field 102, encoding 0, 0x3f zigzag32
  1719. "b8067f" + // field 103, encoding 0, 0x7f zigzag64
  1720. "c506e0ffffff" + // field 104, encoding 5, -32 fixed32
  1721. "c906c0ffffffffffffff" // field 105, encoding 1, -64 fixed64
  1722. o := old()
  1723. bytes, err := Marshal(pb)
  1724. if _, ok := err.(*RequiredNotSetError); !ok {
  1725. fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err)
  1726. o.DebugPrint("", bytes)
  1727. t.Fatalf("expected = %s", expected)
  1728. }
  1729. if !strings.Contains(err.Error(), "RequiredField.Label") {
  1730. t.Errorf("marshal-1 wrong err msg: %v", err)
  1731. }
  1732. if !equal(bytes, expected, t) {
  1733. o.DebugPrint("neq 1", bytes)
  1734. t.Fatalf("expected = %s", expected)
  1735. }
  1736. // Now test Unmarshal by recreating the original buffer.
  1737. pbd := new(GoTest)
  1738. err = Unmarshal(bytes, pbd)
  1739. if _, ok := err.(*RequiredNotSetError); !ok {
  1740. t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err)
  1741. o.DebugPrint("", bytes)
  1742. t.Fatalf("string = %s", expected)
  1743. }
  1744. if !strings.Contains(err.Error(), "RequiredField.Label") && !strings.Contains(err.Error(), "RequiredField.{Unknown}") {
  1745. t.Errorf("unmarshal wrong err msg: %v", err)
  1746. }
  1747. bytes, err = Marshal(pbd)
  1748. if _, ok := err.(*RequiredNotSetError); !ok {
  1749. t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
  1750. o.DebugPrint("", bytes)
  1751. t.Fatalf("string = %s", expected)
  1752. }
  1753. if !strings.Contains(err.Error(), "RequiredField.Label") {
  1754. t.Errorf("marshal-2 wrong err msg: %v", err)
  1755. }
  1756. if !equal(bytes, expected, t) {
  1757. o.DebugPrint("neq 2", bytes)
  1758. t.Fatalf("string = %s", expected)
  1759. }
  1760. }
  1761. func TestRequiredNotSetErrorWithBadWireTypes(t *testing.T) {
  1762. // Required field expects a varint, and properly found a varint.
  1763. if err := Unmarshal([]byte{0x08, 0x00}, new(GoEnum)); err != nil {
  1764. t.Errorf("Unmarshal = %v, want nil", err)
  1765. }
  1766. // Required field expects a varint, but found a fixed32 instead.
  1767. if err := Unmarshal([]byte{0x0d, 0x00, 0x00, 0x00, 0x00}, new(GoEnum)); err == nil {
  1768. t.Errorf("Unmarshal = nil, want RequiredNotSetError")
  1769. }
  1770. // Required field expects a varint, and found both a varint and fixed32 (ignored).
  1771. m := new(GoEnum)
  1772. if err := Unmarshal([]byte{0x08, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00}, m); err != nil {
  1773. t.Errorf("Unmarshal = %v, want nil", err)
  1774. }
  1775. if !bytes.Equal(m.XXX_unrecognized, []byte{0x0d, 0x00, 0x00, 0x00, 0x00}) {
  1776. t.Errorf("expected fixed32 to appear as unknown bytes: %x", m.XXX_unrecognized)
  1777. }
  1778. }
  1779. func fuzzUnmarshal(t *testing.T, data []byte) {
  1780. defer func() {
  1781. if e := recover(); e != nil {
  1782. t.Errorf("These bytes caused a panic: %+v", data)
  1783. t.Logf("Stack:\n%s", debug.Stack())
  1784. t.FailNow()
  1785. }
  1786. }()
  1787. pb := new(MyMessage)
  1788. Unmarshal(data, pb)
  1789. }
  1790. func TestMapFieldMarshal(t *testing.T) {
  1791. m := &MessageWithMap{
  1792. NameMapping: map[int32]string{
  1793. 1: "Rob",
  1794. 4: "Ian",
  1795. 8: "Dave",
  1796. },
  1797. }
  1798. b, err := Marshal(m)
  1799. if err != nil {
  1800. t.Fatalf("Marshal: %v", err)
  1801. }
  1802. // b should be the concatenation of these three byte sequences in some order.
  1803. parts := []string{
  1804. "\n\a\b\x01\x12\x03Rob",
  1805. "\n\a\b\x04\x12\x03Ian",
  1806. "\n\b\b\x08\x12\x04Dave",
  1807. }
  1808. ok := false
  1809. for i := range parts {
  1810. for j := range parts {
  1811. if j == i {
  1812. continue
  1813. }
  1814. for k := range parts {
  1815. if k == i || k == j {
  1816. continue
  1817. }
  1818. try := parts[i] + parts[j] + parts[k]
  1819. if bytes.Equal(b, []byte(try)) {
  1820. ok = true
  1821. break
  1822. }
  1823. }
  1824. }
  1825. }
  1826. if !ok {
  1827. t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
  1828. }
  1829. t.Logf("FYI b: %q", b)
  1830. (new(Buffer)).DebugPrint("Dump of b", b)
  1831. }
  1832. func TestMapFieldDeterministicMarshal(t *testing.T) {
  1833. m := &MessageWithMap{
  1834. NameMapping: map[int32]string{
  1835. 1: "Rob",
  1836. 4: "Ian",
  1837. 8: "Dave",
  1838. },
  1839. }
  1840. marshal := func(m Message) []byte {
  1841. var b Buffer
  1842. b.SetDeterministic(true)
  1843. if err := b.Marshal(m); err != nil {
  1844. t.Fatalf("Marshal failed: %v", err)
  1845. }
  1846. return b.Bytes()
  1847. }
  1848. want := marshal(m)
  1849. for i := 0; i < 10; i++ {
  1850. if got := marshal(m); !bytes.Equal(got, want) {
  1851. t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
  1852. }
  1853. }
  1854. }
  1855. func TestMapFieldRoundTrips(t *testing.T) {
  1856. m := &MessageWithMap{
  1857. NameMapping: map[int32]string{
  1858. 1: "Rob",
  1859. 4: "Ian",
  1860. 8: "Dave",
  1861. },
  1862. MsgMapping: map[int64]*FloatingPoint{
  1863. 0x7001: {F: Float64(2.0)},
  1864. },
  1865. ByteMapping: map[bool][]byte{
  1866. false: []byte("that's not right!"),
  1867. true: []byte("aye, 'tis true!"),
  1868. },
  1869. }
  1870. b, err := Marshal(m)
  1871. if err != nil {
  1872. t.Fatalf("Marshal: %v", err)
  1873. }
  1874. t.Logf("FYI b: %q", b)
  1875. m2 := new(MessageWithMap)
  1876. if err := Unmarshal(b, m2); err != nil {
  1877. t.Fatalf("Unmarshal: %v", err)
  1878. }
  1879. if !Equal(m, m2) {
  1880. t.Errorf("Map did not survive a round trip.\ninitial: %v\n final: %v", m, m2)
  1881. }
  1882. }
  1883. func TestMapFieldWithNil(t *testing.T) {
  1884. m1 := &MessageWithMap{
  1885. MsgMapping: map[int64]*FloatingPoint{
  1886. 1: nil,
  1887. },
  1888. }
  1889. b, err := Marshal(m1)
  1890. if err != nil {
  1891. t.Fatalf("Marshal: %v", err)
  1892. }
  1893. m2 := new(MessageWithMap)
  1894. if err := Unmarshal(b, m2); err != nil {
  1895. t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
  1896. }
  1897. if v, ok := m2.MsgMapping[1]; !ok {
  1898. t.Error("msg_mapping[1] not present")
  1899. } else if v != nil {
  1900. t.Errorf("msg_mapping[1] not nil: %v", v)
  1901. }
  1902. }
  1903. func TestMapFieldWithNilBytes(t *testing.T) {
  1904. m1 := &MessageWithMap{
  1905. ByteMapping: map[bool][]byte{
  1906. false: {},
  1907. true: nil,
  1908. },
  1909. }
  1910. n := Size(m1)
  1911. b, err := Marshal(m1)
  1912. if err != nil {
  1913. t.Fatalf("Marshal: %v", err)
  1914. }
  1915. if n != len(b) {
  1916. t.Errorf("Size(m1) = %d; want len(Marshal(m1)) = %d", n, len(b))
  1917. }
  1918. m2 := new(MessageWithMap)
  1919. if err := Unmarshal(b, m2); err != nil {
  1920. t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
  1921. }
  1922. if v, ok := m2.ByteMapping[false]; !ok {
  1923. t.Error("byte_mapping[false] not present")
  1924. } else if len(v) != 0 {
  1925. t.Errorf("byte_mapping[false] not empty: %#v", v)
  1926. }
  1927. if v, ok := m2.ByteMapping[true]; !ok {
  1928. t.Error("byte_mapping[true] not present")
  1929. } else if len(v) != 0 {
  1930. t.Errorf("byte_mapping[true] not empty: %#v", v)
  1931. }
  1932. }
  1933. func TestDecodeMapFieldMissingKey(t *testing.T) {
  1934. b := []byte{
  1935. 0x0A, 0x03, // message, tag 1 (name_mapping), of length 3 bytes
  1936. // no key
  1937. 0x12, 0x01, 0x6D, // string value of length 1 byte, value "m"
  1938. }
  1939. got := &MessageWithMap{}
  1940. err := Unmarshal(b, got)
  1941. if err != nil {
  1942. t.Fatalf("failed to marshal map with missing key: %v", err)
  1943. }
  1944. want := &MessageWithMap{NameMapping: map[int32]string{0: "m"}}
  1945. if !Equal(got, want) {
  1946. t.Errorf("Unmarshaled map with no key was not as expected. got: %v, want %v", got, want)
  1947. }
  1948. }
  1949. func TestDecodeMapFieldMissingValue(t *testing.T) {
  1950. b := []byte{
  1951. 0x0A, 0x02, // message, tag 1 (name_mapping), of length 2 bytes
  1952. 0x08, 0x01, // varint key, value 1
  1953. // no value
  1954. }
  1955. got := &MessageWithMap{}
  1956. err := Unmarshal(b, got)
  1957. if err != nil {
  1958. t.Fatalf("failed to marshal map with missing value: %v", err)
  1959. }
  1960. want := &MessageWithMap{NameMapping: map[int32]string{1: ""}}
  1961. if !Equal(got, want) {
  1962. t.Errorf("Unmarshaled map with no value was not as expected. got: %v, want %v", got, want)
  1963. }
  1964. }
  1965. func TestOneof(t *testing.T) {
  1966. m := &Communique{}
  1967. b, err := Marshal(m)
  1968. if err != nil {
  1969. t.Fatalf("Marshal of empty message with oneof: %v", err)
  1970. }
  1971. if len(b) != 0 {
  1972. t.Errorf("Marshal of empty message yielded too many bytes: %v", b)
  1973. }
  1974. m = &Communique{
  1975. Union: &Communique_Name{"Barry"},
  1976. }
  1977. // Round-trip.
  1978. b, err = Marshal(m)
  1979. if err != nil {
  1980. t.Fatalf("Marshal of message with oneof: %v", err)
  1981. }
  1982. if len(b) != 7 { // name tag/wire (1) + name len (1) + name (5)
  1983. t.Errorf("Incorrect marshal of message with oneof: %v", b)
  1984. }
  1985. m.Reset()
  1986. if err := Unmarshal(b, m); err != nil {
  1987. t.Fatalf("Unmarshal of message with oneof: %v", err)
  1988. }
  1989. if x, ok := m.Union.(*Communique_Name); !ok || x.Name != "Barry" {
  1990. t.Errorf("After round trip, Union = %+v", m.Union)
  1991. }
  1992. if name := m.GetName(); name != "Barry" {
  1993. t.Errorf("After round trip, GetName = %q, want %q", name, "Barry")
  1994. }
  1995. // Let's try with a message in the oneof.
  1996. m.Union = &Communique_Msg{&Strings{StringField: String("deep deep string")}}
  1997. b, err = Marshal(m)
  1998. if err != nil {
  1999. t.Fatalf("Marshal of message with oneof set to message: %v", err)
  2000. }
  2001. if len(b) != 20 { // msg tag/wire (1) + msg len (1) + msg (1 + 1 + 16)
  2002. t.Errorf("Incorrect marshal of message with oneof set to message: %v", b)
  2003. }
  2004. m.Reset()
  2005. if err := Unmarshal(b, m); err != nil {
  2006. t.Fatalf("Unmarshal of message with oneof set to message: %v", err)
  2007. }
  2008. ss, ok := m.Union.(*Communique_Msg)
  2009. if !ok || ss.Msg.GetStringField() != "deep deep string" {
  2010. t.Errorf("After round trip with oneof set to message, Union = %+v", m.Union)
  2011. }
  2012. }
  2013. func TestOneofNilBytes(t *testing.T) {
  2014. // A oneof with nil byte slice should marshal to tag + 0 (size), with no error.
  2015. m := &Communique{Union: &Communique_Data{Data: nil}}
  2016. b, err := Marshal(m)
  2017. if err != nil {
  2018. t.Fatalf("Marshal failed: %v", err)
  2019. }
  2020. want := []byte{
  2021. 7<<3 | 2, // tag 7, wire type 2
  2022. 0, // size
  2023. }
  2024. if !bytes.Equal(b, want) {
  2025. t.Errorf("Wrong result of Marshal: got %x, want %x", b, want)
  2026. }
  2027. }
  2028. func TestInefficientPackedBool(t *testing.T) {
  2029. // https://github.com/golang/protobuf/issues/76
  2030. inp := []byte{
  2031. 0x12, 0x02, // 0x12 = 2<<3|2; 2 bytes
  2032. // Usually a bool should take a single byte,
  2033. // but it is permitted to be any varint.
  2034. 0xb9, 0x30,
  2035. }
  2036. if err := Unmarshal(inp, new(MoreRepeated)); err != nil {
  2037. t.Error(err)
  2038. }
  2039. }
  2040. // Make sure pure-reflect-based implementation handles
  2041. // []int32-[]enum conversion correctly.
  2042. func TestRepeatedEnum2(t *testing.T) {
  2043. pb := &RepeatedEnum{
  2044. Color: []RepeatedEnum_Color{RepeatedEnum_RED},
  2045. }
  2046. b, err := Marshal(pb)
  2047. if err != nil {
  2048. t.Fatalf("Marshal failed: %v", err)
  2049. }
  2050. x := new(RepeatedEnum)
  2051. err = Unmarshal(b, x)
  2052. if err != nil {
  2053. t.Fatalf("Unmarshal failed: %v", err)
  2054. }
  2055. if !Equal(pb, x) {
  2056. t.Errorf("Incorrect result: want: %v got: %v", pb, x)
  2057. }
  2058. }
  2059. // TestConcurrentMarshal makes sure that it is safe to marshal
  2060. // same message in multiple goroutines concurrently.
  2061. func TestConcurrentMarshal(t *testing.T) {
  2062. pb := initGoTest(true)
  2063. const N = 100
  2064. b := make([][]byte, N)
  2065. var wg sync.WaitGroup
  2066. for i := 0; i < N; i++ {
  2067. wg.Add(1)
  2068. go func(i int) {
  2069. defer wg.Done()
  2070. var err error
  2071. b[i], err = Marshal(pb)
  2072. if err != nil {
  2073. t.Errorf("marshal error: %v", err)
  2074. }
  2075. }(i)
  2076. }
  2077. wg.Wait()
  2078. for i := 1; i < N; i++ {
  2079. if !bytes.Equal(b[0], b[i]) {
  2080. t.Errorf("concurrent marshal result not same: b[0] = %v, b[%d] = %v", b[0], i, b[i])
  2081. }
  2082. }
  2083. }
  2084. func TestInvalidUTF8(t *testing.T) {
  2085. const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"
  2086. tests := []struct {
  2087. label string
  2088. proto2 Message
  2089. proto3 Message
  2090. want []byte
  2091. }{{
  2092. label: "Scalar",
  2093. proto2: &TestUTF8{Scalar: String(invalidUTF8)},
  2094. proto3: &pb3.TestUTF8{Scalar: invalidUTF8},
  2095. want: []byte{0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
  2096. }, {
  2097. label: "Vector",
  2098. proto2: &TestUTF8{Vector: []string{invalidUTF8}},
  2099. proto3: &pb3.TestUTF8{Vector: []string{invalidUTF8}},
  2100. want: []byte{0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
  2101. }, {
  2102. label: "Oneof",
  2103. proto2: &TestUTF8{Oneof: &TestUTF8_Field{invalidUTF8}},
  2104. proto3: &pb3.TestUTF8{Oneof: &pb3.TestUTF8_Field{invalidUTF8}},
  2105. want: []byte{0x1a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
  2106. }, {
  2107. label: "MapKey",
  2108. proto2: &TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
  2109. proto3: &pb3.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
  2110. want: []byte{0x22, 0x0b, 0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff, 0x10, 0x00},
  2111. }, {
  2112. label: "MapValue",
  2113. proto2: &TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
  2114. proto3: &pb3.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
  2115. want: []byte{0x2a, 0x0b, 0x08, 0x00, 0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
  2116. }}
  2117. for _, tt := range tests {
  2118. // Proto2 should not validate UTF-8.
  2119. b, err := Marshal(tt.proto2)
  2120. if err != nil {
  2121. t.Errorf("Marshal(proto2.%s) = %v, want nil", tt.label, err)
  2122. }
  2123. if !bytes.Equal(b, tt.want) {
  2124. t.Errorf("Marshal(proto2.%s) = %x, want %x", tt.label, b, tt.want)
  2125. }
  2126. m := Clone(tt.proto2)
  2127. m.Reset()
  2128. if err = Unmarshal(tt.want, m); err != nil {
  2129. t.Errorf("Unmarshal(proto2.%s) = %v, want nil", tt.label, err)
  2130. }
  2131. if !Equal(m, tt.proto2) {
  2132. t.Errorf("proto2.%s: output mismatch:\ngot %v\nwant %v", tt.label, m, tt.proto2)
  2133. }
  2134. // Proto3 should validate UTF-8.
  2135. b, err = Marshal(tt.proto3)
  2136. if err == nil {
  2137. t.Errorf("Marshal(proto3.%s) = %v, want non-nil", tt.label, err)
  2138. }
  2139. if !bytes.Equal(b, tt.want) {
  2140. t.Errorf("Marshal(proto3.%s) = %x, want %x", tt.label, b, tt.want)
  2141. }
  2142. m = Clone(tt.proto3)
  2143. m.Reset()
  2144. err = Unmarshal(tt.want, m)
  2145. if err == nil {
  2146. t.Errorf("Unmarshal(proto3.%s) = %v, want non-nil", tt.label, err)
  2147. }
  2148. if !Equal(m, tt.proto3) {
  2149. t.Errorf("proto3.%s: output mismatch:\ngot %v\nwant %v", tt.label, m, tt.proto2)
  2150. }
  2151. }
  2152. }
  2153. func TestRequired(t *testing.T) {
  2154. // The F_BoolRequired field appears after all of the required fields.
  2155. // It should still be handled even after multiple required field violations.
  2156. m := &GoTest{F_BoolRequired: Bool(true)}
  2157. got, err := Marshal(m)
  2158. if _, ok := err.(*RequiredNotSetError); !ok {
  2159. t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
  2160. }
  2161. if want := []byte{0x50, 0x01}; !bytes.Equal(got, want) {
  2162. t.Errorf("Marshal() = %x, want %x", got, want)
  2163. }
  2164. m = new(GoTest)
  2165. err = Unmarshal(got, m)
  2166. if _, ok := err.(*RequiredNotSetError); !ok {
  2167. t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
  2168. }
  2169. if !m.GetF_BoolRequired() {
  2170. t.Error("m.F_BoolRequired = false, want true")
  2171. }
  2172. }
  2173. // Benchmarks
  2174. func testMsg() *GoTest {
  2175. pb := initGoTest(true)
  2176. const N = 1000 // Internally the library starts much smaller.
  2177. pb.F_Int32Repeated = make([]int32, N)
  2178. pb.F_DoubleRepeated = make([]float64, N)
  2179. for i := 0; i < N; i++ {
  2180. pb.F_Int32Repeated[i] = int32(i)
  2181. pb.F_DoubleRepeated[i] = float64(i)
  2182. }
  2183. return pb
  2184. }
  2185. func bytesMsg() *GoTest {
  2186. pb := initGoTest(true)
  2187. buf := make([]byte, 4000)
  2188. for i := range buf {
  2189. buf[i] = byte(i)
  2190. }
  2191. pb.F_BytesDefaulted = buf
  2192. return pb
  2193. }
  2194. func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) {
  2195. d, _ := marshal(pb)
  2196. b.SetBytes(int64(len(d)))
  2197. b.ResetTimer()
  2198. for i := 0; i < b.N; i++ {
  2199. marshal(pb)
  2200. }
  2201. }
  2202. func benchmarkBufferMarshal(b *testing.B, pb Message) {
  2203. p := NewBuffer(nil)
  2204. benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
  2205. p.Reset()
  2206. err := p.Marshal(pb0)
  2207. return p.Bytes(), err
  2208. })
  2209. }
  2210. func benchmarkSize(b *testing.B, pb Message) {
  2211. benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
  2212. Size(pb)
  2213. return nil, nil
  2214. })
  2215. }
  2216. func newOf(pb Message) Message {
  2217. in := reflect.ValueOf(pb)
  2218. if in.IsNil() {
  2219. return pb
  2220. }
  2221. return reflect.New(in.Type().Elem()).Interface().(Message)
  2222. }
  2223. func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) {
  2224. d, _ := Marshal(pb)
  2225. b.SetBytes(int64(len(d)))
  2226. pbd := newOf(pb)
  2227. b.ResetTimer()
  2228. for i := 0; i < b.N; i++ {
  2229. unmarshal(d, pbd)
  2230. }
  2231. }
  2232. func benchmarkBufferUnmarshal(b *testing.B, pb Message) {
  2233. p := NewBuffer(nil)
  2234. benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error {
  2235. p.SetBuf(d)
  2236. return p.Unmarshal(pb0)
  2237. })
  2238. }
  2239. // Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes}
  2240. func BenchmarkMarshal(b *testing.B) {
  2241. benchmarkMarshal(b, testMsg(), Marshal)
  2242. }
  2243. func BenchmarkBufferMarshal(b *testing.B) {
  2244. benchmarkBufferMarshal(b, testMsg())
  2245. }
  2246. func BenchmarkSize(b *testing.B) {
  2247. benchmarkSize(b, testMsg())
  2248. }
  2249. func BenchmarkUnmarshal(b *testing.B) {
  2250. benchmarkUnmarshal(b, testMsg(), Unmarshal)
  2251. }
  2252. func BenchmarkBufferUnmarshal(b *testing.B) {
  2253. benchmarkBufferUnmarshal(b, testMsg())
  2254. }
  2255. func BenchmarkMarshalBytes(b *testing.B) {
  2256. benchmarkMarshal(b, bytesMsg(), Marshal)
  2257. }
  2258. func BenchmarkBufferMarshalBytes(b *testing.B) {
  2259. benchmarkBufferMarshal(b, bytesMsg())
  2260. }
  2261. func BenchmarkSizeBytes(b *testing.B) {
  2262. benchmarkSize(b, bytesMsg())
  2263. }
  2264. func BenchmarkUnmarshalBytes(b *testing.B) {
  2265. benchmarkUnmarshal(b, bytesMsg(), Unmarshal)
  2266. }
  2267. func BenchmarkBufferUnmarshalBytes(b *testing.B) {
  2268. benchmarkBufferUnmarshal(b, bytesMsg())
  2269. }
  2270. func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
  2271. b.StopTimer()
  2272. pb := initGoTestField()
  2273. skip := &GoSkipTest{
  2274. SkipInt32: Int32(32),
  2275. SkipFixed32: Uint32(3232),
  2276. SkipFixed64: Uint64(6464),
  2277. SkipString: String("skipper"),
  2278. Skipgroup: &GoSkipTest_SkipGroup{
  2279. GroupInt32: Int32(75),
  2280. GroupString: String("wxyz"),
  2281. },
  2282. }
  2283. pbd := new(GoTestField)
  2284. p := NewBuffer(nil)
  2285. p.Marshal(pb)
  2286. p.Marshal(skip)
  2287. p2 := NewBuffer(nil)
  2288. b.StartTimer()
  2289. for i := 0; i < b.N; i++ {
  2290. p2.SetBuf(p.Bytes())
  2291. p2.Unmarshal(pbd)
  2292. }
  2293. }
  2294. // TestRace tests whether there are races among the different marshalers.
  2295. func TestRace(t *testing.T) {
  2296. m := &descriptorpb.FileDescriptorProto{
  2297. Options: &descriptorpb.FileOptions{
  2298. GoPackage: String("path/to/my/package"),
  2299. },
  2300. }
  2301. wg := &sync.WaitGroup{}
  2302. defer wg.Wait()
  2303. wg.Add(1)
  2304. go func() {
  2305. defer wg.Done()
  2306. Marshal(m)
  2307. }()
  2308. wg.Add(1)
  2309. go func() {
  2310. defer wg.Done()
  2311. (&jsonpb.Marshaler{}).MarshalToString(m)
  2312. }()
  2313. wg.Add(1)
  2314. go func() {
  2315. defer wg.Done()
  2316. m.String()
  2317. }()
  2318. }