-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_test.go
151 lines (136 loc) · 3.51 KB
/
packet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package utp_go
import (
"errors"
"math"
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)
func (p *PacketHeaderV1) Generate(rand *rand.Rand, size int) reflect.Value {
packetType := PacketType(rand.Intn(5))
extension := byte(rand.Intn(256))
header := &PacketHeaderV1{
PacketType: packetType,
Version: PROTOCOL_VERSION_ONE,
Extension: extension,
ConnectionId: uint16(rand.Intn(math.MaxUint16)),
Timestamp: int64(rand.Intn(math.MaxUint32)),
TimestampDiff: uint32(rand.Intn(math.MaxUint32)),
WndSize: uint32(rand.Intn(math.MaxUint32)),
SeqNum: uint16(rand.Intn(math.MaxUint16)),
AckNum: uint16(rand.Intn(math.MaxUint16)),
}
return reflect.ValueOf(header)
}
func (a *SelectiveAck) Generate(rand *rand.Rand, size int) reflect.Value {
bits := rand.Intn(size)
acked := make([]bool, bits)
for i := 0; i < bits; i++ {
acked[i] = rand.Intn(2) == 1
}
//if len(acked) == 0 {
// var empty [32]bool
// acked = empty[:]
//}
return reflect.ValueOf(NewSelectiveAck(acked))
}
func TestPacketHeaderEncodeDecode(t *testing.T) {
config := &quick.Config{
MaxCount: 1000,
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
checkPacketHeader := func(header *PacketHeaderV1) bool {
encoded := header.EncodeToBytes()
if len(encoded) != 20 {
return false
}
headerFromDecode, err := DecodePacketHeader(encoded)
if err != nil {
return false
}
return reflect.DeepEqual(header, headerFromDecode)
}
if err := quick.Check(checkPacketHeader, config); err != nil {
t.Error(err)
}
}
func TestSelectiveAckEncodeDecode(t *testing.T) {
config := &quick.Config{
MaxCount: 1000,
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
checkSelective := func(ack *SelectiveAck) bool {
encodedLen := ack.EncodedLen()
encoded := ack.Encode()
if len(encoded)%(SELECTIVE_ACK_BITS/8) != 0 {
return false
}
if len(encoded) != encodedLen {
return false
}
ackFromDecode, err := DecodeSelectiveAck(encoded)
if err != nil {
if encodedLen < 4 && errors.Is(err, ErrInsufficientSelectiveAckLen) {
return true
}
t.Logf("expected err to be nil, got %v", err)
return false
}
res := reflect.DeepEqual(ack, ackFromDecode)
if !res {
t.Logf("expected %v, got %v", ack, ackFromDecode)
}
return res
}
if err := quick.Check(checkSelective, config); err != nil {
t.Error(err)
}
}
func TestPacket(t *testing.T) {
config := &quick.Config{
MaxCount: 1000,
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
// Helper function to check property
checkPacket := func(header *PacketHeaderV1, selectiveAck *SelectiveAck, payload []byte) bool {
// Check empty payload
if len(payload) == 0 {
return true
}
// Handle selective ack
if len(selectiveAck.acked) > 0 {
header.Extension = 1
} else {
selectiveAck = nil
header.Extension = 0
}
// Create packet
packetInst := &packet{
Header: header,
Eack: selectiveAck,
Body: payload,
}
// Get encoded length
encodedLen := packetInst.EncodedLen()
// Encode packet
encoded := packetInst.Encode()
// Check length
if len(encoded) != encodedLen {
t.Errorf("encoded length mismatch: got %d, want %d", len(encoded), encodedLen)
return false
}
// Decode packet
decoded, err := DecodePacket(encoded)
if err != nil {
t.Errorf("failed to decode packet: %v", err)
return false
}
// Compare packets
return reflect.DeepEqual(decoded, packetInst)
}
if err := quick.Check(checkPacket, config); err != nil {
t.Error(err)
}
}