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 unittest 31 32import command 33import config 34import thread_cert 35 36# Test description: 37# 38# This test verifies the behavior of DNS client feature where the SRP 39# server address (from SRP client auto-start feature) is used as the 40# default IPv6 address for DNS resolver config when user does not 41# explicitly set a default address 42# 43# Topology: two nodes 44# 45# leader (SRP server & client & DNS client) --- router (SRP client & DNS client) 46# 47 48LEADER = 1 49ROUTER = 2 50 51DEFAULT_ADDRESS = 'fd00:1:2:3:4:5:6:7' 52 53 54class DnsClientConfigAutoStart(thread_cert.TestCase): 55 USE_MESSAGE_FACTORY = False 56 SUPPORT_NCP = False 57 58 TOPOLOGY = { 59 LEADER: { 60 'name': 'LEADER', 61 'mode': 'rdn', 62 }, 63 ROUTER: { 64 'name': 'ROUTER', 65 'mode': 'rdn', 66 }, 67 } 68 69 def test(self): 70 leader = self.nodes[LEADER] 71 router = self.nodes[ROUTER] 72 73 # Form network. 74 75 leader.start() 76 self.simulator.go(config.LEADER_STARTUP_DELAY) 77 self.assertEqual(leader.get_state(), 'leader') 78 79 router.start() 80 self.simulator.go(config.ROUTER_STARTUP_DELAY) 81 self.assertEqual(router.get_state(), 'router') 82 83 # On leader set DNS config explicitly. 84 85 leader.dns_set_config(DEFAULT_ADDRESS) 86 dns_config = leader.dns_get_config() 87 self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) 88 89 # Start leader to act as SRP server. 90 91 leader.srp_server_set_enabled(True) 92 93 # Enable SRP client auto start on both nodes. 94 95 leader.srp_client_enable_auto_start_mode() 96 router.srp_client_enable_auto_start_mode() 97 self.simulator.go(5) 98 99 # Verify that on router the default DNS config is changed 100 # to the same address being used by SRP client. 101 102 srp_server_address = router.srp_client_get_server_address() 103 self.assertEqual(leader.srp_client_get_server_address(), srp_server_address) 104 105 dns_config = router.dns_get_config() 106 self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) 107 108 # Verify that on leader the default DNS config remains 109 # as before (the address explicitly set earlier). 110 111 dns_config = leader.dns_get_config() 112 self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) 113 114 # On leader clear DNS config (the explicitly set address) 115 # and verify that it adopts the SRP server address. 116 117 leader.dns_set_config("::") 118 dns_config = leader.dns_get_config() 119 self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) 120 121 # On leader set DNS config explicitly again. 122 123 leader.dns_set_config(DEFAULT_ADDRESS) 124 dns_config = leader.dns_get_config() 125 self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) 126 127 # Stop SRP server on leader and start it on router. 128 129 leader.srp_server_set_enabled(False) 130 router.srp_server_set_enabled(True) 131 self.simulator.go(5) 132 133 # Verify that SRP client on router switched to new SRP server. 134 135 self.assertNotEqual(srp_server_address, router.srp_client_get_server_address()) 136 self.assertEqual(router.srp_client_get_server_address(), leader.srp_client_get_server_address()) 137 138 # Verify that config on router gets changed while on leader 139 # it remains unchanged. 140 141 dns_config = router.dns_get_config() 142 srp_server_address = router.srp_client_get_server_address() 143 self.assertEqual(dns_config['Server'], '[{}]:53'.format(srp_server_address)) 144 145 dns_config = leader.dns_get_config() 146 self.assertEqual(dns_config['Server'], '[{}]:53'.format(DEFAULT_ADDRESS)) 147 148 149if __name__ == '__main__': 150 unittest.main() 151