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