From f98609d48d831bc1f2c797d96aefcbe430455be0 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sat, 15 Jan 2022 23:11:56 +1030 Subject: [PATCH] Commit basic test --- secure/secure_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 secure/secure_test.go diff --git a/secure/secure_test.go b/secure/secure_test.go new file mode 100644 index 0000000..78ae67f --- /dev/null +++ b/secure/secure_test.go @@ -0,0 +1,126 @@ +package secure + +import ( + "bytes" + "net" + "testing" + "time" +) + +func TestPacketBasic(t *testing.T) { + // pSrc := PacketStart{ + // OperationType: 0, + // ClientName: "test1", + // ProtocolVersion: "test2", + // AuthToken: "test3", + // } + // pDst := PacketStart{} + + // buf := bytes.Buffer{} + + 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) + } + } + +} + +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) + 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) + } + } + +}