Skip to content

Commit

Permalink
Merge pull request #19 from libp2p/feat/refactor
Browse files Browse the repository at this point in the history
wrap net conns, not transport conns
  • Loading branch information
Stebalien authored Jun 6, 2018
2 parents 11f3d9b + 34c0789 commit 8684d2b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
4 changes: 2 additions & 2 deletions p2p/net/pnet/protector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pnet
import (
"fmt"
"io"
"net"

ipnet "github.com/libp2p/go-libp2p-interface-pnet"
tconn "github.com/libp2p/go-libp2p-transport"
)

var _ ipnet.Protector = (*protector)(nil)
Expand Down Expand Up @@ -33,7 +33,7 @@ type protector struct {
fingerprint []byte
}

func (p protector) Protect(in tconn.Conn) (tconn.Conn, error) {
func (p protector) Protect(in net.Conn) (net.Conn, error) {
return newPSKConn(p.psk, in)
}
func (p protector) Fingerprint() []byte {
Expand Down
8 changes: 4 additions & 4 deletions p2p/net/pnet/psk_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"crypto/cipher"
"crypto/rand"
"io"
"net"

salsa20 "github.com/davidlazar/go-crypto/salsa20"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
tconn "github.com/libp2p/go-libp2p-transport"
mpool "github.com/libp2p/go-msgio/mpool"
)

Expand All @@ -20,7 +20,7 @@ var (
)

type pskConn struct {
tconn.Conn
net.Conn
psk *[32]byte

writeS20 cipher.Stream
Expand Down Expand Up @@ -73,9 +73,9 @@ func (c *pskConn) Write(in []byte) (int, error) {
return c.Conn.Write(out) // send
}

var _ tconn.Conn = (*pskConn)(nil)
var _ net.Conn = (*pskConn)(nil)

func newPSKConn(psk *[32]byte, insecure tconn.Conn) (tconn.Conn, error) {
func newPSKConn(psk *[32]byte, insecure net.Conn) (net.Conn, error) {
if insecure == nil {
return nil, errInsecureNil
}
Expand Down
36 changes: 22 additions & 14 deletions p2p/net/pnet/psk_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import (
"bytes"
"context"
"math/rand"
"net"
"testing"

dconn "github.com/libp2p/go-libp2p-dummy-conn"
tconn "github.com/libp2p/go-libp2p-transport"
)

var testPSK = [32]byte{} // null bytes are as good test key as any other key

func setupPSKConns(ctx context.Context, t *testing.T) (tconn.Conn, tconn.Conn) {
conn1, conn2, err := dconn.NewDummyConnPair()
if err != nil {
t.Fatal(err)
}
func setupPSKConns(ctx context.Context, t *testing.T) (net.Conn, net.Conn) {
conn1, conn2 := net.Pipe()

psk1, err := newPSKConn(&testPSK, conn1)
if err != nil {
Expand All @@ -37,14 +32,21 @@ func TestPSKSimpelMessges(t *testing.T) {
msg1 := []byte("hello world")
out1 := make([]byte, len(msg1))

_, err := psk1.Write(msg1)
wch := make(chan error)
go func() {
_, err := psk1.Write(msg1)
wch <- err
}()
n, err := psk2.Read(out1)
if err != nil {
t.Fatal(err)
}
n, err := psk2.Read(out1)

err = <-wch
if err != nil {
t.Fatal(err)
}

if n != len(out1) {
t.Fatalf("expected to read %d bytes, read: %d", len(out1), n)
}
Expand All @@ -68,10 +70,11 @@ func TestPSKFragmentation(t *testing.T) {

out := make([]byte, 100)

_, err = psk1.Write(in)
if err != nil {
t.Fatal(err)
}
wch := make(chan error)
go func() {
_, err := psk1.Write(in)
wch <- err
}()

for i := 0; i < 10; i++ {
_, err = psk2.Read(out)
Expand All @@ -80,4 +83,9 @@ func TestPSKFragmentation(t *testing.T) {
}
in = in[100:]
}

err = <-wch
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 8684d2b

Please sign in to comment.