2016-08-22 17:37:43 -07:00
|
|
|
package multibase
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-19 14:34:26 +01:00
|
|
|
var sampleBytes = []byte("Decentralize everything!!")
|
|
|
|
|
var encodedSamples = map[Encoding]string{
|
2017-08-28 17:38:04 -04:00
|
|
|
Identity: string(0x00) + "Decentralize everything!!",
|
|
|
|
|
Base16: "f446563656e7472616c697a652065766572797468696e672121",
|
2017-08-28 17:48:25 -04:00
|
|
|
Base16Upper: "F446563656E7472616C697A652065766572797468696E672121",
|
2017-08-28 17:38:04 -04:00
|
|
|
Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb",
|
|
|
|
|
Base32Upper: "BIRSWGZLOORZGC3DJPJSSAZLWMVZHS5DINFXGOIJB",
|
|
|
|
|
Base32pad: "cirswgzloorzgc3djpjssazlwmvzhs5dinfxgoijb",
|
|
|
|
|
Base32padUpper: "CIRSWGZLOORZGC3DJPJSSAZLWMVZHS5DINFXGOIJB",
|
|
|
|
|
Base32hex: "v8him6pbeehp62r39f9ii0pbmclp7it38d5n6e891",
|
|
|
|
|
Base32hexUpper: "V8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E891",
|
|
|
|
|
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e891",
|
|
|
|
|
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E891",
|
|
|
|
|
Base58BTC: "zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe",
|
|
|
|
|
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchIQ==",
|
|
|
|
|
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchIQ==",
|
2016-12-19 14:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string) {
|
|
|
|
|
actual, err := Encode(encoding, bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if actual != expected {
|
|
|
|
|
t.Errorf("encoding failed for %c (%d), expected: %s, got: %s", encoding, encoding, expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testDecode(t *testing.T, expectedEncoding Encoding, expectedBytes []byte, data string) {
|
|
|
|
|
actualEncoding, actualBytes, err := Decode(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if actualEncoding != expectedEncoding {
|
|
|
|
|
t.Errorf("wrong encoding code, expected: %c (%d), got %c (%d)", expectedEncoding, expectedEncoding, actualEncoding, actualEncoding)
|
|
|
|
|
}
|
|
|
|
|
if !bytes.Equal(actualBytes, expectedBytes) {
|
|
|
|
|
t.Errorf("decoding failed for %c (%d), expected: %v, got %v", actualEncoding, actualEncoding, expectedBytes, actualBytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEncode(t *testing.T) {
|
|
|
|
|
for encoding, data := range encodedSamples {
|
|
|
|
|
testEncode(t, encoding, sampleBytes, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
|
|
|
|
for encoding, data := range encodedSamples {
|
|
|
|
|
testDecode(t, encoding, sampleBytes, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
func TestRoundTrip(t *testing.T) {
|
2016-12-19 14:34:26 +01:00
|
|
|
buf := make([]byte, 17)
|
2016-08-22 17:37:43 -07:00
|
|
|
rand.Read(buf)
|
|
|
|
|
|
2016-12-19 14:34:26 +01:00
|
|
|
baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad}
|
2016-08-22 17:37:43 -07:00
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
for _, base := range baseList {
|
|
|
|
|
enc, err := Encode(base, buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2016-08-22 17:37:43 -07:00
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
e, out, err := Decode(enc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if e != base {
|
|
|
|
|
t.Fatal("got wrong encoding out")
|
|
|
|
|
}
|
2016-08-22 17:37:43 -07:00
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
if !bytes.Equal(buf, out) {
|
|
|
|
|
t.Fatal("input wasnt the same as output", buf, out)
|
|
|
|
|
}
|
2016-08-22 17:37:43 -07:00
|
|
|
}
|
2016-10-24 15:31:20 -07:00
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
_, _, err := Decode("")
|
2016-10-24 15:31:20 -07:00
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("shouldnt be able to decode empty string")
|
|
|
|
|
}
|
2016-08-22 17:37:43 -07:00
|
|
|
}
|