|
|
@ -2,8 +2,8 @@ package p2p |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"sync" |
|
|
|
"testing" |
|
|
|
|
|
|
|
acm "github.com/tendermint/tendermint/account" |
|
|
@ -30,35 +30,103 @@ func TestSecretConnectionHandshake(t *testing.T) { |
|
|
|
barPubKey := barPrvKey.PubKey().(acm.PubKeyEd25519) |
|
|
|
|
|
|
|
var fooConn, barConn *SecretConnection |
|
|
|
var wg sync.WaitGroup |
|
|
|
wg.Add(2) |
|
|
|
go func() { |
|
|
|
defer wg.Done() |
|
|
|
Parallel(func() { |
|
|
|
var err error |
|
|
|
fooConn, err = MakeSecretConnection(foo, fooPrvKey) |
|
|
|
if err != nil { |
|
|
|
t.Errorf("Failed to establish SecretConnection for foo: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
if !bytes.Equal(fooConn.RemotePubKey(), fooPubKey) { |
|
|
|
t.Errorf("Unexpected fooConn.RemotePubKey. Expected %X, got %X", |
|
|
|
fooPubKey, fooConn.RemotePubKey()) |
|
|
|
if !bytes.Equal(fooConn.RemotePubKey(), barPubKey) { |
|
|
|
t.Errorf("Unexpected fooConn.RemotePubKey. Expected %v, got %v", |
|
|
|
barPubKey, fooConn.RemotePubKey()) |
|
|
|
} |
|
|
|
}() |
|
|
|
go func() { |
|
|
|
defer wg.Done() |
|
|
|
}, func() { |
|
|
|
var err error |
|
|
|
barConn, err = MakeSecretConnection(bar, barPrvKey) |
|
|
|
if barConn == nil { |
|
|
|
t.Errorf("Failed to establish SecretConnection for bar: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
if !bytes.Equal(barConn.RemotePubKey(), barPubKey) { |
|
|
|
t.Errorf("Unexpected barConn.RemotePubKey. Expected %X, got %X", |
|
|
|
barPubKey, barConn.RemotePubKey()) |
|
|
|
if !bytes.Equal(barConn.RemotePubKey(), fooPubKey) { |
|
|
|
t.Errorf("Unexpected barConn.RemotePubKey. Expected %v, got %v", |
|
|
|
fooPubKey, barConn.RemotePubKey()) |
|
|
|
} |
|
|
|
}() |
|
|
|
wg.Wait() |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func TestSecretConnectionReadWrite(t *testing.T) { |
|
|
|
foo, bar := makeReadWriterPair() |
|
|
|
fooPrvKey := acm.PrivKeyEd25519(CRandBytes(32)) |
|
|
|
barPrvKey := acm.PrivKeyEd25519(CRandBytes(32)) |
|
|
|
fooWrites, barWrites := []string{}, []string{} |
|
|
|
fooReads, barReads := []string{}, []string{} |
|
|
|
|
|
|
|
for i := 0; i < 2; i++ { |
|
|
|
fooWrites = append(fooWrites, RandStr((RandInt()%(dataMaxSize*5))+1)) |
|
|
|
barWrites = append(barWrites, RandStr((RandInt()%(dataMaxSize*5))+1)) |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println("fooWrotes", fooWrites, "\n") |
|
|
|
fmt.Println("barWrotes", barWrites, "\n") |
|
|
|
|
|
|
|
var fooConn, barConn *SecretConnection |
|
|
|
Parallel(func() { |
|
|
|
var err error |
|
|
|
fooConn, err = MakeSecretConnection(foo, fooPrvKey) |
|
|
|
if err != nil { |
|
|
|
t.Errorf("Failed to establish SecretConnection for foo: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
Parallel(func() { |
|
|
|
for _, fooWrite := range fooWrites { |
|
|
|
fmt.Println("will write foo") |
|
|
|
n, err := fooConn.Write([]byte(fooWrite)) |
|
|
|
if err != nil { |
|
|
|
t.Errorf("Failed to write to fooConn: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
if n != len(fooWrite) { |
|
|
|
t.Errorf("Failed to write all bytes. Expected %v, wrote %v", len(fooWrite), n) |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Println("Done writing foo") |
|
|
|
// TODO close foo
|
|
|
|
}, func() { |
|
|
|
fmt.Println("TODO do foo reads") |
|
|
|
}) |
|
|
|
}, func() { |
|
|
|
var err error |
|
|
|
barConn, err = MakeSecretConnection(bar, barPrvKey) |
|
|
|
if err != nil { |
|
|
|
t.Errorf("Failed to establish SecretConnection for bar: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
Parallel(func() { |
|
|
|
readBuffer := make([]byte, dataMaxSize) |
|
|
|
for { |
|
|
|
fmt.Println("will read bar") |
|
|
|
n, err := barConn.Read(readBuffer) |
|
|
|
if err == io.EOF { |
|
|
|
return |
|
|
|
} else if err != nil { |
|
|
|
t.Errorf("Failed to read from barConn: %v", err) |
|
|
|
return |
|
|
|
} |
|
|
|
barReads = append(barReads, string(readBuffer[:n])) |
|
|
|
} |
|
|
|
// XXX This does not get called
|
|
|
|
fmt.Println("Done reading bar") |
|
|
|
}, func() { |
|
|
|
fmt.Println("TODO do bar writes") |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
fmt.Println("fooWrites", fooWrites) |
|
|
|
fmt.Println("barReads", barReads) |
|
|
|
fmt.Println("barWrites", barWrites) |
|
|
|
fmt.Println("fooReads", fooReads) |
|
|
|
} |
|
|
|
|
|
|
|
func BenchmarkSecretConnection(b *testing.B) { |
|
|
|