101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
package tests
|
|
|
|
import (
|
|
"bytes"
|
|
"lux/proto"
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
func TestWriteGrow(t *testing.T) {
|
|
first, second := []byte{1, 2, 3, 4}, []byte{5, 6, 7, 8}
|
|
must := []byte{1, 2, 3, 4, 5, 6, 7, 8}
|
|
|
|
buf := proto.AllocLuxBuffer(4)
|
|
buf.WriteBytes(first)
|
|
t.Log(buf.AllBytes())
|
|
|
|
buf.WriteBytes(second)
|
|
t.Log(buf.AllBytes())
|
|
|
|
if !bytes.Equal(must, buf.AllBytes()) {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestInts(t *testing.T) {
|
|
wd := proto.AllocLuxBuffer(1)
|
|
wd.WriteUint16(1234)
|
|
wd.WriteUint16(4321)
|
|
wd.WriteUint64(69)
|
|
wd.WriteUint32(2967279234)
|
|
|
|
if rd, _ := wd.ReadUint16(); rd != 1234 {
|
|
t.Fatal(rd)
|
|
}
|
|
if rd, _ := wd.ReadUint16(); rd != 4321 {
|
|
t.Fatal(rd)
|
|
}
|
|
if rd, _ := wd.ReadUint64(); rd != 69 {
|
|
t.Fatal(rd)
|
|
}
|
|
if rd, _ := wd.ReadUint32(); rd != 2967279234 {
|
|
t.Fatal(rd)
|
|
}
|
|
}
|
|
|
|
func TestStrings(t *testing.T) {
|
|
wd := proto.AllocLuxBuffer(1)
|
|
wd.WriteString("a") // 4
|
|
wd.WriteString("hello") // 8
|
|
|
|
if rd, _ := wd.ReadString(); rd != "a" {
|
|
t.Fatal(rd)
|
|
}
|
|
if rd, _ := wd.ReadString(); rd != "hello" {
|
|
t.Fatal(rd)
|
|
}
|
|
if wd.Length() != 12 {
|
|
t.Fatalf("string misaligned size: %d\n", wd.Length())
|
|
}
|
|
}
|
|
|
|
func TestReadOverrun(t *testing.T) {
|
|
rd := proto.FromSlice(make([]byte, 2))
|
|
if _, err := rd.ReadNext(3); err == nil {
|
|
t.Fatalf("no error when rd.ReadNext(3) for %d\n", rd.Length())
|
|
}
|
|
|
|
rd = proto.FromSlice(make([]byte, 1))
|
|
rd.ReadNext(1)
|
|
if _, err := rd.ReadNext(1); err == nil {
|
|
t.Fatalf("no error when reading at offset 1 of 1 byte buffer\n")
|
|
}
|
|
}
|
|
|
|
func TestIP(t *testing.T) {
|
|
ip4 := net.ParseIP("10.1.0.5")
|
|
ip6 := net.ParseIP("fd10:101::d47d:65ff:fe31:9367")
|
|
|
|
wd := proto.NewLuxBuffer()
|
|
wd.WriteIP(proto.LuxProtoIPToAddr(ip4))
|
|
wd.WriteIP(proto.LuxProtoIPToAddr(ip6))
|
|
|
|
rd := proto.FromSlice(wd.AllBytes())
|
|
|
|
newIp4, err := rd.ReadIP()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.LuxProtoAddrToIP(newIp4).Equal(ip4) {
|
|
t.Fatalf("IPv4 does not equal: %v and %v", ip4.To4(), newIp4.AsSlice())
|
|
}
|
|
|
|
newIp6, err := rd.ReadIP()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.LuxProtoAddrToIP(newIp6).Equal(ip6) {
|
|
t.Fatalf("IPv6 does not equal: %v and %v", ip4.To4(), newIp4.AsSlice())
|
|
}
|
|
}
|