1 % CoAP layer test campaign 2 3 + Syntax check 4 = Import the CoAP layer 5 from scapy.contrib.coap import * 6 7 + Test CoAP 8 = CoAP default values 9 assert(raw(CoAP()) == b'\x40\x00\x00\x00') 10 11 = Token length calculation 12 p = CoAP(token='foobar') 13 assert(CoAP(raw(p)).tkl == 6) 14 15 = CON GET dissect 16 p = CoAP(b'\x40\x01\xd9\xe1\xbb\x2e\x77\x65\x6c\x6c\x2d\x6b\x6e\x6f\x77\x6e\x04\x63\x6f\x72\x65') 17 assert(p.code == 1) 18 assert(p.ver == 1) 19 assert(p.tkl == 0) 20 assert(p.tkl == 0) 21 assert(p.msg_id == 55777) 22 assert(p.token == b'') 23 assert(p.type == 0) 24 assert(p.options == [('Uri-Path', b'.well-known'), ('Uri-Path', b'core')]) 25 26 = Extended option delta 27 assert(raw(CoAP(options=[("Uri-Query", "query")])) == b'\x40\x00\x00\x00\xd5\x02\x71\x75\x65\x72\x79') 28 29 = Extended option length 30 assert(raw(CoAP(options=[("Location-Path", 'x' * 280)])) == b'\x40\x00\x00\x00\x8e\x0b\x00' + b'\x78' * 280) 31 32 = Options should be ordered by option number 33 assert(raw(CoAP(options=[("Uri-Query", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x61\x41\x62') 34 35 = Options of the same type should not be reordered 36 assert(raw(CoAP(options=[("Uri-Path", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x62\x01\x61') 37 38 + Test layer binding 39 = Destination port 40 p = UDP()/CoAP() 41 assert(p[UDP].dport == 5683) 42 43 = Source port 44 s = b'\x16\x33\xa0\xa4\x00\x78\xfe\x8b\x60\x45\xd9\xe1\xc1\x28\xff\x3c\x2f\x3e\x3b\x74\x69\x74\x6c\x65\x3d\x22\x47\x65' \ 45 b'\x6e\x65\x72\x61\x6c\x20\x49\x6e\x66\x6f\x22\x3b\x63\x74\x3d\x30\x2c\x3c\x2f\x74\x69\x6d\x65\x3e\x3b\x69\x66\x3d' \ 46 b'\x22\x63\x6c\x6f\x63\x6b\x22\x3b\x72\x74\x3d\x22\x54\x69\x63\x6b\x73\x22\x3b\x74\x69\x74\x6c\x65\x3d\x22\x49\x6e' \ 47 b'\x74\x65\x72\x6e\x61\x6c\x20\x43\x6c\x6f\x63\x6b\x22\x3b\x63\x74\x3d\x30\x3b\x6f\x62\x73\x2c\x3c\x2f\x61\x73\x79' \ 48 b'\x6e\x63\x3e\x3b\x63\x74\x3d\x30' 49 assert(CoAP in UDP(s)) 50 51 = building with a text/plain payload 52 p = CoAP(ver = 1, type = 0, code = 0x42, msg_id = 0xface, options=[("Content-Format", b"\x00")], paymark = b"\xff") 53 p /= Raw(b"\xde\xad\xbe\xef") 54 assert(raw(p) == b'\x40\x42\xfa\xce\xc1\x00\xff\xde\xad\xbe\xef') 55 56 = dissection with a text/plain payload 57 p = CoAP(raw(p)) 58 assert(p.ver == 1) 59 assert(p.type == 0) 60 assert(p.code == 0x42) 61 assert(p.msg_id == 0xface) 62 assert(isinstance(p.payload, Raw)) 63 assert(p.payload.load == b'\xde\xad\xbe\xef') 64