Skip to content

Commit

Permalink
Merge pull request #26 from libp2p/feat/no-multicodec
Browse files Browse the repository at this point in the history
remove dependency on go-multicodec
  • Loading branch information
Kubuxu authored Mar 22, 2019
2 parents 03d3fdb + 50f09fa commit 410c629
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 18 deletions.
61 changes: 46 additions & 15 deletions p2p/net/pnet/codec.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,66 @@
package pnet

import (
"bufio"
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"io"

mc "github.com/multiformats/go-multicodec"
bmux "github.com/multiformats/go-multicodec/base/mux"
)

var (
pathPSKv1 = []byte("/key/swarm/psk/1.0.0/")
headerPSKv1 = mc.Header(pathPSKv1)
pathPSKv1 = []byte("/key/swarm/psk/1.0.0/")
pathBin = "/bin/"
pathBase16 = "/base16/"
pathBase64 = "/base64/"
)

func decodeV1PSK(in io.Reader) (*[32]byte, error) {
var err error
in, err = mc.WrapTransformPathToHeader(in)
func readHeader(r *bufio.Reader) ([]byte, error) {
header, err := r.ReadBytes('\n')
if err != nil {
return nil, err
}
err = mc.ConsumeHeader(in, headerPSKv1)

return bytes.TrimRight(header, "\r\n"), nil
}

func expectHeader(r *bufio.Reader, expected []byte) error {
header, err := readHeader(r)
if err != nil {
return nil, fmt.Errorf("psk header error: %s", err.Error())
return err
}
if !bytes.Equal(header, expected) {
return fmt.Errorf("expected file header %s, got: %s", pathPSKv1, header)
}
return nil
}

in, err = mc.WrapTransformPathToHeader(in)
func decodeV1PSK(in io.Reader) (*[32]byte, error) {
reader := bufio.NewReader(in)
if err := expectHeader(reader, pathPSKv1); err != nil {
return nil, err
}
header, err := readHeader(reader)
if err != nil {
return nil, fmt.Errorf("wrapping error: %s", err.Error())
return nil, err
}
out := [32]byte{}

err = bmux.AllBasesMux().Decoder(in).Decode(out[:])
return &out, err
var decoder io.Reader
switch string(header) {
case pathBase16:
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)
}
out := new([32]byte)
_, err = io.ReadFull(decoder, out[:])
if err != nil {
return nil, err
}
return out, nil
}
68 changes: 65 additions & 3 deletions p2p/net/pnet/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,44 @@ import (
"testing"
)

func bufWithBase(base string) *bytes.Buffer {
func bufWithBase(base string, windows bool) *bytes.Buffer {

b := &bytes.Buffer{}
b.Write(pathPSKv1)
if windows {
b.WriteString("\r")
}
b.WriteString("\n")
b.WriteString(base)
if windows {
b.WriteString("\r")
}
b.WriteString("\n")
return b
}

func TestDecodeHex(t *testing.T) {
b := bufWithBase("/base16/")
testDecodeHex(t, true)
testDecodeHex(t, false)
}

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")

_, err := decodeV1PSK(b)
if err == nil {
t.Fatal("expected 'unknown encoding' got nil")
}
}

func testDecodeHex(t *testing.T, windows bool) {
b := bufWithBase("/base16/", windows)
for i := 0; i < 32; i++ {
b.WriteString("FF")
}
Expand All @@ -35,7 +61,12 @@ func TestDecodeHex(t *testing.T) {
}

func TestDecodeB64(t *testing.T) {
b := bufWithBase("/base64/")
testDecodeB64(t, true)
testDecodeB64(t, false)
}

func testDecodeB64(t *testing.T, windows bool) {
b := bufWithBase("/base64/", windows)
key := make([]byte, 32)
for i := 0; i < 32; i++ {
key[i] = byte(i)
Expand All @@ -46,6 +77,37 @@ func TestDecodeB64(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = e.Close()
if err != nil {
t.Fatal(err)
}

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

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

}

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 {
Expand Down

0 comments on commit 410c629

Please sign in to comment.