netgiv/secure/secure_test.go

174 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-01-15 23:11:56 +10:30
package secure
import (
"bytes"
2022-01-26 10:47:29 +10:30
"encoding/gob"
2022-01-15 23:11:56 +10:30
"net"
"testing"
"time"
)
2022-01-26 10:47:29 +10:30
func TestBasic(t *testing.T) {
2022-01-15 23:11:56 +10:30
srcConn, dstConn := net.Pipe()
srcSecConn := SecureConnection{
Conn: srcConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
dstSecConn := SecureConnection{
Conn: dstConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
testData := [][]byte{
[]byte("HELLOGDIJFDGIOJDFGOIJGFDOIJGFDOI"),
[]byte("Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ"),
}
big := []byte{}
for i := 0; i < 400; i++ {
big = append(big, 0xdd)
}
testData = append(testData, big)
for _, b := range testData {
go func() {
srcSecConn.Write(b)
}()
time.Sleep(time.Second)
out := make([]byte, 16384)
n, err := dstSecConn.Read(out)
if err != nil {
t.Errorf("got error %v", err)
}
if n != len(b) {
t.Errorf("wrong length expected %d got %d", len(b), n)
}
if !bytes.Equal(out[:n], b) {
t.Errorf("%v not equal to %v", out[:n], b)
}
}
2022-01-26 10:47:29 +10:30
}
func TestPacketBasic(t *testing.T) {
// test encoding/decoding of packets over the encrypted wire
srcConn, dstConn := net.Pipe()
srcSecConn := SecureConnection{
Conn: srcConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
dstSecConn := SecureConnection{
Conn: dstConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
enc := gob.NewEncoder(&srcSecConn)
dec := gob.NewDecoder(&dstSecConn)
packet := PacketStartRequest{
OperationType: OperationTypeReceive,
ClientName: "foo",
ProtocolVersion: "1.1",
AuthToken: "abc123",
}
go func() { enc.Encode(packet) }()
recvPacket := PacketStartRequest{}
dec.Decode(&recvPacket)
if recvPacket.OperationType != OperationTypeReceive {
t.Error("bad OperationType")
}
if recvPacket.ClientName != "foo" {
t.Error("bad ClientName")
}
if recvPacket.ClientName != "foo" {
t.Error("bad ClientName")
}
if recvPacket.AuthToken != "abc123" {
t.Error("bad AuthToken")
}
if recvPacket.ProtocolVersion != "1.1" {
t.Error("bad ProtocolVersion")
}
2022-01-15 23:11:56 +10:30
}
func BenchmarkPPS(b *testing.B) {
srcConn, dstConn := net.Pipe()
srcSecConn := SecureConnection{
Conn: srcConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
dstSecConn := SecureConnection{
Conn: dstConn,
SharedKey: &[32]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
},
Buffer: &bytes.Buffer{},
}
testdata := []byte{}
for i := 0; i < 1024; i++ {
testdata = append(testdata, 0xdd)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
srcSecConn.Write(testdata)
}()
out := make([]byte, 16384)
n, err := dstSecConn.Read(out)
2022-01-15 23:11:56 +10:30
if err != nil {
b.Errorf("got error %v", err)
}
if n != len(testdata) {
b.Errorf("wrong length expected %d got %d", len(testdata), n)
}
if !bytes.Equal(out[:n], testdata) {
b.Errorf("%v not equal to %v", out[:n], testdata)
}
}
}