• 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 ipaddress
31import unittest
32import logging
33
34import command
35import config
36import thread_cert
37
38# Test description:
39#   This test verifies SRP/DNS client/server support for Service Instance names with special and unicode characters.
40#
41# Topology:
42#
43#     LEADER (SRP server)
44#       |
45#       |
46#     ROUTER (SRP client)
47#
48
49SERVER = 1
50CLIENT = 2
51
52# TODO: support Instance names with dots (.)
53SPECIAL_INSTANCE_NAME = 'O\\T 网关'
54
55
56class TestDnssdNameWithSpecialChars(thread_cert.TestCase):
57    USE_MESSAGE_FACTORY = False
58    SUPPORT_NCP = False
59
60    TOPOLOGY = {
61        SERVER: {
62            'name': 'SERVER',
63            'mode': 'rdn',
64        },
65        CLIENT: {
66            'name': '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(15)
88
89        # Register a single service with the instance name containing special chars
90        client.srp_client_set_host_name('host1')
91        client.srp_client_set_host_address('2001::1')
92        client.srp_client_add_service(SPECIAL_INSTANCE_NAME, '_srv._udp', 1977)
93        self.simulator.go(5)
94        self.__check_service_discovery(server, client)
95
96    def __check_service_discovery(self, server, client):
97        # Check the service on client
98        client.dns_set_config(server.get_rloc())
99        services = client.dns_browse('_srv._udp.default.service.arpa')
100        self.assertEqual(1, len(services))
101        self.assertIn(SPECIAL_INSTANCE_NAME, services)
102        self.__verify_service(services[SPECIAL_INSTANCE_NAME])
103
104        service = client.dns_resolve_service(SPECIAL_INSTANCE_NAME, '_srv._udp.default.service.arpa')
105        self.__verify_service(service)
106
107        service = client.dns_resolve_service(SPECIAL_INSTANCE_NAME.lower(), '_srv._udp.default.service.arpa')
108        self.__verify_service(service)
109
110    def __verify_service(self, service):
111        logging.info('service discovered: %r', service)
112        self.assertEqual(service['port'], 1977)
113        self.assertEqual(service['host'], 'host1.default.service.arpa.')
114
115
116if __name__ == '__main__':
117    unittest.main()
118