2024-06-06 04:51:18 +00:00
|
|
|
package packet
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"slices"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
// pack header
|
|
|
|
|
|
|
|
func TestPackHeader(t *testing.T) {
|
|
|
|
header := PacketHeader{
|
|
|
|
Flags: PacketFlags{
|
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: true,
|
|
|
|
},
|
|
|
|
Ack: 10,
|
|
|
|
NumChunks: 0,
|
|
|
|
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
|
|
|
|
}
|
|
|
|
got := header.Pack()
|
|
|
|
want := []byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pack flags
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
func TestPackFlagsUnset(t *testing.T) {
|
|
|
|
flags := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: false,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got := flags.Pack()
|
2024-06-02 02:58:09 +00:00
|
|
|
want := []byte{0b0000}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackFlagsCompressionSet(t *testing.T) {
|
|
|
|
flags := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: false,
|
|
|
|
Compression: true,
|
|
|
|
Resend: false,
|
|
|
|
Control: false,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got := flags.Pack()
|
2024-06-02 02:58:09 +00:00
|
|
|
want := []byte{0b0100}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackFlagsAllSet(t *testing.T) {
|
|
|
|
flags := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: true,
|
|
|
|
Compression: true,
|
|
|
|
Resend: true,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got := flags.Pack()
|
2024-06-02 02:58:09 +00:00
|
|
|
want := []byte{0b1111}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unpack
|
|
|
|
|
|
|
|
func TestUnpackFlagsAllSet(t *testing.T) {
|
|
|
|
got := PacketFlags{}
|
|
|
|
want := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: true,
|
|
|
|
Compression: true,
|
|
|
|
Resend: true,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack([]byte{0b00111100})
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackFlagsControlSet(t *testing.T) {
|
|
|
|
got := PacketFlags{}
|
|
|
|
want := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack([]byte{0b00000100})
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackFlagsUnset(t *testing.T) {
|
|
|
|
got := PacketFlags{}
|
|
|
|
want := PacketFlags{
|
2024-06-06 04:51:18 +00:00
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: false,
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack([]byte{0b00000000})
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// packet header unpack
|
|
|
|
|
|
|
|
func TestUnpackCloseWithReason(t *testing.T) {
|
|
|
|
got := PacketHeader{}
|
|
|
|
want := PacketHeader{
|
2024-06-06 04:51:18 +00:00
|
|
|
Flags: PacketFlags{
|
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
},
|
2024-06-06 04:51:18 +00:00
|
|
|
Ack: 10,
|
|
|
|
NumChunks: 0,
|
|
|
|
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack(slices.Concat([]byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d, 0x04}, []byte("shutdown"), []byte{0x00}))
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackHeaderFlagsControlSet(t *testing.T) {
|
|
|
|
got := PacketHeader{}
|
|
|
|
want := PacketHeader{
|
2024-06-06 04:51:18 +00:00
|
|
|
Flags: PacketFlags{
|
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
},
|
2024-06-06 04:51:18 +00:00
|
|
|
Ack: 0,
|
|
|
|
NumChunks: 0,
|
|
|
|
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack([]byte{0b00000100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackHeaderFlagsAllSet(t *testing.T) {
|
|
|
|
got := PacketHeader{}
|
|
|
|
want := PacketHeader{
|
2024-06-06 04:51:18 +00:00
|
|
|
Flags: PacketFlags{
|
|
|
|
Connless: true,
|
|
|
|
Compression: true,
|
|
|
|
Resend: true,
|
|
|
|
Control: true,
|
2024-06-02 02:58:09 +00:00
|
|
|
},
|
2024-06-06 04:51:18 +00:00
|
|
|
Ack: 0,
|
|
|
|
NumChunks: 0,
|
|
|
|
Token: [4]byte{0xff, 0xff, 0xff, 0xff},
|
2024-06-02 02:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 04:51:18 +00:00
|
|
|
got.Unpack([]byte{0b00111100, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff})
|
2024-06-02 02:58:09 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("got %v, wanted %v", got, want)
|
|
|
|
}
|
|
|
|
}
|