Skip to content

Commit

Permalink
add back support for binary encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Mar 22, 2019
1 parent 8c9004d commit 50f09fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions p2p/net/pnet/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var (
pathPSKv1 = []byte("/key/swarm/psk/1.0.0/")
pathBin = "/bin/"
pathBase16 = "/base16/"
pathBase64 = "/base64/"
)
Expand Down Expand Up @@ -51,6 +52,8 @@ func decodeV1PSK(in io.Reader) (*[32]byte, error) {
decoder = hex.NewDecoder(reader)
case pathBase64:
decoder = base64.NewDecoder(base64.StdEncoding, reader)
case pathBin:
decoder = reader
default:
return nil, fmt.Errorf("unknown encoding: %s", header)
}
Expand Down
28 changes: 28 additions & 0 deletions p2p/net/pnet/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestDecodeBad(t *testing.T) {
testDecodeBad(t, true)
testDecodeBad(t, false)
}

func testDecodeBad(t *testing.T, windows bool) {
b := bufWithBase("/verybadbase/", windows)
b.WriteString("Have fun decoding that key")
Expand Down Expand Up @@ -93,3 +94,30 @@ func testDecodeB64(t *testing.T, windows bool) {
}

}

func TestDecodeBin(t *testing.T) {
testDecodeBin(t, true)
testDecodeBin(t, false)
}

func testDecodeBin(t *testing.T, windows bool) {
b := bufWithBase("/bin/", windows)
key := make([]byte, 32)
for i := 0; i < 32; i++ {
key[i] = byte(i)
}

b.Write(key)

psk, err := decodeV1PSK(b)
if err != nil {
t.Fatal(err)
}

for i, b := range psk {
if b != psk[i] {
t.Fatal("byte was wrong")
}
}

}

0 comments on commit 50f09fa

Please sign in to comment.