mirror of https://github.com/secdev/scapy.git
commit
32c8d95f7f
|
@ -29,6 +29,7 @@ from scapy.fields import *
|
|||
from scapy.layers.inet import UDP
|
||||
from scapy.packet import *
|
||||
from scapy.error import warning
|
||||
from scapy.compat import raw
|
||||
|
||||
coap_codes = {
|
||||
0: "Empty",
|
||||
|
@ -150,7 +151,7 @@ class _CoAPOpt(Packet):
|
|||
return Packet.do_build(self)
|
||||
|
||||
def guess_payload_class(self, payload):
|
||||
if payload[0] != b'\xff':
|
||||
if payload[:1] != b"\xff":
|
||||
return _CoAPOpt
|
||||
else:
|
||||
return Packet.guess_payload_class(self, payload)
|
||||
|
@ -184,7 +185,7 @@ class _CoAPOptsField(StrField):
|
|||
|
||||
def i2m(self, pkt, x):
|
||||
if not x:
|
||||
return ""
|
||||
return b""
|
||||
opt_lst = []
|
||||
for o in x:
|
||||
if isinstance(o[0], str):
|
||||
|
@ -199,7 +200,7 @@ class _CoAPOptsField(StrField):
|
|||
opts = opts / _CoAPOpt(delta=o[0] - high_opt, opt_val=o[1])
|
||||
high_opt = o[0]
|
||||
|
||||
return str(opts)
|
||||
return raw(opts)
|
||||
|
||||
class _CoAPPaymark(StrField):
|
||||
|
||||
|
@ -211,9 +212,9 @@ class _CoAPPaymark(StrField):
|
|||
return s[u:], m
|
||||
|
||||
def m2i(self, pkt, x):
|
||||
if len(x) > 0 and x[0] == b'\xff':
|
||||
if len(x) > 0 and x[:1] == b"\xff":
|
||||
return 1, b'\xff'
|
||||
return 0, '';
|
||||
return 0, b'';
|
||||
|
||||
def i2m(self, pkt, x):
|
||||
return x
|
||||
|
@ -230,7 +231,7 @@ class CoAP(Packet):
|
|||
ShortField("msg_id", 0),
|
||||
StrLenField("token", "", length_from=lambda pkt: pkt.tkl),
|
||||
_CoAPOptsField("options", []),
|
||||
_CoAPPaymark("paymark", "")
|
||||
_CoAPPaymark("paymark", b"")
|
||||
]
|
||||
|
||||
def getfieldval(self, attr):
|
||||
|
|
|
@ -6,11 +6,11 @@ from scapy.contrib.coap import *
|
|||
|
||||
+ Test CoAP
|
||||
= CoAP default values
|
||||
assert(str(CoAP()) == b'\x40\x00\x00\x00')
|
||||
assert(raw(CoAP()) == b'\x40\x00\x00\x00')
|
||||
|
||||
= Token length calculation
|
||||
p = CoAP(token='foobar')
|
||||
assert(CoAP(str(p)).tkl == 6)
|
||||
assert(CoAP(raw(p)).tkl == 6)
|
||||
|
||||
= CON GET dissect
|
||||
p = CoAP(b'\x40\x01\xd9\xe1\xbb\x2e\x77\x65\x6c\x6c\x2d\x6b\x6e\x6f\x77\x6e\x04\x63\x6f\x72\x65')
|
||||
|
@ -19,21 +19,21 @@ assert(p.ver == 1)
|
|||
assert(p.tkl == 0)
|
||||
assert(p.tkl == 0)
|
||||
assert(p.msg_id == 55777)
|
||||
assert(p.token == '')
|
||||
assert(p.token == b'')
|
||||
assert(p.type == 0)
|
||||
assert(p.options == [('Uri-Path', '.well-known'), ('Uri-Path', 'core')])
|
||||
assert(p.options == [('Uri-Path', b'.well-known'), ('Uri-Path', b'core')])
|
||||
|
||||
= Extended option delta
|
||||
assert(str(CoAP(options=[("Uri-Query", "query")])) == b'\x40\x00\x00\x00\xd5\x02\x71\x75\x65\x72\x79')
|
||||
assert(raw(CoAP(options=[("Uri-Query", "query")])) == b'\x40\x00\x00\x00\xd5\x02\x71\x75\x65\x72\x79')
|
||||
|
||||
= Extended option length
|
||||
assert(str(CoAP(options=[("Location-Path", 'x' * 280)])) == b'\x40\x00\x00\x00\x8e\x0b\x00' + b'\x78' * 280)
|
||||
assert(raw(CoAP(options=[("Location-Path", 'x' * 280)])) == b'\x40\x00\x00\x00\x8e\x0b\x00' + b'\x78' * 280)
|
||||
|
||||
= Options should be ordered by option number
|
||||
assert(str(CoAP(options=[("Uri-Query", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x61\x41\x62')
|
||||
assert(raw(CoAP(options=[("Uri-Query", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x61\x41\x62')
|
||||
|
||||
= Options of the same type should not be reordered
|
||||
assert(str(CoAP(options=[("Uri-Path", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x62\x01\x61')
|
||||
assert(raw(CoAP(options=[("Uri-Path", "b"),("Uri-Path","a")])) == b'\x40\x00\x00\x00\xb1\x62\x01\x61')
|
||||
|
||||
+ Test layer binding
|
||||
= Destination port
|
||||
|
@ -51,10 +51,10 @@ assert(CoAP in UDP(s))
|
|||
= building with a text/plain payload
|
||||
p = CoAP(ver = 1, type = 0, code = 0x42, msg_id = 0xface, options=[("Content-Format", b"\x00")], paymark = b"\xff")
|
||||
p /= Raw(b"\xde\xad\xbe\xef")
|
||||
assert(str(p) == b'\x40\x42\xfa\xce\xc1\x00\xff\xde\xad\xbe\xef')
|
||||
assert(raw(p) == b'\x40\x42\xfa\xce\xc1\x00\xff\xde\xad\xbe\xef')
|
||||
|
||||
= dissection with a text/plain payload
|
||||
p = CoAP(str(p))
|
||||
p = CoAP(raw(p))
|
||||
assert(p.ver == 1)
|
||||
assert(p.type == 0)
|
||||
assert(p.code == 0x42)
|
||||
|
|
Loading…
Reference in New Issue