• 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#   This test verifies SRP function that SRP clients can register up to 500 services to one SRP server.
37#
38# Topology:
39#     LEADER (SRP server)
40#       |
41#       |
42#     ROUTER1 ... ROUTER12 ROUTER13     (SRP clients)
43#       |            |
44#      FED14  ...   FED25               (SRP clients)
45#
46
47NUM_SRP_CLIENTS = 25
48INSTANCE_NUM_PER_CLIENT = 20
49NUM_ROUTERS = (NUM_SRP_CLIENTS // 2) + (NUM_SRP_CLIENTS % 2)
50NUM_FEDS = NUM_SRP_CLIENTS - NUM_ROUTERS
51assert (NUM_FEDS <= NUM_ROUTERS)
52
53SERVER = NUM_SRP_CLIENTS + 1
54CLIENT1 = 1
55
56ROUTER_IDS = tuple(range(1, NUM_ROUTERS + 1))
57FED_IDS = tuple(range(NUM_ROUTERS + 1, NUM_SRP_CLIENTS + 1))
58assert len(FED_IDS) == NUM_FEDS
59CLIENT_IDS = tuple(range(1, NUM_SRP_CLIENTS + 1))
60INSTANCE_IDS = tuple(range(1, INSTANCE_NUM_PER_CLIENT + 1))
61SERVICE_NAME = '_srpclient._tcp'
62SERVICE_PORT = 12345
63
64
65class SrpRegister500Services(thread_cert.TestCase):
66    USE_MESSAGE_FACTORY = False
67    SUPPORT_NCP = False
68
69    TOPOLOGY = {
70        SERVER: {
71            'name': 'SRP_SERVER',
72            'mode': 'rdn',
73            'allowlist': [],
74        },
75    }
76
77    for nodeid in ROUTER_IDS:
78        TOPOLOGY[nodeid] = {
79            'name': f'SRP_CLIENT_{nodeid}',
80            'mode': 'rdn',
81            'allowlist': [SERVER],
82        }
83        TOPOLOGY[SERVER]['allowlist'].append(nodeid)
84
85    for nodeid in FED_IDS:
86        parentid = nodeid - NUM_ROUTERS
87        TOPOLOGY[nodeid] = {
88            'name': f'SRP_CLIENT_{nodeid}',
89            'mode': 'rdn',
90            'router_eligible': False,
91            'allowlist': [parentid],
92        }
93        TOPOLOGY[parentid]['allowlist'].append(nodeid)
94
95    def test(self):
96        self._test_impl()
97
98    def _test_impl(self):
99        server = self.nodes[SERVER]
100
101        def routers():
102            for nodeid in ROUTER_IDS:
103                yield self.nodes[nodeid]
104
105        def feds():
106            for nodeid in FED_IDS:
107                yield self.nodes[nodeid]
108
109        def clients():
110            for nodeid in CLIENT_IDS:
111                yield self.nodes[nodeid]
112
113        # Start the server & clients.
114        server.srp_server_set_enabled(True)
115        server.start()
116        self.simulator.go(config.LEADER_STARTUP_DELAY)
117        self.assertEqual(server.get_state(), 'leader')
118
119        for router in routers():
120            router.srp_server_set_enabled(False)
121            router.start()
122            self.simulator.go(config.ROUTER_STARTUP_DELAY)
123
124        for router in routers():
125            self.assertEqual(router.get_state(), 'router')
126
127        for fed in feds():
128            fed.srp_server_set_enabled(False)
129            fed.start()
130            self.simulator.go(3)
131
132        for fed in feds():
133            self.assertEqual(fed.get_state(), 'child')
134
135        for clientid in CLIENT_IDS:
136            client = self.nodes[clientid]
137            self.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled')
138            client.srp_client_set_host_name(f'client{clientid}')
139            client.srp_client_set_host_address(f'2001::{clientid}')
140
141            for instanceid in INSTANCE_IDS:
142                client.srp_client_add_service(f'client{clientid}_{instanceid}', SERVICE_NAME, SERVICE_PORT)
143
144                if instanceid % 10 == 0 or instanceid == INSTANCE_IDS[-1]:
145                    self._wait_client_all_services_registered(client)
146
147            self._check_client_host_and_services(client, clientid)
148
149        # Make sure all service are registered successfully
150        self._check_server_hosts_and_services(server)
151
152    def _wait_client_all_services_registered(self, client, timeout=60):
153        all_registered = False
154        while timeout > 0:
155            self.simulator.go(3)
156            timeout -= 3
157
158            client_services = client.srp_client_get_services()
159            all_registered = all(service['state'] == 'Registered' for service in client_services)
160            if all_registered:
161                break
162
163        self.assertTrue(all_registered, "Some services are not registered successfully")
164
165    def _check_client_host_and_services(self, client, clientid):
166        client_services = client.srp_client_get_services()
167        self.assertEqual(len(client_services), INSTANCE_NUM_PER_CLIENT)
168        service_instance_names = {f'client{clientid}_{instanceid}' for instanceid in INSTANCE_IDS}
169        for service in client_services:
170            self.assertEqual(service['name'], SERVICE_NAME)
171            self.assertEqual(service['state'], 'Registered')
172            self.assertEqual(int(service['port']), SERVICE_PORT)
173            self.assertIn(service['instance'], service_instance_names)
174            service_instance_names.discard(service['instance'])
175
176    def _check_server_hosts_and_services(self, server):
177        server_services = server.srp_server_get_services()
178        self.assertEqual(len(server_services), NUM_SRP_CLIENTS * INSTANCE_NUM_PER_CLIENT)
179
180        service_instance_names = {
181            f'client{clientid}_{instanceid}' for clientid in CLIENT_IDS for instanceid in INSTANCE_IDS
182        }
183
184        for service in server_services:
185            self.assertEqual(service['name'], SERVICE_NAME)
186            self.assertIn(service['instance'], service_instance_names)
187            service_instance_names.discard(service['instance'])
188
189
190if __name__ == '__main__':
191    unittest.main()
192