1#!/usr/bin/env python3 2# 3# Copyright (c) 2021, 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 logging 31import unittest 32 33import pktverify 34from pktverify import packet_verifier 35import config 36import thread_cert 37 38# Test description: 39# The purpose of this test is to verify the Router's routing behavior using LLA as source address. 40# 41# Topology: 42# 43# 44# ROUTER_2 ----- ROUTER_1 ---- ROUTER_3 45# 46# 47 48ROUTER_1 = 1 49ROUTER_2 = 2 50ROUTER_3 = 3 51 52 53class TestPing(thread_cert.TestCase): 54 USE_MESSAGE_FACTORY = False 55 SUPPORT_NCP = False 56 57 TOPOLOGY = { 58 ROUTER_1: { 59 'name': 'Router_1', 60 'allowlist': [ROUTER_2, ROUTER_3], 61 }, 62 ROUTER_2: { 63 'name': 'Router_2', 64 'allowlist': [ROUTER_1], 65 }, 66 ROUTER_3: { 67 'name': 'Router_3', 68 'allowlist': [ROUTER_1], 69 }, 70 } 71 72 def test(self): 73 router1 = self.nodes[ROUTER_1] 74 router2 = self.nodes[ROUTER_2] 75 router3 = self.nodes[ROUTER_3] 76 77 router1.start() 78 self.simulator.go(config.LEADER_STARTUP_DELAY) 79 self.assertEqual('leader', router1.get_state()) 80 81 router2.start() 82 self.simulator.go(config.ROUTER_STARTUP_DELAY) 83 self.assertEqual('router', router2.get_state()) 84 85 router3.start() 86 self.simulator.go(config.ROUTER_STARTUP_DELAY) 87 self.assertEqual('router', router3.get_state()) 88 89 self.simulator.go(10) 90 91 left, center, right = router2, router1, router3 92 93 left_lla = left.get_ip6_address(config.ADDRESS_TYPE.LINK_LOCAL) 94 95 # Ping a neighbor with LLA src should succeed. 96 self.assertTrue(left.ping(center.get_mleid(), interface=left_lla)) 97 self.simulator.go(3) 98 99 # Ping a non-neighbor with LLA src should fail. 100 self.assertFalse(left.ping(right.get_mleid(), interface=left_lla)) 101 self.simulator.go(3) 102 103 center.add_route('2001::/64', stable=True) 104 center.register_netdata() 105 106 self.simulator.go(3) 107 108 # Make sure external routes are not used for LLA src. 109 self.assertFalse(left.ping("2001::1", interface=left_lla)) 110 self.simulator.go(3) 111 112 def verify(self, pv: pktverify.packet_verifier.PacketVerifier): 113 pkts = pv.pkts 114 # Make sure the Ping Request to 2001::1 was not sent 115 pkts.filter_ipv6_dst('2001::1').filter_ping_request().must_not_next() 116 117 118if __name__ == '__main__': 119 unittest.main() 120