xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/test_srp_ttl.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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#   This test verifies the SRP server and client properly handle SRP host
39#   and service instance TTLs.
40#
41# Topology:
42#     LEADER (SRP server)
43#       |
44#       |
45#     ROUTER (SRP client)
46#
47
48SERVER = 1
49CLIENT = 2
50KEY_LEASE = 240  # Seconds
51
52
53class SrpTtl(thread_cert.TestCase):
54    USE_MESSAGE_FACTORY = False
55    SUPPORT_NCP = False
56
57    TOPOLOGY = {
58        SERVER: {
59            'name': 'SRP_SERVER',
60            'mode': 'rdn',
61        },
62        CLIENT: {
63            'name': 'SRP_CLIENT',
64            'mode': 'rdn',
65        },
66    }
67
68    def test(self):
69        server = self.nodes[SERVER]
70        client = self.nodes[CLIENT]
71
72        #
73        # Start the server and client devices.
74        #
75
76        server.srp_server_set_enabled(True)
77        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
78        server.start()
79        self.simulator.go(config.LEADER_STARTUP_DELAY)
80        self.assertEqual(server.get_state(), 'leader')
81        self.simulator.go(5)
82
83        client.srp_server_set_enabled(False)
84        client.start()
85        self.simulator.go(config.ROUTER_STARTUP_DELAY)
86        self.assertEqual(client.get_state(), 'router')
87
88        self.simulator.go(15)
89        self.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled')
90
91        client.srp_client_set_host_name('my-host')
92        client.srp_client_set_host_address('2001::1')
93        client.srp_client_add_service('my-service', '_ipps._tcp', 12345)
94        self.simulator.go(2)
95
96        #
97        # CLIENT_TTL < TTL_MIN < LEASE_MAX ==> TTL_MIN
98        #
99
100        client.srp_client_set_ttl(100)
101        server.srp_server_set_ttl_range(120, 240)
102        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
103        self.simulator.go(KEY_LEASE)
104        self.check_ttl(120)
105
106        #
107        # TTL_MIN < CLIENT_TTL < TTL_MAX < LEASE_MAX ==> CLIENT_TTL
108        #
109
110        client.srp_client_set_ttl(100)
111        server.srp_server_set_ttl_range(60, 120)
112        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
113        self.simulator.go(KEY_LEASE)
114        self.check_ttl(100)
115
116        #
117        # TTL_MAX < LEASE_MAX < CLIENT_TTL ==> TTL_MAX
118        #
119
120        client.srp_client_set_ttl(240)
121        server.srp_server_set_ttl_range(60, 120)
122        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
123        self.simulator.go(KEY_LEASE)
124        self.check_ttl(120)
125
126        #
127        # LEASE_MAX < TTL_MAX < CLIENT_TTL ==> LEASE_MAX
128        #
129
130        client.srp_client_set_ttl(240)
131        server.srp_server_set_ttl_range(60, 120)
132        server.srp_server_set_lease_range(30, 60, KEY_LEASE, KEY_LEASE)
133        self.simulator.go(KEY_LEASE)
134        self.check_ttl(60)
135
136    def check_ttl(self, ttl):
137        """Check that we have properly registered host and service instance.
138        """
139
140        server = self.nodes[SERVER]
141
142        server_services = server.srp_server_get_services()
143        print(server_services)
144        self.assertEqual(len(server_services), 1)
145        server_service = server_services[0]
146
147        # Verify that the server accepted the SRP registration and stored
148        # the same service resources.
149        self.assertEqual(int(server_service['ttl']), ttl)
150
151
152if __name__ == '__main__':
153    unittest.main()
154