1#!/usr/bin/env python3
2#
3#  Copyright (c) 2019, 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
34import mle
35import network_layer
36
37LEADER = 1
38ROUTER_1 = 2
39
40
41class Router_5_1_01(thread_cert.TestCase):
42    TOPOLOGY = {
43        LEADER: {
44            'version': '1.2'
45        },
46        ROUTER_1: {
47            'version': '1.2'
48        },
49    }
50    """All nodes are created with default configurations"""
51
52    def test(self):
53        self.nodes[ROUTER_1].set_router_selection_jitter(1)
54
55        self.nodes[LEADER].start()
56        self.simulator.go(config.LEADER_STARTUP_DELAY)
57        self.assertEqual(self.nodes[LEADER].get_state(), 'leader')
58
59        self.nodes[ROUTER_1].start()
60        self.simulator.go(config.ROUTER_STARTUP_DELAY)
61        self.assertEqual(self.nodes[ROUTER_1].get_state(), 'router')
62
63        leader_messages = self.simulator.get_messages_sent_by(LEADER)
64        router_messages = self.simulator.get_messages_sent_by(ROUTER_1)
65
66        # 1 - Leader transmits MLE advertisements
67        msg = leader_messages.next_mle_message(mle.CommandType.ADVERTISEMENT)
68        msg.assertSentWithHopLimit(255)
69        msg.assertSentToDestinationAddress('ff02::1')
70        msg.assertMleMessageContainsTlv(mle.SourceAddress)
71        msg.assertMleMessageContainsTlv(mle.LeaderData)
72        msg.assertMleMessageContainsTlv(mle.Route64)
73
74        # 2 - Router_1 begins attach process by sending a multicast MLE Parent Request
75        msg = router_messages.next_mle_message(mle.CommandType.PARENT_REQUEST)
76        msg.assertSentWithHopLimit(255)
77        msg.assertSentToDestinationAddress('ff02::2')
78        msg.assertMleMessageContainsTlv(mle.Mode)
79        msg.assertMleMessageContainsTlv(mle.Challenge)
80        msg.assertMleMessageContainsTlv(mle.ScanMask)
81        msg.assertMleMessageContainsTlv(mle.Version)
82        self.assertGreaterEqual(msg.get_mle_message_tlv(mle.Version).version, config.THREAD_VERSION_1_2)
83
84        scan_mask_tlv = msg.get_mle_message_tlv(mle.ScanMask)
85        self.assertEqual(1, scan_mask_tlv.router)
86        self.assertEqual(0, scan_mask_tlv.end_device)
87
88        # 3 - Leader sends a MLE Parent Response
89        msg = leader_messages.next_mle_message(mle.CommandType.PARENT_RESPONSE)
90        msg.assertSentToNode(self.nodes[ROUTER_1])
91        msg.assertMleMessageContainsTlv(mle.SourceAddress)
92        msg.assertMleMessageContainsTlv(mle.LeaderData)
93        msg.assertMleMessageContainsTlv(mle.LinkLayerFrameCounter)
94        msg.assertMleMessageContainsOptionalTlv(mle.MleFrameCounter)
95        msg.assertMleMessageContainsTlv(mle.Response)
96        msg.assertMleMessageContainsTlv(mle.Challenge)
97        msg.assertMleMessageContainsTlv(mle.LinkMargin)
98        msg.assertMleMessageContainsTlv(mle.Connectivity)
99        msg.assertMleMessageContainsTlv(mle.Version)
100        self.assertGreaterEqual(msg.get_mle_message_tlv(mle.Version).version, config.THREAD_VERSION_1_2)
101
102        # 4 - Router_1 receives the MLE Parent Response and sends a Child ID Request
103        msg = router_messages.next_mle_message(mle.CommandType.CHILD_ID_REQUEST)
104        msg.assertSentToNode(self.nodes[LEADER])
105        msg.assertMleMessageContainsTlv(mle.Response)
106        msg.assertMleMessageContainsTlv(mle.LinkLayerFrameCounter)
107        msg.assertMleMessageContainsOptionalTlv(mle.MleFrameCounter)
108        msg.assertMleMessageContainsTlv(mle.Mode)
109        msg.assertMleMessageContainsTlv(mle.Timeout)
110        msg.assertMleMessageContainsTlv(mle.Version)
111        msg.assertMleMessageContainsTlv(mle.TlvRequest)
112        msg.assertMleMessageDoesNotContainTlv(mle.AddressRegistration)
113        self.assertGreaterEqual(msg.get_mle_message_tlv(mle.Version).version, config.THREAD_VERSION_1_2)
114
115        # 5 - Leader responds with a Child ID Response
116        msg = leader_messages.next_mle_message(mle.CommandType.CHILD_ID_RESPONSE)
117        msg.assertSentToNode(self.nodes[ROUTER_1])
118        msg.assertMleMessageContainsTlv(mle.SourceAddress)
119        msg.assertMleMessageContainsTlv(mle.LeaderData)
120        msg.assertMleMessageContainsTlv(mle.Address16)
121        msg.assertMleMessageContainsOptionalTlv(mle.NetworkData)
122        msg.assertMleMessageContainsOptionalTlv(mle.Route64)
123        msg.assertMleMessageContainsOptionalTlv(mle.AddressRegistration)
124
125        # 6 - Router_1 sends an Address Solicit Request
126        msg = router_messages.next_coap_message('0.02')
127        msg.assertCoapMessageRequestUriPath('/a/as')
128        msg.assertCoapMessageContainsTlv(network_layer.MacExtendedAddress)
129        msg.assertCoapMessageContainsTlv(network_layer.Status)
130
131        # 7 - Leader sends an Address Solicit Response
132        msg = leader_messages.next_coap_message('2.04')
133        msg.assertCoapMessageContainsTlv(network_layer.Status)
134        msg.assertCoapMessageContainsOptionalTlv(network_layer.RouterMask)
135
136        status_tlv = msg.get_coap_message_tlv(network_layer.Status)
137        self.assertEqual(network_layer.StatusValues.SUCCESS, status_tlv.status)
138
139        # 8 - Router_1 Transmit MLE advertisements
140        msg = router_messages.next_mle_message(mle.CommandType.ADVERTISEMENT)
141        msg.assertSentWithHopLimit(255)
142        msg.assertSentToDestinationAddress('ff02::1')
143        msg.assertMleMessageContainsTlv(mle.SourceAddress)
144        msg.assertMleMessageContainsTlv(mle.LeaderData)
145        msg.assertMleMessageContainsTlv(mle.Route64)
146
147        # 9 - Verify connectivity by sending an ICMPv6 Echo Request to the DUT link local address
148        self.assertTrue(self.nodes[LEADER].ping(self.nodes[ROUTER_1].get_linklocal()))
149        self.assertTrue(self.nodes[ROUTER_1].ping(self.nodes[LEADER].get_linklocal()))
150
151
152if __name__ == '__main__':
153    unittest.main()
154