am-freescan/integrations/freescan-service/tests/calibration_files_test.cpp

215 lines
12 KiB
C++
Raw Normal View History

#include "../src/calibration_files.hpp"
#include <iostream>
// OFFLINE ONLY: every file belongs to a newly created temporary fixture.
// These tests never inspect an installed SDK, connect a device or bind HTTP.
using freescan::Json;
namespace fs = std::filesystem;
using freescan::detail::snapshot_calibration_files;
namespace {
size_t passed = 0;
void check(bool condition, const std::string &message) {
if (!condition)
throw std::runtime_error(message);
++passed;
}
struct Fixture {
fs::path parent, directory;
const std::wstring prefix = L"freescan_calibration_files_offline_";
Fixture() {
parent = fs::canonical(fs::temp_directory_path());
directory = parent / (prefix + freescan::wide(freescan::new_id()));
if (!fs::create_directory(directory))
throw std::runtime_error("Cannot create a unique calibration-file fixture.");
}
Fixture(const Fixture &) = delete;
Fixture &operator=(const Fixture &) = delete;
~Fixture() {
std::error_code error;
const auto absolute = fs::weakly_canonical(directory, error);
// Never recursively delete a path until its absolute parent and unique
// test prefix have both been checked against the original fixture.
if (error || !absolute.is_absolute() || absolute.parent_path() != parent ||
absolute.filename().wstring().rfind(prefix, 0) != 0 || absolute != directory) {
std::cerr << "Refusing cleanup of an unverified fixture path.\n";
return;
}
fs::remove_all(absolute, error);
if (error)
std::cerr << "Calibration-file fixture cleanup failed: " << error.message() << "\n";
}
fs::path write(const std::wstring &name, bool nonempty = true) const {
const auto path = directory / name;
std::ofstream output(path, std::ios::binary);
if (nonempty)
output << "synthetic calibration metadata fixture";
output.close();
if (!output)
throw std::runtime_error("Cannot write a calibration-file fixture.");
return path;
}
};
void set_modified_ticks(const fs::path &path, uint64_t fileTimeTicks) {
freescan::Handle file(CreateFileW(path.c_str(), FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
if (!file)
throw std::runtime_error("Cannot open a fixture to set its file time.");
ULARGE_INTEGER ticks{};
ticks.QuadPart = fileTimeTicks;
const FILETIME modified{ticks.LowPart, ticks.HighPart};
if (!SetFileTime(file.get(), nullptr, nullptr, &modified))
throw std::runtime_error("Cannot set a fixture file time.");
}
void set_modified(const fs::path &path, int64_t unixMs) {
set_modified_ticks(path, static_cast<uint64_t>(unixMs) * 10000ull + 116444736000000000ull);
}
void empty(const Json &value, const std::string &context) {
check(value.at("lastCalibrationSource") == "local_calibration_file_modified_time",
context + ": the source remains explicit when no file is available.");
check(value.at("lastCalibrationUtc").is_null(), context + ": no time is invented.");
check(value.at("calibrationFileTimes").is_array() && value.at("calibrationFileTimes").empty(),
context + ": file details are an empty array.");
}
const Json &entry(const Json &value, const std::string &name) {
for (const auto &item : value.at("calibrationFileTimes"))
if (item.at("fileName") == name)
return item;
throw std::runtime_error("Missing fixture metadata: " + name);
}
void no_vendor_dll() {
check(GetModuleHandleW(L"SDKCWrapper.dll") == nullptr &&
GetModuleHandleW(L"FreeScanNativeBridge.dll") == nullptr,
"Calibration-file tests never load the vendor SDK or bridge.");
}
void missing_and_empty_paths() {
Fixture fixture;
empty(snapshot_calibration_files(fs::path{}), "An unspecified path");
empty(snapshot_calibration_files(fixture.directory), "An empty directory");
empty(snapshot_calibration_files(fixture.directory / L"missing"), "A missing directory");
const auto ordinaryFile = fixture.write(L"ordinary.txt");
empty(snapshot_calibration_files(ordinaryFile), "A file used as the directory");
empty(snapshot_calibration_files(ordinaryFile / L"not-a-directory"), "A path below a regular file");
}
void exact_timestamps_and_all_components() {
Fixture fixture;
const auto tracker = fixture.write(L"aTrackCalibData.bin");
const auto scanner = fixture.write(L"0BScannerCalibData.bin");
const auto handEye = fixture.write(L"c04f0edf600db332HandEyeCalibData.bin");
set_modified(tracker, 1700000000123LL);
set_modified(scanner, 1700000001456LL);
set_modified(handEye, 1700000002789LL);
const auto value = snapshot_calibration_files(fixture.directory);
check(value.at("lastCalibrationSource") == "local_calibration_file_modified_time",
"The snapshot identifies local file metadata rather than hardware readback.");
check(value.at("lastCalibrationUtc") == "2023-11-14T22:13:22.789Z",
"The combined time is the newest component's exact UTC millisecond time.");
check(value.at("calibrationFileTimes").size() == 3, "All three calibration-file types are retained.");
const auto &trackerResult = entry(value, "aTrackCalibData.bin");
const auto &scannerResult = entry(value, "0BScannerCalibData.bin");
const auto &handEyeResult = entry(value, "c04f0edf600db332HandEyeCalibData.bin");
check(trackerResult.at("type") == "tracker" &&
trackerResult.at("modifiedUtc") == "2023-11-14T22:13:20.123Z",
"Tracker metadata uses UTC with the original milliseconds.");
check(scannerResult.at("type") == "scanner" &&
scannerResult.at("modifiedUtc") == "2023-11-14T22:13:21.456Z",
"Scanner metadata uses UTC with the original milliseconds.");
check(handEyeResult.at("type") == "handEye" &&
handEyeResult.at("modifiedUtc") == "2023-11-14T22:13:22.789Z",
"Hand-eye metadata uses UTC with the original milliseconds.");
std::vector<std::string> names;
for (const auto &item : value.at("calibrationFileTimes"))
names.push_back(item.at("fileName").get<std::string>());
check(std::is_sorted(names.begin(), names.end()), "File metadata has deterministic filename ordering.");
}
void multiple_devices_and_case_insensitive_names() {
Fixture fixture;
const auto first = fixture.write(L"1TrackCalibData.bin");
const auto second = fixture.write(L"abcdef01234567890123456789ABCDEFtRaCkCaLiBdAtA.BIN");
const auto scanner = fixture.write(L"ABCscannerCALIBdata.BiN");
const auto handEye = fixture.write(L"FeHaNdEyEcAlIbDaTa.bIN");
set_modified(first, 1700000000000LL);
set_modified(second, 1700000000999LL);
set_modified(scanner, 1700000000001LL);
set_modified(handEye, 1700000000002LL);
const auto value = snapshot_calibration_files(fixture.directory);
check(value.at("calibrationFileTimes").size() == 4,
"Multiple file prefixes of the same type are all retained without choosing a device.");
check(entry(value, "1TrackCalibData.bin").at("type") == "tracker" &&
entry(value, "abcdef01234567890123456789ABCDEFtRaCkCaLiBdAtA.BIN").at("type") == "tracker",
"Nonempty hexadecimal prefixes have no fixed length and suffix matching ignores case.");
check(entry(value, "ABCscannerCALIBdata.BiN").at("type") == "scanner" &&
entry(value, "FeHaNdEyEcAlIbDaTa.bIN").at("type") == "handEye",
"All accepted suffixes are case insensitive.");
check(value.at("lastCalibrationUtc") == "2023-11-14T22:13:20.999Z",
"A second device's newer file contributes to the directory-wide latest time.");
}
void filtering_and_live_file_changes() {
Fixture fixture;
const auto accepted = fixture.write(L"123HandEyeCalibData.bin");
set_modified(accepted, 1700000000123LL);
for (const auto &name : {L"123TrackCalibData.asc", L"123ScannerCalibData.bin.bak",
L"backup123ScannerCalibData.bin", L"12_3TrackCalibData.bin",
L"12-3HandEyeCalibData.bin", L"TrackCalibData.bin",
L"xyzTrackCalibData.bin", L"123UnknownCalibData.bin"})
fixture.write(name);
fixture.write(L"123ScannerCalibData.bin", false);
fs::create_directory(fixture.directory / L"456TrackCalibData.bin");
fs::create_directory(fixture.directory / L"nested");
const auto nested = fixture.write(L"nested/789ScannerCalibData.bin");
set_modified(nested, 1800000000999LL);
auto value = snapshot_calibration_files(fixture.directory);
check(value.at("calibrationFileTimes").size() == 1,
"Empty files, geometry, backups, invalid prefixes, directories and nested files are excluded.");
check(value.at("lastCalibrationUtc") == "2023-11-14T22:13:20.123Z",
"Rejected newer files cannot change the latest calibration time.");
set_modified(accepted, 1700000004567LL);
value = snapshot_calibration_files(fixture.directory);
check(value.at("lastCalibrationUtc") == "2023-11-14T22:13:24.567Z" &&
entry(value, "123HandEyeCalibData.bin").at("modifiedUtc") == "2023-11-14T22:13:24.567Z",
"A subsequent snapshot reflects updated file metadata without restarting a service.");
check(fs::remove(accepted), "The accepted fixture file can be deleted.");
empty(snapshot_calibration_files(fixture.directory), "After deleting the final accepted file");
}
void timestamp_boundaries_and_truncation() {
Fixture fixture;
const auto epochFile = fixture.write(L"0TrackCalibData.bin");
const auto tooOld = fixture.write(L"1ScannerCalibData.bin");
const auto tooNew = fixture.write(L"2HandEyeCalibData.bin");
constexpr uint64_t epoch = 116444736000000000ull;
set_modified_ticks(epochFile, epoch);
set_modified_ticks(tooOld, epoch - 1);
set_modified(tooNew, 253402300800000LL); // 10000-01-01 UTC is outside RFC3339's four-digit year.
auto value = snapshot_calibration_files(fixture.directory);
check(value.at("calibrationFileTimes").size() == 1 &&
value.at("lastCalibrationUtc") == "1970-01-01T00:00:00.000Z",
"The Unix epoch is accepted, while pre-epoch and five-digit-year timestamps are excluded.");
check(entry(value, "0TrackCalibData.bin").at("modifiedUtc") == "1970-01-01T00:00:00.000Z",
"An exact epoch timestamp is a real value rather than a missing-time sentinel.");
set_modified_ticks(epochFile, epoch + 1700000000123ull * 10000ull + 9999ull);
value = snapshot_calibration_files(fixture.directory);
check(value.at("lastCalibrationUtc") == "2023-11-14T22:13:20.123Z" &&
entry(value, "0TrackCalibData.bin").at("modifiedUtc") == "2023-11-14T22:13:20.123Z",
"Sub-millisecond FILETIME precision is truncated rather than rounded or shifted by a clock epoch.");
check(fs::remove(epochFile), "The valid boundary fixture can be deleted.");
empty(snapshot_calibration_files(fixture.directory), "When only invalid-time files remain");
}
} // namespace
int main() {
try {
no_vendor_dll();
missing_and_empty_paths();
exact_timestamps_and_all_components();
multiple_devices_and_case_insensitive_names();
filtering_and_live_file_changes();
timestamp_boundaries_and_truncation();
no_vendor_dll();
std::cout << "OFFLINE_TEST_FIXTURE: C++ calibration-file tests passed: " << passed
<< "; temporary files only, no installed SDK, device or HTTP listener.\n";
return 0;
} catch (const std::exception &error) {
std::cerr << "FAIL (OFFLINE ONLY): " << error.what() << "\n";
return 1;
}
}