911576ea6ed6e6caee4023815ae3ef92f44cfdf7
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / tests / scripts / thread-cert / Cert_6_1_09_EDSynchronization.py
1 #!/usr/bin/env python3
2 #
3 #  Copyright (c) 2016, 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
30 import unittest
31
32 import thread_cert
33 from pktverify.consts import MLE_CHILD_ID_RESPONSE, MLE_LINK_REQUEST, MLE_LINK_ACCEPT, CHALLENGE_TLV, LEADER_DATA_TLV, SOURCE_ADDRESS_TLV, VERSION_TLV
34 from pktverify.packet_verifier import PacketVerifier
35
36 LEADER = 1
37 ROUTER1 = 2
38 ED = 3
39 ROUTER2 = 4
40 ROUTER3 = 5
41
42
43 class Cert_6_1_9_EDSynchronization(thread_cert.TestCase):
44     TOPOLOGY = {
45         LEADER: {
46             'name': 'LEADER',
47             'mode': 'rsdn',
48             'panid': 0xface,
49             'whitelist': [ROUTER1, ED, ROUTER2]
50         },
51         ROUTER1: {
52             'name': 'ROUTER_1',
53             'mode': 'rsdn',
54             'panid': 0xface,
55             'router_selection_jitter': 1,
56             'whitelist': [LEADER, ED, ROUTER3]
57         },
58         ED: {
59             'name': 'ED',
60             'panid': 0xface,
61             'router_upgrade_threshold': 0,
62             'whitelist': [LEADER]
63         },
64         ROUTER2: {
65             'name': 'ROUTER_2',
66             'mode': 'rsdn',
67             'panid': 0xface,
68             'router_selection_jitter': 1,
69             'whitelist': [LEADER, ED, ROUTER3]
70         },
71         ROUTER3: {
72             'name': 'ROUTER_3',
73             'mode': 'rsdn',
74             'panid': 0xface,
75             'router_selection_jitter': 1,
76             'whitelist': [ED, ROUTER1, ROUTER2]
77         },
78     }
79
80     def test(self):
81         self.nodes[LEADER].start()
82         self.simulator.go(3)
83         self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
84
85         self.nodes[ROUTER1].start()
86         self.simulator.go(3)
87         self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
88
89         self.nodes[ROUTER2].start()
90         self.simulator.go(3)
91         self.assertEqual(self.nodes[ROUTER2].get_state(), 'router')
92
93         self.nodes[ROUTER3].start()
94         self.simulator.go(3)
95         self.assertEqual(self.nodes[ROUTER3].get_state(), 'router')
96
97         self.nodes[ED].start()
98         self.simulator.go(3)
99         self.assertEqual(self.nodes[ED].get_state(), 'child')
100
101         self.nodes[ED].add_whitelist(self.nodes[ROUTER1].get_addr64())
102         self.nodes[ED].add_whitelist(self.nodes[ROUTER2].get_addr64())
103         self.nodes[ED].add_whitelist(self.nodes[ROUTER3].get_addr64())
104         self.nodes[ED].enable_whitelist()
105         self.simulator.go(10)
106
107     def verify(self, pv):
108         pkts = pv.pkts
109         pv.summary.show()
110
111         LEADER = pv.vars['LEADER']
112         ED = pv.vars['ED']
113
114         # Step 3: The DUT MUST send a unicast Link Request
115         # to Router 1, Router 2 & Router 3
116         pkts.filter_wpan_src64(LEADER).filter_wpan_dst64(ED).filter_mle_cmd(MLE_CHILD_ID_RESPONSE).must_next()
117         for i in range(1, 3):
118             _pkts = pkts.copy()
119             _pkts.filter_wpan_src64(ED).filter_wpan_dst64(
120                 pv.vars['ROUTER_%d' % i]).filter_mle_cmd(MLE_LINK_REQUEST).must_next().must_verify(
121                     lambda p: {CHALLENGE_TLV, LEADER_DATA_TLV, SOURCE_ADDRESS_TLV, VERSION_TLV} <= set(p.mle.tlv.type))
122
123             # Step 4: Router_1, Router_2 & Router_3 MUST all send a
124             # Link Accept message to the DUT
125             _pkts.filter_wpan_src64(pv.vars['ROUTER_%d' %
126                                             i]).filter_wpan_dst64(ED).filter_mle_cmd(MLE_LINK_ACCEPT).must_next()
127
128
129 if __name__ == '__main__':
130     unittest.main()