1#!/usr/bin/env python3 2# 3# Copyright (c) 2020, 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 thread_cert 34 35 36class test_router_reattach(thread_cert.TestCase): 37 TOPOLOGY = { 38 1: { 39 'mode': 'rdn', 40 'router_downgrade_threshold': 32, 41 'router_upgrade_threshold': 32 42 }, 43 2: { 44 'mode': 'rdn', 45 'router_downgrade_threshold': 32, 46 'router_upgrade_threshold': 32 47 }, 48 3: { 49 'mode': 'rdn', 50 'router_downgrade_threshold': 32, 51 'router_upgrade_threshold': 32 52 }, 53 4: { 54 'mode': 'rdn', 55 'router_downgrade_threshold': 32, 56 'router_upgrade_threshold': 32 57 }, 58 5: { 59 'mode': 'rdn', 60 'router_downgrade_threshold': 32, 61 'router_upgrade_threshold': 32 62 }, 63 6: { 64 'mode': 'rdn', 65 'router_downgrade_threshold': 32, 66 'router_upgrade_threshold': 32 67 }, 68 7: { 69 'mode': 'rdn', 70 'router_downgrade_threshold': 32, 71 'router_upgrade_threshold': 32 72 }, 73 8: { 74 'mode': 'rdn', 75 'router_downgrade_threshold': 32, 76 'router_upgrade_threshold': 32 77 }, 78 9: { 79 'mode': 'rdn', 80 'router_downgrade_threshold': 32, 81 'router_upgrade_threshold': 32 82 }, 83 10: { 84 'mode': 'rdn', 85 'router_downgrade_threshold': 32, 86 'router_upgrade_threshold': 32 87 }, 88 11: { 89 'mode': 'rdn', 90 'router_downgrade_threshold': 32, 91 'router_upgrade_threshold': 32 92 }, 93 12: { 94 'mode': 'rdn', 95 'router_downgrade_threshold': 32, 96 'router_upgrade_threshold': 32 97 }, 98 13: { 99 'mode': 'rdn', 100 'router_downgrade_threshold': 32, 101 'router_upgrade_threshold': 32 102 }, 103 14: { 104 'mode': 'rdn', 105 'router_downgrade_threshold': 32, 106 'router_upgrade_threshold': 32 107 }, 108 15: { 109 'mode': 'rdn', 110 'router_downgrade_threshold': 32, 111 'router_upgrade_threshold': 32 112 }, 113 16: { 114 'mode': 'rdn', 115 'router_downgrade_threshold': 32, 116 'router_upgrade_threshold': 32 117 }, 118 17: { 119 'mode': 'rdn', 120 'router_downgrade_threshold': 32, 121 'router_upgrade_threshold': 32 122 }, 123 18: { 124 'mode': 'rdn', 125 'router_downgrade_threshold': 32, 126 'router_upgrade_threshold': 32 127 }, 128 19: { 129 'mode': 'rdn', 130 'router_downgrade_threshold': 32, 131 'router_upgrade_threshold': 32 132 }, 133 20: { 134 'mode': 'rdn', 135 'router_downgrade_threshold': 32, 136 'router_upgrade_threshold': 32 137 }, 138 21: { 139 'mode': 'rdn', 140 'router_downgrade_threshold': 32, 141 'router_upgrade_threshold': 32 142 }, 143 22: { 144 'mode': 'rdn', 145 'router_downgrade_threshold': 32, 146 'router_upgrade_threshold': 32 147 }, 148 23: { 149 'mode': 'rdn', 150 'router_downgrade_threshold': 32, 151 'router_upgrade_threshold': 32 152 }, 153 24: { 154 'mode': 'rdn', 155 'router_downgrade_threshold': 32, 156 'router_upgrade_threshold': 32 157 }, 158 25: { 159 'mode': 'rdn', 160 'router_downgrade_threshold': 32, 161 'router_upgrade_threshold': 32 162 }, 163 26: { 164 'mode': 'rdn', 165 'router_downgrade_threshold': 32, 166 'router_upgrade_threshold': 32 167 }, 168 27: { 169 'mode': 'rdn', 170 'router_downgrade_threshold': 32, 171 'router_upgrade_threshold': 32 172 }, 173 28: { 174 'mode': 'rdn', 175 'router_downgrade_threshold': 32, 176 'router_upgrade_threshold': 32 177 }, 178 29: { 179 'mode': 'rdn', 180 'router_downgrade_threshold': 32, 181 'router_upgrade_threshold': 32 182 }, 183 30: { 184 'mode': 'rdn', 185 'router_downgrade_threshold': 32, 186 'router_upgrade_threshold': 32 187 }, 188 31: { 189 'mode': 'rdn', 190 'router_downgrade_threshold': 32, 191 'router_upgrade_threshold': 32 192 }, 193 32: { 194 'mode': 'rdn', 195 'router_downgrade_threshold': 32, 196 'router_upgrade_threshold': 32 197 }, 198 } 199 200 def test(self): 201 self.nodes[1].start() 202 self.simulator.go(config.LEADER_STARTUP_DELAY) 203 self.assertEqual(self.nodes[1].get_state(), 'leader') 204 205 for i in range(2, 33): 206 self.nodes[i].start() 207 self.simulator.go(config.ROUTER_STARTUP_DELAY) 208 self.assertEqual(self.nodes[i].get_state(), 'router') 209 210 self.nodes[2].reset() 211 self.nodes[2].set_router_selection_jitter(3) 212 self.nodes[2].set_router_upgrade_threshold(32) 213 self.nodes[2].set_router_downgrade_threshold(32) 214 215 self.nodes[2].start() 216 self.assertEqual(self.nodes[2].get_router_downgrade_threshold(), 32) 217 # Verify that the node restored as Router. 218 self.simulator.go(1) 219 self.assertEqual(self.nodes[2].get_state(), 'router') 220 # Verify that the node does not downgrade after Router Selection Jitter. 221 self.simulator.go(5) 222 self.assertEqual(self.nodes[2].get_state(), 'router') 223 224 225if __name__ == '__main__': 226 unittest.main() 227