base58check_test.go raw
1 package base58_test
2
3 import (
4 "testing"
5
6 "github.com/p9c/p9/pkg/base58"
7 )
8
9 var checkEncodingStringTests = []struct {
10 version byte
11 in string
12 out string
13 }{
14 {20, "", "3MNQE1X"},
15 {20, " ", "B2Kr6dBE"},
16 {20, "-", "B3jv1Aft"},
17 {20, "0", "B482yuaX"},
18 {20, "1", "B4CmeGAC"},
19 {20, "-1", "mM7eUf6kB"},
20 {20, "11", "mP7BMTDVH"},
21 {20, "abc", "4QiVtDjUdeq"},
22 {20, "1234598760", "ZmNb8uQn5zvnUohNCEPP"},
23 {20, "abcdefghijklmnopqrstuvwxyz", "K2RYDcKfupxwXdWhSAxQPCeiULntKm63UXyx5MvEH2"},
24 {20, "00000000000000000000000000000000000000000000000000000000000000",
25 "bi1EWXwJay2udZVxLJozuTb8Meg4W9c6xnmJaRDjg6pri5MBAxb9XwrpQXbtnqEoRV5U2pixnFfwyXC8tRAVC8XxnjK",
26 },
27 }
28
29 func TestBase58Check(t *testing.T) {
30 for x, test := range checkEncodingStringTests {
31 // test encoding
32 if res := base58.CheckEncode([]byte(test.in), test.version); res != test.out {
33 t.Errorf("CheckEncode test #%d failed: got %s, want: %s", x, res, test.out)
34 }
35 // test decoding
36 res, version, e := base58.CheckDecode(test.out)
37 if e != nil {
38 t.Errorf("CheckDecode test #%d failed with e: %v", x, e)
39 } else if version != test.version {
40 t.Errorf("CheckDecode test #%d failed: got version: %d want: %d", x, version, test.version)
41 } else if string(res) != test.in {
42 t.Errorf("CheckDecode test #%d failed: got: %s want: %s", x, res, test.in)
43 }
44 }
45 // test the two decoding failure cases
46 // case 1: checksum error
47 var e error
48 _, _, e = base58.CheckDecode("3MNQE1Y")
49 if e != base58.ErrChecksum {
50 t.Error("Checkdecode test failed, expected ErrChecksum")
51 }
52 // case 2: invalid formats (string lengths below 5 mean the version byte and/or the checksum bytes are missing).
53 testString := ""
54 for length := 0; length < 4; length++ {
55 // make a string of length `len`
56 _, _, e = base58.CheckDecode(testString)
57 if e != base58.ErrInvalidFormat {
58 t.Error("Checkdecode test failed, expected ErrInvalidFormat")
59 }
60 }
61 }
62