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 mle
34import thread_cert
35from 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
36from pktverify.packet_verifier import PacketVerifier
37from pktverify.null_field import nullField
38
39LEADER = 1
40REED = 2
41ROUTER2 = 3
42ROUTER1 = 4
43
44# Test Purpose and Description:
45# -----------------------------
46# The purpose of this test case is to validate that DUT will
47# attach to a REED with the highest link quality, when routers
48# with the highest link quality are not available.
49#
50# Test Topology:
51# -------------
52#       Leader
53#       /  \
54# Router2   REED1
55#       \  /(better link_quality)
56#      Router1[DUT]
57#
58# DUT Types:
59# ----------
60#  Router
61
62
63class Cert_5_1_11_REEDAttachLinkQuality(thread_cert.TestCase):
64    USE_MESSAGE_FACTORY = False
65
66    TOPOLOGY = {
67        LEADER: {
68            'name': 'LEADER',
69            'mode': 'rdn',
70            'allowlist': [REED, ROUTER2]
71        },
72        REED: {
73            'name': 'REED_1',
74            'mode': 'rdn',
75            'router_upgrade_threshold': 0,
76            'allowlist': [LEADER, ROUTER1]
77        },
78        ROUTER2: {
79            'name': 'ROUTER_2',
80            'mode': 'rdn',
81            'allowlist': [LEADER, (ROUTER1, -85)]
82        },
83        ROUTER1: {
84            'name': 'ROUTER_1',
85            'mode': 'rdn',
86            'allowlist': [REED, ROUTER2]
87        },
88    }
89
90    def test(self):
91        self.nodes[LEADER].start()
92        self.simulator.go(config.LEADER_STARTUP_DELAY)
93        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
94
95        self.nodes[REED].start()
96        self.simulator.go(config.ROUTER_STARTUP_DELAY)
97        self.assertEqual(self.nodes[REED].get_state(), 'child')
98
99        self.nodes[ROUTER2].start()
100        self.simulator.go(config.ROUTER_STARTUP_DELAY)
101        self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
102
103        self.nodes[ROUTER1].start()
104        self.simulator.go(config.ROUTER_STARTUP_DELAY)
105        self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
106        self.assertEqual(self.nodes[REED].get_state(), 'router')
107
108    def verify(self, pv):
109        pkts = pv.pkts
110        pv.summary.show()
111
112        LEADER = pv.vars['LEADER']
113        REED_1 = pv.vars['REED_1']
114        ROUTER_1 = pv.vars['ROUTER_1']
115        ROUTER_2 = pv.vars['ROUTER_2']
116
117        # Step 1: Verify ROUTER_2 and Leader are sending MLE advertisements.
118        pkts.filter_wpan_src64(LEADER).\
119                   filter_mle_cmd(MLE_ADVERTISEMENT).\
120                   must_next()
121        pv.verify_attached('ROUTER_2')
122
123        # Step 3: The DUT sends a MLE Parent Request with an IP hop limit of
124        #         255 to the Link-Local All Routers multicast address (FF02::2).
125        #         The following TLVs MUST be present in the MLE Parent Request:
126        #            - Challenge TLV
127        #            - Mode TLV
128        #            - Scan Mask TLV
129        #                If the DUT sends multiple MLE Parent Requests
130        #                    - The first one MUST be sent only to all Routers
131        #                    - Subsequent ones MAY be sent to all Routers and REEDS
132        #            -  Version TLV
133        #         If the first MLE Parent Request was sent to all Routers and
134        #         REEDS, the test fails.
135
136        pkts.filter_wpan_src64(ROUTER_1).\
137            filter_LLARMA().\
138            filter_mle_cmd(MLE_PARENT_REQUEST).\
139            filter(lambda p: {
140                              CHALLENGE_TLV,
141                              MODE_TLV,
142                              SCAN_MASK_TLV,
143                              VERSION_TLV
144                              } <= set(p.mle.tlv.type) and\
145                   p.ipv6.hlim == 255 and\
146                   p.mle.tlv.scan_mask.r == 1 and\
147                   p.mle.tlv.scan_mask.e == 0
148                   ).\
149            must_next()
150
151        # Step 5: The DUT MUST send a MLE Parent Request with an IP hop limit of
152        #         255 to the Link-Local All Routers multicast address (FF02::2).
153        #         The following TLVs MUST be present in the MLE Parent Request:
154        #            - Challenge TLV
155        #            - Mode TLV
156        #            - Scan Mask TLV
157        #               - The Scan Mask TLV MUST be sent to Routers And REEDs
158        #            -  Version TLV
159        #
160
161        pkts.filter_wpan_src64(ROUTER_1).\
162            filter_LLARMA().\
163            filter_mle_cmd(MLE_PARENT_REQUEST).\
164            filter(lambda p: {
165                              CHALLENGE_TLV,
166                              MODE_TLV,
167                              SCAN_MASK_TLV,
168                              VERSION_TLV
169                              } <= set(p.mle.tlv.type) and\
170                   p.ipv6.hlim == 255 and\
171                   p.mle.tlv.scan_mask.r == 1 and\
172                   p.mle.tlv.scan_mask.e == 1
173                   ).\
174            must_next()
175
176        # Step 6: The DUT MUST initiate the attach process with REED_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_1).\
193            filter_wpan_dst64(REED_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