1#!/usr/bin/env python3
2#
3#  Copyright (c) 2022, 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 ipaddress
31import unittest
32
33import command
34import config
35import thread_cert
36
37# Test description:
38#
39#   This test verifies SRP client behavior when there are many services
40#   causing SRP Update message going over the IPv6 MTU size. In such
41#   a case the SRP client attempts to register a single service at
42#   a time.
43#
44# Topology:
45#
46#     LEADER (SRP server)
47#       |
48#       |
49#     ROUTER (SRP client)
50#
51
52SERVER = 1
53CLIENT = 2
54
55
56class SrpManyServicesMtuCheck(thread_cert.TestCase):
57    USE_MESSAGE_FACTORY = False
58    SUPPORT_NCP = False
59
60    TOPOLOGY = {
61        SERVER: {
62            'name': 'SRP_SERVER',
63            'mode': 'rdn',
64        },
65        CLIENT: {
66            'name': 'SRP_CLIENT',
67            'mode': 'rdn',
68        },
69    }
70
71    def test(self):
72        server = self.nodes[SERVER]
73        client = self.nodes[CLIENT]
74
75        # Start the server & client devices.
76
77        server.start()
78        self.simulator.go(config.LEADER_STARTUP_DELAY)
79        self.assertEqual(server.get_state(), 'leader')
80
81        client.start()
82        self.simulator.go(config.ROUTER_STARTUP_DELAY)
83        self.assertEqual(client.get_state(), 'router')
84
85        server.srp_server_set_enabled(True)
86        client.srp_client_enable_auto_start_mode()
87        self.simulator.go(5)
88
89        # Register 8 services with long name, 6 sub-types and long txt record.
90        # The 8 services won't be fit in a single MTU (1280 bytes) UDP message
91        # SRP Client would then register services one by one.
92
93        num_services = 8
94
95        client.srp_client_set_host_name('hosthosthosthosthosthosthosthosthosthosthosthosthosthosthosthos')
96        client.srp_client_set_host_address('2001::1')
97
98        txt_info = ["xyz=987654321", "uvw=abcdefghijklmnopqrstu", "qwp=564321abcdefghij"]
99
100        for num in range(num_services):
101            name = chr(ord('a') + num) * 63
102            client.srp_client_add_service(
103                name,
104                '_longlongsrvname._udp,_subtype1,_subtype2,_subtype3,_subtype4,_subtype5,_subtype6',
105                1977,
106                txt_entries=txt_info)
107
108        self.simulator.go(10)
109
110        # Make sure all services are successfully registered
111        # (from both client and server perspectives).
112
113        client_services = client.srp_client_get_services()
114        self.assertEqual(len(client_services), num_services)
115
116        for service in client_services:
117            self.assertEqual(service['state'], 'Registered')
118
119        server_services = server.srp_server_get_services()
120        self.assertEqual(len(server_services), num_services)
121
122        # Remove all 8 services.
123
124        for num in range(num_services):
125            name = chr(ord('a') + num) * 63
126            client.srp_client_remove_service(name, '_longlongsrvname._udp')
127
128        self.simulator.go(10)
129
130        client_services = client.srp_client_get_services()
131        self.assertEqual(len(client_services), 0)
132
133        server_services = server.srp_server_get_services()
134        self.assertEqual(len(server_services), num_services)
135
136        for service in server_services:
137            self.assertEqual(service['deleted'], 'true')
138
139
140if __name__ == '__main__':
141    unittest.main()
142