|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, The OpenThread Authors. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * 3. Neither the name of the copyright holder nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include "platform/nexus_core.hpp" |
| 33 | +#include "platform/nexus_node.hpp" |
| 34 | + |
| 35 | +namespace ot { |
| 36 | +namespace Nexus { |
| 37 | + |
| 38 | +/** |
| 39 | + * Time to advance for a node to form a network and become leader, in milliseconds. |
| 40 | + */ |
| 41 | +static constexpr uint32_t kFormNetworkTime = 13 * 1000; |
| 42 | + |
| 43 | +/** |
| 44 | + * Time to advance for a node to join as a child and upgrade to a router. |
| 45 | + */ |
| 46 | +static constexpr uint32_t kAttachToRouterTime = 200 * 1000; |
| 47 | + |
| 48 | +/** |
| 49 | + * Time to advance for SRP server information to be updated in Network Data. |
| 50 | + */ |
| 51 | +static constexpr uint32_t kSrpServerInfoUpdateTime = 60 * 1000; |
| 52 | + |
| 53 | +/** |
| 54 | + * Time to advance for SRP registration, in milliseconds. |
| 55 | + */ |
| 56 | +static constexpr uint32_t kSrpRegistrationTime = 60 * 1000; |
| 57 | + |
| 58 | +/** |
| 59 | + * Number of reboot times. |
| 60 | + */ |
| 61 | +static constexpr uint16_t kRebootTimes = 25; |
| 62 | + |
| 63 | +void TestSrpServerRebootPort(void) |
| 64 | +{ |
| 65 | + Core nexus; |
| 66 | + |
| 67 | + Node &client = nexus.CreateNode(); |
| 68 | + Node &server = nexus.CreateNode(); |
| 69 | + |
| 70 | + client.SetName("CLIENT"); |
| 71 | + server.SetName("SERVER"); |
| 72 | + |
| 73 | + nexus.AdvanceTime(0); |
| 74 | + |
| 75 | + SuccessOrQuit(Instance::SetGlobalLogLevel(kLogLevelNote)); |
| 76 | + |
| 77 | + // |
| 78 | + // 0. Start the server & client devices. |
| 79 | + // |
| 80 | + |
| 81 | + Log("0. Start the server & client devices."); |
| 82 | + client.Get<Mle::Mle>().SetRouterUpgradeThreshold(2); |
| 83 | + server.Get<Mle::Mle>().SetRouterUpgradeThreshold(2); |
| 84 | + |
| 85 | + client.Form(); |
| 86 | + nexus.AdvanceTime(kFormNetworkTime); |
| 87 | + |
| 88 | + server.Join(client); |
| 89 | + nexus.AdvanceTime(kAttachToRouterTime); |
| 90 | + VerifyOrQuit(server.Get<Mle::Mle>().IsRouter()); |
| 91 | + |
| 92 | + SuccessOrQuit(server.Get<Srp::Server>().SetAddressMode(Srp::Server::kAddressModeUnicast)); |
| 93 | + server.Get<Srp::Server>().SetEnabled(true); |
| 94 | + |
| 95 | + // Wait for SRP server to be running (published in network data) |
| 96 | + for (int i = 0; i < 10; i++) |
| 97 | + { |
| 98 | + nexus.AdvanceTime(10000); |
| 99 | + if (server.Get<Srp::Server>().GetState() == Srp::Server::kStateRunning) |
| 100 | + { |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + VerifyOrQuit(server.Get<Srp::Server>().GetState() == Srp::Server::kStateRunning); |
| 105 | + |
| 106 | + // |
| 107 | + // 1. Check auto start mode on client and check that server is used. |
| 108 | + // |
| 109 | + |
| 110 | + Log("1. Check auto start mode on client and check that server is used."); |
| 111 | + // We set a host name so the client has something to register and thus "runs". |
| 112 | + SuccessOrQuit(client.Get<Srp::Client>().SetHostName("my-host")); |
| 113 | + client.Get<Srp::Client>().EnableAutoStartMode(nullptr, nullptr); |
| 114 | + nexus.AdvanceTime(kSrpRegistrationTime); |
| 115 | + |
| 116 | + VerifyOrQuit(client.Get<Srp::Client>().GetServerAddress().GetAddress() == server.Get<Mle::Mle>().GetMeshLocalEid()); |
| 117 | + |
| 118 | + // |
| 119 | + // 2. Reboot the server without any service registered. The server should |
| 120 | + // switch to a new port after re-enabling. |
| 121 | + // |
| 122 | + |
| 123 | + Log("2. Reboot the server without any service registered."); |
| 124 | + { |
| 125 | + uint16_t oldPort = server.Get<Srp::Server>().GetPort(); |
| 126 | + server.Get<Srp::Server>().SetEnabled(false); |
| 127 | + nexus.AdvanceTime(5000); |
| 128 | + server.Get<Srp::Server>().SetEnabled(true); |
| 129 | + nexus.AdvanceTime(kSrpServerInfoUpdateTime); |
| 130 | + VerifyOrQuit(server.Get<Srp::Server>().GetState() == Srp::Server::kStateRunning); |
| 131 | + VerifyOrQuit(server.Get<Srp::Server>().GetPort() != oldPort); |
| 132 | + } |
| 133 | + |
| 134 | + // |
| 135 | + // 3. Register a service |
| 136 | + // |
| 137 | + |
| 138 | + Log("3. Register a service"); |
| 139 | + static Srp::Client::Service service; |
| 140 | + SuccessOrQuit(client.Get<Srp::Client>().EnableAutoHostAddress()); |
| 141 | + |
| 142 | + memset(&service, 0, sizeof(service)); |
| 143 | + service.mName = "_ipps._tcp"; |
| 144 | + service.mInstanceName = "my-service"; |
| 145 | + service.mPort = 12345; |
| 146 | + SuccessOrQuit(client.Get<Srp::Client>().AddService(service)); |
| 147 | + |
| 148 | + nexus.AdvanceTime(kSrpRegistrationTime); |
| 149 | + |
| 150 | + // Reboot the SRP server several times |
| 151 | + for (uint16_t i = 0; i < kRebootTimes; i++) |
| 152 | + { |
| 153 | + Log("Reboot iteration %u", i); |
| 154 | + |
| 155 | + // |
| 156 | + // 4. Disable server and check client is stopped/disabled. |
| 157 | + // |
| 158 | + uint16_t oldPort = server.Get<Srp::Server>().GetPort(); |
| 159 | + server.Get<Srp::Server>().SetEnabled(false); |
| 160 | + nexus.AdvanceTime(5000); |
| 161 | + |
| 162 | + // |
| 163 | + // 5. Enable server and check client starts again. Verify that the |
| 164 | + // server is using a different port, and the service have been |
| 165 | + // re-registered. |
| 166 | + // |
| 167 | + server.Get<Srp::Server>().SetEnabled(true); |
| 168 | + nexus.AdvanceTime(kSrpServerInfoUpdateTime); |
| 169 | + nexus.AdvanceTime(kSrpRegistrationTime); |
| 170 | + |
| 171 | + VerifyOrQuit(client.Get<Srp::Client>().GetHostInfo().GetState() == Srp::Client::kRegistered); |
| 172 | + VerifyOrQuit(server.Get<Srp::Server>().GetNextHost(nullptr)->GetServices().GetHead()->GetPort() == 12345); |
| 173 | + VerifyOrQuit(server.Get<Srp::Server>().GetPort() != oldPort); |
| 174 | + } |
| 175 | + |
| 176 | + Log("All steps completed."); |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace Nexus |
| 180 | +} // namespace ot |
| 181 | + |
| 182 | +int main(void) |
| 183 | +{ |
| 184 | + ot::Nexus::TestSrpServerRebootPort(); |
| 185 | + |
| 186 | + printf("All tests passed\n"); |
| 187 | + return 0; |
| 188 | +} |
0 commit comments