base58_test.go raw
1 package base58_test
2
3 import (
4 "bytes"
5 "encoding/hex"
6 "testing"
7
8 "github.com/p9c/p9/pkg/base58"
9 )
10
11 var stringTests = []struct {
12 in string
13 out string
14 }{
15 {"", ""},
16 {" ", "Z"},
17 {"-", "n"},
18 {"0", "q"},
19 {"1", "r"},
20 {"-1", "4SU"},
21 {"11", "4k8"},
22 {"abc", "ZiCa"},
23 {"1234598760", "3mJr7AoUXx2Wqd"},
24 {"abcdefghijklmnopqrstuvwxyz", "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f"},
25 {"00000000000000000000000000000000000000000000000000000000000000",
26 "3sN2THZeE9Eh9eYrwkvZqNstbHGvrxSAM7gXUXvyFQP8XvQLUqNCS27icwUeDT7ckHm4FUHM2mTVh1vbLmk7y",
27 },
28 }
29 var invalidStringTests = []struct {
30 in string
31 out string
32 }{
33 {"0", ""},
34 {"O", ""},
35 {"I", ""},
36 {"l", ""},
37 {"3mJr0", ""},
38 {"O3yxU", ""},
39 {"3sNI", ""},
40 {"4kl8", ""},
41 {"0OIl", ""},
42 {"!@#$%^&*()-_=+~`", ""},
43 }
44 var hexTests = []struct {
45 in string
46 out string
47 }{
48 {"61", "2g"},
49 {"626262", "a3gV"},
50 {"636363", "aPEr"},
51 {"73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"},
52 {"00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"},
53 {"516b6fcd0f", "ABnLTmg"},
54 {"bf4f89001e670274dd", "3SEo3LWLoPntC"},
55 {"572e4794", "3EFU7m"},
56 {"ecac89cad93923c02321", "EJDM8drfXA6uyA"},
57 {"10c8511e", "Rt5zm"},
58 {"00000000000000000000", "1111111111"},
59 }
60
61 func TestBase58(t *testing.T) {
62 // Encode tests
63 for x, test := range stringTests {
64 tmp := []byte(test.in)
65 if res := base58.Encode(tmp); res != test.out {
66 t.Errorf("Encode test #%d failed: got: %s want: %s",
67 x, res, test.out,
68 )
69 continue
70 }
71 }
72 // Decode tests
73 for x, test := range hexTests {
74 b, e := hex.DecodeString(test.in)
75 if e != nil {
76 t.Errorf("hex.DecodeString failed failed #%d: got: %s", x, test.in)
77 continue
78 }
79 if res := base58.Decode(test.out); !bytes.Equal(res, b) {
80 t.Errorf("Decode test #%d failed: got: %q want: %q",
81 x, res, test.in,
82 )
83 continue
84 }
85 }
86 // Decode with invalid input
87 for x, test := range invalidStringTests {
88 if res := base58.Decode(test.in); string(res) != test.out {
89 t.Errorf("Decode invalidString test #%d failed: got: %q want: %q",
90 x, res, test.out,
91 )
92 continue
93 }
94 }
95 }
96