-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrlpdump_test.go
81 lines (76 loc) · 2.11 KB
/
rlpdump_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2023 The nufi-go Authors
// This file is part of nufi-go.
//
// nufi-go is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// nufi-go is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with nufi-go. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/thenuchain/nufi-core/common"
"github.com/thenuchain/nufi-core/common/hexutil"
)
func TestRoundtrip(t *testing.T) {
for i, want := range []string{
"0xf880806482520894d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0d0a1010000000000000000000000000000000000000000000000000000000000000001801ba0c16787a8e25e941d67691954642876c08f00996163ae7dfadbbfd6cd436f549da06180e5626cae31590f40641fe8f63734316c4bfeb4cdfab6714198c1044d2e28",
"0xd5c0d3cb84746573742a2a808213378667617a6f6e6b",
"0xc780c0c1c0825208",
} {
var out strings.Builder
err := rlpToText(bytes.NewReader(common.FromHex(want)), &out)
if err != nil {
t.Fatal(err)
}
text := out.String()
rlpBytes, err := textToRlp(strings.NewReader(text))
if err != nil {
t.Errorf("test %d: error %v", i, err)
continue
}
have := fmt.Sprintf("%#x", rlpBytes)
if have != want {
t.Errorf("test %d: have\n%v\nwant:\n%v\n", i, have, want)
}
}
}
func TestTextToRlp(t *testing.T) {
type tc struct {
text string
want string
}
cases := []tc{
{
text: `[
"",
[],
[
[],
],
5208,
]`,
want: "0xc780c0c1c0825208",
},
}
for i, tc := range cases {
have, err := textToRlp(strings.NewReader(tc.text))
if err != nil {
t.Errorf("test %d: error %v", i, err)
continue
}
if hexutil.Encode(have) != tc.want {
t.Errorf("test %d:\nhave %v\nwant %v", i, hexutil.Encode(have), tc.want)
}
}
}