netgiv/secure/secure_test.go
Jason Fowler a1e3c205f9
add burn operation to the client, server, and protocol (#2)
* add burn operation to the client, server, and protocol

this provides a method for removing files from the server remotely without needing to restart the server
example use case for this is if your server is publicly accessible but you don't expose SSH publicly and you're transferring data between two cloud servers and don't want the data to be stored on the server any longer than it has to be

* updating documentation
2025-04-25 15:51:43 +09:30

179 lines
3.8 KiB
Go

package secure
import (
"bytes"
"encoding/gob"
"net"
"testing"
"time"
)
func TestBasic(t *testing.T) {
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 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")
}
}
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)
}
}
}