~fnux/yggdrasil-go-coap

yggdrasil-go-coap/blockwise_test.go -rw-r--r-- 10.0 KiB
614f652bTimothée Floure Add syntax highlighting to code snippets in README 1 year, 6 months ago
                                                                                
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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)
}

var helloWorld = []byte("Hello world")

// EchoServerUsingWrite echoes request payloads using ResponseWriter.Write
func EchoServerUsingWrite(w ResponseWriter, r *Request) {
	w.SetCode(Content)
	if mt, ok := r.Msg.Option(ContentFormat).(MediaType); ok {
		w.SetContentFormat(mt)
		_, err := w.Write(r.Msg.Payload())
		if err != nil {
			log.Printf("Cannot write echo %v", err)
		}
	} else {
		w.SetContentFormat(TextPlain)
		_, err := w.Write(helloWorld)
		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: MessageBase{
			typ:     Acknowledgement,
			code:    Content,
			payload: req.Payload(),
			token:   req.Token(),
		},
		messageID: req.MessageID(),
	}
	expectedMsg.SetOption(ContentFormat, req.Option(ContentFormat))

	assertEqualMessages(t, expectedMsg, m)
}

func TestServingUDPBlockWiseWithClientWithoutBlockWise(t *testing.T) {
	HandleFunc("/test-with-write", EchoServerUsingWrite)
	defer HandleRemove("/test-with-write")

	payload := make([]byte, 8)

	_, addr, _, err := RunLocalUDPServer("udp", ":0", true, BlockWiseSzx16)
	if err != nil {
		t.Fatalf("Unexpected error '%v'", err)
	}

	BlockWiseTransfer := false
	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: MessageBase{
			typ:     Acknowledgement,
			code:    Content,
			payload: req.Payload(),
			token:   req.Token(),
		},
		messageID: req.MessageID(),
	}

	expectedMsg.SetOption(ContentFormat, TextPlain)
	expectedMsg.SetOption(Block2, uint32(0))
	expectedMsg.SetOption(Size2, uint32(len(req.Payload())))

	assertEqualMessages(t, expectedMsg, m)

	getReq, err := co.NewGetRequest("/test-with-write")
	if err != nil {
		t.Fatal("cannot create request", err)
	}

	getResp, err := co.Exchange(getReq)
	if err != nil {
		t.Fatal("failed to exchange", err)
	}
	expectedGetMsg := DgramMessage{
		MessageBase: MessageBase{
			typ:     Acknowledgement,
			code:    Content,
			payload: helloWorld,
			token:   getReq.Token(),
		},
		messageID: getReq.MessageID(),
	}

	if etag, ok := getResp.Option(ETag).([]byte); ok {
		expectedGetMsg.SetOption(ETag, etag)
	}

	expectedGetMsg.SetOption(ContentFormat, TextPlain)
	expectedGetMsg.SetOption(Block2, uint32(0))
	expectedGetMsg.SetOption(Size2, uint32(len(helloWorld)))

	assertEqualMessages(t, &expectedGetMsg, getResp)
}