• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2021, 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
34
35# Test description:
36#
37#   This test covers the SRP server's behavior using different address
38#   modes (unicast or anycast). The address mode indicates how the SRP
39#   server determines its address and port and how this info is
40#   published in the Thread Network Data. When using anycast address
41#   mode, the SRP server and DNS server/resolver will both listen on port
42#   53 and they re-use the same socket. This test verifies the behavior of
43#   both modules in such a situation.
44#
45#
46# Topology:
47#
48#   One leader and two router nodes, all connected. The leader acts as SRP
49#   server and DNS resolver. One router acts as SRP client, and the
50#   other acts as DNS client browsing for registered service names.
51#
52
53SERVER = 1
54CLIENT = 2
55BROWSER = 3
56
57DOMAIN = 'default.service.arpa.'
58HOST = 'host'
59INSTANCE = 'ins1'
60SERVICE = '_srv._udp'
61SERVICE_PORT = 1313
62
63SRP_SERVER_ANYCAST_PORT = 53
64SRP_SERVER_ANYCAST_SEQ_NUM = 17
65DNS_RESOLVER_PORT = 53
66
67THREAD_ENTERPRISE_NUMBER = 44970
68ANYCAST_SERVICE_NUM = 0x5c
69UNICAST_SERVICE_NUM = 0x5d
70
71
72class TestSrpServerAnycastMode(thread_cert.TestCase):
73    SUPPORT_NCP = False
74    USE_MESSAGE_FACTORY = False
75
76    TOPOLOGY = {
77        SERVER: {
78            'mode': 'rdn',
79        },
80        CLIENT: {
81            'mode': 'rdn',
82        },
83        BROWSER: {
84            'mode': 'rdn',
85        },
86    }
87
88    def test(self):
89        server = self.nodes[SERVER]
90        client = self.nodes[CLIENT]
91        browser = self.nodes[BROWSER]
92
93        #-------------------------------------------------------------------
94        # Form the network.
95
96        server.start()
97        self.simulator.go(config.LEADER_STARTUP_DELAY)
98        self.assertEqual(server.get_state(), 'leader')
99
100        client.start()
101        browser.start()
102        self.simulator.go(config.ROUTER_STARTUP_DELAY)
103        self.assertEqual(client.get_state(), 'router')
104        self.assertEqual(browser.get_state(), 'router')
105
106        #-------------------------------------------------------------------
107        # Go through the entire test twice, first time using anycast address
108        # mode and second time using unicast address mode.
109
110        for addr_mode in ['anycast', 'unicast']:
111
112            #---------------------------------------------------------------
113            # Set the SRP server address mode and start the SRP server.
114
115            server.srp_server_set_addr_mode(addr_mode)
116
117            if addr_mode == 'anycast':
118                server.srp_server_set_anycast_seq_num(SRP_SERVER_ANYCAST_SEQ_NUM)
119                self.assertEqual(server.srp_server_get_anycast_seq_num(), SRP_SERVER_ANYCAST_SEQ_NUM)
120
121            self.assertEqual(server.srp_server_get_addr_mode(), addr_mode)
122            server.srp_server_set_enabled(True)
123            self.simulator.go(5)
124
125            #---------------------------------------------------------------
126            # Verify the published SRP server info in the Network Data.
127
128            netdata_services = client.get_services()
129            self.assertEqual(len(netdata_services), 1)
130            netdata_service = netdata_services[0]
131
132            self.assertEqual(int(netdata_service[0]), THREAD_ENTERPRISE_NUMBER)
133            data = bytes.fromhex(netdata_service[1])
134            self.assertEqual(netdata_service[3], 's')
135
136            if addr_mode == 'anycast':
137                self.assertTrue(len(data) >= 2)
138                self.assertEqual(data[0], ANYCAST_SERVICE_NUM)
139                self.assertEqual(data[1], SRP_SERVER_ANYCAST_SEQ_NUM)
140            else:
141                self.assertTrue(len(data) >= 1)
142                self.assertEqual(data[0], UNICAST_SERVICE_NUM)
143                self.assertEqual(netdata_service[3], 's')
144
145            #---------------------------------------------------------------
146            # Enable auto-start on SRP client. Verify that it does find the
147            # server and uses the proper address and port number.
148
149            client.srp_client_enable_auto_start_mode()
150            self.simulator.go(5)
151
152            if addr_mode == 'anycast':
153                server_alocs = server.get_ip6_address(config.ADDRESS_TYPE.ALOC)
154                self.assertEqual(client.srp_client_get_state(), 'Enabled')
155                self.assertIn(client.srp_client_get_server_address(), server_alocs)
156                self.assertEqual(client.srp_client_get_server_port(), SRP_SERVER_ANYCAST_PORT)
157            else:
158                self.assertIn(client.srp_client_get_server_address(), server.get_mleid())
159                self.assertEqual(client.srp_client_get_server_port(), server.get_srp_server_port())
160
161            #---------------------------------------------------------------
162            # Add a service on the SRP client and verify its successful
163            # registration with SRP server.
164
165            client.srp_client_set_host_name(HOST)
166            client.srp_client_set_host_address(client.get_mleid())
167            client.srp_client_add_service(INSTANCE, SERVICE, SERVICE_PORT)
168            self.simulator.go(5)
169
170            client_services = client.srp_client_get_services()
171            self.assertEqual(len(client_services), 1)
172            client_service = client_services[0]
173            self.assertEqual(client_service['instance'], INSTANCE)
174            self.assertEqual(client_service['name'], SERVICE)
175            self.assertEqual(int(client_service['port']), SERVICE_PORT)
176            self.assertEqual(client_service['state'], 'Registered')
177
178            #---------------------------------------------------------------
179            # Browse for a matching service name and verify that the registered
180            # service is successfully found.
181
182            service_instances = browser.dns_browse(f'{SERVICE}.{DOMAIN}', server.get_mleid(), DNS_RESOLVER_PORT)
183            self.assertEqual({INSTANCE}, set(service_instances.keys()))
184
185            service_instance = service_instances[INSTANCE]
186            self.assertEqual(service_instance['host'], f'{HOST}.{DOMAIN}')
187            self.assertEqual(int(service_instance['port']), SERVICE_PORT)
188            self.assertEqual(service_instance['address'], client.get_mleid())
189
190            #---------------------------------------------------------------
191            # Stop SRP client and server and clear host (and service) on the
192            # client.
193
194            client.srp_client_clear_host()
195            client.srp_client_stop()
196            server.srp_server_set_enabled(False)
197
198
199if __name__ == '__main__':
200    unittest.main()
201