- interface gtkmm-4 : véhicules, ajout par VIN, mises à jour, dossier, téléchargement avec reprise et arrêt, choix de la clé, confirmation, écriture avec progression, fin avec guide d'installation ; travail dans un thread, résultats renvoyés via Glib::Dispatcher - préférences : dossier de téléchargement, langue (système par défaut), recherche automatique de nouvelle version d'OpenPU - recherche de nouvelle version (releases GitHub, format tags Gitea accepté) avec ouverture de la page dans le navigateur ; openpu-cli app-update - versions : project(VERSION) + git describe régénéré à chaque build, openpu-cli --version, À propos, User-Agent, CHANGELOG, tools/release.sh (étiquette annotée, push vers tous les remotes, release GitHub) - traductions gettext : textes anglais, fr.po complet (96 chaînes), extracteur de secours sans xgettext - core : module job (téléchargement licence/fw/carte), liens des guides PDF, ordre des véhicules conservé (ordered_json) - licence MIT (LICENSE, en-têtes SPDX) ; dépôt principal sur GitHub - validé dans WSLg : parcours complet jusqu'à l'écriture d'une clé loop (FAT32 propre, fichiers téléchargés supprimés) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
155 lines
4.7 KiB
C++
155 lines
4.7 KiB
C++
// SPDX-License-Identifier: MIT
|
|
#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 + "\"}");
|
|
}
|