binaryserializer_test.go 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package feed
  17. import (
  18. "encoding/json"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. )
  23. // KV mocks a key value store
  24. type KV map[string]string
  25. func (kv KV) Get(key string) string {
  26. return kv[key]
  27. }
  28. func (kv KV) Set(key, value string) {
  29. kv[key] = value
  30. }
  31. func compareByteSliceToExpectedHex(t *testing.T, variableName string, actualValue []byte, expectedHex string) {
  32. if hexutil.Encode(actualValue) != expectedHex {
  33. t.Fatalf("%s: Expected %s to be %s, got %s", t.Name(), variableName, expectedHex, hexutil.Encode(actualValue))
  34. }
  35. }
  36. func testBinarySerializerRecovery(t *testing.T, bin binarySerializer, expectedHex string) {
  37. name := reflect.TypeOf(bin).Elem().Name()
  38. serialized := make([]byte, bin.binaryLength())
  39. if err := bin.binaryPut(serialized); err != nil {
  40. t.Fatalf("%s.binaryPut error when trying to serialize structure: %s", name, err)
  41. }
  42. compareByteSliceToExpectedHex(t, name, serialized, expectedHex)
  43. recovered := reflect.New(reflect.TypeOf(bin).Elem()).Interface().(binarySerializer)
  44. if err := recovered.binaryGet(serialized); err != nil {
  45. t.Fatalf("%s.binaryGet error when trying to deserialize structure: %s", name, err)
  46. }
  47. if !reflect.DeepEqual(bin, recovered) {
  48. t.Fatalf("Expected that the recovered %s equals the marshalled %s", name, name)
  49. }
  50. serializedWrongLength := make([]byte, 1)
  51. copy(serializedWrongLength[:], serialized)
  52. if err := recovered.binaryGet(serializedWrongLength); err == nil {
  53. t.Fatalf("Expected %s.binaryGet to fail since data is too small", name)
  54. }
  55. }
  56. func testBinarySerializerLengthCheck(t *testing.T, bin binarySerializer) {
  57. name := reflect.TypeOf(bin).Elem().Name()
  58. // make a slice that is too small to contain the metadata
  59. serialized := make([]byte, bin.binaryLength()-1)
  60. if err := bin.binaryPut(serialized); err == nil {
  61. t.Fatalf("Expected %s.binaryPut to fail, since target slice is too small", name)
  62. }
  63. }
  64. func testValueSerializer(t *testing.T, v valueSerializer, expected KV) {
  65. name := reflect.TypeOf(v).Elem().Name()
  66. kv := make(KV)
  67. v.AppendValues(kv)
  68. if !reflect.DeepEqual(expected, kv) {
  69. expj, _ := json.Marshal(expected)
  70. gotj, _ := json.Marshal(kv)
  71. t.Fatalf("Expected %s.AppendValues to return %s, got %s", name, string(expj), string(gotj))
  72. }
  73. recovered := reflect.New(reflect.TypeOf(v).Elem()).Interface().(valueSerializer)
  74. err := recovered.FromValues(kv)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if !reflect.DeepEqual(recovered, v) {
  79. t.Fatalf("Expected recovered %s to be the same", name)
  80. }
  81. }