Files
amnezia-client/client/tests/testAndroidRestoreBackup.cpp
T
NickVs2015andMrMirDan b76c88bba3 feat: migrate test to main repo (#3172)
* basic tests  adaptation

* fix: ui servers model and controller test awg config

* fix: platforms test builds

* build dirs inspection

* fix: changed paths for tests and tests artifacts

* fix: 'deploy' parent dir

* windows ctest configuration and macos dependencies inspection

* windows check executable and macos copy openssl libs

* windows test executables check and macos copy openssl

* changed some path on windows

* android apk list

* fix: updated android apk path

* Verify Android test files

* fix: build tests only with AMNEZIA_BUILD_TESTS

* fix: copy openssl runtime next to test_openssl_library

* fix: awg2 test config and inverted description checks

* chore: move run_android_tests.sh to client/tests and drop personal paths

* chore: gate tests by option, scope secrets and drop global concurrency in ci

* chore: serialized vps test job, scoped secrets, backup secret guard, test data lookup

---------

Co-authored-by: MrMirDan <8mirdan8@gmail.com>
2026-09-16 17:05:43 +08:00

153 lines
6.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <QTest>
#include <QDebug>
#include <QSignalSpy>
#include <QJsonDocument>
#include <QJsonObject>
#include <QUuid>
#include "utils/testCoreController.h"
#include "utils/testUtils.h"
#include "core/controllers/settingsController.h"
#include "core/repositories/secureServersRepository.h"
#include "core/repositories/secureAppSettingsRepository.h"
#include "vpnConnection.h"
#include "secureQSettings.h"
using namespace amnezia;
using namespace amnezia::test;
class TestAndroidRestoreBackup : public QObject
{
Q_OBJECT
private:
TestCoreController *m_coreController;
SecureQSettings *m_settings;
void importServer(const QString &key)
{
auto result = m_coreController->m_importCoreController->extractConfigFromData(key);
QVERIFY2(result.errorCode == ErrorCode::NoError, "Server import must succeed");
m_coreController->m_importCoreController->importConfig(result.config);
}
private slots:
void initTestCase()
{
const QString testOrg = "AmneziaVPN-Test-" + QUuid::createUuid().toString();
m_settings = new SecureQSettings(testOrg, "amnezia-client", nullptr, false);
auto vpnConnection = QSharedPointer<VpnConnection>::create(nullptr, nullptr);
m_coreController = new TestCoreController(vpnConnection, m_settings, nullptr, this);
}
void cleanupTestCase()
{
m_settings->clearSettings();
delete m_coreController;
delete m_settings;
}
void init()
{
m_settings->clearSettings();
m_coreController->m_serversRepository->invalidateCache();
}
// -----------------------------------------------------------------------
// Android safety: killSwitch and autoConnect must be DISABLED after restore
// even if the backup was created with them enabled.
// (SettingsController::restoreAppConfigFromData unconditionally resets
// these flags on Android / iOS – see Q_OS_ANDROID block in the impl.)
// -----------------------------------------------------------------------
void testRestoreDisablesDangerousSettingsOnAndroid()
{
const QString key = getEnvValue("THIRD_PARTY_AWG_VPN_KEY");
if (!isEnvValueConfigured(key)) {
QSKIP("THIRD_PARTY_AWG_VPN_KEY not set – skipping");
}
importServer(key);
// Enable dangerous settings before creating the backup
m_coreController->m_appSettingsRepository->setKillSwitchEnabled(true);
m_coreController->m_appSettingsRepository->setAutoConnect(true);
QVERIFY(m_coreController->m_appSettingsRepository->isKillSwitchEnabled());
QVERIFY(m_coreController->m_appSettingsRepository->isAutoConnect());
const QByteArray backup = m_coreController->m_settingsController->backupAppConfig();
QVERIFY(!backup.isEmpty());
const QJsonObject backupJson = QJsonDocument::fromJson(backup).object();
qInfo() << "Backup killSwitchEnabled :" << backupJson.value("Conf/killSwitchEnabled").toBool();
qInfo() << "Backup autoConnect :" << backupJson.value("Conf/autoConnect").toBool();
m_settings->clearSettings();
m_coreController->m_serversRepository->invalidateCache();
const ErrorCode err = m_coreController->m_settingsController->restoreAppConfigFromData(backup);
QVERIFY2(err == ErrorCode::NoError, "Restore must succeed");
const bool killSwitch = m_coreController->m_appSettingsRepository->isKillSwitchEnabled();
const bool autoConnect = m_coreController->m_appSettingsRepository->isAutoConnect();
qInfo() << "killSwitchEnabled after restore:" << killSwitch;
qInfo() << "autoConnect after restore :" << autoConnect;
QVERIFY2(!killSwitch,
"killSwitch must be DISABLED after restore on Android (safety reset)");
QVERIFY2(!autoConnect,
"autoConnect must be DISABLED after restore on Android (safety reset)");
}
// -----------------------------------------------------------------------
// Cross-platform backup: restoring a backup from another platform must
// succeed and must clear the app split-tunneling apps list on Android.
// -----------------------------------------------------------------------
void testRestoreFromOtherPlatformClearsAppSplitTunnelingList()
{
const QString key = getEnvValue("THIRD_PARTY_AWG_VPN_KEY");
if (!isEnvValueConfigured(key)) {
QSKIP("THIRD_PARTY_AWG_VPN_KEY not set – skipping");
}
importServer(key);
// Build a synthetic backup that claims to come from Windows
QByteArray baseBackup = m_coreController->m_settingsController->backupAppConfig();
QJsonObject obj = QJsonDocument::fromJson(baseBackup).object();
obj["AppPlatform"] = "Windows";
const QByteArray crossPlatformBackup = QJsonDocument(obj).toJson();
QSignalSpy clearAppsListSpy(m_coreController->m_settingsController,
&SettingsController::appSplitTunnelingClearAppsList);
m_settings->clearSettings();
m_coreController->m_serversRepository->invalidateCache();
const ErrorCode err =
m_coreController->m_settingsController->restoreAppConfigFromData(crossPlatformBackup);
QVERIFY2(err == ErrorCode::NoError, "Cross-platform restore must succeed");
qInfo() << "appSplitTunnelingClearAppsList emitted:" << clearAppsListSpy.count() << "time(s)";
QVERIFY2(clearAppsListSpy.count() == 1,
"appSplitTunnelingClearAppsList must be emitted when restoring from different platform");
}
// -----------------------------------------------------------------------
// Invalid data must return an error, not crash
// -----------------------------------------------------------------------
void testRestoreInvalidDataReturnsError()
{
const QByteArray garbage = QByteArray("not a valid backup {{{{");
const ErrorCode err = m_coreController->m_settingsController->restoreAppConfigFromData(garbage);
qInfo() << "ErrorCode for invalid backup:" << static_cast<int>(err);
QVERIFY2(err != ErrorCode::NoError,
"Restoring invalid data must return an error code");
}
};
QTEST_MAIN(TestAndroidRestoreBackup)
#include "testAndroidRestoreBackup.moc"