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