core: client API Majestic, modèles et CLI check
- libopenpu : validation VIN, modèles JSON (getAvailableUpdate, devices), classification ecuType -> type/guide, sélection des mises à jour - couche Transport abstraite + implémentation libcurl (proxy env, redirections, streaming, timeout d'inactivité) - tolérance au EOF TLS sans close_notify de la passerelle Majestic, limitée aux corps délimités par la fermeture (ni Content-Length ni chunked) - openpu-cli : check / raw-devices / raw-updates - tests doctest (VIN, parsing, classification, client API avec faux transport), fixtures dont une capture réelle anonymisée (NAC wave 2) - qualité : presets CMake (debug, asan, tidy), .clang-tidy, tools/check.sh (tests, ASan/UBSan, valgrind, clang-tidy, cppcheck) - validé contre l'API réelle sur 3 véhicules (NAC w4, RCC, NAC w2) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
1 parent
660a420a3e
commit
95704f9882
28 files changed
+1877
-2
No files matched your search
@@ -0,0 +1,153 @@
|
||||
#include "openpu/api_client.hpp"
|
||||
|
||||
#include <deque>
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
using namespace openpu;
|
||||
|
||||
namespace {
|
||||
|
||||
/// Replays canned responses and records the requests it received.
|
||||
class FakeTransport final : public Transport {
|
||||
public:
|
||||
std::deque<HttpResponse> responses;
|
||||
std::vector<HttpRequest> requests;
|
||||
bool failNetwork = false;
|
||||
|
||||
HttpResponse perform(const HttpRequest& request) override
|
||||
{
|
||||
requests.push_back(request);
|
||||
if (failNetwork)
|
||||
throw NetworkError("could not resolve host");
|
||||
REQUIRE_FALSE(responses.empty());
|
||||
HttpResponse r = responses.front();
|
||||
responses.pop_front();
|
||||
return r;
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponse ok(std::string body)
|
||||
{
|
||||
HttpResponse r;
|
||||
r.status = 200;
|
||||
r.body = std::move(body);
|
||||
return r;
|
||||
}
|
||||
|
||||
HttpResponse status(long code)
|
||||
{
|
||||
HttpResponse r;
|
||||
r.status = code;
|
||||
return r;
|
||||
}
|
||||
|
||||
const char* const kVin = "VR3ABCDEFGH123456";
|
||||
|
||||
ApiConfig testConfig()
|
||||
{
|
||||
ApiConfig c;
|
||||
c.devicesUrl = "https://api.test/v1/devices/";
|
||||
c.updatesUrl = "https://api.test/v1/getAvailableUpdate";
|
||||
c.clientId = "cid";
|
||||
return c;
|
||||
}
|
||||
|
||||
const char* const kUpdates = R"({"requestResult":"OK","software":[
|
||||
{"softwareType":"rcc-firmware","updateRequestResult":"OK","update":[
|
||||
{"updateId":"1","updateSize":"100","updateVersion":"v2","updateDate":"2024-01-01",
|
||||
"updateURL":"https://dl.test/UpdateDownload?updateId=1","licenseURL":"https://dl.test/LicenseDownload?mediaVersion=1"}]},
|
||||
{"softwareType":"map-eur","updateRequestResult":"OK","update":[
|
||||
{"updateId":"2","updateSize":"50","updateVersion":"m9","updateURL":"https://dl.test/UpdateDownload?updateId=2"}]}]})";
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("checkVehicle issues devices then getAvailableUpdate with client_id")
|
||||
{
|
||||
FakeTransport t;
|
||||
t.responses.push_back(ok(R"({"devices":[{"ecuType":"RCC_EU_W3"}]})"));
|
||||
t.responses.push_back(ok(kUpdates));
|
||||
ApiClient api(t, testConfig());
|
||||
|
||||
const VehicleCheck c = api.checkVehicle(kVin);
|
||||
CHECK(c.exists);
|
||||
CHECK(c.type == HeadUnitType::Rcc);
|
||||
|
||||
REQUIRE(t.requests.size() == 2);
|
||||
CHECK(t.requests[0].method == HttpMethod::Get);
|
||||
CHECK(t.requests[0].url == std::string("https://api.test/v1/devices/") + kVin + "?client_id=cid");
|
||||
CHECK(t.requests[1].method == HttpMethod::Post);
|
||||
CHECK(t.requests[1].url == "https://api.test/v1/getAvailableUpdate?client_id=cid");
|
||||
CHECK(t.requests[1].body.find("\"crony2-map-western_europe\"") != std::string::npos); // all 7 types
|
||||
bool hasContentType = false;
|
||||
for (const auto& [k, v] : t.requests[1].headers)
|
||||
hasContentType |= (k == "Content-Type" && v == "application/json");
|
||||
CHECK(hasContentType);
|
||||
}
|
||||
|
||||
TEST_CASE("checkVehicle: 404 on devices means unknown VIN, no second call")
|
||||
{
|
||||
FakeTransport t;
|
||||
t.responses.push_back(status(404));
|
||||
ApiClient api(t, testConfig());
|
||||
CHECK_FALSE(api.checkVehicle(kVin).exists);
|
||||
CHECK(t.requests.size() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("errors are mapped to ApiError kinds")
|
||||
{
|
||||
FakeTransport t;
|
||||
ApiClient api(t, testConfig());
|
||||
|
||||
SUBCASE("server error")
|
||||
{
|
||||
t.responses.push_back(status(503));
|
||||
try {
|
||||
api.checkVehicle(kVin);
|
||||
FAIL("expected ApiError");
|
||||
} catch (const ApiError& e) {
|
||||
CHECK(e.kind() == ApiError::Kind::Server);
|
||||
CHECK(e.status() == 503);
|
||||
}
|
||||
}
|
||||
SUBCASE("network error")
|
||||
{
|
||||
t.failNetwork = true;
|
||||
try {
|
||||
api.getUpdates(kVin, HeadUnitType::Rcc);
|
||||
FAIL("expected ApiError");
|
||||
} catch (const ApiError& e) {
|
||||
CHECK(e.kind() == ApiError::Kind::Network);
|
||||
}
|
||||
}
|
||||
SUBCASE("bad body")
|
||||
{
|
||||
t.responses.push_back(ok("<html>maintenance</html>"));
|
||||
try {
|
||||
api.getUpdates(kVin, HeadUnitType::Rcc);
|
||||
FAIL("expected ApiError");
|
||||
} catch (const ApiError& e) {
|
||||
CHECK(e.kind() == ApiError::Kind::BadResponse);
|
||||
}
|
||||
}
|
||||
SUBCASE("invalid VIN never reaches the network")
|
||||
{
|
||||
CHECK_THROWS_AS(api.checkVehicle("VF3SHORT"), ApiError);
|
||||
CHECK(t.requests.empty());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("getUpdates requests the pair of types of the head unit")
|
||||
{
|
||||
FakeTransport t;
|
||||
t.responses.push_back(ok(kUpdates));
|
||||
ApiClient api(t, testConfig());
|
||||
const AvailableUpdates u = api.getUpdates(kVin, HeadUnitType::Rcc);
|
||||
REQUIRE(u.firmware.has_value());
|
||||
CHECK(u.firmware->version == "v2");
|
||||
CHECK(u.firmware->size == 100);
|
||||
REQUIRE(u.map.has_value());
|
||||
CHECK(u.map->version == "m9");
|
||||
CHECK(t.requests[0].body ==
|
||||
std::string(R"({"softwareTypes":[{"softwareType":"rcc-firmware"},{"softwareType":"map-eur"}],"vin":")") +
|
||||
kVin + "\"}");
|
||||
}
|
||||
Reference in new issue
Block a user