[tests] fix key_id_mode handling and add exception handle (#4927)

This commit fixes key_id_mode handling and added exception handling
when key_id_mode is 0.

According to IEEE Std 802.15.4-2015, when Key Id Mode is 0x00:

    Key is determined implicitly from the originator and recipient(s)
    of the frame, as indicated in the frame header.

Our test scripts cannot handle such packets. If we leave key_id_mode =
0 case unhandled, we would get some exceptions that may cause
developers to think that something is wrong. However dropping packets
in this case is a normal behavior.
This commit is contained in:
Li Cao
2020-05-07 21:33:30 -07:00
committed by GitHub
parent 60aa241594
commit 417093e247
3 changed files with 49 additions and 22 deletions
+31 -19
View File
@@ -41,6 +41,10 @@ import mle
from enum import IntEnum
class DropPacketException(Exception):
pass
class MessageType(IntEnum):
MLE = 0
COAP = 1
@@ -598,30 +602,38 @@ class MessageFactory:
self._lowpan_parser.set_lowpan_context(cid, prefix)
def create(self, data):
message = Message()
message.channel = struct.unpack(">B", data.read(1))
try:
message = Message()
message.channel = struct.unpack(">B", data.read(1))
# Parse MAC header
mac_frame = self._parse_mac_frame(data)
message.mac_header = mac_frame.header
# Parse MAC header
mac_frame = self._parse_mac_frame(data)
message.mac_header = mac_frame.header
if message.mac_header.frame_type != mac802154.MacHeader.FrameType.DATA:
return [message]
if message.mac_header.frame_type != mac802154.MacHeader.FrameType.DATA:
return [message]
message_info = common.MessageInfo()
message_info.source_mac_address = message.mac_header.src_address
message_info.destination_mac_address = message.mac_header.dest_address
message_info = common.MessageInfo()
message_info.source_mac_address = message.mac_header.src_address
message_info.destination_mac_address = message.mac_header.dest_address
# Create stream with 6LoWPAN datagram
lowpan_payload = io.BytesIO(mac_frame.payload.data)
# Create stream with 6LoWPAN datagram
lowpan_payload = io.BytesIO(mac_frame.payload.data)
ipv6_packet = self._lowpan_parser.parse(lowpan_payload, message_info)
if ipv6_packet is None:
return [message]
ipv6_packet = self._lowpan_parser.parse(lowpan_payload,
message_info)
if ipv6_packet is None:
return [message]
message.ipv6_packet = ipv6_packet
message.ipv6_packet = ipv6_packet
if message.type == MessageType.MLE:
self._add_device_descriptors(message)
if message.type == MessageType.MLE:
self._add_device_descriptors(message)
return message.try_extract_dtls_messages()
return message.try_extract_dtls_messages()
except mac802154.KeyIdMode0Exception:
print(
'Received packet with key_id_mode = 0, cannot be handled in test scripts'
)
raise DropPacketException