Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / tests / scripts / thread-cert / Cert_6_1_02_REEDAttach_SED.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 CheckType
36 import config
37 import mac802154
38 import message
39 import mle
40
41 LEADER = 1
42 REED = 2
43 SED = 3
44
45
46 class Cert_6_1_2_REEDAttach_SED(thread_cert.TestCase):
47     TOPOLOGY = {
48         LEADER: {
49             'mode': 'rsdn',
50             'panid': 0xface,
51             'whitelist': [REED]
52         },
53         REED: {
54             'mode': 'rsdn',
55             'panid': 0xface,
56             'router_upgrade_threshold': 0,
57             'whitelist': [LEADER, SED]
58         },
59         SED: {
60             'is_mtd': True,
61             'mode': 's',
62             'panid': 0xface,
63             'timeout': config.DEFAULT_CHILD_TIMEOUT,
64             'whitelist': [REED]
65         },
66     }
67
68     def test(self):
69         self.nodes[LEADER].start()
70         self.simulator.go(5)
71         self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
72
73         self.nodes[REED].start()
74         self.simulator.go(5)
75         self.assertEqual(self.nodes[REED].get_state(), 'child')
76
77         self.nodes[SED].start()
78         self.simulator.go(5)
79         self.assertEqual(self.nodes[SED].get_state(), 'child')
80         self.assertEqual(self.nodes[REED].get_state(), 'router')
81
82         sed_messages = self.simulator.get_messages_sent_by(SED)
83
84         # Step 2 - DUT sends MLE Parent Request
85         msg = sed_messages.next_mle_message(mle.CommandType.PARENT_REQUEST)
86         check_parent_request(msg, is_first_request=True)
87
88         # Step 4 - DUT sends MLE Parent Request again
89         msg = sed_messages.next_mle_message(mle.CommandType.PARENT_REQUEST)
90         check_parent_request(msg, is_first_request=False)
91
92         # Step 6 - DUT sends Child ID Request
93         msg = sed_messages.next_mle_message(mle.CommandType.CHILD_ID_REQUEST, sent_to_node=self.nodes[REED])
94         check_child_id_request(
95             msg,
96             address_registration=CheckType.CONTAIN,
97             tlv_request=CheckType.CONTAIN,
98             mle_frame_counter=CheckType.OPTIONAL,
99             route64=CheckType.OPTIONAL,
100         )
101
102         # Wait DEFAULT_CHILD_TIMEOUT seconds,
103         # ensure SED has received the CHILD_ID_RESPONSE,
104         # and the next data requests would be keep-alive messages
105         self.simulator.go(config.DEFAULT_CHILD_TIMEOUT)
106         sed_messages = self.simulator.get_messages_sent_by(SED)
107
108         # Step 11 - SED sends periodic 802.15.4 Data Request messages
109         msg = sed_messages.next_message()
110         self.assertEqual(
111             False, msg.isMacAddressTypeLong())  # Extra check, keep-alive messages are of short types of mac address
112         self.assertEqual(msg.type, message.MessageType.COMMAND)
113         self.assertEqual(
114             msg.mac_header.command_type,
115             mac802154.MacHeader.CommandIdentifier.DATA_REQUEST,
116         )
117
118         # Step 12 - REED sends ICMPv6 echo request, to DUT link local address
119         sed_addrs = self.nodes[SED].get_addrs()
120         for addr in sed_addrs:
121             if addr[0:4] == 'fe80':
122                 self.assertTrue(self.nodes[REED].ping(addr))
123
124
125 if __name__ == '__main__':
126     unittest.main()