gobencdec_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file contains tests of the GobEncoder/GobDecoder support.
  5. package gob
  6. import (
  7. "bytes"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "net"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. // Types that implement the GobEncoder/Decoder interfaces.
  17. type ByteStruct struct {
  18. a byte // not an exported field
  19. }
  20. type StringStruct struct {
  21. s string // not an exported field
  22. }
  23. type ArrayStruct struct {
  24. a [8192]byte // not an exported field
  25. }
  26. type Gobber int
  27. type ValueGobber string // encodes with a value, decodes with a pointer.
  28. type BinaryGobber int
  29. type BinaryValueGobber string
  30. type TextGobber int
  31. type TextValueGobber string
  32. // The relevant methods
  33. func (g *ByteStruct) GobEncode() ([]byte, error) {
  34. b := make([]byte, 3)
  35. b[0] = g.a
  36. b[1] = g.a + 1
  37. b[2] = g.a + 2
  38. return b, nil
  39. }
  40. func (g *ByteStruct) GobDecode(data []byte) error {
  41. if g == nil {
  42. return errors.New("NIL RECEIVER")
  43. }
  44. // Expect N sequential-valued bytes.
  45. if len(data) == 0 {
  46. return io.EOF
  47. }
  48. g.a = data[0]
  49. for i, c := range data {
  50. if c != g.a+byte(i) {
  51. return errors.New("invalid data sequence")
  52. }
  53. }
  54. return nil
  55. }
  56. func (g *StringStruct) GobEncode() ([]byte, error) {
  57. return []byte(g.s), nil
  58. }
  59. func (g *StringStruct) GobDecode(data []byte) error {
  60. // Expect N sequential-valued bytes.
  61. if len(data) == 0 {
  62. return io.EOF
  63. }
  64. a := data[0]
  65. for i, c := range data {
  66. if c != a+byte(i) {
  67. return errors.New("invalid data sequence")
  68. }
  69. }
  70. g.s = string(data)
  71. return nil
  72. }
  73. func (a *ArrayStruct) GobEncode() ([]byte, error) {
  74. return a.a[:], nil
  75. }
  76. func (a *ArrayStruct) GobDecode(data []byte) error {
  77. if len(data) != len(a.a) {
  78. return errors.New("wrong length in array decode")
  79. }
  80. copy(a.a[:], data)
  81. return nil
  82. }
  83. func (g *Gobber) GobEncode() ([]byte, error) {
  84. return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
  85. }
  86. func (g *Gobber) GobDecode(data []byte) error {
  87. _, err := fmt.Sscanf(string(data), "VALUE=%d", (*int)(g))
  88. return err
  89. }
  90. func (g *BinaryGobber) MarshalBinary() ([]byte, error) {
  91. return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
  92. }
  93. func (g *BinaryGobber) UnmarshalBinary(data []byte) error {
  94. _, err := fmt.Sscanf(string(data), "VALUE=%d", (*int)(g))
  95. return err
  96. }
  97. func (g *TextGobber) MarshalText() ([]byte, error) {
  98. return []byte(fmt.Sprintf("VALUE=%d", *g)), nil
  99. }
  100. func (g *TextGobber) UnmarshalText(data []byte) error {
  101. _, err := fmt.Sscanf(string(data), "VALUE=%d", (*int)(g))
  102. return err
  103. }
  104. func (v ValueGobber) GobEncode() ([]byte, error) {
  105. return []byte(fmt.Sprintf("VALUE=%s", v)), nil
  106. }
  107. func (v *ValueGobber) GobDecode(data []byte) error {
  108. _, err := fmt.Sscanf(string(data), "VALUE=%s", (*string)(v))
  109. return err
  110. }
  111. func (v BinaryValueGobber) MarshalBinary() ([]byte, error) {
  112. return []byte(fmt.Sprintf("VALUE=%s", v)), nil
  113. }
  114. func (v *BinaryValueGobber) UnmarshalBinary(data []byte) error {
  115. _, err := fmt.Sscanf(string(data), "VALUE=%s", (*string)(v))
  116. return err
  117. }
  118. func (v TextValueGobber) MarshalText() ([]byte, error) {
  119. return []byte(fmt.Sprintf("VALUE=%s", v)), nil
  120. }
  121. func (v *TextValueGobber) UnmarshalText(data []byte) error {
  122. _, err := fmt.Sscanf(string(data), "VALUE=%s", (*string)(v))
  123. return err
  124. }
  125. // Structs that include GobEncodable fields.
  126. type GobTest0 struct {
  127. X int // guarantee we have something in common with GobTest*
  128. G *ByteStruct
  129. }
  130. type GobTest1 struct {
  131. X int // guarantee we have something in common with GobTest*
  132. G *StringStruct
  133. }
  134. type GobTest2 struct {
  135. X int // guarantee we have something in common with GobTest*
  136. G string // not a GobEncoder - should give us errors
  137. }
  138. type GobTest3 struct {
  139. X int // guarantee we have something in common with GobTest*
  140. G *Gobber
  141. B *BinaryGobber
  142. T *TextGobber
  143. }
  144. type GobTest4 struct {
  145. X int // guarantee we have something in common with GobTest*
  146. V ValueGobber
  147. BV BinaryValueGobber
  148. TV TextValueGobber
  149. }
  150. type GobTest5 struct {
  151. X int // guarantee we have something in common with GobTest*
  152. V *ValueGobber
  153. BV *BinaryValueGobber
  154. TV *TextValueGobber
  155. }
  156. type GobTest6 struct {
  157. X int // guarantee we have something in common with GobTest*
  158. V ValueGobber
  159. W *ValueGobber
  160. BV BinaryValueGobber
  161. BW *BinaryValueGobber
  162. TV TextValueGobber
  163. TW *TextValueGobber
  164. }
  165. type GobTest7 struct {
  166. X int // guarantee we have something in common with GobTest*
  167. V *ValueGobber
  168. W ValueGobber
  169. BV *BinaryValueGobber
  170. BW BinaryValueGobber
  171. TV *TextValueGobber
  172. TW TextValueGobber
  173. }
  174. type GobTestIgnoreEncoder struct {
  175. X int // guarantee we have something in common with GobTest*
  176. }
  177. type GobTestValueEncDec struct {
  178. X int // guarantee we have something in common with GobTest*
  179. G StringStruct // not a pointer.
  180. }
  181. type GobTestIndirectEncDec struct {
  182. X int // guarantee we have something in common with GobTest*
  183. G ***StringStruct // indirections to the receiver.
  184. }
  185. type GobTestArrayEncDec struct {
  186. X int // guarantee we have something in common with GobTest*
  187. A ArrayStruct // not a pointer.
  188. }
  189. type GobTestIndirectArrayEncDec struct {
  190. X int // guarantee we have something in common with GobTest*
  191. A ***ArrayStruct // indirections to a large receiver.
  192. }
  193. func TestGobEncoderField(t *testing.T) {
  194. b := new(bytes.Buffer)
  195. // First a field that's a structure.
  196. enc := NewEncoder(b)
  197. err := enc.Encode(GobTest0{17, &ByteStruct{'A'}})
  198. if err != nil {
  199. t.Fatal("encode error:", err)
  200. }
  201. dec := NewDecoder(b)
  202. x := new(GobTest0)
  203. err = dec.Decode(x)
  204. if err != nil {
  205. t.Fatal("decode error:", err)
  206. }
  207. if x.G.a != 'A' {
  208. t.Errorf("expected 'A' got %c", x.G.a)
  209. }
  210. // Now a field that's not a structure.
  211. b.Reset()
  212. gobber := Gobber(23)
  213. bgobber := BinaryGobber(24)
  214. tgobber := TextGobber(25)
  215. err = enc.Encode(GobTest3{17, &gobber, &bgobber, &tgobber})
  216. if err != nil {
  217. t.Fatal("encode error:", err)
  218. }
  219. y := new(GobTest3)
  220. err = dec.Decode(y)
  221. if err != nil {
  222. t.Fatal("decode error:", err)
  223. }
  224. if *y.G != 23 || *y.B != 24 || *y.T != 25 {
  225. t.Errorf("expected '23 got %d", *y.G)
  226. }
  227. }
  228. // Even though the field is a value, we can still take its address
  229. // and should be able to call the methods.
  230. func TestGobEncoderValueField(t *testing.T) {
  231. b := new(bytes.Buffer)
  232. // First a field that's a structure.
  233. enc := NewEncoder(b)
  234. err := enc.Encode(&GobTestValueEncDec{17, StringStruct{"HIJKL"}})
  235. if err != nil {
  236. t.Fatal("encode error:", err)
  237. }
  238. dec := NewDecoder(b)
  239. x := new(GobTestValueEncDec)
  240. err = dec.Decode(x)
  241. if err != nil {
  242. t.Fatal("decode error:", err)
  243. }
  244. if x.G.s != "HIJKL" {
  245. t.Errorf("expected `HIJKL` got %s", x.G.s)
  246. }
  247. }
  248. // GobEncode/Decode should work even if the value is
  249. // more indirect than the receiver.
  250. func TestGobEncoderIndirectField(t *testing.T) {
  251. b := new(bytes.Buffer)
  252. // First a field that's a structure.
  253. enc := NewEncoder(b)
  254. s := &StringStruct{"HIJKL"}
  255. sp := &s
  256. err := enc.Encode(GobTestIndirectEncDec{17, &sp})
  257. if err != nil {
  258. t.Fatal("encode error:", err)
  259. }
  260. dec := NewDecoder(b)
  261. x := new(GobTestIndirectEncDec)
  262. err = dec.Decode(x)
  263. if err != nil {
  264. t.Fatal("decode error:", err)
  265. }
  266. if (***x.G).s != "HIJKL" {
  267. t.Errorf("expected `HIJKL` got %s", (***x.G).s)
  268. }
  269. }
  270. // Test with a large field with methods.
  271. func TestGobEncoderArrayField(t *testing.T) {
  272. b := new(bytes.Buffer)
  273. enc := NewEncoder(b)
  274. var a GobTestArrayEncDec
  275. a.X = 17
  276. for i := range a.A.a {
  277. a.A.a[i] = byte(i)
  278. }
  279. err := enc.Encode(&a)
  280. if err != nil {
  281. t.Fatal("encode error:", err)
  282. }
  283. dec := NewDecoder(b)
  284. x := new(GobTestArrayEncDec)
  285. err = dec.Decode(x)
  286. if err != nil {
  287. t.Fatal("decode error:", err)
  288. }
  289. for i, v := range x.A.a {
  290. if v != byte(i) {
  291. t.Errorf("expected %x got %x", byte(i), v)
  292. break
  293. }
  294. }
  295. }
  296. // Test an indirection to a large field with methods.
  297. func TestGobEncoderIndirectArrayField(t *testing.T) {
  298. b := new(bytes.Buffer)
  299. enc := NewEncoder(b)
  300. var a GobTestIndirectArrayEncDec
  301. a.X = 17
  302. var array ArrayStruct
  303. ap := &array
  304. app := &ap
  305. a.A = &app
  306. for i := range array.a {
  307. array.a[i] = byte(i)
  308. }
  309. err := enc.Encode(a)
  310. if err != nil {
  311. t.Fatal("encode error:", err)
  312. }
  313. dec := NewDecoder(b)
  314. x := new(GobTestIndirectArrayEncDec)
  315. err = dec.Decode(x)
  316. if err != nil {
  317. t.Fatal("decode error:", err)
  318. }
  319. for i, v := range (***x.A).a {
  320. if v != byte(i) {
  321. t.Errorf("expected %x got %x", byte(i), v)
  322. break
  323. }
  324. }
  325. }
  326. // As long as the fields have the same name and implement the
  327. // interface, we can cross-connect them. Not sure it's useful
  328. // and may even be bad but it works and it's hard to prevent
  329. // without exposing the contents of the object, which would
  330. // defeat the purpose.
  331. func TestGobEncoderFieldsOfDifferentType(t *testing.T) {
  332. // first, string in field to byte in field
  333. b := new(bytes.Buffer)
  334. enc := NewEncoder(b)
  335. err := enc.Encode(GobTest1{17, &StringStruct{"ABC"}})
  336. if err != nil {
  337. t.Fatal("encode error:", err)
  338. }
  339. dec := NewDecoder(b)
  340. x := new(GobTest0)
  341. err = dec.Decode(x)
  342. if err != nil {
  343. t.Fatal("decode error:", err)
  344. }
  345. if x.G.a != 'A' {
  346. t.Errorf("expected 'A' got %c", x.G.a)
  347. }
  348. // now the other direction, byte in field to string in field
  349. b.Reset()
  350. err = enc.Encode(GobTest0{17, &ByteStruct{'X'}})
  351. if err != nil {
  352. t.Fatal("encode error:", err)
  353. }
  354. y := new(GobTest1)
  355. err = dec.Decode(y)
  356. if err != nil {
  357. t.Fatal("decode error:", err)
  358. }
  359. if y.G.s != "XYZ" {
  360. t.Fatalf("expected `XYZ` got %q", y.G.s)
  361. }
  362. }
  363. // Test that we can encode a value and decode into a pointer.
  364. func TestGobEncoderValueEncoder(t *testing.T) {
  365. // first, string in field to byte in field
  366. b := new(bytes.Buffer)
  367. enc := NewEncoder(b)
  368. err := enc.Encode(GobTest4{17, ValueGobber("hello"), BinaryValueGobber("Καλημέρα"), TextValueGobber("こんにちは")})
  369. if err != nil {
  370. t.Fatal("encode error:", err)
  371. }
  372. dec := NewDecoder(b)
  373. x := new(GobTest5)
  374. err = dec.Decode(x)
  375. if err != nil {
  376. t.Fatal("decode error:", err)
  377. }
  378. if *x.V != "hello" || *x.BV != "Καλημέρα" || *x.TV != "こんにちは" {
  379. t.Errorf("expected `hello` got %s", *x.V)
  380. }
  381. }
  382. // Test that we can use a value then a pointer type of a GobEncoder
  383. // in the same encoded value. Bug 4647.
  384. func TestGobEncoderValueThenPointer(t *testing.T) {
  385. v := ValueGobber("forty-two")
  386. w := ValueGobber("six-by-nine")
  387. bv := BinaryValueGobber("1nanocentury")
  388. bw := BinaryValueGobber("πseconds")
  389. tv := TextValueGobber("gravitationalacceleration")
  390. tw := TextValueGobber("π²ft/s²")
  391. // this was a bug: encoding a GobEncoder by value before a GobEncoder
  392. // pointer would cause duplicate type definitions to be sent.
  393. b := new(bytes.Buffer)
  394. enc := NewEncoder(b)
  395. if err := enc.Encode(GobTest6{42, v, &w, bv, &bw, tv, &tw}); err != nil {
  396. t.Fatal("encode error:", err)
  397. }
  398. dec := NewDecoder(b)
  399. x := new(GobTest6)
  400. if err := dec.Decode(x); err != nil {
  401. t.Fatal("decode error:", err)
  402. }
  403. if got, want := x.V, v; got != want {
  404. t.Errorf("v = %q, want %q", got, want)
  405. }
  406. if got, want := x.W, w; got == nil {
  407. t.Errorf("w = nil, want %q", want)
  408. } else if *got != want {
  409. t.Errorf("w = %q, want %q", *got, want)
  410. }
  411. if got, want := x.BV, bv; got != want {
  412. t.Errorf("bv = %q, want %q", got, want)
  413. }
  414. if got, want := x.BW, bw; got == nil {
  415. t.Errorf("bw = nil, want %q", want)
  416. } else if *got != want {
  417. t.Errorf("bw = %q, want %q", *got, want)
  418. }
  419. if got, want := x.TV, tv; got != want {
  420. t.Errorf("tv = %q, want %q", got, want)
  421. }
  422. if got, want := x.TW, tw; got == nil {
  423. t.Errorf("tw = nil, want %q", want)
  424. } else if *got != want {
  425. t.Errorf("tw = %q, want %q", *got, want)
  426. }
  427. }
  428. // Test that we can use a pointer then a value type of a GobEncoder
  429. // in the same encoded value.
  430. func TestGobEncoderPointerThenValue(t *testing.T) {
  431. v := ValueGobber("forty-two")
  432. w := ValueGobber("six-by-nine")
  433. bv := BinaryValueGobber("1nanocentury")
  434. bw := BinaryValueGobber("πseconds")
  435. tv := TextValueGobber("gravitationalacceleration")
  436. tw := TextValueGobber("π²ft/s²")
  437. b := new(bytes.Buffer)
  438. enc := NewEncoder(b)
  439. if err := enc.Encode(GobTest7{42, &v, w, &bv, bw, &tv, tw}); err != nil {
  440. t.Fatal("encode error:", err)
  441. }
  442. dec := NewDecoder(b)
  443. x := new(GobTest7)
  444. if err := dec.Decode(x); err != nil {
  445. t.Fatal("decode error:", err)
  446. }
  447. if got, want := x.V, v; got == nil {
  448. t.Errorf("v = nil, want %q", want)
  449. } else if *got != want {
  450. t.Errorf("v = %q, want %q", *got, want)
  451. }
  452. if got, want := x.W, w; got != want {
  453. t.Errorf("w = %q, want %q", got, want)
  454. }
  455. if got, want := x.BV, bv; got == nil {
  456. t.Errorf("bv = nil, want %q", want)
  457. } else if *got != want {
  458. t.Errorf("bv = %q, want %q", *got, want)
  459. }
  460. if got, want := x.BW, bw; got != want {
  461. t.Errorf("bw = %q, want %q", got, want)
  462. }
  463. if got, want := x.TV, tv; got == nil {
  464. t.Errorf("tv = nil, want %q", want)
  465. } else if *got != want {
  466. t.Errorf("tv = %q, want %q", *got, want)
  467. }
  468. if got, want := x.TW, tw; got != want {
  469. t.Errorf("tw = %q, want %q", got, want)
  470. }
  471. }
  472. func TestGobEncoderFieldTypeError(t *testing.T) {
  473. // GobEncoder to non-decoder: error
  474. b := new(bytes.Buffer)
  475. enc := NewEncoder(b)
  476. err := enc.Encode(GobTest1{17, &StringStruct{"ABC"}})
  477. if err != nil {
  478. t.Fatal("encode error:", err)
  479. }
  480. dec := NewDecoder(b)
  481. x := &GobTest2{}
  482. err = dec.Decode(x)
  483. if err == nil {
  484. t.Fatal("expected decode error for mismatched fields (encoder to non-decoder)")
  485. }
  486. if !strings.Contains(err.Error(), "type") {
  487. t.Fatal("expected type error; got", err)
  488. }
  489. // Non-encoder to GobDecoder: error
  490. b.Reset()
  491. err = enc.Encode(GobTest2{17, "ABC"})
  492. if err != nil {
  493. t.Fatal("encode error:", err)
  494. }
  495. y := &GobTest1{}
  496. err = dec.Decode(y)
  497. if err == nil {
  498. t.Fatal("expected decode error for mismatched fields (non-encoder to decoder)")
  499. }
  500. if !strings.Contains(err.Error(), "type") {
  501. t.Fatal("expected type error; got", err)
  502. }
  503. }
  504. // Even though ByteStruct is a struct, it's treated as a singleton at the top level.
  505. func TestGobEncoderStructSingleton(t *testing.T) {
  506. b := new(bytes.Buffer)
  507. enc := NewEncoder(b)
  508. err := enc.Encode(&ByteStruct{'A'})
  509. if err != nil {
  510. t.Fatal("encode error:", err)
  511. }
  512. dec := NewDecoder(b)
  513. x := new(ByteStruct)
  514. err = dec.Decode(x)
  515. if err != nil {
  516. t.Fatal("decode error:", err)
  517. }
  518. if x.a != 'A' {
  519. t.Errorf("expected 'A' got %c", x.a)
  520. }
  521. }
  522. func TestGobEncoderNonStructSingleton(t *testing.T) {
  523. b := new(bytes.Buffer)
  524. enc := NewEncoder(b)
  525. var g Gobber = 1234
  526. err := enc.Encode(&g)
  527. if err != nil {
  528. t.Fatal("encode error:", err)
  529. }
  530. dec := NewDecoder(b)
  531. var x Gobber
  532. err = dec.Decode(&x)
  533. if err != nil {
  534. t.Fatal("decode error:", err)
  535. }
  536. if x != 1234 {
  537. t.Errorf("expected 1234 got %d", x)
  538. }
  539. }
  540. func TestGobEncoderIgnoreStructField(t *testing.T) {
  541. b := new(bytes.Buffer)
  542. // First a field that's a structure.
  543. enc := NewEncoder(b)
  544. err := enc.Encode(GobTest0{17, &ByteStruct{'A'}})
  545. if err != nil {
  546. t.Fatal("encode error:", err)
  547. }
  548. dec := NewDecoder(b)
  549. x := new(GobTestIgnoreEncoder)
  550. err = dec.Decode(x)
  551. if err != nil {
  552. t.Fatal("decode error:", err)
  553. }
  554. if x.X != 17 {
  555. t.Errorf("expected 17 got %c", x.X)
  556. }
  557. }
  558. func TestGobEncoderIgnoreNonStructField(t *testing.T) {
  559. b := new(bytes.Buffer)
  560. // First a field that's a structure.
  561. enc := NewEncoder(b)
  562. gobber := Gobber(23)
  563. bgobber := BinaryGobber(24)
  564. tgobber := TextGobber(25)
  565. err := enc.Encode(GobTest3{17, &gobber, &bgobber, &tgobber})
  566. if err != nil {
  567. t.Fatal("encode error:", err)
  568. }
  569. dec := NewDecoder(b)
  570. x := new(GobTestIgnoreEncoder)
  571. err = dec.Decode(x)
  572. if err != nil {
  573. t.Fatal("decode error:", err)
  574. }
  575. if x.X != 17 {
  576. t.Errorf("expected 17 got %c", x.X)
  577. }
  578. }
  579. func TestGobEncoderIgnoreNilEncoder(t *testing.T) {
  580. b := new(bytes.Buffer)
  581. // First a field that's a structure.
  582. enc := NewEncoder(b)
  583. err := enc.Encode(GobTest0{X: 18}) // G is nil
  584. if err != nil {
  585. t.Fatal("encode error:", err)
  586. }
  587. dec := NewDecoder(b)
  588. x := new(GobTest0)
  589. err = dec.Decode(x)
  590. if err != nil {
  591. t.Fatal("decode error:", err)
  592. }
  593. if x.X != 18 {
  594. t.Errorf("expected x.X = 18, got %v", x.X)
  595. }
  596. if x.G != nil {
  597. t.Errorf("expected x.G = nil, got %v", x.G)
  598. }
  599. }
  600. type gobDecoderBug0 struct {
  601. foo, bar string
  602. }
  603. func (br *gobDecoderBug0) String() string {
  604. return br.foo + "-" + br.bar
  605. }
  606. func (br *gobDecoderBug0) GobEncode() ([]byte, error) {
  607. return []byte(br.String()), nil
  608. }
  609. func (br *gobDecoderBug0) GobDecode(b []byte) error {
  610. br.foo = "foo"
  611. br.bar = "bar"
  612. return nil
  613. }
  614. // This was a bug: the receiver has a different indirection level
  615. // than the variable.
  616. func TestGobEncoderExtraIndirect(t *testing.T) {
  617. gdb := &gobDecoderBug0{"foo", "bar"}
  618. buf := new(bytes.Buffer)
  619. e := NewEncoder(buf)
  620. if err := e.Encode(gdb); err != nil {
  621. t.Fatalf("encode: %v", err)
  622. }
  623. d := NewDecoder(buf)
  624. var got *gobDecoderBug0
  625. if err := d.Decode(&got); err != nil {
  626. t.Fatalf("decode: %v", err)
  627. }
  628. if got.foo != gdb.foo || got.bar != gdb.bar {
  629. t.Errorf("got = %q, want %q", got, gdb)
  630. }
  631. }
  632. // Another bug: this caused a crash with the new Go1 Time type.
  633. // We throw in a gob-encoding array, to test another case of isZero,
  634. // and a struct containing a nil interface, to test a third.
  635. type isZeroBug struct {
  636. T time.Time
  637. S string
  638. I int
  639. A isZeroBugArray
  640. F isZeroBugInterface
  641. }
  642. type isZeroBugArray [2]uint8
  643. // Receiver is value, not pointer, to test isZero of array.
  644. func (a isZeroBugArray) GobEncode() (b []byte, e error) {
  645. b = append(b, a[:]...)
  646. return b, nil
  647. }
  648. func (a *isZeroBugArray) GobDecode(data []byte) error {
  649. if len(data) != len(a) {
  650. return io.EOF
  651. }
  652. a[0] = data[0]
  653. a[1] = data[1]
  654. return nil
  655. }
  656. type isZeroBugInterface struct {
  657. I any
  658. }
  659. func (i isZeroBugInterface) GobEncode() (b []byte, e error) {
  660. return []byte{}, nil
  661. }
  662. func (i *isZeroBugInterface) GobDecode(data []byte) error {
  663. return nil
  664. }
  665. func TestGobEncodeIsZero(t *testing.T) {
  666. x := isZeroBug{time.Unix(1e9, 0), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
  667. b := new(bytes.Buffer)
  668. enc := NewEncoder(b)
  669. err := enc.Encode(x)
  670. if err != nil {
  671. t.Fatal("encode:", err)
  672. }
  673. var y isZeroBug
  674. dec := NewDecoder(b)
  675. err = dec.Decode(&y)
  676. if err != nil {
  677. t.Fatal("decode:", err)
  678. }
  679. if x != y {
  680. t.Fatalf("%v != %v", x, y)
  681. }
  682. }
  683. func TestGobEncodePtrError(t *testing.T) {
  684. var err error
  685. b := new(bytes.Buffer)
  686. enc := NewEncoder(b)
  687. err = enc.Encode(&err)
  688. if err != nil {
  689. t.Fatal("encode:", err)
  690. }
  691. dec := NewDecoder(b)
  692. err2 := fmt.Errorf("foo")
  693. err = dec.Decode(&err2)
  694. if err != nil {
  695. t.Fatal("decode:", err)
  696. }
  697. if err2 != nil {
  698. t.Fatalf("expected nil, got %v", err2)
  699. }
  700. }
  701. func TestNetIP(t *testing.T) {
  702. // Encoding of net.IP{1,2,3,4} in Go 1.1.
  703. enc := []byte{0x07, 0x0a, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}
  704. var ip net.IP
  705. err := NewDecoder(bytes.NewReader(enc)).Decode(&ip)
  706. if err != nil {
  707. t.Fatalf("decode: %v", err)
  708. }
  709. if ip.String() != "1.2.3.4" {
  710. t.Errorf("decoded to %v, want 1.2.3.4", ip.String())
  711. }
  712. }