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 thread_cert
33from 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
34from pktverify.packet_verifier import PacketVerifier
35
36COMMISSIONER = 1
37JOINER_ROUTER = 2
38JOINER = 3
39
40
41class Cert_8_2_02_JoinerRouter(thread_cert.TestCase):
42    SUPPORT_NCP = False
43
44    TOPOLOGY = {
45        COMMISSIONER: {
46            'name': 'COMMISSIONER',
47            'networkkey': '00112233445566778899aabbccddeeff',
48            'mode': 'rdn',
49        },
50        JOINER_ROUTER: {
51            'name': 'JOINER_ROUTER',
52            'networkkey': 'deadbeefdeadbeefdeadbeefdeadbeef',
53            'mode': 'rdn',
54        },
55        JOINER: {
56            'name': 'JOINER',
57            'networkkey': 'deadbeefdeadbeefdeadbeefdeadbeef',
58            'mode': 'rdn',
59        },
60    }
61
62    def test(self):
63        self.nodes[COMMISSIONER].interface_up()
64        self.nodes[COMMISSIONER].thread_start()
65        self.simulator.go(5)
66        self.assertEqual(self.nodes[COMMISSIONER].get_state(), 'leader')
67
68        self.nodes[COMMISSIONER].commissioner_start()
69        self.simulator.go(5)
70        self.nodes[COMMISSIONER].commissioner_add_joiner(self.nodes[JOINER_ROUTER].get_eui64(), 'PSKD01')
71        self.nodes[COMMISSIONER].commissioner_add_joiner(self.nodes[JOINER].get_eui64(), 'PSKD02')
72        self.simulator.go(5)
73
74        self.nodes[JOINER_ROUTER].interface_up()
75        self.nodes[JOINER_ROUTER].joiner_start('PSKD01')
76        self.simulator.go(10)
77        self.assertEqual(
78            self.nodes[JOINER_ROUTER].get_networkkey(),
79            self.nodes[COMMISSIONER].get_networkkey(),
80        )
81
82        self.nodes[JOINER_ROUTER].thread_start()
83        self.simulator.go(5)
84        self.assertEqual(self.nodes[JOINER_ROUTER].get_state(), 'router')
85
86        self.nodes[COMMISSIONER].enable_allowlist()
87        self.nodes[COMMISSIONER].add_allowlist(self.nodes[JOINER_ROUTER].get_addr64())
88
89        self.nodes[JOINER].enable_allowlist()
90        self.nodes[JOINER].add_allowlist(self.nodes[JOINER_ROUTER].get_addr64())
91
92        self.nodes[JOINER].interface_up()
93        self.nodes[JOINER].joiner_start('20DKSP')
94        self.simulator.go(10)
95        self.collect_rloc16s()
96
97    def verify(self, pv):
98        pkts = pv.pkts
99        pv.summary.show()
100
101        COMMISSIONER = pv.vars['COMMISSIONER']
102        _cpkts = pkts.filter_wpan_src64(COMMISSIONER)
103        _cpkts.filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
104
105        # Step 3: Verify that the following details occur in the exchange between the Joiner,
106        # the Joiner_Router and the Commissioner
107        # 1. UDP port (Specified by the Commissioner: in Discovery Response) is used as destination port
108        # for UDP datagrams from Joiner_1 to the Commissioner.
109        pkts.range(_cpkts.index).filter_mle_cmd(MLE_DISCOVERY_RESPONSE).must_next().must_verify(
110            lambda p: {
111                NM_EXTENDED_PAN_ID_TLV, NM_NETWORK_NAME_TLV, NM_STEERING_DATA_TLV, NM_COMMISSIONER_UDP_PORT_TLV,
112                NM_JOINER_UDP_PORT_TLV, NM_DISCOVERY_RESPONSE_TLV
113            } == set(p.thread_meshcop.tlv.type))
114
115        # 2. Joiner_1 sends an initial DTLS-ClientHello handshake record to the Commissioner
116        pkts.filter(lambda p: p.dtls.handshake.type == [HANDSHAKE_CLIENT_HELLO]).must_next()
117
118        # 3. The Joiner_Router receives the initial DTLS-ClientHello handshake record and sends a RLY_RX.ntf
119        # message to the Commissioner
120        # Todo: verify coap payload
121        jr_rloc16 = pv.vars["JOINER_ROUTER_RLOC16"]
122        c_rloc16 = pv.vars["COMMISSIONER_RLOC16"]
123        pkts.filter_coap_request(RLY_RX_URI).must_next().must_verify(
124            lambda p: p.wpan.src16 == jr_rloc16 and p.wpan.dst16 == c_rloc16)
125
126        # 4. The Commissioner receives the RLY_RX.ntf message and sends a RLY_TX.ntf message to the Joiner_Router
127        pkts.filter_coap_request(RLY_TX_URI).must_next().must_verify(
128            lambda p: p.wpan.src16 == c_rloc16 and p.wpan.dst16 == jr_rloc16)
129
130
131if __name__ == '__main__':
132    unittest.main()
133