1#!/usr/bin/env python3 2# 3# Copyright (c) 2016, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29 30import unittest 31 32import config 33import thread_cert 34from pktverify.consts import MLE_CHILD_ID_RESPONSE, MLE_DISCOVERY_RESPONSE, HANDSHAKE_CLIENT_HELLO, NM_EXTENDED_PAN_ID_TLV, NM_NETWORK_NAME_TLV, NM_STEERING_DATA_TLV, NM_COMMISSIONER_UDP_PORT_TLV, NM_JOINER_UDP_PORT_TLV, NM_DISCOVERY_RESPONSE_TLV, RLY_RX_URI, RLY_TX_URI 35from pktverify.packet_verifier import PacketVerifier 36 37COMMISSIONER = 1 38JOINER_ROUTER = 2 39JOINER = 3 40 41 42class Cert_8_2_02_JoinerRouter(thread_cert.TestCase): 43 SUPPORT_NCP = False 44 45 TOPOLOGY = { 46 COMMISSIONER: { 47 'name': 'COMMISSIONER', 48 'networkkey': '00112233445566778899aabbccddeeff', 49 'mode': 'rdn', 50 }, 51 JOINER_ROUTER: { 52 'name': 'JOINER_ROUTER', 53 'networkkey': 'deadbeefdeadbeefdeadbeefdeadbeef', 54 'mode': 'rdn', 55 }, 56 JOINER: { 57 'name': 'JOINER', 58 'networkkey': 'deadbeefdeadbeefdeadbeefdeadbeef', 59 'mode': 'rdn', 60 }, 61 } 62 63 def test(self): 64 self.nodes[COMMISSIONER].interface_up() 65 self.nodes[COMMISSIONER].thread_start() 66 self.simulator.go(config.LEADER_STARTUP_DELAY) 67 self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'leader') 68 69 self.nodes[COMMISSIONER].commissioner_start() 70 self.simulator.go(5) 71 self.nodes[COMMISSIONER].commissioner_add_joiner(self.nodes[JOINER_ROUTER].get_eui64(), 'PSKD01') 72 self.nodes[COMMISSIONER].commissioner_add_joiner(self.nodes[JOINER].get_eui64(), 'PSKD02') 73 self.simulator.go(5) 74 75 self.nodes[JOINER_ROUTER].interface_up() 76 self.nodes[JOINER_ROUTER].joiner_start('PSKD01') 77 self.simulator.go(10) 78 self.assertEqual( 79 self.nodes[JOINER_ROUTER].get_networkkey(), 80 self.nodes[COMMISSIONER].get_networkkey(), 81 ) 82 83 self.nodes[JOINER_ROUTER].thread_start() 84 self.simulator.go(5) 85 self.assertEqual(self.nodes[JOINER_ROUTER].get_state(), 'router') 86 87 self.nodes[COMMISSIONER].enable_allowlist() 88 self.nodes[COMMISSIONER].add_allowlist(self.nodes[JOINER_ROUTER].get_addr64()) 89 90 self.nodes[JOINER].enable_allowlist() 91 self.nodes[JOINER].add_allowlist(self.nodes[JOINER_ROUTER].get_addr64()) 92 93 self.nodes[JOINER].interface_up() 94 self.nodes[JOINER].joiner_start('20DKSP') 95 self.simulator.go(10) 96 self.collect_rloc16s() 97 98 def verify(self, pv): 99 pkts = pv.pkts 100 pv.summary.show() 101 102 COMMISSIONER = pv.vars['COMMISSIONER'] 103 _cpkts = pkts.filter_wpan_src64(COMMISSIONER) 104 _cpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next() 105 106 # Step 3: Verify that the following details occur in the exchange between the Joiner, 107 # the Joiner_Router and the Commissioner 108 # 1. UDP port (Specified by the Commissioner: in Discovery Response) is used as destination port 109 # for UDP datagrams from Joiner_1 to the Commissioner. 110 pkts.range(_cpkts.index).filter_mle_cmd(MLE_DISCOVERY_RESPONSE).must_next().must_verify( 111 lambda p: { 112 NM_EXTENDED_PAN_ID_TLV, NM_NETWORK_NAME_TLV, NM_STEERING_DATA_TLV, NM_COMMISSIONER_UDP_PORT_TLV, 113 NM_JOINER_UDP_PORT_TLV, NM_DISCOVERY_RESPONSE_TLV 114 } <= set(p.thread_meshcop.tlv.type)) 115 116 # 2. Joiner_1 sends an initial DTLS-ClientHello handshake record to the Commissioner 117 pkts.filter(lambda p: p.dtls.handshake.type == [HANDSHAKE_CLIENT_HELLO]).must_next() 118 119 # 3. The Joiner_Router receives the initial DTLS-ClientHello handshake record and sends a RLY_RX.ntf 120 # message to the Commissioner 121 # Todo: verify coap payload 122 jr_rloc16 = pv.vars["JOINER_ROUTER_RLOC16"] 123 c_rloc16 = pv.vars["COMMISSIONER_RLOC16"] 124 pkts.filter_coap_request(RLY_RX_URI).must_next().must_verify( 125 lambda p: p.wpan.src16 == jr_rloc16 and p.wpan.dst16 == c_rloc16) 126 127 # 4. The Commissioner receives the RLY_RX.ntf message and sends a RLY_TX.ntf message to the Joiner_Router 128 pkts.filter_coap_request(RLY_TX_URI).must_next().must_verify( 129 lambda p: p.wpan.src16 == c_rloc16 and p.wpan.dst16 == jr_rloc16) 130 131 132if __name__ == '__main__': 133 unittest.main() 134