83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
package utp
|
|
|
|
import "testing"
|
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
h header
|
|
}{
|
|
{
|
|
name: "syn",
|
|
h: header{
|
|
typ: Syn,
|
|
ver: 1,
|
|
ext: 0,
|
|
connID: 1234,
|
|
timestamp: 12345678,
|
|
timeDiff: 1000,
|
|
wnd: 65535,
|
|
seq: 1,
|
|
ack: 0,
|
|
},
|
|
},
|
|
{
|
|
name: "data",
|
|
h: header{
|
|
typ: Data,
|
|
ver: 1,
|
|
connID: 5678,
|
|
timestamp: 99999999,
|
|
timeDiff: 500,
|
|
wnd: 32768,
|
|
seq: 100,
|
|
ack: 99,
|
|
},
|
|
},
|
|
{
|
|
name: "state",
|
|
h: header{
|
|
typ: State,
|
|
ver: 1,
|
|
connID: 1234,
|
|
wnd: 65535,
|
|
seq: 1,
|
|
ack: 1,
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
buf := make([]byte, headerSize)
|
|
encode(&tt.h, buf)
|
|
got := decode(buf)
|
|
if got != tt.h {
|
|
t.Errorf("got %+v, want %+v", got, tt.h)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSeqLess(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
a, b uint16
|
|
want bool
|
|
}{
|
|
{"1 < 2", 1, 2, true},
|
|
{"2 > 1", 2, 1, false},
|
|
{"wrap 0xFFFF < 0", 0xFFFF, 0, true},
|
|
{"wrap 0 > 0xFFFF", 0, 0xFFFF, false},
|
|
{"half 0x7FFF > 0", 0x7FFF, 0, false},
|
|
{"half 0 < 0x7FFF", 0, 0x7FFF, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := seqLess(tt.a, tt.b)
|
|
if got != tt.want {
|
|
t.Errorf("seqLess(%d, %d) = %v, want %v", tt.a, tt.b, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|