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 "next.orly.dev/pkg/nostr/crypto/ec/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 {
28 20, "abcdefghijklmnopqrstuvwxyz",
29 "K2RYDcKfupxwXdWhSAxQPCeiULntKm63UXyx5MvEH2",
30 },
31 {
32 20, "00000000000000000000000000000000000000000000000000000000000000",
33 "bi1EWXwJay2udZVxLJozuTb8Meg4W9c6xnmJaRDjg6pri5MBAxb9XwrpQXbtnqEoRV5U2pixnFfwyXC8tRAVC8XxnjK",
34 },
35 }
36
37 func TestBase58Check(t *testing.T) {
38 for x, test := range checkEncodingStringTests {
39 // test encoding
40 if res := base58.CheckEncode(
41 []byte(test.in),
42 test.version,
43 ); res != test.out {
44 t.Errorf(
45 "CheckEncode test #%d failed: got %s, want: %s", x, res,
46 test.out,
47 )
48 }
49
50 // test decoding
51 res, version, err := base58.CheckDecode(test.out)
52 switch {
53 case err != nil:
54 t.Errorf("CheckDecode test #%d failed with err: %v", x, err)
55
56 case version != test.version:
57 t.Errorf(
58 "CheckDecode test #%d failed: got version: %d want: %d", x,
59 version, test.version,
60 )
61
62 case string(res) != test.in:
63 t.Errorf(
64 "CheckDecode test #%d failed: got: %s want: %s", x, res,
65 test.in,
66 )
67 }
68 }
69
70 // test the two decoding failure cases
71 // case 1: checksum error
72 _, _, err := base58.CheckDecode("3MNQE1Y")
73 if err != base58.ErrChecksum {
74 t.Error("Checkdecode test failed, expected ErrChecksum")
75 }
76 // case 2: invalid formats (string lengths below 5 mean the version byte and/or the checksum
77 // bytes are missing).
78 testString := ""
79 for len := 0; len < 4; len++ {
80 testString += "x"
81 _, _, err = base58.CheckDecode(testString)
82 if err != base58.ErrInvalidFormat {
83 t.Error("Checkdecode test failed, expected ErrInvalidFormat")
84 }
85 }
86
87 }
88