145 lines
5.0 KiB
C++
145 lines
5.0 KiB
C++
#include "ota/ota_endpoint.h"
|
|
|
|
#include <array>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
|
|
namespace {
|
|
|
|
using namespace borla;
|
|
|
|
void TestRolloverEndpointDetection()
|
|
{
|
|
// Build/staging defaults must be recognised so a device that once talked
|
|
// to a retired host recovers instead of retrying it forever.
|
|
assert(IsRolloverEndpoint("wss://borlaws.lkall.com/v1/ws"));
|
|
assert(IsRolloverEndpoint("wss://borla-dev.lkall.com/v1/ws"));
|
|
assert(IsRolloverEndpoint("wss://borla-moe.lkall.com/v1/ws"));
|
|
assert(IsRolloverEndpoint("https://borla-ota.lkall.com/xiaozhi/ota/"));
|
|
assert(IsRolloverEndpoint("ws://192.168.1.121:30001/v1/ws"));
|
|
assert(IsRolloverEndpoint("ws://117.50.44.247:8000/v1/ws"));
|
|
|
|
// A genuinely provisioned production endpoint must be preserved.
|
|
assert(!IsRolloverEndpoint("wss://prod.example.com/v1/ws"));
|
|
// .lkall.com alone is not enough — only the listed staging hosts roll over.
|
|
assert(!IsRolloverEndpoint("wss://customer.lkall.com/v1/ws"));
|
|
assert(!IsRolloverEndpoint(""));
|
|
assert(!IsRolloverEndpoint(nullptr));
|
|
}
|
|
|
|
void TestWildcardHostSubstitution()
|
|
{
|
|
std::array<char, 128> output{};
|
|
assert(SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws",
|
|
"https://ota.example.com/xiaozhi/ota/",
|
|
output.data(),
|
|
output.size()));
|
|
assert(std::strcmp(
|
|
output.data(), "ws://ota.example.com:8000/v1/ws") == 0);
|
|
|
|
// Host with an explicit port on the OTA side: only the host is taken.
|
|
output.fill('\0');
|
|
assert(SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws",
|
|
"http://10.1.2.3:9000/ota/",
|
|
output.data(),
|
|
output.size()));
|
|
assert(std::strcmp(output.data(), "ws://10.1.2.3:8000/v1/ws") == 0);
|
|
|
|
// No wildcard in the candidate → nothing to substitute.
|
|
assert(!SubstituteWildcardHost(
|
|
"wss://real.example.com/v1/ws",
|
|
"https://ota.example.com/ota/",
|
|
output.data(),
|
|
output.size()));
|
|
|
|
// Unusable OTA URLs must not produce a half-rewritten endpoint.
|
|
assert(!SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws", "ota.example.com",
|
|
output.data(), output.size()));
|
|
assert(!SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws", "https://0.0.0.0/ota/",
|
|
output.data(), output.size()));
|
|
assert(!SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws", nullptr,
|
|
output.data(), output.size()));
|
|
|
|
// Capacity is respected.
|
|
std::array<char, 8> tiny{};
|
|
assert(!SubstituteWildcardHost(
|
|
"ws://0.0.0.0:8000/v1/ws",
|
|
"https://ota.example.com/ota/",
|
|
tiny.data(),
|
|
tiny.size()));
|
|
}
|
|
|
|
void TestResolveRealtimeUrl()
|
|
{
|
|
constexpr const char* kOtaUrl = "https://ota.example.com/xiaozhi/ota/";
|
|
constexpr const char* kDefault = "wss://borlaws.lkall.com/v1/ws";
|
|
std::array<char, 128> output{};
|
|
|
|
// A real production endpoint passes through untouched.
|
|
assert(ResolveRealtimeUrl(
|
|
"wss://prod.example.com/v1/ws", kOtaUrl, kDefault,
|
|
output.data(), output.size()));
|
|
assert(std::strcmp(
|
|
output.data(), "wss://prod.example.com/v1/ws") == 0);
|
|
|
|
// A wildcard endpoint is rewritten to the reachable OTA host, and that
|
|
// result is NOT then treated as a rollover default.
|
|
output.fill('\0');
|
|
assert(ResolveRealtimeUrl(
|
|
"ws://0.0.0.0:8000/v1/ws", kOtaUrl, kDefault,
|
|
output.data(), output.size()));
|
|
assert(std::strcmp(
|
|
output.data(), "ws://ota.example.com:8000/v1/ws") == 0);
|
|
|
|
// A retired staging endpoint falls back to the current default.
|
|
output.fill('\0');
|
|
assert(ResolveRealtimeUrl(
|
|
"wss://borla-dev.lkall.com/v1/ws", kOtaUrl, kDefault,
|
|
output.data(), output.size()));
|
|
assert(std::strcmp(output.data(), kDefault) == 0);
|
|
|
|
// An unresolvable wildcard falls back rather than shipping "0.0.0.0".
|
|
output.fill('\0');
|
|
assert(ResolveRealtimeUrl(
|
|
"ws://0.0.0.0:8000/v1/ws", "not-a-url", kDefault,
|
|
output.data(), output.size()));
|
|
assert(std::strcmp(output.data(), kDefault) == 0);
|
|
|
|
// Empty/absent candidate falls back.
|
|
output.fill('\0');
|
|
assert(ResolveRealtimeUrl(
|
|
"", kOtaUrl, kDefault, output.data(), output.size()));
|
|
assert(std::strcmp(output.data(), kDefault) == 0);
|
|
output.fill('\0');
|
|
assert(ResolveRealtimeUrl(
|
|
nullptr, kOtaUrl, kDefault, output.data(), output.size()));
|
|
assert(std::strcmp(output.data(), kDefault) == 0);
|
|
|
|
// No candidate and no default → nothing usable.
|
|
assert(!ResolveRealtimeUrl(
|
|
nullptr, kOtaUrl, "", output.data(), output.size()));
|
|
|
|
// Too small an output buffer is a failure, not a truncation.
|
|
std::array<char, 4> tiny{};
|
|
assert(!ResolveRealtimeUrl(
|
|
"wss://prod.example.com/v1/ws", kOtaUrl, kDefault,
|
|
tiny.data(), tiny.size()));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
TestRolloverEndpointDetection();
|
|
TestWildcardHostSubstitution();
|
|
TestResolveRealtimeUrl();
|
|
std::cout << "ota endpoint tests passed" << std::endl;
|
|
return 0;
|
|
}
|