2019-12-25 12:05:54 +00:00
|
|
|
package base58
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-25 14:06:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-25 22:44:14 +00:00
|
|
|
)
|
|
|
|
|
2019-12-25 12:05:54 +00:00
|
|
|
func TestCheckEncodeDecode(t *testing.T) {
|
2019-09-09 10:09:46 +00:00
|
|
|
var b58CsumEncoded = "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9o"
|
|
|
|
var b58CsumDecodedHex = "802bfe58ab6d9fd575bdc3a624e4825dd2b375d64ac033fbc46ea79dbab4f69a3e01"
|
|
|
|
|
|
|
|
b58CsumDecoded, _ := hex.DecodeString(b58CsumDecodedHex)
|
2019-12-25 12:05:54 +00:00
|
|
|
encoded := CheckEncode(b58CsumDecoded)
|
|
|
|
decoded, err := CheckDecode(b58CsumEncoded)
|
2019-09-09 10:09:46 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, encoded, b58CsumEncoded)
|
|
|
|
assert.Equal(t, decoded, b58CsumDecoded)
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 12:05:54 +00:00
|
|
|
func TestCheckDecodeFailures(t *testing.T) {
|
2019-09-09 10:09:46 +00:00
|
|
|
badbase58 := "BASE%*"
|
2019-12-25 12:05:54 +00:00
|
|
|
_, err := CheckDecode(badbase58)
|
2019-09-09 10:09:46 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
shortbase58 := "THqY"
|
2019-12-25 12:05:54 +00:00
|
|
|
_, err = CheckDecode(shortbase58)
|
2019-09-09 10:09:46 +00:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
badcsum := "KxhEDBQyyEFymvfJD96q8stMbJMbZUb6D1PmXqBWZDU2WvbvVs9A"
|
2019-12-25 12:05:54 +00:00
|
|
|
_, err = CheckDecode(badcsum)
|
2019-09-09 10:09:46 +00:00
|
|
|
assert.NotNil(t, err)
|
2019-02-25 22:44:14 +00:00
|
|
|
}
|
2019-12-25 14:06:38 +00:00
|
|
|
|
|
|
|
func TestBase58LeadingZeroes(t *testing.T) {
|
|
|
|
buf := []byte{0, 0, 0, 1}
|
|
|
|
b58 := CheckEncode(buf)
|
|
|
|
dec, err := CheckDecode(b58)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, buf, dec)
|
|
|
|
}
|