Skip to content

Commit 2a126d9

Browse files
committed
Unit tests for SR and SR1, added hash functions for the CoAP packet.
1 parent f6b86b5 commit 2a126d9

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

scapy/contrib/coap.py

+6
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ def post_dissect(self, pay):
278278
self.content_format = k[1]
279279
return pay
280280

281+
def hashret(self):
282+
return struct.pack('I', self.msg_id) + self.token
283+
284+
def answers(self, other):
285+
return True
286+
281287

282288
bind_layers(UDP, CoAP, sport=5683)
283289
bind_layers(UDP, CoAP, dport=5683)

scapy/contrib/coap_socket.py

+14
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ def send(self, x):
152152
self.impl.send(x.dst, x.dport, x[CoAP])
153153
return len(x)
154154

155+
@staticmethod
156+
def select(sockets, remain=None):
157+
# type: (list[SuperSocket], Optional[float]) -> list[SuperSocket]
158+
"""This function is called during sendrecv() routine to wait for
159+
sockets to be ready to receive
160+
"""
161+
obj_pipes = [x.impl.rx_queue for x in sockets if
162+
isinstance(x, CoAPSocket) and not x.closed]
163+
164+
ready_pipes = select_objects(obj_pipes, 0)
165+
166+
return [x for x in sockets if isinstance(x, CoAPSocket) and
167+
not x.closed and x.impl.rx_queue in ready_pipes]
168+
155169
@staticmethod
156170
def make_coap_req_packet(method=GET, uri="", options=None, payload=b""):
157171
# type: (int, str, list[tuple], bytes) -> Packet

scapy/sendrecv.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def _sndrcv_snd(self):
243243
# Populate the dictionary of _sndrcv_rcv
244244
# _sndrcv_rcv won't miss the answer of a packet that
245245
# has not been sent
246-
self.hsent.setdefault(p.hashret(), []).append(p)
246+
h = p[self.pks.basecls]
247+
self.hsent.setdefault(h.hashret(), []).append(p)
247248
# Send packet
248249
self.pks.send(p)
249250
time.sleep(self.inter)

test/contrib/coap_socket.uts

+23
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,26 @@ with CoAPSocket("127.0.0.1", 5683, lst_resources=lst_resources) as coap_server,
127127
# assert res.msg_id == req.msg_id - This assert doesn't make sense because it will send with another msg_id
128128
assert res.token == req.token
129129
assert res.payload.load == responses[1]
130+
131+
= SR1
132+
133+
with CoAPSocket("127.0.0.1", 5683, lst_resources=lst_resources) as coap_server, CoAPSocket("127.0.0.1", 5684) as coap_client:
134+
req = CoAPSocket.make_coap_req_packet(uri="/dummy", payload=b"")
135+
res = coap_client.sr1(IP(dst="127.0.0.1")/UDP(dport=5683)/req)
136+
assert res.payload.load == responses[0]
137+
assert res.type == ACK
138+
assert res.code == CONTENT_205
139+
assert res.msg_id == req.msg_id
140+
assert res.token == req.token
141+
142+
= SR
143+
144+
with CoAPSocket("127.0.0.1", 5683, lst_resources=lst_resources) as coap_server, CoAPSocket("127.0.0.1", 5684) as coap_client:
145+
pkt = CoAPSocket.make_coap_req_packet(uri="/dummy", payload=b"")
146+
ans, _ = coap_client.sr(IP(dst="127.0.0.1")/UDP(dport=5683)/pkt)
147+
for _, rcv in ans:
148+
assert rcv.payload.load == responses[0]
149+
assert rcv.type == ACK
150+
assert rcv.code == CONTENT_205
151+
assert rcv.msg_id == pkt.msg_id
152+
assert rcv.token == pkt.token

0 commit comments

Comments
 (0)