[tests] add traffic analysis for Cert_5_2_01 as TestPlan (#2463)

This commit modifies the method assertSentToNode to confirm if the destination Mac address of
the message is the node's Mac address. Add method check_parent_request, check_parent_response,
check_child_id_request and check_child_id_request to command.py.
This commit is contained in:
Zhanglong Xia
2018-01-15 19:36:38 +00:00
committed by Jonathan Hui
parent 33065099e1
commit 836377093e
6 changed files with 238 additions and 84 deletions
@@ -1,82 +0,0 @@
#!/usr/bin/env python
#
# Copyright (c) 2016, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import time
import unittest
import node
LEADER = 1
ROUTER1 = 2
REED = 3
class Cert_5_2_1_BecomeActiveRouter(unittest.TestCase):
def setUp(self):
self.nodes = {}
for i in range(1,4):
self.nodes[i] = node.Node(i)
self.nodes[LEADER].set_panid(0xface)
self.nodes[LEADER].set_mode('rsdn')
self.nodes[LEADER].add_whitelist(self.nodes[ROUTER1].get_addr64())
self.nodes[LEADER].enable_whitelist()
self.nodes[ROUTER1].set_panid(0xface)
self.nodes[ROUTER1].set_mode('rsdn')
self.nodes[ROUTER1].add_whitelist(self.nodes[LEADER].get_addr64())
self.nodes[ROUTER1].add_whitelist(self.nodes[REED].get_addr64())
self.nodes[ROUTER1].enable_whitelist()
self.nodes[ROUTER1].set_router_selection_jitter(1)
self.nodes[REED].set_panid(0xface)
self.nodes[REED].set_mode('rsdn')
self.nodes[REED].add_whitelist(self.nodes[ROUTER1].get_addr64())
self.nodes[REED].enable_whitelist()
self.nodes[REED].set_router_selection_jitter(1)
def tearDown(self):
for node in list(self.nodes.values()):
node.stop()
del self.nodes
def test(self):
self.nodes[LEADER].start()
self.nodes[LEADER].set_state('leader')
self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
self.nodes[ROUTER1].start()
time.sleep(5)
self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
self.nodes[REED].start()
time.sleep(5)
self.assertEqual(self.nodes[REED].get_state(), 'router')
if __name__ == '__main__':
unittest.main()
+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/env python
#
# Copyright (c) 2018, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import time
import unittest
import node
import mle
import config
import command
import shutil
import os
LEADER = 1
DUT_ROUTER1 = 2
REED1 = 3
MED1 = 4
class Cert_5_2_01_REEDAttach(unittest.TestCase):
def setUp(self):
self.nodes = {}
for i in range(1, 5):
self.nodes[i] = node.Node(i, i == MED1)
self.nodes[LEADER].set_panid(0xface)
self.nodes[LEADER].set_mode('rsdn')
self.nodes[LEADER].add_whitelist(self.nodes[DUT_ROUTER1].get_addr64())
self.nodes[LEADER].enable_whitelist()
self.nodes[DUT_ROUTER1].set_panid(0xface)
self.nodes[DUT_ROUTER1].set_mode('rsdn')
self.nodes[DUT_ROUTER1].add_whitelist(self.nodes[LEADER].get_addr64())
self.nodes[DUT_ROUTER1].add_whitelist(self.nodes[REED1].get_addr64())
self.nodes[DUT_ROUTER1].enable_whitelist()
self.nodes[DUT_ROUTER1].set_router_selection_jitter(1)
self.nodes[REED1].set_panid(0xface)
self.nodes[REED1].set_mode('rsdn')
self.nodes[REED1].add_whitelist(self.nodes[DUT_ROUTER1].get_addr64())
self.nodes[REED1].add_whitelist(self.nodes[MED1].get_addr64())
self.nodes[REED1].enable_whitelist()
self.nodes[REED1].set_router_selection_jitter(1)
self.nodes[REED1].set_router_upgrade_threshold(1)
self.nodes[MED1].set_panid(0xface)
self.nodes[MED1].set_mode('rsn')
self.nodes[MED1].add_whitelist(self.nodes[REED1].get_addr64())
self.nodes[MED1].enable_whitelist()
self.sniffer = config.create_default_thread_sniffer()
self.sniffer.start()
def tearDown(self):
self.sniffer.stop()
del self.sniffer
for node in list(self.nodes.values()):
node.stop()
del self.nodes
def test(self):
self.nodes[LEADER].start()
self.nodes[LEADER].set_state('leader')
self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
# 1 DUT_ROUTER1: Attach to LEADER
self.nodes[DUT_ROUTER1].start()
time.sleep(5)
self.assertEqual(self.nodes[DUT_ROUTER1].get_state(), 'router')
# DUT_ROUTER1: Verify MLE advertisements
router1_messages = self.sniffer.get_messages_sent_by(DUT_ROUTER1)
msg = router1_messages.next_mle_message(mle.CommandType.ADVERTISEMENT)
command.check_mle_advertisement(msg)
# 2 REED1: Attach to DUT_ROUTER1
self.nodes[REED1].start()
time.sleep(5)
self.assertEqual(self.nodes[REED1].get_state(), 'child')
# 3 DUT_ROUTER1: Verify MLE Parent Response
router1_messages = self.sniffer.get_messages_sent_by(DUT_ROUTER1)
msg = router1_messages.next_mle_message(mle.CommandType.PARENT_RESPONSE)
msg.assertSentToNode(self.nodes[REED1])
command.check_parent_response(msg)
# 4 DUT_ROUTER1: Verify MLE Child ID Response
msg = router1_messages.next_mle_message(mle.CommandType.CHILD_ID_RESPONSE)
msg.assertSentToNode(self.nodes[REED1])
command.check_child_id_response(msg)
# 5 Omitted
# 6 MED1: Attach to REED1
self.nodes[MED1].start()
time.sleep(5)
self.assertEqual(self.nodes[MED1].get_state(), 'child')
# 7 REED1: Verify sending Address Solicit Request to DUT_ROUTER1
reed1_messages = self.sniffer.get_messages_sent_by(REED1)
msg = reed1_messages.next_coap_message('0.02')
reed1_ipv6_address = msg.ipv6_packet.ipv6_header.source_address.compressed
msg.assertSentToNode(self.nodes[DUT_ROUTER1]);
msg.assertCoapMessageRequestUriPath('/a/as')
# 8 DUT_ROUTER1: Verify forwarding REED1's Address Solicit Request to LEADER
router1_messages = self.sniffer.get_messages_sent_by(DUT_ROUTER1)
msg = router1_messages.next_coap_message('0.02')
msg.assertSentToNode(self.nodes[LEADER]);
msg.assertCoapMessageRequestUriPath('/a/as')
# DUT_ROUTER1: Verify forwarding LEADER's Address Solicit Response to REED1
msg = router1_messages.next_coap_message('2.04')
msg.assertSentToDestinationAddress(reed1_ipv6_address)
# 9 LEADER: Verify connectivity by sending an ICMPv6 Echo Request to REED1
for addr in self.nodes[REED1].get_addrs():
if addr[0:4] != 'fe80':
self.assertTrue(self.nodes[LEADER].ping(addr))
if __name__ == '__main__':
unittest.main()
+2 -2
View File
@@ -44,7 +44,7 @@ EXTRA_DIST = \
Cert_5_1_11_REEDAttachLinkQuality.py \
Cert_5_1_12_NewRouterNeighborSync.py \
Cert_5_1_13_RouterReset.py \
Cert_5_2_01_BecomeActiveRouter.py \
Cert_5_2_01_REEDAttach.py \
Cert_5_2_03_LeaderReject2Hops.py \
Cert_5_2_04_REEDUpgrade.py \
Cert_5_2_05_AddressQuery.py \
@@ -174,7 +174,7 @@ check_SCRIPTS = \
Cert_5_1_11_REEDAttachLinkQuality.py \
Cert_5_1_12_NewRouterNeighborSync.py \
Cert_5_1_13_RouterReset.py \
Cert_5_2_01_BecomeActiveRouter.py \
Cert_5_2_01_REEDAttach.py \
Cert_5_2_05_AddressQuery.py \
Cert_5_2_06_RouterDowngrade.py \
Cert_5_2_07_REEDSynchronization.py \
+76
View File
@@ -4,6 +4,13 @@ import network_layer
import config
import mle
from enum import IntEnum
class CheckType(IntEnum):
CONTAIN = 0
NOT_CONTAIN = 1
OPTIONAL = 2
def check_address_query(command_msg, source_node, destination_address):
"""Verify source_node sent a properly formatted Address Query Request message to the destination_address.
"""
@@ -125,9 +132,78 @@ def get_routing_cost(command_msg, router_id):
return tlv.link_quality_and_route_data[routing_entry_pos].route
def check_mle_optional_tlv(command_msg, type, tlv):
if (type == CheckType.CONTAIN):
command_msg.assertMleMessageContainsTlv(tlv)
elif (type == CheckType.NOT_CONTAIN):
command_msg.assertMleMessageDoesNotContainTlv(tlv)
elif (type == CheckType.OPTIONAL):
command_msg.assertMleMessageContainsOptionalTlv(tlv)
else:
raise ValueError("Invalid check type")
def check_mle_advertisement(command_msg):
command_msg.assertSentWithHopLimit(255)
command_msg.assertSentToDestinationAddress(config.LINK_LOCAL_ALL_NODES_ADDRESS)
command_msg.assertMleMessageContainsTlv(mle.SourceAddress)
command_msg.assertMleMessageContainsTlv(mle.LeaderData)
command_msg.assertMleMessageContainsTlv(mle.Route64)
def check_parent_request(command_msg):
"""Verify a properly formatted Parent Request command message.
"""
command_msg.assertSentWithHopLimit(255)
command_msg.assertSentToDestinationAddress(config.LINK_LOCAL_ALL_ROUTERS_ADDRESS)
command_msg.assertMleMessageContainsTlv(mle.Mode)
command_msg.assertMleMessageContainsTlv(mle.Challenge)
command_msg.assertMleMessageContainsTlv(mle.ScanMask)
command_msg.assertMleMessageContainsTlv(mle.Version)
def check_parent_response(command_msg, mle_frame_counter = CheckType.OPTIONAL):
"""Verify a properly formatted Parent Response command message.
"""
command_msg.assertMleMessageContainsTlv(mle.Challenge)
command_msg.assertMleMessageContainsTlv(mle.Connectivity)
command_msg.assertMleMessageContainsTlv(mle.LeaderData)
command_msg.assertMleMessageContainsTlv(mle.LinkLayerFrameCounter)
command_msg.assertMleMessageContainsTlv(mle.LinkMargin)
command_msg.assertMleMessageContainsTlv(mle.Response)
command_msg.assertMleMessageContainsTlv(mle.SourceAddress)
command_msg.assertMleMessageContainsTlv(mle.Version)
check_mle_optional_tlv(command_msg, mle_frame_counter, mle.MleFrameCounter)
def check_child_id_request(command_msg, tlv_request = CheckType.OPTIONAL, \
mle_frame_counter = CheckType.OPTIONAL, address_registration = CheckType.OPTIONAL, \
active_timestamp = CheckType.OPTIONAL, pending_timestamp = CheckType.OPTIONAL):
"""Verify a properly formatted Child Id Request command message.
"""
command_msg.assertMleMessageContainsTlv(mle.LinkLayerFrameCounter)
command_msg.assertMleMessageContainsTlv(mle.Mode)
command_msg.assertMleMessageContainsTlv(mle.Response)
command_msg.assertMleMessageContainsTlv(mle.Timeout)
command_msg.assertMleMessageContainsTlv(mle.Version)
check_mle_optional_tlv(command_msg, tlv_request, mle.TlvRequest)
check_mle_optional_tlv(command_msg, mle_frame_counter, mle.MleFrameCounter)
check_mle_optional_tlv(command_msg, address_registration, mle.AddressRegistration)
check_mle_optional_tlv(command_msg, active_timestamp, mle.ActiveTimestamp)
check_mle_optional_tlv(command_msg, pending_timestamp, mle.PendingTimestamp)
def check_child_id_response(command_msg, route64 = CheckType.OPTIONAL, network_data = CheckType.OPTIONAL, \
address_registration = CheckType.OPTIONAL, active_timestamp = CheckType.OPTIONAL, \
pending_timestamp = CheckType.OPTIONAL, active_operational_dataset = CheckType.OPTIONAL, \
pending_operational_dataset = CheckType.OPTIONAL):
"""Verify a properly formatted Child Id Response command message.
"""
command_msg.assertMleMessageContainsTlv(mle.SourceAddress)
command_msg.assertMleMessageContainsTlv(mle.LeaderData)
command_msg.assertMleMessageContainsTlv(mle.Address16)
check_mle_optional_tlv(command_msg, route64, mle.Route64)
check_mle_optional_tlv(command_msg, network_data, mle.NetworkData)
check_mle_optional_tlv(command_msg, address_registration, mle.AddressRegistration)
check_mle_optional_tlv(command_msg, active_timestamp, mle.ActiveTimestamp)
check_mle_optional_tlv(command_msg, pending_timestamp, mle.PendingTimestamp)
check_mle_optional_tlv(command_msg, active_operational_dataset, mle.ActiveOperationalDataset)
check_mle_optional_tlv(command_msg, pending_operational_dataset, mle.PendingOperationalDataset)
+1
View File
@@ -50,6 +50,7 @@ LINK_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff32:40:fdde:ad00:beef:0:0:1'
REALM_LOCAL_All_THREAD_NODES_MULTICAST_ADDRESS = 'ff33:40:fdde:ad00:beef:0:0:1'
REALM_LOCAL_ALL_ROUTERS_ADDRESS = 'ff03::2'
LINK_LOCAL_ALL_NODES_ADDRESS = 'ff02::1'
LINK_LOCAL_ALL_ROUTERS_ADDRESS = 'ff02::2'
DEFAULT_MASTER_KEY = bytearray([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
+10
View File
@@ -270,6 +270,16 @@ class Message(object):
if dst_addr == ipaddress.ip_address(addr):
sent_to_node = True
if self.mac_header.dest_address.type == common.MacAddressType.SHORT:
mac_address = common.MacAddress.from_rloc16(node.get_addr16())
if self.mac_header.dest_address == mac_address:
sent_to_node = True
elif self.mac_header.dest_address.type == common.MacAddressType.LONG:
mac_address = common.MacAddress.from_eui64(bytearray(node.get_addr64(), encoding="utf-8"))
if self.mac_header.dest_address == mac_address:
sent_to_node = True
assert sent_to_node == True
def assertSentToDestinationAddress(self, ipv6_address):