|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "math/big" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/ethereum/go-ethereum/rlp" |
| 14 | +) |
| 15 | + |
| 16 | +type RLPTest struct { |
| 17 | + In interface{} |
| 18 | + Out string |
| 19 | +} |
| 20 | + |
| 21 | +func RunRLPTest(file string, skip []string) error { |
| 22 | + f, err := os.Open(file) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + defer f.Close() |
| 27 | + return RunRLPTestWithReader(f, skip) |
| 28 | +} |
| 29 | + |
| 30 | +func RunRLPTestWithReader(r io.Reader, skip []string) error { |
| 31 | + var tests map[string]*RLPTest |
| 32 | + if err := readJson(r, &tests); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + for _, s := range skip { |
| 36 | + delete(tests, s) |
| 37 | + } |
| 38 | + for name, test := range tests { |
| 39 | + if err := test.Run(); err != nil { |
| 40 | + return fmt.Errorf("test %q failed: %v", name, err) |
| 41 | + } |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// Run executes the test. |
| 47 | +func (t *RLPTest) Run() error { |
| 48 | + outb, err := hex.DecodeString(t.Out) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("invalid hex in Out") |
| 51 | + } |
| 52 | + if t.In == "VALID" || t.In == "INVALID" { |
| 53 | + return checkDecodeInterface(outb, t.In == "VALID") |
| 54 | + } |
| 55 | + |
| 56 | + // Check whether encoding the value produces the same bytes. |
| 57 | + in := translateJSON(t.In) |
| 58 | + b, err := rlp.EncodeToBytes(in) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("encode failed: %v", err) |
| 61 | + } |
| 62 | + if !bytes.Equal(b, outb) { |
| 63 | + return fmt.Errorf("encode produced %x, want %x", b, outb) |
| 64 | + } |
| 65 | + // Test decoding from a stream. |
| 66 | + s := rlp.NewStream(bytes.NewReader(outb), 0) |
| 67 | + return checkDecodeFromJSON(s, in) |
| 68 | +} |
| 69 | + |
| 70 | +func checkDecodeInterface(b []byte, isValid bool) error { |
| 71 | + err := rlp.DecodeBytes(b, new(interface{})) |
| 72 | + switch { |
| 73 | + case isValid && err != nil: |
| 74 | + return fmt.Errorf("decoding failed: %v", err) |
| 75 | + case !isValid && err == nil: |
| 76 | + return fmt.Errorf("decoding of invalid value succeeded") |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// translateJSON makes test json values encodable with RLP. |
| 82 | +func translateJSON(v interface{}) interface{} { |
| 83 | + switch v := v.(type) { |
| 84 | + case float64: |
| 85 | + return uint64(v) |
| 86 | + case string: |
| 87 | + if len(v) > 0 && v[0] == '#' { // # starts a faux big int. |
| 88 | + big, ok := new(big.Int).SetString(v[1:], 10) |
| 89 | + if !ok { |
| 90 | + panic(fmt.Errorf("bad test: bad big int: %q", v)) |
| 91 | + } |
| 92 | + return big |
| 93 | + } |
| 94 | + return []byte(v) |
| 95 | + case []interface{}: |
| 96 | + new := make([]interface{}, len(v)) |
| 97 | + for i := range v { |
| 98 | + new[i] = translateJSON(v[i]) |
| 99 | + } |
| 100 | + return new |
| 101 | + default: |
| 102 | + panic(fmt.Errorf("can't handle %T", v)) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// checkDecodeFromJSON decodes from s guided by exp. For each JSON |
| 107 | +// value, the value decoded from the RLP stream must match. |
| 108 | +func checkDecodeFromJSON(s *rlp.Stream, exp interface{}) error { |
| 109 | + switch exp := exp.(type) { |
| 110 | + case uint64: |
| 111 | + i, err := s.Uint() |
| 112 | + if err != nil { |
| 113 | + return addStack("Uint", exp, err) |
| 114 | + } |
| 115 | + if i != exp { |
| 116 | + return addStack("Uint", exp, fmt.Errorf("result mismatch: got %d", i)) |
| 117 | + } |
| 118 | + case *big.Int: |
| 119 | + big := new(big.Int) |
| 120 | + if err := s.Decode(&big); err != nil { |
| 121 | + return addStack("Big", exp, err) |
| 122 | + } |
| 123 | + if big.Cmp(exp) != 0 { |
| 124 | + return addStack("Big", exp, fmt.Errorf("result mismatch: got %d", big)) |
| 125 | + } |
| 126 | + case []byte: |
| 127 | + b, err := s.Bytes() |
| 128 | + if err != nil { |
| 129 | + return addStack("Bytes", exp, err) |
| 130 | + } |
| 131 | + if !bytes.Equal(b, exp) { |
| 132 | + return addStack("Bytes", exp, fmt.Errorf("result mismatch: got %x", b)) |
| 133 | + } |
| 134 | + case []interface{}: |
| 135 | + if _, err := s.List(); err != nil { |
| 136 | + return addStack("List", exp, err) |
| 137 | + } |
| 138 | + for i, v := range exp { |
| 139 | + if err := checkDecodeFromJSON(s, v); err != nil { |
| 140 | + return addStack(fmt.Sprintf("[%d]", i), exp, err) |
| 141 | + } |
| 142 | + } |
| 143 | + if err := s.ListEnd(); err != nil { |
| 144 | + return addStack("ListEnd", exp, err) |
| 145 | + } |
| 146 | + default: |
| 147 | + panic(fmt.Errorf("unhandled type: %T", exp)) |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func addStack(op string, val interface{}, err error) error { |
| 153 | + lines := strings.Split(err.Error(), "\n") |
| 154 | + lines = append(lines, fmt.Sprintf("\t%s: %v", op, val)) |
| 155 | + return errors.New(strings.Join(lines, "\n")) |
| 156 | +} |
0 commit comments