4eafce9baf643ad4e875aeff7d11c6f02df3f811
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / tests / scripts / thread-cert / Cert_5_7_01_CoapDiagCommands_A.py
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
30 import unittest
31
32 import config
33 import mle
34 import network_diag
35 import network_layer
36 import thread_cert
37 from network_diag import TlvType
38
39 LEADER = 1
40 ROUTER1 = 2
41 REED1 = 3
42 SED1 = 4
43 MED1 = 5
44 FED1 = 6
45
46 DUT = ROUTER1
47 MTDS = [MED1, SED1]
48
49
50 class Cert_5_7_01_CoapDiagCommands_A(thread_cert.TestCase):
51     SUPPORT_NCP = False
52
53     TOPOLOGY = {
54         LEADER: {
55             'whitelist': [ROUTER1],
56         },
57         ROUTER1: {
58             'whitelist': [LEADER, REED1, SED1, MED1, FED1],
59             'router_selection_jitter': 1
60         },
61         REED1: {
62             'whitelist': [ROUTER1],
63             'router_upgrade_threshold': 0
64         },
65         SED1: {
66             'is_mtd': True,
67             'mode': 's',
68             'whitelist': [ROUTER1],
69             'timeout': config.DEFAULT_CHILD_TIMEOUT
70         },
71         MED1: {
72             'is_mtd': True,
73             'mode': 'rsn',
74             'whitelist': [ROUTER1]
75         },
76         FED1: {
77             'whitelist': [ROUTER1],
78             'router_upgrade_threshold': 0
79         },
80     }
81
82     def test(self):
83         # 1 - Form topology
84         self.nodes[LEADER].start()
85         self.simulator.go(5)
86         self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
87
88         self.nodes[ROUTER1].start()
89         self.simulator.go(5)
90         self.assertEqual(self.nodes[ROUTER1].get_state(), 'router')
91
92         self.nodes[REED1].start()
93         self.simulator.go(5)
94         self.assertEqual(self.nodes[REED1].get_state(), 'child')
95
96         self.nodes[SED1].start()
97         self.simulator.go(10)
98         self.assertEqual(self.nodes[SED1].get_state(), 'child')
99
100         self.nodes[MED1].start()
101         self.simulator.go(5)
102         self.assertEqual(self.nodes[MED1].get_state(), 'child')
103
104         self.nodes[FED1].start()
105         self.simulator.go(5)
106         self.assertEqual(self.nodes[FED1].get_state(), 'child')
107
108         dut_rloc = self.nodes[DUT].get_ip6_address(config.ADDRESS_TYPE.RLOC)
109
110         # 2 - Leader sends DIAG_GET.req
111         tlv_types = [
112             TlvType.EXT_ADDRESS, TlvType.ADDRESS16, TlvType.MODE, TlvType.CONNECTIVITY, TlvType.ROUTE64,
113             TlvType.LEADER_DATA, TlvType.NETWORK_DATA, TlvType.IPV6_ADDRESS_LIST, TlvType.CHANNEL_PAGES
114         ]
115         self.nodes[LEADER].send_network_diag_get(dut_rloc, tlv_types)
116         self.simulator.go(2)
117
118         dut_messages = self.simulator.get_messages_sent_by(DUT)
119
120         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
121         diag_get_rsp.assertCoapMessageContainsTlv(network_layer.MacExtendedAddress)
122         diag_get_rsp.assertCoapMessageContainsTlv(mle.Address16)
123         diag_get_rsp.assertCoapMessageContainsTlv(mle.Mode)
124         diag_get_rsp.assertCoapMessageContainsTlv(mle.Connectivity)
125         diag_get_rsp.assertCoapMessageContainsTlv(mle.Route64)
126         diag_get_rsp.assertCoapMessageContainsTlv(mle.LeaderData)
127         diag_get_rsp.assertCoapMessageContainsTlv(mle.NetworkData)
128         diag_get_rsp.assertCoapMessageContainsTlv(network_diag.Ipv6AddressList)
129         diag_get_rsp.assertCoapMessageContainsTlv(network_diag.ChannelPages)
130
131         # 3 - Leader sends DIAG_GET.req (MAC Counters TLV type included)
132         self.nodes[LEADER].send_network_diag_get(dut_rloc, [TlvType.MAC_COUNTERS])
133         self.simulator.go(2)
134
135         dut_messages = self.simulator.get_messages_sent_by(DUT)
136         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
137         diag_get_rsp.assertCoapMessageContainsTlv(network_diag.MacCounters)
138         mac_counters = diag_get_rsp.get_coap_message_tlv(network_diag.MacCounters)
139
140         # 4 - Leader sends DIAG_GET.req (Timeout/Polling Period TLV type included)
141         self.nodes[LEADER].send_network_diag_get(dut_rloc, [TlvType.POLLING_PERIOD])
142         self.simulator.go(2)
143
144         dut_messages = self.simulator.get_messages_sent_by(DUT)
145         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
146         diag_get_rsp.assertCoapMessageDoesNotContainTlv(mle.Timeout)
147
148         # 5 - Leader sends DIAG_GET.req (Battery Level and Supply Voltage TLV types included)
149         self.nodes[LEADER].send_network_diag_get(dut_rloc, [TlvType.BATTERY_LEVEL, TlvType.SUPPLY_VOLTAGE])
150         self.simulator.go(2)
151
152         dut_messages = self.simulator.get_messages_sent_by(DUT)
153         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
154         diag_get_rsp.assertCoapMessageContainsOptionalTlv(network_diag.BatteryLevel)
155         diag_get_rsp.assertCoapMessageContainsOptionalTlv(network_diag.SupplyVoltage)
156
157         # 6 - Leader sends DIAG_GET.req (Child Table TLV types included)
158         self.nodes[LEADER].send_network_diag_get(dut_rloc, [TlvType.CHILD_TABLE])
159         self.simulator.go(2)
160
161         dut_messages = self.simulator.get_messages_sent_by(DUT)
162         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
163         diag_get_rsp.assertCoapMessageContainsTlv(network_diag.ChildTable)
164         child_table = diag_get_rsp.get_coap_message_tlv(network_diag.ChildTable)
165         self.assertEqual(len(child_table.children), 4)
166         # TODO(wgtdkp): more validations
167
168         # 7 - Leader sends DIAG_RST.ntf (MAC Counters TLV type included)
169         self.nodes[LEADER].send_network_diag_reset(dut_rloc, [TlvType.MAC_COUNTERS])
170         self.simulator.go(2)
171
172         dut_messages = self.simulator.get_messages_sent_by(DUT)
173
174         # Make sure the response is there.
175         dut_messages.next_coap_message(code='2.04')
176
177         # 8 - Leader Sends DIAG_GET.req (MAC Counters TLV type included)
178         self.nodes[LEADER].send_network_diag_get(dut_rloc, [TlvType.MAC_COUNTERS])
179         self.simulator.go(2)
180
181         dut_messages = self.simulator.get_messages_sent_by(DUT)
182         diag_get_rsp = dut_messages.next_coap_message(code='2.04')
183         diag_get_rsp.assertCoapMessageContainsTlv(network_diag.MacCounters)
184         reset_mac_counters = diag_get_rsp.get_coap_message_tlv(network_diag.MacCounters)
185
186         self.assertEqual(len(mac_counters.counters), len(reset_mac_counters.counters))
187         for old_counter, new_counter in zip(mac_counters.counters, reset_mac_counters.counters):
188             self.assertTrue(new_counter == 0 or new_counter < old_counter)
189
190
191 if __name__ == '__main__':
192     unittest.main()