• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertEqual(client.srp_client_get_auto_start_mode(), 'Enabled')
89
90        client.srp_client_set_host_name('my-host')
91        client.srp_client_set_host_address('2001::1')
92        client.srp_client_add_service('my-service', '_ipps._tcp', 12345)
93        self.simulator.go(2)
94
95        #
96        # CLIENT_TTL < TTL_MIN < LEASE_MAX ==> TTL_MIN
97        #
98
99        client.srp_client_set_ttl(100)
100        server.srp_server_set_ttl_range(120, 240)
101        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
102        self.simulator.go(KEY_LEASE)
103        self.check_ttl(120)
104
105        #
106        # TTL_MIN < CLIENT_TTL < TTL_MAX < LEASE_MAX ==> CLIENT_TTL
107        #
108
109        client.srp_client_set_ttl(100)
110        server.srp_server_set_ttl_range(60, 120)
111        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
112        self.simulator.go(KEY_LEASE)
113        self.check_ttl(100)
114
115        #
116        # TTL_MAX < LEASE_MAX < CLIENT_TTL ==> TTL_MAX
117        #
118
119        client.srp_client_set_ttl(240)
120        server.srp_server_set_ttl_range(60, 120)
121        server.srp_server_set_lease_range(120, 240, KEY_LEASE, KEY_LEASE)
122        self.simulator.go(KEY_LEASE)
123        self.check_ttl(120)
124
125        #
126        # LEASE_MAX < TTL_MAX < CLIENT_TTL ==> LEASE_MAX
127        #
128
129        client.srp_client_set_ttl(240)
130        server.srp_server_set_ttl_range(60, 120)
131        server.srp_server_set_lease_range(30, 60, KEY_LEASE, KEY_LEASE)
132        self.simulator.go(KEY_LEASE)
133        self.check_ttl(60)
134
135    def check_ttl(self, ttl):
136        """Check that we have properly registered host and service instance.
137        """
138
139        server = self.nodes[SERVER]
140
141        server_services = server.srp_server_get_services()
142        print(server_services)
143        self.assertEqual(len(server_services), 1)
144        server_service = server_services[0]
145
146        # Verify that the server accepted the SRP registration and stored
147        # the same service resources.
148        self.assertEqual(int(server_service['ttl']), ttl)
149
150
151if __name__ == '__main__':
152    unittest.main()
153