1#!/usr/bin/env python3
2#
3#  Copyright (c) 2023, 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 copy
31import unittest
32
33import command
34import config
35import mle
36import thread_cert
37from pktverify.consts import MLE_PARENT_REQUEST, MLE_LINK_REQUEST, MLE_LINK_ACCEPT, MLE_LINK_ACCEPT_AND_REQUEST, SOURCE_ADDRESS_TLV, CHALLENGE_TLV, RESPONSE_TLV, LINK_LAYER_FRAME_COUNTER_TLV, ROUTE64_TLV, ADDRESS16_TLV, LEADER_DATA_TLV, TLV_REQUEST_TLV, VERSION_TLV
38from pktverify.packet_verifier import PacketVerifier
39from pktverify.null_field import nullField
40
41LEADER = 1
42DUT_ROUTER = 2
43MED1 = 3
44MED2 = 4
45MED3 = 5
46MED4 = 6
47MED5 = 7
48MED6 = 8
49
50# Test Purpose and Description:
51# -----------------------------
52# The purpose of this test case is to show that when a router with > 5 children is rebooted, it sends MLE_MAX_CRITICAL_TRANSMISSION_COUNT MLE link request packets if no response is received.
53#
54# Test Topology:
55# -------------
56#   Leader
57#     |
58#   Router ------------------------+
59#   |       |     |     |    |     |
60#   MED1  MED2  MED3  MED4  MED5  MED6
61#
62# DUT Types:
63# ----------
64#  Router
65
66
67class Test_LeaderRebootMultipleLinkRequest(thread_cert.TestCase):
68    #USE_MESSAGE_FACTORY = False
69
70    TOPOLOGY = {
71        LEADER: {
72            'name': 'LEADER',
73            'mode': 'rdn',
74            'allowlist': [DUT_ROUTER]
75        },
76        DUT_ROUTER: {
77            'name': 'ROUTER',
78            'mode': 'rdn',
79            'allowlist': [LEADER, MED1, MED2, MED3, MED4, MED5, MED6]
80        },
81        MED1: {
82            'name': 'MED1',
83            'mode': 'rn',
84            'allowlist': [DUT_ROUTER]
85        },
86        MED2: {
87            'name': 'MED2',
88            'mode': 'rn',
89            'allowlist': [DUT_ROUTER]
90        },
91        MED3: {
92            'name': 'MED3',
93            'mode': 'rn',
94            'allowlist': [DUT_ROUTER]
95        },
96        MED4: {
97            'name': 'MED4',
98            'mode': 'rn',
99            'allowlist': [DUT_ROUTER]
100        },
101        MED5: {
102            'name': 'MED5',
103            'mode': 'rn',
104            'allowlist': [DUT_ROUTER]
105        },
106        MED6: {
107            'name': 'MED6',
108            'mode': 'rn',
109            'allowlist': [DUT_ROUTER]
110        },
111    }
112
113    def test(self):
114        self.nodes[LEADER].start()
115        self.simulator.go(config.LEADER_STARTUP_DELAY)
116        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
117
118        self.nodes[DUT_ROUTER].start()
119        self.simulator.go(config.ROUTER_STARTUP_DELAY)
120        self.assertEqual(self.nodes[DUT_ROUTER].get_state(), 'router')
121
122        for medid in range(MED1, MED6 + 1):
123            self.nodes[medid].start()
124            self.simulator.go(config.ED_STARTUP_DELAY)
125            self.assertEqual(self.nodes[medid].get_state(), 'child')
126
127        self.simulator.go(config.MAX_ADVERTISEMENT_INTERVAL)
128
129        self.nodes[DUT_ROUTER].reset()
130        # Leader will not reply to router's link request
131        self.nodes[LEADER].clear_allowlist()
132
133        self.nodes[DUT_ROUTER].start()
134
135        self.simulator.go(config.LEADER_RESET_DELAY)
136
137    def verify(self, pv):
138        pkts = pv.pkts
139        pv.summary.show()
140
141        LEADER = pv.vars['LEADER']
142        ROUTER = pv.vars['ROUTER']
143
144        # Verify topology is formed correctly.
145        pv.verify_attached('ROUTER', 'LEADER')
146        for i in range(1, 7):
147            pv.verify_attached('MED%d' % i, 'ROUTER', 'MTD')
148
149        pkts.filter_wpan_src64(ROUTER).\
150            filter_mle_advertisement('Router').\
151            must_next()
152
153        # The router MUST send MLE_MAX_CRITICAL_TRANSMISSION_COUNT multicast Link Request
154        # The following TLVs MUST be present in the Link Request:
155        #     - Challenge TLV
156        #     - Version TLV
157        #     - TLV Request TLV: Address16 TLV, Route64 TLV
158        for i in range(0, config.MLE_MAX_CRITICAL_TRANSMISSION_COUNT):
159            pkts.filter_wpan_src64(ROUTER).\
160                filter_LLARMA().\
161                filter_mle_cmd(MLE_LINK_REQUEST).\
162                filter(lambda p: {
163                                CHALLENGE_TLV,
164                                VERSION_TLV,
165                                TLV_REQUEST_TLV,
166                                ADDRESS16_TLV,
167                                ROUTE64_TLV
168                                } <= set(p.mle.tlv.type) and\
169                    p.mle.tlv.addr16 is nullField and\
170                    p.mle.tlv.route64.id_mask is nullField
171                    ).\
172                must_next()
173
174
175if __name__ == '__main__':
176    unittest.main()
177