Files
crossdesk/apps/desktop/tests/config_center_srtp_test.cpp
T

51 lines
2.0 KiB
C++

#include "config_center.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <stdexcept>
using crossdesk::ConfigCenter;
static void Check(bool result, const char* message) {
if (!result) throw std::runtime_error(message);
}
int main(int argc, char** argv) {
try {
Check(argc == 2, "provide a new isolated output directory");
const std::filesystem::path root(argv[1]);
Check(std::filesystem::create_directory(root), "output directory must be new");
unsigned cases = 0;
for (const auto* setting : {"new", "missing", "false", "true", "invalid"}) {
const auto path = root / (std::string(setting) + ".ini");
if (std::string(setting) != "new") {
std::ofstream file(path);
file << "[Settings]\nlanguage=2\nturn_mode=2\nfile_transfer_save_path=/saved-path\n";
if (std::string(setting) != "missing") file << "enable_srtp=" << setting << '\n';
}
ConfigCenter config(path.string());
Check(config.IsEnableSrtp(), "fresh and legacy settings must request SRTP");
if (std::string(setting) != "new") {
Check(config.GetLanguage() == ConfigCenter::LANGUAGE::RUSSIAN &&
config.GetTurnMode() == ConfigCenter::TURN_MODE::FORCE_UDP &&
config.GetFileTransferSavePath() == "/saved-path",
"SRTP migration preserves unrelated preferences");
}
CSimpleIniA saved;
Check(saved.LoadFile(path.string().c_str()) >= 0 &&
saved.GetBoolValue("Settings", "enable_srtp", false),
"SRTP migration persists before any later settings save");
Check(config.Save() == 0 && config.Load() == 0 && config.IsEnableSrtp(),
"settings save/reload cannot disable SRTP");
ConfigCenter reopened(path.string());
Check(reopened.IsEnableSrtp(), "SRTP survives restart");
++cases;
}
std::cout << "PASS SRTP configuration migration cases=" << cases << '\n';
} catch (const std::exception& error) {
std::cerr << "FAIL: " << error.what() << '\n';
return 1;
}
}