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_ID_REQUEST, SOURCE_ADDRESS_TLV, MODE_TLV, TIMEOUT_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_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
35from pktverify.packet_verifier import PacketVerifier
36from pktverify.null_field import nullField
37
38LEADER = 1
39ROUTER1 = 2
40ROUTER2 = 3
41ROUTER3 = 4
42
43# Test Purpose and Description:
44# -----------------------------
45# The purpose of this test case is to validate that the DUT
46# will choose a router with better link quality as its parent.
47#
48# Test Topology:
49# -------------
50#       Leader
51#       /  \
52# ROUTER2   ROUTER1
53#       \  /(better link_quality)
54#      Router3[DUT]
55#
56# DUT Types:
57# ----------
58#  Router
59
60
61class Cert_5_1_10_RouterAttachLinkQuality(thread_cert.TestCase):
62    USE_MESSAGE_FACTORY = False
63
64    TOPOLOGY = {
65        LEADER: {
66            'name': 'LEADER',
67            'mode': 'rdn',
68            'allowlist': [ROUTER1, ROUTER2]
69        },
70        ROUTER1: {
71            'name': 'ROUTER_1',
72            'mode': 'rdn',
73            'allowlist': [LEADER, ROUTER3]
74        },
75        ROUTER2: {
76            'name': 'ROUTER_2',
77            'mode': 'rdn',
78            'allowlist': [LEADER, (ROUTER3, -85)]
79        },
80        ROUTER3: {
81            'name': 'ROUTER_3',
82            'mode': 'rdn',
83            'allowlist': [ROUTER1, ROUTER2]
84        },
85    }
86
87    def test(self):
88        self.nodes[LEADER].start()
89        self.simulator.go(5)
90        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
91
92        self.nodes[ROUTER1].start()
93        self.simulator.go(5)
94        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
95
96        self.nodes[ROUTER2].start()
97        self.simulator.go(5)
98        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
99
100        self.nodes[ROUTER3].start()
101        self.simulator.go(5)
102        self.assertEqual(self.nodes[ROUTER3].get_state(), 'router')
103
104    def verify(self, pv):
105        pkts = pv.pkts
106        pv.summary.show()
107
108        LEADER = pv.vars['LEADER']
109        ROUTER_1 = pv.vars['ROUTER_1']
110        ROUTER_2 = pv.vars['ROUTER_2']
111        ROUTER_3 = pv.vars['ROUTER_3']
112
113        # Step 1: Verify ROUTER_1, Router2 and Leader are sending MLE advertisements.
114        pkts.filter_wpan_src64(LEADER).\
115                   filter_mle_cmd(MLE_ADVERTISEMENT).\
116                   must_next()
117        pv.verify_attached('ROUTER_1')
118        pv.verify_attached('ROUTER_2')
119
120        # Step 3: The DUT sends a MLE Parent Request with an IP hop limit of
121        #         255 to the Link-Local All Routers multicast address (FF02::2).
122        #         The following TLVs MUST be present in the MLE Parent Request:
123        #            - Challenge TLV
124        #            - Mode TLV
125        #            - Scan Mask TLV
126        #                If the DUT sends multiple MLE Parent Requests
127        #                    - The first one MUST be sent only to all Routers
128        #                    - Subsequent ones MAY be sent to all Routers and REEDS
129        #            -  Version TLV
130
131        pkts.filter_wpan_src64(ROUTER_3).\
132            filter_LLARMA().\
133            filter_mle_cmd(MLE_PARENT_REQUEST).\
134            filter(lambda p: {
135                              CHALLENGE_TLV,
136                              MODE_TLV,
137                              SCAN_MASK_TLV,
138                              VERSION_TLV
139                              } <= set(p.mle.tlv.type) and\
140                   p.ipv6.hlim == 255 and\
141                   p.mle.tlv.scan_mask.r == 1 and\
142                   p.mle.tlv.scan_mask.e == 0
143                   ).\
144            must_next()
145
146        # Step 4: ROUTER_1 and ROUTER_2 respond with a MLE Parent Response.
147        #         The following TLVs MUST be present in the MLE Parent Response:
148        #             - Challenge TLV
149        #             - Connectivity TLV
150        #             - Leader Data TLV
151        #             - Link-layer Frame Counter TLV
152        #             - Link Margin TLV
153        #             - Response TLV
154        #             - Source Address
155        #             - Version TLV
156        #             - MLE Frame Counter TLV (optional)
157
158        for i in range(1, 3):
159            with pkts.save_index():
160                pkts.filter_wpan_src64(pv.vars['ROUTER_%d' % i]).\
161                    filter_wpan_dst64(ROUTER_3).\
162                    filter_mle_cmd(MLE_PARENT_RESPONSE).\
163                    filter(lambda p: {
164                                      CHALLENGE_TLV,
165                                      CONNECTIVITY_TLV,
166                                      LEADER_DATA_TLV,
167                                      LINK_LAYER_FRAME_COUNTER_TLV,
168                                      LINK_MARGIN_TLV,
169                                      RESPONSE_TLV,
170                                      SOURCE_ADDRESS_TLV,
171                                      VERSION_TLV
172                                    } <= set(p.mle.tlv.type)
173                           ).\
174                    must_next()
175
176        # Step 5: The DUT MUST initiate the attach process with Router_1 by
177        #         sending an MLE Child ID Request; if not, the test fails.
178        #         The following TLVs MUST be present in the MLE Child ID Request:
179        #             - Link-layer Frame Counter TLV
180        #             - Mode TLV
181        #             - Response TLV
182        #             - Timeout TLV
183        #             - TLV Request TLV
184        #                 - Address16 TLV
185        #                 - Network Data TLV
186        #                 - Route64 TLV (optional)
187        #             - Version TLV
188        #             - MLE Frame Counter TLV (optional)
189        #         The following TLV MUST NOT be present in the MLE Child ID Request:
190        #             - Address Registration TLV
191
192        _pkt = pkts.filter_wpan_src64(ROUTER_3).\
193            filter_wpan_dst64(ROUTER_1).\
194            filter_mle_cmd(MLE_CHILD_ID_REQUEST).\
195            filter(lambda p: {
196                              LINK_LAYER_FRAME_COUNTER_TLV,
197                              MODE_TLV,
198                              RESPONSE_TLV,
199                              TIMEOUT_TLV,
200                              TLV_REQUEST_TLV,
201                              ADDRESS16_TLV,
202                              NETWORK_DATA_TLV,
203                              VERSION_TLV
204                    } <= set(p.mle.tlv.type) and\
205                   p.mle.tlv.addr16 is nullField and\
206                   p.thread_nwd.tlv.type is nullField
207                   ).\
208            must_next()
209        _pkt.must_not_verify(lambda p: (ADDRESS_REGISTRATION_TLV) in p.mle.tlv.type)
210
211
212if __name__ == '__main__':
213    unittest.main()
214