forked from QUIC-Tracker/quic-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streams_test.go
172 lines (142 loc) · 3.42 KB
/
streams_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package quictracker
import (
"testing"
"bytes"
"github.com/davecgh/go-spew/spew"
)
func TestByteIntervalList_Add(t *testing.T) {
l := NewbyteIntervalList()
l.Add(byteInterval{1, 2})
l.Add(byteInterval{4, 5})
if ok, a, e := iterEquals(l, []byteInterval{{1, 2}, {4, 5}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l.Add(byteInterval{2, 4})
if ok, a, e := iterEquals(l, []byteInterval{{1, 5}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l.Add(byteInterval{6, 8})
if ok, a, e := iterEquals(l, []byteInterval{{1, 5}, {6, 8}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
}
func TestByteIntervalList_Fill(t *testing.T) {
l := NewbyteIntervalList()
l.Add(byteInterval{1, 2})
l.Add(byteInterval{4, 6})
l.Fill(byteInterval{4,5})
if ok, a, e := iterEquals(l, []byteInterval{{1, 2}, {5, 6}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l.Add(byteInterval{2, 5})
if ok, a, e := iterEquals(l, []byteInterval{{1, 6}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l.Fill(byteInterval{3,4})
if ok, a, e := iterEquals(l, []byteInterval{{1, 2}, {5, 6}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l.Fill(byteInterval{1,2})
if ok, a, e := iterEquals(l, []byteInterval{{5, 6}}); !ok {
l.Println()
if a != e {
t.Error("Expected ", e, "got ", a)
} else {
t.Error("Lengthes differ")
}
}
l = NewbyteIntervalList()
}
func TestStreamAddToRead (t *testing.T) {
s := NewStream()
readChan := make(chan interface{}, 10)
s.ReadChan.Register(readChan)
s.addToRead(&StreamFrame{Offset: 4, Length: 4, StreamData:[]byte{4, 5, 6, 7}})
select {
case _ = <- readChan:
t.Error("Should not return data")
default:
}
s.addToRead(&StreamFrame{Offset: 0, Length: 4, StreamData: []byte{0, 1, 2, 3}})
var dataRead []byte
read:
for {
select {
case i := <- readChan:
data := i.([]byte)
dataRead = append(dataRead, data...)
default:
break read
}
}
if !bytes.Equal(dataRead, []byte{0, 1, 2, 3, 4, 5, 6, 7}) {
spew.Dump(dataRead)
t.Error("Should be equal")
}
s.addToRead(&StreamFrame{Offset: 12, Length: 4, StreamData: []byte{4, 5, 6, 7}})
s.addToRead(&StreamFrame{Offset: 16, Length: 4, StreamData: []byte{8, 9, 10, 11}, FinBit: true})
if s.ReadClosed {
t.Error("Should not be closed yet")
}
s.addToRead(&StreamFrame{Offset: 8, Length: 4, StreamData: []byte{0, 1, 2, 3}})
if !s.ReadClosed {
t.Error("Should be closed now")
}
read2:
for {
select {
case i := <- readChan:
data := i.([]byte)
dataRead = append(dataRead, data...)
default:
break read2
}
}
if !bytes.Equal(dataRead, []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}) {
spew.Dump(dataRead)
t.Error("Should be equal")
}
}
func iterEquals(l *byteIntervalList, expected []byteInterval) (bool, *byteInterval, *byteInterval) {
i := 0
if l.Len() != len(expected) {
return false, nil, nil
}
for n := l.Front(); n != nil && i < len(expected); n = n.Next() {
if n.Value != expected[i] {
return false, &n.Value, &expected[i]
}
i++
}
return true, nil, nil
}