46 lines
887 B
Go
46 lines
887 B
Go
package tests
|
|
|
|
import (
|
|
"bytes"
|
|
"lux/crypto"
|
|
"lux/net"
|
|
"lux/proto"
|
|
"testing"
|
|
)
|
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
|
key, _ := crypto.NewLuxKey(proto.LuxTypeHost)
|
|
packet := net.LuxPacket{
|
|
Target: key.Id,
|
|
Type: 0x1234,
|
|
Buffer: proto.NewLuxBuffer(),
|
|
}
|
|
packet.Buffer.WriteString("hello world! very unaligned")
|
|
|
|
dgram, err := net.EncryptLuxPacket(packet, key, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
new, err := net.DecryptLuxPacket(dgram, key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !bytes.Equal(key.Id.UUID[:], new.Target.UUID[:]) {
|
|
t.Fatal(key.Id)
|
|
t.Fatal(new.Target.UUID)
|
|
t.Fatal("LuxIDs are not equal")
|
|
}
|
|
if new.Type != 0x1234 {
|
|
t.Fatalf("%x new lux packet type instead of 0x1234", new.Type)
|
|
}
|
|
|
|
str, err := new.Buffer.ReadString()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if str != "hello world! very unaligned" {
|
|
t.Fatalf("wrong string: %s", str)
|
|
}
|
|
}
|