Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / tests / scripts / thread-cert / Cert_6_1_02_REEDAttach_MED.py
1 #!/usr/bin/env python3
2 #
3 #  Copyright (c) 2018, 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 command import check_parent_request
34 from command import check_child_id_request
35 from command import check_child_update_request_from_child
36 from command import CheckType
37 import config
38 import mle
39
40 LEADER = 1
41 REED = 2
42 MED = 3
43
44
45 class Cert_6_1_2_REEDAttach_MED(thread_cert.TestCase):
46     TOPOLOGY = {
47         LEADER: {
48             'mode': 'rsdn',
49             'panid': 0xface,
50             'whitelist': [REED]
51         },
52         REED: {
53             'mode': 'rsdn',
54             'panid': 0xface,
55             'router_upgrade_threshold': 0,
56             'whitelist': [LEADER, MED]
57         },
58         MED: {
59             'is_mtd': True,
60             'mode': 'rsn',
61             'panid': 0xface,
62             'timeout': config.DEFAULT_CHILD_TIMEOUT,
63             'whitelist': [REED]
64         },
65     }
66
67     def test(self):
68         self.nodes[LEADER].start()
69         self.simulator.go(5)
70         self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
71
72         self.nodes[REED].start()
73         self.simulator.go(5)
74         self.assertEqual(self.nodes[REED].get_state(), 'child')
75
76         self.nodes[MED].start()
77
78         self.simulator.go(5)
79         self.assertEqual(self.nodes[MED].get_state(), 'child')
80         self.assertEqual(self.nodes[REED].get_state(), 'router')
81         med_messages = self.simulator.get_messages_sent_by(MED)
82
83         # Step 2 - DUT sends MLE Parent Request
84         msg = med_messages.next_mle_message(mle.CommandType.PARENT_REQUEST)
85         check_parent_request(msg, is_first_request=True)
86
87         # Step 4 - DUT sends MLE Parent Request again
88         msg = med_messages.next_mle_message(mle.CommandType.PARENT_REQUEST)
89         check_parent_request(msg, is_first_request=False)
90
91         # Step 6 - DUT sends Child ID Request
92         msg = med_messages.next_mle_message(mle.CommandType.CHILD_ID_REQUEST, sent_to_node=self.nodes[REED])
93         check_child_id_request(
94             msg,
95             address_registration=CheckType.CONTAIN,
96             tlv_request=CheckType.CONTAIN,
97             mle_frame_counter=CheckType.OPTIONAL,
98             route64=CheckType.OPTIONAL,
99         )
100
101         # Wait additional DEFAULT_CHILD_TIMEOUT to ensure the keep-alive
102         # message (child update request from MED) happens.
103         self.simulator.go(config.DEFAULT_CHILD_TIMEOUT)
104         med_messages = self.simulator.get_messages_sent_by(MED)
105
106         # Step 8 - DUT sends Child Update messages
107         msg = med_messages.next_mle_message(mle.CommandType.CHILD_UPDATE_REQUEST)
108         check_child_update_request_from_child(
109             msg,
110             source_address=CheckType.CONTAIN,
111             leader_data=CheckType.CONTAIN,
112         )
113
114         # Step 10 - Leader sends ICMPv6 echo request, to DUT link local address
115         med_addrs = self.nodes[MED].get_addrs()
116         for addr in med_addrs:
117             if addr[0:4] == 'fe80':
118                 self.assertTrue(self.nodes[REED].ping(addr))
119
120
121 if __name__ == '__main__':
122     unittest.main()