2016-08-22 17:37:43 -07:00
|
|
|
package multibase
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-27 23:56:23 +01:00
|
|
|
func TestRoundTrip(t *testing.T) {
|
2016-08-22 17:37:43 -07:00
|
|
|
buf := make([]byte, 16)
|
|
|
|
|
rand.Read(buf)
|
|
|
|
|
|
2016-12-21 20:42:49 +01:00
|
|
|
baseList := []Encoding{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
|
|
|
}
|