Skip to content

Beef up tests before adding new codec #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test: deps
go test -race -v ./...
go test -count=1 -race -v ./...

export IPFS_API ?= v04x.ipfs.io

Expand Down
74 changes: 57 additions & 17 deletions multibase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,68 @@ func TestDecode(t *testing.T) {
}

func TestRoundTrip(t *testing.T) {
buf := make([]byte, 17)
rand.Read(buf)
buf := make([]byte, 37+16) // sufficiently large prime number of bytes (37) + another 16 to test leading 0s
rand.Read(buf[16:])

baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad}
for base := range EncodingToStr {

for _, base := range baseList {
enc, err := Encode(base, buf)
if err != nil {
t.Fatal(err)
}
// test roundtrip from the full zero-prefixed buffer down to a single byte
for i := 0; i <= len(buf)-1; i++ {

e, out, err := Decode(enc)
if err != nil {
t.Fatal(err)
}
// use a copy to verify we are not overwriting the supplied buffer
newBuf := make([]byte, len(buf)-i)
copy(newBuf, buf[i:])

if e != base {
t.Fatal("got wrong encoding out")
}
enc, err := Encode(base, newBuf)
if err != nil {
t.Fatal(err)
}

e, out, err := Decode(enc)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(buf, out) {
t.Fatal("input wasnt the same as output", buf, out)
if e != base {
t.Fatal("got wrong encoding out")
}

if !bytes.Equal(newBuf, buf[i:]) {
t.Fatal("the provided buffer was modified", buf[i:], out)
}

if !bytes.Equal(buf[i:], out) {
t.Fatal("input wasnt the same as output", buf[i:], out)
}

// When we have 3 leading zeroes, and this is a case-insensitive codec
// semi-randomly swap case in enc and try again
if i == 13 {
name := EncodingToStr[base]
if name[len(name)-5:] == "upper" || Encodings[name+"upper"] > 0 {
caseTamperedEnc := []byte(enc)

for _, j := range []int{3, 5, 8, 13, 21, 23, 29} {
if caseTamperedEnc[j] >= 65 && caseTamperedEnc[j] <= 90 {
caseTamperedEnc[j] += 32
} else if caseTamperedEnc[j] >= 97 && caseTamperedEnc[j] <= 122 {
caseTamperedEnc[j] -= 32
}
}

e, out, err := Decode(string(caseTamperedEnc))
if err != nil {
t.Fatal(err)
}

if e != base {
t.Fatal("got wrong encoding out")
}
if !bytes.Equal(buf[i:], out) {
t.Fatal("input wasnt the same as output", buf[i:], out)
}
}
}
}
}

Expand Down