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