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 mle
33import thread_cert
34from pktverify.consts import MLE_ADVERTISEMENT, MLE_PARENT_REQUEST, MLE_PARENT_RESPONSE, MLE_CHILD_UPDATE_RESPONSE, MLE_CHILD_ID_REQUEST, MLE_CHILD_ID_RESPONSE, MLE_LINK_REQUEST, MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST, ADDR_SOL_URI, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, MLE_FRAME_COUNTER_TLV, ROUTE64_TLV, ADDRESS16_TLV, LEADER_DATA_TLV, NETWORK_DATA_TLV, TLV_REQUEST_TLV, SCAN_MASK_TLV, CONNECTIVITY_TLV, LINK_MARGIN_TLV, VERSION_TLV, ADDRESS_REGISTRATION_TLV, NL_MAC_EXTENDED_ADDRESS_TLV, NL_RLOC16_TLV, NL_STATUS_TLV, NL_ROUTER_MASK_TLV, COAP_CODE_ACK
35from pktverify.packet_verifier import PacketVerifier
36from pktverify.null_field import nullField
37
38LEADER = 1
39ROUTER1 = 2
40ROUTER2 = 3
41
42# Test Purpose and Description:
43# -----------------------------
44# The purpose of this test case is to validate that when the DUT sees a new router for the first time, it will synchronize using the New Router Neighbor Synchronization procedure.
45#
46# Test Topology:
47# -------------
48#     Leader
49#      /  \
50# Router2  Router1[DUT]
51#
52# DUT Types:
53# ----------
54#  Router
55
56
57class Cert_5_1_12_NewRouterSync(thread_cert.TestCase):
58    USE_MESSAGE_FACTORY = False
59
60    TOPOLOGY = {
61        LEADER: {
62            'name': 'LEADER',
63            'mode': 'rdn',
64            'allowlist': [ROUTER1, ROUTER2]
65        },
66        ROUTER1: {
67            'name': 'ROUTER_1',
68            'mode': 'rdn',
69            'allowlist': [LEADER]
70        },
71        ROUTER2: {
72            'name': 'ROUTER_2',
73            'mode': 'rdn',
74            'allowlist': [LEADER]
75        },
76    }
77
78    def test(self):
79        self.nodes[LEADER].start()
80        self.simulator.go(5)
81        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
82
83        self.nodes[ROUTER1].start()
84        self.simulator.go(5)
85        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
86
87        self.nodes[ROUTER2].start()
88        self.simulator.go(5)
89        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
90
91        self.simulator.go(10)
92
93        self.nodes[ROUTER1].add_allowlist(self.nodes[ROUTER2].get_addr64())
94        self.nodes[ROUTER2].add_allowlist(self.nodes[ROUTER1].get_addr64())
95
96        self.simulator.go(35)
97
98    def verify(self, pv):
99        pkts = pv.pkts
100        pv.summary.show()
101
102        LEADER = pv.vars['LEADER']
103        ROUTER_1 = pv.vars['ROUTER_1']
104        ROUTER_2 = pv.vars['ROUTER_2']
105
106        # Step 1: Verify topology is formed correctly.
107
108        # Step 2: The DUT MUST send properly formatted MLE Advertisements with
109        #         an IP Hop Limit of 255 to the Link-Local All Nodes multicast
110        #         address (FF02::1).
111        #          The following TLVs MUST be present in the MLE Advertisements:
112        #              - Leader Data TLV
113        #              - Route64 TLV
114        #              - Source Address TLV
115
116        pv.verify_attached('ROUTER_1')
117        pkts.filter_wpan_src64(ROUTER_1).\
118            filter_LLANMA().\
119            filter_mle_cmd(MLE_ADVERTISEMENT).\
120            filter(lambda p: {
121                              LEADER_DATA_TLV,
122                              ROUTE64_TLV,
123                              SOURCE_ADDRESS_TLV
124                              } == set(p.mle.tlv.type) and\
125                   p.ipv6.hlim == 255).\
126            must_next()
127
128        pv.verify_attached('ROUTER_2')
129        pkts.filter_wpan_src64(ROUTER_2).\
130            filter_LLANMA().\
131            filter_mle_cmd(MLE_ADVERTISEMENT).\
132            filter(lambda p: {
133                              LEADER_DATA_TLV,
134                              ROUTE64_TLV,
135                              SOURCE_ADDRESS_TLV
136                              } == set(p.mle.tlv.type) and\
137                   p.ipv6.hlim == 255).\
138            must_next()
139
140        # Step 4: The DUT and Router_2 exchange unicast Link Request and unicast
141        #         Link Accept messages OR Link Accept and Request messages.
142        #
143        #         The Link Request Message MUST be unicast and contain
144        #         the following TLVs:
145        #             - Challenge TLV
146        #             - Leader Data TLV
147        #             - Source Address TLV
148        #             - Version TLV
149        #             - TLV Request TLV: Link Margin
150        #
151        #         Link Accept or Link Accept And Request Messages MUST be
152        #         Unicast.
153        #         The following TLVs MUST be present in the Link Accept or
154        #         Link Accept And Request Messages :
155        #             - Leader Data TLV
156        #             - Link-layer Frame Counter TLV
157        #             - Link Margin TLV
158        #             - Response TLV
159        #             - Source Address TLV
160        #             - Version TLV
161        #             - TLV Request TLV: Link Margin
162        #             - Challenge TLV (optional)
163        #             - MLE Frame Counter TLV (optional)
164        #         The Challenge TLV and TLV Request TLV MUST be included
165        #         if the response is an Accept and Request message.
166
167        lq_src = ROUTER_1
168        lq_dst = ROUTER_2
169        _pkt = pkts.filter_mle_cmd(MLE_LINK_REQUEST).\
170                filter(lambda p: {
171                                  CHALLENGE_TLV,
172                                  LEADER_DATA_TLV,
173                                  SOURCE_ADDRESS_TLV,
174                                  VERSION_TLV,
175                                  TLV_REQUEST_TLV,
176                                  LINK_MARGIN_TLV
177                                  } <= set(p.mle.tlv.type) and\
178                       p.mle.tlv.link_margin is nullField and\
179                       (p.wpan.src64 == ROUTER_1 or\
180                        p.wpan.src64 == ROUTER_2)
181                       ).\
182                must_next()
183        if _pkt.wpan.src64 != ROUTER_1:
184            _pkt.must_verify(lambda p: p.wpan.dst64 == ROUTER_1)
185            lq_src = ROUTER_2
186            lq_dst = ROUTER_1
187
188        _pkt = pkts.filter_wpan_src64(lq_dst).\
189                filter_wpan_dst64(lq_src).\
190                filter_mle_cmd2(MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST).\
191                filter(lambda p: {
192                                  LEADER_DATA_TLV,
193                                  LINK_LAYER_FRAME_COUNTER_TLV,
194                                  LINK_MARGIN_TLV,
195                                  RESPONSE_TLV,
196                                  SOURCE_ADDRESS_TLV,
197                                  VERSION_TLV
198                                  } <= set(p.mle.tlv.type) and\
199                       p.mle.tlv.link_margin is not nullField
200                       ).\
201                       must_next()
202        if _pkt.mle.cmd == MLE_LINK_ACCEPT_AND_REQUEST:
203            _pkt.must_verify(lambda p: {CHALLENGE_TLV, TLV_REQUEST_TLV} <= set(p.mle.tlv.type))
204
205
206if __name__ == '__main__':
207    unittest.main()
208