forked from Tanganelli/CoAPthon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcedirectory_test.py
606 lines (536 loc) · 25 KB
/
resourcedirectory_test.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import unittest
import threading
import socket
import re
import random
from time import sleep
from coapthon.resource_directory.resourceDirectory import ResourceDirectory
from coapthon.messages.response import Response
from coapthon.messages.request import Request
from coapthon import defines
from coapthon.serializer import Serializer
from pymongo import MongoClient
from coapthon.client.helperclient import HelperClient
__author__ = 'Carmelo Aparo'
class ResourceDirectoryTest(unittest.TestCase):
def setUp(self):
self.server_address = ("127.0.0.1", 5683)
self.current_mid = random.randint(1, 1000)
self.server = ResourceDirectory("127.0.0.1", 5683, start_mongo=False)
self.server_thread = threading.Thread(target=self.server.listen, args=(10,))
self.server_thread.start()
self.delete_database()
def tearDown(self):
self.server.close()
self.server_thread.join(timeout=25)
self.server = None
@staticmethod
def delete_database():
database = defines.MONGO_DATABASE
connection = MongoClient(defines.MONGO_HOST, defines.MONGO_PORT, username=defines.MONGO_USER,
password=defines.MONGO_PWD, authSource=database, authMechanism='SCRAM-SHA-1')
collection = connection[database].resources
try:
collection.delete_many({})
except:
print("Error in delete_database")
@staticmethod
def parse_core_link_format(link_format):
data = []
while len(link_format) > 0:
pattern = "<([^>]*)>"
result = re.match(pattern, link_format)
path = result.group(1)
link_format = link_format[result.end(1) + 2:]
pattern = "([^<,])*"
result = re.match(pattern, link_format)
attributes = result.group(0)
dict_att = {}
if len(attributes) > 0:
attributes = attributes.split(";")
for att in attributes:
a = att.split("=")
if len(a) > 1:
if a[1].isdigit():
a[1] = int(a[1])
else:
a[1] = a[1].replace('"', '')
dict_att[a[0]] = a[1]
else:
dict_att[a[0]] = a[0]
link_format = link_format[result.end(0) + 1:]
tmp = {'path': path}
dict_att.update(tmp)
data.append(dict_att)
return data
def _test_check(self, message_list, timeout=0):
serializer = Serializer()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for message, expected in message_list:
if message is not None:
datagram = serializer.serialize(message)
sleep(timeout)
sock.sendto(datagram, message.destination)
if expected is not None:
datagram, source = sock.recvfrom(4096)
received_message = serializer.deserialize(datagram, source)
if expected.type is not None:
self.assertEqual(received_message.type, expected.type)
if expected.mid is not None:
self.assertEqual(received_message.mid, expected.mid)
self.assertEqual(received_message.code, expected.code)
if expected.source is not None:
self.assertEqual(received_message.source, source)
if expected.token is not None:
self.assertEqual(received_message.token, expected.token)
if expected.content_type is not None:
self.assertEqual(received_message.content_type, expected.content_type)
if expected.payload is not None:
expected_list = self.parse_core_link_format(expected.payload)
received_list = self.parse_core_link_format(received_message.payload)
self.assertEqual(len(expected_list), len(received_list))
all_list = []
for expected_elem in expected_list:
for received_elem in received_list:
if expected_elem['path'] == received_elem['path']:
all_list_elem = (expected_elem, received_elem)
all_list.append(all_list_elem)
self.assertEqual(len(expected_list), len(all_list))
for data in all_list:
for k in data[1]:
self.assertIn(k, data[0])
if (k != "lt") and (k in data[0]):
self.assertEqual(data[0][k], data[1][k])
else:
self.assertEqual(expected.payload, received_message.payload)
sock.close()
def test_uri_discovery(self):
print("Uri discovery")
path = ".well-known/core"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '</rd-lookup/res>;rt="core.rd-lookup-res";ct=40,</rd>;rt="core.rd";ct=40,' \
'</rd-lookup/ep>;rt="core.rd-lookup-ep";ct=40'
self.current_mid += 1
self._test_check([(req, expected)])
def test_registration(self):
print("Registration")
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = defines.Content_types["application/link-format"]
req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \
'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"'
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CREATED.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_lookup_res(self):
print("Resource lookup")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd-lookup/res?ep=node1&rt=temperature-c"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '<coap://local-proxy-old.example.com:5683/sensors/temp>;ct=41;rt="temperature-c";' \
'if="sensor";anchor="coap://spurious.example.com:5683"'
self.current_mid += 1
self._test_check([(req, expected)])
def test_lookup_ep(self):
print("Endpoint lookup")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683&et=oic.d.sensor"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
loc_path = response.location_path
client.stop()
path = "rd-lookup/ep?et=oic.d.sensor"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '</' + loc_path + '>;con="coap://local-proxy-old.example.com:5683";ep="node1";' \
'et="oic.d.sensor";lt=500'
self.current_mid += 1
self._test_check([(req, expected)])
def test_update(self):
print("Update")
client = HelperClient(self.server_address)
path = "rd?ep=endpoint1<=500&con=coap://local-proxy-old.example.com:5683&et=oic.d.sensor"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
loc_path = response.location_path
client.stop()
path = loc_path + "?con=coaps://new.example.com:5684"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CHANGED.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_read_endpoint_links(self):
print("Read endpoint links")
client = HelperClient(self.server_address)
path = "rd?ep=endpoint1<=500&con=coap://local-proxy-old.example.com:5683&et=oic.d.sensor"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
loc_path = response.location_path
client.stop()
path = loc_path
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '<coap://local-proxy-old.example.com:5683/sensors/temp>;ct=41;rt="temperature-c";' \
'if="sensor";anchor="coap://spurious.example.com:5683",' \
'<coap://local-proxy-old.example.com:5683/sensors/light>;ct=41;rt="light-lux";if="sensor"'
self.current_mid += 1
self._test_check([(req, expected)])
def test_delete(self):
print("Delete")
client = HelperClient(self.server_address)
path = "rd?ep=endpoint1<=500&con=coap://local-proxy-old.example.com:5683&et=oic.d.sensor"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
loc_path = response.location_path
client.stop()
path = loc_path
req = Request()
req.code = defines.Codes.DELETE.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.DELETED.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_lookup_expired_res(self):
print("Expired resource lookup")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd-lookup/res?ep=node1&rt=temperature-c"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)], 61)
def test_lookup_expired_ep(self):
print("Expired endpoint lookup")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd-lookup/ep?ep=node1&rt=temperature-c"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = None
self.current_mid += 1
# After 61 seconds the resource will be expired
self._test_check([(req, expected)], 61)
def test_update_expired(self):
print("Update expired registration resource")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
# After 61 seconds the resource will be expired
sleep(61)
loc_path = response.location_path
client.post(loc_path, None)
client.stop()
path = "rd-lookup/res?ep=node1&rt=temperature-c"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '<coap://local-proxy-old.example.com:5683/sensors/temp>;ct=41;rt="temperature-c";' \
'if="sensor";anchor="coap://spurious.example.com:5683"'
self.current_mid += 1
self._test_check([(req, expected)])
def test_wrong_ep(self):
print("Endpoint name already exists")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683<=60"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = defines.Content_types["application/link-format"]
req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \
'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"'
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.SERVICE_UNAVAILABLE.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_no_ep(self):
print("Registration without endpoint name")
path = "rd?con=coap://local-proxy-old.example.com:5683"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = defines.Content_types["application/link-format"]
req.payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";' \
'anchor="coap://spurious.example.com:5683",</sensors/light>;ct=41;rt="light-lux";if="sensor"'
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.BAD_REQUEST.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_update_res_not_found(self):
print("Resource not found on update")
path = "rd/4521?con=coaps://new.example.com:5684"
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.NOT_FOUND.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_delete_res_not_found(self):
print("Resource not found on delete")
path = "rd/4521"
req = Request()
req.code = defines.Codes.DELETE.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.NOT_FOUND.number
expected.token = None
expected.content_type = 0
expected.payload = None
self.current_mid += 1
self._test_check([(req, expected)])
def test_wildcard_res(self):
print("Use wildcard * to find resources")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
client.post(path, payload, None, None, **ct)
path = "rd?ep=node2&con=coap://[2001:db8:3::123]:61616"
payload = '</temp>;rt="temperature";anchor="coap://[2001:db8:3::123]:61616"'
client.post(path, payload, None, None, **ct)
client.stop()
path = "rd-lookup/res?rt=temp*"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '<coap://local-proxy-old.example.com:5683/sensors/temp>;ct=41;rt="temperature-c";' \
'if="sensor";anchor="coap://spurious.example.com:5683",' \
'<coap://[2001:db8:3::123]:61616/temp>;rt="temperature";' \
'anchor="coap://[2001:db8:3::123]:61616"'
self.current_mid += 1
self._test_check([(req, expected)])
def test_wildcard_ep(self):
print("Use wildcard * to find endpoints")
client = HelperClient(self.server_address)
path = "rd?ep=node1&con=coap://local-proxy-old.example.com:5683"
ct = {'content_type': defines.Content_types["application/link-format"]}
payload = '</sensors/temp>;ct=41;rt="temperature-c";if="sensor";anchor="coap://spurious.example.com:5683",' \
'</sensors/light>;ct=41;rt="light-lux";if="sensor"'
response = client.post(path, payload, None, None, **ct)
loc_path1 = response.location_path
path = "rd?ep=node2&con=coap://[2001:db8:3::123]:61616"
payload = '</temp>;rt="temperature";anchor="coap://[2001:db8:3::123]:61616"'
response = client.post(path, payload, None, None, **ct)
loc_path2 = response.location_path
client.stop()
path = "rd-lookup/ep?rt=temp*"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
req._mid = self.current_mid
req.destination = self.server_address
req.content_type = 0
req.payload = None
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = self.current_mid
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.content_type = defines.Content_types["application/link-format"]
expected.payload = '</' + loc_path1 + '>;con="coap://local-proxy-old.example.com:5683";ep="node1";lt=500,' \
'</' + loc_path2 + '>;con="coap://[2001:db8:3::123]:61616";' \
'ep="node2";lt=500'
self.current_mid += 1
self._test_check([(req, expected)])
if __name__ == '__main__':
unittest.main()