You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.4 KiB
109 lines
3.4 KiB
// tests/test_config_updater.cpp — Verify universal config updater |
|
#include "../transport/config_updater.h" |
|
#include <QFile> |
|
#include <QString> |
|
#include <QIODevice> |
|
|
|
static void test_new_file() { |
|
const char* path = "/tmp/test_cfg_new.ini"; |
|
QFile::remove(path); |
|
|
|
bool ok = config_update_option(path, "global", "my_name", "testnode"); |
|
Q_ASSERT(ok); |
|
|
|
QFile f(path); |
|
Q_ASSERT(f.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
QString content = f.readAll(); |
|
f.close(); |
|
Q_ASSERT(content.contains("[global]")); |
|
Q_ASSERT(content.contains("my_name=testnode")); |
|
QFile::remove(path); |
|
} |
|
|
|
static void test_replace_existing() { |
|
const char* path = "/tmp/test_cfg_replace.ini"; |
|
QFile f(path); |
|
Q_ASSERT(f.open(QIODevice::WriteOnly | QIODevice::Text)); |
|
f.write("[global]\nmy_name=oldname\nmy_id=abc\n\n[control]\naddr=1.2.3.4\n"); |
|
f.close(); |
|
|
|
bool ok = config_update_option(path, "global", "my_name", "newname"); |
|
Q_ASSERT(ok); |
|
|
|
Q_ASSERT(f.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
QString content = f.readAll(); |
|
f.close(); |
|
Q_ASSERT(!content.contains("my_name=oldname")); |
|
Q_ASSERT(content.contains("my_name=newname")); |
|
Q_ASSERT(content.contains("my_id=abc")); |
|
Q_ASSERT(content.contains("[control]")); |
|
Q_ASSERT(content.contains("addr=1.2.3.4")); |
|
QFile::remove(path); |
|
} |
|
|
|
static void test_replace_new_param_in_section() { |
|
const char* path = "/tmp/test_cfg_newparam.ini"; |
|
QFile f(path); |
|
Q_ASSERT(f.open(QIODevice::WriteOnly | QIODevice::Text)); |
|
f.write("[global]\nmy_name=test\n\n[control]\naddr=1.2.3.4\n"); |
|
f.close(); |
|
|
|
bool ok = config_update_option(path, "global", "new_param", "newval"); |
|
Q_ASSERT(ok); |
|
|
|
Q_ASSERT(f.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
QString content = f.readAll(); |
|
f.close(); |
|
Q_ASSERT(content.contains("my_name=test")); |
|
Q_ASSERT(content.contains("new_param=newval")); |
|
Q_ASSERT(content.contains("[control]")); |
|
QFile::remove(path); |
|
} |
|
|
|
static void test_append_mode() { |
|
const char* path = "/tmp/test_cfg_append.ini"; |
|
QFile f(path); |
|
Q_ASSERT(f.open(QIODevice::WriteOnly | QIODevice::Text)); |
|
f.write("[client:peer1]\nlink=srv:addr1:port1\nlink=srv:addr2:port2\n\n[global]\nname=x\n"); |
|
f.close(); |
|
|
|
bool ok = config_update_option(path, "client:peer1", "link", "srv:addr3:port3", CONFIG_UPDATE_APPEND); |
|
Q_ASSERT(ok); |
|
|
|
Q_ASSERT(f.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
QString content = f.readAll(); |
|
f.close(); |
|
Q_ASSERT(content.contains("link=srv:addr1:port1")); |
|
Q_ASSERT(content.contains("link=srv:addr2:port2")); |
|
Q_ASSERT(content.contains("link=srv:addr3:port3")); |
|
QFile::remove(path); |
|
} |
|
|
|
static void test_new_section() { |
|
const char* path = "/tmp/test_cfg_newsect.ini"; |
|
QFile f(path); |
|
Q_ASSERT(f.open(QIODevice::WriteOnly | QIODevice::Text)); |
|
f.write("[global]\nname=x\n"); |
|
f.close(); |
|
|
|
bool ok = config_update_option(path, "gui", "theme", "dark"); |
|
Q_ASSERT(ok); |
|
|
|
Q_ASSERT(f.open(QIODevice::ReadOnly | QIODevice::Text)); |
|
QString content = f.readAll(); |
|
f.close(); |
|
Q_ASSERT(content.contains("[global]")); |
|
Q_ASSERT(content.contains("name=x")); |
|
Q_ASSERT(content.contains("[gui]")); |
|
Q_ASSERT(content.contains("theme=dark")); |
|
QFile::remove(path); |
|
} |
|
|
|
int main() { |
|
test_new_file(); |
|
test_replace_existing(); |
|
test_replace_new_param_in_section(); |
|
test_append_mode(); |
|
test_new_section(); |
|
return 0; |
|
}
|
|
|