Debian Codegraph @

util/base58/base58check_test.go

raw
1 // Copyright (c) 2013-2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4
5 package base58_test
6
7 import (
8 "testing"
9
10 "maunium.net/go/mautrix/util/base58"
11 )
12
13 var checkEncodingStringTests = []struct {
14 version byte
15 in string
16 out string
17 }{
18 {20, "", "3MNQE1X"},
19 {20, " ", "B2Kr6dBE"},
20 {20, "-", "B3jv1Aft"},
21 {20, "0", "B482yuaX"},
22 {20, "1", "B4CmeGAC"},
23 {20, "-1", "mM7eUf6kB"},
24 {20, "11", "mP7BMTDVH"},
25 {20, "abc", "4QiVtDjUdeq"},
26 {20, "1234598760", "ZmNb8uQn5zvnUohNCEPP"},
27 {20, "abcdefghijklmnopqrstuvwxyz", "K2RYDcKfupxwXdWhSAxQPCeiULntKm63UXyx5MvEH2"},
28 {20, "00000000000000000000000000000000000000000000000000000000000000", "bi1EWXwJay2udZVxLJozuTb8Meg4W9c6xnmJaRDjg6pri5MBAxb9XwrpQXbtnqEoRV5U2pixnFfwyXC8tRAVC8XxnjK"},
29 }
30
31 func TestBase58Check(t *testing.T) {
32 for x, test := range checkEncodingStringTests {
33 // test encoding
34 if res := base58.CheckEncode([]byte(test.in), test.version); res != test.out {
35 t.Errorf("CheckEncode test #%d failed: got %s, want: %s", x, res, test.out)
36 }
37
38 // test decoding
39 res, version, err := base58.CheckDecode(test.out)
40 switch {
41 case err != nil:
42 t.Errorf("CheckDecode test #%d failed with err: %v", x, err)
43
44 case version != test.version:
45 t.Errorf("CheckDecode test #%d failed: got version: %d want: %d", x, version, test.version)
46
47 case string(res) != test.in:
48 t.Errorf("CheckDecode test #%d failed: got: %s want: %s", x, res, test.in)
49 }
50 }
51
52 // test the two decoding failure cases
53 // case 1: checksum error
54 _, _, err := base58.CheckDecode("3MNQE1Y")
55 if err != base58.ErrChecksum {
56 t.Error("Checkdecode test failed, expected ErrChecksum")
57 }
58 // case 2: invalid formats (string lengths below 5 mean the version byte and/or the checksum
59 // bytes are missing).
60 testString := ""
61 for len := 0; len < 4; len++ {
62 testString += "x"
63 _, _, err = base58.CheckDecode(testString)
64 if err != base58.ErrInvalidFormat {
65 t.Error("Checkdecode test failed, expected ErrInvalidFormat")
66 }
67 }
68
69 }