• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2022, 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#
29import logging
30import unittest
31
32import config
33import thread_cert
34from pktverify.consts import MLE_LINK_REQUEST
35from pktverify.packet_verifier import PacketVerifier
36
37LEADER = 1
38REED = 2
39ROUTER1, ROUTER2, ROUTER3 = 3, 4, 5
40
41# Test Purpose and Description:
42# -----------------------------
43# This test verifies if a device can quickly and efficiently establish links with
44# neighboring routers by sending a multicast Link Request message after becoming
45# a router
46#
47# Test Topology:
48# -------------
49#     Leader------+
50#    |     \       \
51# Router1 Router2 Router3
52#    \     |        |
53#     +---REED-----+
54#
55
56# REED should establish links to the three Routers within the delay threshold.
57# The max delay consists:
58#   Leader may delay 1 second for MLE Advertisement
59#   Router may delay 1 second for MLE Advertisement
60#   Router may delay 1 second for MLE Link Accept after receiving MLE Link Request (multicast)
61LINK_ESTABLISH_DELAY_THRESHOLD = 3
62
63
64class TestRouterMulticastLinkRequest(thread_cert.TestCase):
65    USE_MESSAGE_FACTORY = False
66
67    TOPOLOGY = {
68        LEADER: {
69            'name': 'LEADER',
70            'mode': 'rdn',
71            'allowlist': [ROUTER1, ROUTER2, ROUTER3]
72        },
73        ROUTER1: {
74            'name': 'ROUTER1',
75            'mode': 'rdn',
76            'allowlist': [LEADER, REED]
77        },
78        ROUTER2: {
79            'name': 'ROUTER2',
80            'mode': 'rdn',
81            'allowlist': [LEADER, REED]
82        },
83        ROUTER3: {
84            'name': 'ROUTER3',
85            'mode': 'rdn',
86            'allowlist': [LEADER, REED]
87        },
88        REED: {
89            'name': 'REED',
90            'mode': 'rdn',
91            'allowlist': [ROUTER1, ROUTER2, ROUTER3]
92        },
93    }
94
95    def test(self):
96        self.nodes[LEADER].start()
97        self.simulator.go(config.LEADER_STARTUP_DELAY)
98        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
99
100        for routerid in (ROUTER1, ROUTER2, ROUTER3):
101            self.nodes[routerid].start()
102            self.simulator.go(config.ROUTER_STARTUP_DELAY)
103            self.assertEqual(self.nodes[routerid].get_state(), 'router')
104
105        # Wait for the network to stabilize
106        self.simulator.go(60)
107
108        self.nodes[REED].start()
109        self.simulator.go(config.ROUTER_STARTUP_DELAY)
110        self.assertEqual(self.nodes[REED].get_state(), 'router')
111        self.simulator.go(LINK_ESTABLISH_DELAY_THRESHOLD + 3)
112
113        # Verify that REED has established link with all routers
114        reed_table = self.nodes[REED].router_table()
115        reed_id = self.nodes[REED].get_router_id()
116        for router in [self.nodes[ROUTER1], self.nodes[ROUTER2], self.nodes[ROUTER3]]:
117            self.assertEqual(reed_table[router.get_router_id()]['link'], 1)
118            self.assertEqual(router.router_table()[reed_id]['link'], 1)
119
120
121if __name__ == '__main__':
122    unittest.main()
123