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
35LEADER = 1
36ROUTER = 2
37ED1 = 3
38SED1 = 4
39
40MTDS = [ED1, SED1]
41
42
43class Test_OnMeshPrefix(thread_cert.TestCase):
44    TOPOLOGY = {
45        LEADER: {
46            'mode': 'rdn',
47            'allowlist': [ROUTER]
48        },
49        ROUTER: {
50            'mode': 'rdn',
51            'allowlist': [LEADER, ED1, SED1]
52        },
53        ED1: {
54            'is_mtd': True,
55            'mode': 'rn',
56            'allowlist': [ROUTER]
57        },
58        SED1: {
59            'is_mtd': True,
60            'mode': '-',
61            'timeout': config.DEFAULT_CHILD_TIMEOUT,
62            'allowlist': [ROUTER]
63        },
64    }
65
66    def test(self):
67        self.nodes[LEADER].start()
68        self.simulator.go(config.LEADER_STARTUP_DELAY)
69        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
70
71        self.nodes[ROUTER].start()
72        self.simulator.go(config.ROUTER_STARTUP_DELAY)
73        self.assertEqual(self.nodes[ROUTER].get_state(), 'router')
74
75        self.nodes[ED1].start()
76        self.simulator.go(5)
77        self.assertEqual(self.nodes[ED1].get_state(), 'child')
78
79        self.nodes[SED1].start()
80        self.simulator.go(5)
81        self.assertEqual(self.nodes[SED1].get_state(), 'child')
82
83        self.nodes[ROUTER].add_prefix('2001:2:0:1::/64', 'paros')
84        self.nodes[ROUTER].add_prefix('2001:2:0:2::/64', 'paro')
85        self.nodes[ROUTER].register_netdata()
86
87        # Set lowpan context of sniffer
88        self.simulator.set_lowpan_context(1, '2001:2:0:1::/64')
89        self.simulator.set_lowpan_context(2, '2001:2:0:2::/64')
90
91        self.simulator.go(10)
92
93        addrs = self.nodes[ED1].get_addrs()
94        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
95        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
96        for addr in addrs:
97            if addr[0:3] == '200':
98                self.assertTrue(self.nodes[LEADER].ping(addr))
99
100        addrs = self.nodes[SED1].get_addrs()
101        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
102        self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs))
103        for addr in addrs:
104            if addr[0:3] == '200':
105                self.assertTrue(self.nodes[LEADER].ping(addr))
106
107        self.nodes[ROUTER].add_prefix('2002:2:0:3::/64', 'pars')
108        self.nodes[ROUTER].register_netdata()
109
110        # Set lowpan context of sniffer
111        self.simulator.set_lowpan_context(3, '2002:2:0:3::/64')
112
113        self.simulator.go(10)
114
115        addrs = self.nodes[ED1].get_addrs()
116        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
117        self.assertTrue(any('2001:2:0:2' in addr[0:10] for addr in addrs))
118        self.assertTrue(any('2002:2:0:3' in addr[0:10] for addr in addrs))
119        for addr in addrs:
120            if addr[0:4] == '2001':
121                self.assertTrue(self.nodes[LEADER].ping(addr))
122            elif addr[0:4] == '2002':
123                self.assertFalse(self.nodes[LEADER].ping(addr))
124
125        addrs = self.nodes[SED1].get_addrs()
126        self.assertTrue(any('2001:2:0:1' in addr[0:10] for addr in addrs))
127        self.assertFalse(any('2001:2:0:2' in addr[0:10] for addr in addrs))
128        self.assertTrue(any('2002:2:0:3' in addr[0:10] for addr in addrs))
129        for addr in addrs:
130            if addr[0:4] == '2001':
131                self.assertTrue(self.nodes[LEADER].ping(addr))
132            elif addr[0:4] == '2002':
133                self.assertFalse(self.nodes[LEADER].ping(addr))
134
135
136if __name__ == '__main__':
137    unittest.main()
138