forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockwise_test.go
236 lines (201 loc) · 7.81 KB
/
blockwise_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package coap
import (
"bytes"
"fmt"
"log"
"testing"
)
func testMarshal(t *testing.T, szx BlockWiseSzx, blockNumber uint, moreBlocksFollowing bool, expectedBlock uint32) {
fmt.Printf("testMarshal szx=%v, num=%v more=%v\n", szx, blockNumber, moreBlocksFollowing)
block, err := MarshalBlockOption(szx, blockNumber, moreBlocksFollowing)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if block != expectedBlock {
t.Fatalf("unexpected value of block %v, expected %v", block, expectedBlock)
}
}
func testUnmarshal(t *testing.T, block uint32, expectedSzx BlockWiseSzx, expectedNum uint, expectedMoreBlocksFollowing bool) {
fmt.Printf("testUnmarshal %v\n", block)
szx, num, more, err := UnmarshalBlockOption(block)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if szx != expectedSzx {
t.Fatalf("unexpected szx of block %v, expected %v", szx, expectedSzx)
}
if num != expectedNum {
t.Fatalf("unexpected num of block %v, expected %v", num, expectedNum)
}
if more != expectedMoreBlocksFollowing {
t.Fatalf("unexpected more of block %v, expected %v", more, expectedMoreBlocksFollowing)
}
}
func TestBlockWiseBlockMarshal(t *testing.T) {
testMarshal(t, BlockWiseSzx16, 0, false, uint32(0))
testMarshal(t, BlockWiseSzx16, 0, true, uint32(8))
testMarshal(t, BlockWiseSzx32, 0, false, uint32(1))
testMarshal(t, BlockWiseSzx32, 0, true, uint32(9))
testMarshal(t, BlockWiseSzx64, 0, false, uint32(2))
testMarshal(t, BlockWiseSzx64, 0, true, uint32(10))
testMarshal(t, BlockWiseSzx128, 0, false, uint32(3))
testMarshal(t, BlockWiseSzx128, 0, true, uint32(11))
testMarshal(t, BlockWiseSzx256, 0, false, uint32(4))
testMarshal(t, BlockWiseSzx256, 0, true, uint32(12))
testMarshal(t, BlockWiseSzx512, 0, false, uint32(5))
testMarshal(t, BlockWiseSzx512, 0, true, uint32(13))
testMarshal(t, BlockWiseSzx1024, 0, false, uint32(6))
testMarshal(t, BlockWiseSzx1024, 0, true, uint32(14))
testMarshal(t, BlockWiseSzxBERT, 0, false, uint32(7))
testMarshal(t, BlockWiseSzxBERT, 0, true, uint32(15))
val, err := MarshalBlockOption(BlockWiseSzx16, maxBlockNumber+1, false)
if err == nil {
t.Fatalf("expected error, block %v", val)
}
}
func TestBlockWiseBlockUnmarshal(t *testing.T) {
testUnmarshal(t, uint32(0), BlockWiseSzx16, 0, false)
testUnmarshal(t, uint32(8), BlockWiseSzx16, 0, true)
testUnmarshal(t, uint32(1), BlockWiseSzx32, 0, false)
testUnmarshal(t, uint32(9), BlockWiseSzx32, 0, true)
testUnmarshal(t, uint32(2), BlockWiseSzx64, 0, false)
testUnmarshal(t, uint32(10), BlockWiseSzx64, 0, true)
testUnmarshal(t, uint32(3), BlockWiseSzx128, 0, false)
testUnmarshal(t, uint32(11), BlockWiseSzx128, 0, true)
testUnmarshal(t, uint32(4), BlockWiseSzx256, 0, false)
testUnmarshal(t, uint32(12), BlockWiseSzx256, 0, true)
testUnmarshal(t, uint32(5), BlockWiseSzx512, 0, false)
testUnmarshal(t, uint32(13), BlockWiseSzx512, 0, true)
testUnmarshal(t, uint32(6), BlockWiseSzx1024, 0, false)
testUnmarshal(t, uint32(14), BlockWiseSzx1024, 0, true)
testUnmarshal(t, uint32(7), BlockWiseSzxBERT, 0, false)
testUnmarshal(t, uint32(15), BlockWiseSzxBERT, 0, true)
szx, num, m, err := UnmarshalBlockOption(0x1000000)
if err == nil {
t.Fatalf("expected error, szx %v, num %v, m %v", szx, num, m)
}
}
func TestServingUDPBlockWiseSzx16(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx16, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx32(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx32, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx64(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx64, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx128(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx128, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx256(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx256, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx512(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx512, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzx1024(t *testing.T) {
testServingTCPWithMsg(t, "udp", true, BlockWiseSzx1024, make([]byte, 128), simpleMsg)
}
func TestServingUDPBlockWiseSzxBERT(t *testing.T) {
_, addr, _, err := RunLocalUDPServer("udp", ":0", true, BlockWiseSzx1024)
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
BlockWiseTransfer := true
BlockWiseTransferSzx := BlockWiseSzxBERT
c := Client{Net: "udp", BlockWiseTransfer: &BlockWiseTransfer, BlockWiseTransferSzx: &BlockWiseTransferSzx}
_, err = c.Dial(addr)
if err != nil {
if err.Error() != ErrInvalidBlockWiseSzx.Error() {
t.Fatalf("Expected error '%v', got '%v'", err, ErrInvalidBlockWiseSzx)
}
} else {
t.Fatalf("Expected error '%v'", ErrInvalidBlockWiseSzx)
}
}
func TestServingTCPBlockWiseSzx16(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx16, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx32(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx32, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx64(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx64, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx128(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx128, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx256(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx256, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx512(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx512, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzx1024(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx1024, make([]byte, 128), simpleMsg)
}
func TestServingTCPBlockWiseSzxBERT(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzxBERT, make([]byte, 128), simpleMsg)
}
func TestServingTCPBigMsgBlockWiseSzx1024(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzx1024, make([]byte, 1024), simpleMsg)
}
func TestServingTCPBigMsgBlockWiseSzxBERT(t *testing.T) {
testServingTCPWithMsg(t, "tcp", true, BlockWiseSzxBERT, make([]byte, 10*1024*1024), simpleMsg)
}
// EchoServerUsingWrite echoes request payloads using ResponseWriter.Write
func EchoServerUsingWrite(w ResponseWriter, r *Request) {
if r.Msg.IsConfirmable() {
w.SetCode(Content)
w.SetContentFormat(r.Msg.Option(ContentFormat).(MediaType))
_, err := w.Write(r.Msg.Payload())
if err != nil {
log.Printf("Cannot write echo %v", err)
}
}
}
func TestServingUDPBlockWiseUsingWrite(t *testing.T) {
// Test that responding to blockwise requests using ResponseWrite.write
// works correctly (as opposed to using WriteMsg directly)
HandleFunc("/test-with-write", EchoServerUsingWrite)
defer HandleRemove("/test-with-write")
payload := make([]byte, 512)
_, addr, _, err := RunLocalUDPServer("udp", ":0", true, BlockWiseSzx1024)
if err != nil {
t.Fatalf("Unexpected error '%v'", err)
}
BlockWiseTransfer := true
BlockWiseTransferSzx := BlockWiseSzx128
c := &Client{
Net: "udp",
BlockWiseTransfer: &BlockWiseTransfer,
BlockWiseTransferSzx: &BlockWiseTransferSzx,
MaxMessageSize: ^uint32(0),
}
co, err := c.Dial(addr)
if err != nil {
t.Fatal("cannot dial", err)
}
req, err := co.NewPostRequest("/test-with-write", TextPlain, bytes.NewBuffer(payload))
if err != nil {
t.Fatal("cannot create request", err)
}
m, err := co.Exchange(req)
if err != nil {
t.Fatal("failed to exchange", err)
}
if m == nil {
t.Fatalf("Didn't receive CoAP response")
}
expectedMsg := &DgramMessage{
MessageBase{
typ: Acknowledgement,
code: Content,
messageID: req.MessageID(),
payload: req.Payload(),
token: req.Token(),
},
}
expectedMsg.SetOption(ContentFormat, req.Option(ContentFormat))
assertEqualMessages(t, expectedMsg, m)
}