|
| 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 | +static void CheckTtl(Core &aNexus, Node &aServer, uint32_t aExpectedTtl) |
| 39 | +{ |
| 40 | + const Srp::Server::Host *host = nullptr; |
| 41 | + |
| 42 | + // We advance time enough to trigger a refresh from the client. |
| 43 | + // The lease interval is at most 240 seconds in our test cases. |
| 44 | + aNexus.AdvanceTime(300 * Time::kOneSecondInMsec); |
| 45 | + |
| 46 | + while ((host = aServer.Get<Srp::Server>().GetNextHost(host)) != nullptr) |
| 47 | + { |
| 48 | + if (StringMatch(host->GetFullName(), "my-host.default.service.arpa.", kStringCaseInsensitiveMatch)) |
| 49 | + { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + VerifyOrQuit(host != nullptr); |
| 55 | + |
| 56 | + const Srp::Server::Service *serverService = host->GetNextService(nullptr); |
| 57 | + |
| 58 | + VerifyOrQuit(serverService != nullptr); |
| 59 | + Log("Expected TTL: %u, Actual TTL: %u", aExpectedTtl, serverService->GetTtl()); |
| 60 | + VerifyOrQuit(serverService->GetTtl() == aExpectedTtl); |
| 61 | +} |
| 62 | + |
| 63 | +void TestSrpTtl(void) |
| 64 | +{ |
| 65 | + Core nexus; |
| 66 | + Node &server = nexus.CreateNode(); |
| 67 | + Node &client = nexus.CreateNode(); |
| 68 | + |
| 69 | + Log("----------------------------------------------------------------------------------------------------------"); |
| 70 | + Log("Testing SRP TTL"); |
| 71 | + |
| 72 | + server.Form(); |
| 73 | + nexus.AdvanceTime(10 * Time::kOneSecondInMsec); |
| 74 | + VerifyOrQuit(server.Get<Mle::Mle>().IsLeader()); |
| 75 | + |
| 76 | + server.Get<Srp::Server>().SetEnabled(true); |
| 77 | + nexus.AdvanceTime(1 * Time::kOneSecondInMsec); |
| 78 | + |
| 79 | + client.Join(server, Node::kAsFed); |
| 80 | + nexus.AdvanceTime(10 * Time::kOneSecondInMsec); |
| 81 | + VerifyOrQuit(client.Get<Mle::Mle>().IsChild()); |
| 82 | + |
| 83 | + client.Get<Srp::Client>().EnableAutoStartMode(nullptr, nullptr); |
| 84 | + nexus.AdvanceTime(1 * Time::kOneSecondInMsec); |
| 85 | + |
| 86 | + SuccessOrQuit(client.Get<Srp::Client>().SetHostName("my-host")); |
| 87 | + |
| 88 | + Ip6::Address address; |
| 89 | + SuccessOrQuit(address.FromString("2001::1")); |
| 90 | + SuccessOrQuit(client.Get<Srp::Client>().SetHostAddresses(&address, 1)); |
| 91 | + |
| 92 | + Srp::Client::Service service; |
| 93 | + ClearAllBytes(service); |
| 94 | + service.mName = "_ipps._tcp"; |
| 95 | + service.mInstanceName = "my-service"; |
| 96 | + service.mPort = 12345; |
| 97 | + SuccessOrQuit(client.Get<Srp::Client>().AddService(service)); |
| 98 | + |
| 99 | + // Case 1: CLIENT_TTL < TTL_MIN < LEASE_MAX ==> TTL_MIN |
| 100 | + Log("Case 1: CLIENT_TTL < TTL_MIN < LEASE_MAX ==> TTL_MIN"); |
| 101 | + { |
| 102 | + Srp::Server::TtlConfig ttlConfig; |
| 103 | + ttlConfig.mMinTtl = 120; |
| 104 | + ttlConfig.mMaxTtl = 240; |
| 105 | + SuccessOrQuit(server.Get<Srp::Server>().SetTtlConfig(ttlConfig)); |
| 106 | + |
| 107 | + Srp::Server::LeaseConfig leaseConfig; |
| 108 | + leaseConfig.mMinLease = 120; |
| 109 | + leaseConfig.mMaxLease = 240; |
| 110 | + leaseConfig.mMinKeyLease = 240; |
| 111 | + leaseConfig.mMaxKeyLease = 240; |
| 112 | + SuccessOrQuit(server.Get<Srp::Server>().SetLeaseConfig(leaseConfig)); |
| 113 | + |
| 114 | + client.Get<Srp::Client>().SetTtl(100); |
| 115 | + CheckTtl(nexus, server, 120); |
| 116 | + } |
| 117 | + |
| 118 | + // Case 2: TTL_MIN < CLIENT_TTL < TTL_MAX < LEASE_MAX ==> CLIENT_TTL |
| 119 | + Log("Case 2: TTL_MIN < CLIENT_TTL < TTL_MAX < LEASE_MAX ==> CLIENT_TTL"); |
| 120 | + { |
| 121 | + Srp::Server::TtlConfig ttlConfig; |
| 122 | + ttlConfig.mMinTtl = 60; |
| 123 | + ttlConfig.mMaxTtl = 120; |
| 124 | + SuccessOrQuit(server.Get<Srp::Server>().SetTtlConfig(ttlConfig)); |
| 125 | + |
| 126 | + Srp::Server::LeaseConfig leaseConfig; |
| 127 | + leaseConfig.mMinLease = 120; |
| 128 | + leaseConfig.mMaxLease = 240; |
| 129 | + leaseConfig.mMinKeyLease = 240; |
| 130 | + leaseConfig.mMaxKeyLease = 240; |
| 131 | + SuccessOrQuit(server.Get<Srp::Server>().SetLeaseConfig(leaseConfig)); |
| 132 | + |
| 133 | + client.Get<Srp::Client>().SetTtl(100); |
| 134 | + CheckTtl(nexus, server, 100); |
| 135 | + } |
| 136 | + |
| 137 | + // Case 3: TTL_MAX < LEASE_MAX < CLIENT_TTL ==> TTL_MAX |
| 138 | + Log("Case 3: TTL_MAX < LEASE_MAX < CLIENT_TTL ==> TTL_MAX"); |
| 139 | + { |
| 140 | + Srp::Server::TtlConfig ttlConfig; |
| 141 | + ttlConfig.mMinTtl = 60; |
| 142 | + ttlConfig.mMaxTtl = 120; |
| 143 | + SuccessOrQuit(server.Get<Srp::Server>().SetTtlConfig(ttlConfig)); |
| 144 | + |
| 145 | + Srp::Server::LeaseConfig leaseConfig; |
| 146 | + leaseConfig.mMinLease = 120; |
| 147 | + leaseConfig.mMaxLease = 240; |
| 148 | + leaseConfig.mMinKeyLease = 240; |
| 149 | + leaseConfig.mMaxKeyLease = 240; |
| 150 | + SuccessOrQuit(server.Get<Srp::Server>().SetLeaseConfig(leaseConfig)); |
| 151 | + |
| 152 | + client.Get<Srp::Client>().SetTtl(240); |
| 153 | + CheckTtl(nexus, server, 120); |
| 154 | + } |
| 155 | + |
| 156 | + // Case 4: LEASE_MAX < TTL_MAX < CLIENT_TTL ==> LEASE_MAX |
| 157 | + Log("Case 4: LEASE_MAX < TTL_MAX < CLIENT_TTL ==> LEASE_MAX"); |
| 158 | + { |
| 159 | + Srp::Server::TtlConfig ttlConfig; |
| 160 | + ttlConfig.mMinTtl = 60; |
| 161 | + ttlConfig.mMaxTtl = 120; |
| 162 | + SuccessOrQuit(server.Get<Srp::Server>().SetTtlConfig(ttlConfig)); |
| 163 | + |
| 164 | + Srp::Server::LeaseConfig leaseConfig; |
| 165 | + leaseConfig.mMinLease = 30; |
| 166 | + leaseConfig.mMaxLease = 60; |
| 167 | + leaseConfig.mMinKeyLease = 240; |
| 168 | + leaseConfig.mMaxKeyLease = 240; |
| 169 | + SuccessOrQuit(server.Get<Srp::Server>().SetLeaseConfig(leaseConfig)); |
| 170 | + |
| 171 | + client.Get<Srp::Client>().SetTtl(240); |
| 172 | + CheckTtl(nexus, server, 60); |
| 173 | + } |
| 174 | + |
| 175 | + Log("Test passed successfully"); |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace Nexus |
| 179 | +} // namespace ot |
| 180 | + |
| 181 | +int main(void) |
| 182 | +{ |
| 183 | + ot::Nexus::TestSrpTtl(); |
| 184 | + printf("All tests passed\n"); |
| 185 | + return 0; |
| 186 | +} |
0 commit comments