|
| 1 | +/* |
| 2 | + * This software is in the public domain, furnished "as is", without technical |
| 3 | + * support, and with no warranty, express or implied, as to its usefulness for |
| 4 | + * any purpose. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include <qglobal.h> |
| 9 | +#include <QTemporaryDir> |
| 10 | +#include <QtTest> |
| 11 | +#include <QtTest/qtestcase.h> |
| 12 | + |
| 13 | +#include "common/utility.h" |
| 14 | +#include "folderman.h" |
| 15 | +#include "account.h" |
| 16 | +#include "accountstate.h" |
| 17 | +#include <accountmanager.h> |
| 18 | +#include "configfile.h" |
| 19 | +#include "syncenginetestutils.h" |
| 20 | +#include "testhelper.h" |
| 21 | +#include "version.h" |
| 22 | + |
| 23 | +using namespace OCC; |
| 24 | + |
| 25 | +class TestMigration: public QObject |
| 26 | +{ |
| 27 | + Q_OBJECT |
| 28 | + |
| 29 | + ConfigFile _configFile; |
| 30 | + QTemporaryDir _temporaryDir; |
| 31 | + std::unique_ptr<FolderMan> _folderMan; |
| 32 | + |
| 33 | +private: |
| 34 | + static constexpr char legacyAppName[] = "ownCloud"; |
| 35 | + static constexpr char standardAppName[] = "Nextcloud"; |
| 36 | + static constexpr char brandedAppName[] = "Branded"; |
| 37 | + static constexpr char legacyAppConfigContent[] = "[General]\n" |
| 38 | + "clientVersion=5.3.2.15463\n" |
| 39 | + "issuesWidgetFilter=FatalError, BlacklistedError, Excluded, Message, FilenameReserved\n" |
| 40 | + "logHttp=false\n" |
| 41 | + "optionalDesktopNotifications=true\n" |
| 42 | + "\n" |
| 43 | + "[Accounts]e\n" |
| 44 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\davUrl=@Variant(http://oc.de/remote.php/dav/files/admin/)\n" |
| 45 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\deployed=false\n" |
| 46 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\displayString=ownCloud\n" |
| 47 | + "0\\Folders\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\ignoreHiddenFiles=true\n" |
| 48 | + "0\\Folders\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\journalPath=.sync_journal.db\n" |
| 49 | + "0\\Folders\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\localPath=/ownCloud/\n" |
| 50 | + "0\\Folders\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\paused=false\n" |
| 51 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\priority=0\n" |
| 52 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\targetPath=/\n" |
| 53 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\version=13\n" |
| 54 | + "0\\Folders\\2ba4b09a-1223-aaaa-abcd-c2df238816d8\\virtualFilesMode=off\n" |
| 55 | + "0\\capabilities=@QVariant()\n" |
| 56 | + "0\\dav_user=admin\n" |
| 57 | + "0\\default_sync_root=/ownCloud\n" |
| 58 | + "0\\display-name=admin\n" |
| 59 | + "0\\http_CredentialVersion=1\n" |
| 60 | + "0\\http_oauth=false\n" |
| 61 | + "0\\http_user=admin\n" |
| 62 | + "0\\supportsSpaces=true\n" |
| 63 | + "0\\url=http://oc.de/\n" |
| 64 | + "0\\user=admin\n" |
| 65 | + "0\\userExplicitlySignedOut=false\n" |
| 66 | + "0\\uuid=@Variant()\n" |
| 67 | + "0\\version=13\n" |
| 68 | + "version=13\n" |
| 69 | + "\n" |
| 70 | + "[Credentials]\n" |
| 71 | + "ownCloud_credentials%oc.de%2ba4b09a-1223-aaaa-abcd-c2df238816d8\\http\\password=true"; |
| 72 | + |
| 73 | +private slots: |
| 74 | + void setupStandardConfigFolder() |
| 75 | + { |
| 76 | + QVERIFY(QDir(_temporaryDir.path()).mkpath(standardAppName)); |
| 77 | + const auto standardConfigFolder = QString(_temporaryDir.path() + "/" + standardAppName); |
| 78 | + _configFile.setConfDir(standardConfigFolder); |
| 79 | + } |
| 80 | + |
| 81 | + void setupStandarConfig(const QString &version) |
| 82 | + { |
| 83 | + setupStandardConfigFolder(); |
| 84 | + QSettings settings(_configFile.configFile(), QSettings::IniFormat); |
| 85 | + _configFile.setClientVersionString(version); |
| 86 | + _configFile.setOptionalServerNotifications(true); |
| 87 | + _configFile.setShowChatNotifications(true); |
| 88 | + _configFile.setShowCallNotifications(true); |
| 89 | + _configFile.setShowInExplorerNavigationPane(true); |
| 90 | + _configFile.setShowInExplorerNavigationPane(true); |
| 91 | + _configFile.setRemotePollInterval(std::chrono::milliseconds(1000)); |
| 92 | + _configFile.setAutoUpdateCheck(true, QString()); |
| 93 | + _configFile.setUpdateChannel("beta"); |
| 94 | + _configFile.setOverrideServerUrl("http://example.de"); |
| 95 | + _configFile.setOverrideLocalDir("A"); |
| 96 | + _configFile.setVfsEnabled(true); |
| 97 | + _configFile.setProxyType(0); |
| 98 | + _configFile.setVfsEnabled(true); |
| 99 | + _configFile.setUseUploadLimit(0); |
| 100 | + _configFile.setUploadLimit(1); |
| 101 | + _configFile.setUseDownloadLimit(0); |
| 102 | + _configFile.setUseDownloadLimit(1); |
| 103 | + _configFile.setNewBigFolderSizeLimit(true, 500); |
| 104 | + _configFile.setNotifyExistingFoldersOverLimit(true); |
| 105 | + _configFile.setStopSyncingExistingFoldersOverLimit(true); |
| 106 | + _configFile.setConfirmExternalStorage(true); |
| 107 | + _configFile.setMoveToTrash(true); |
| 108 | + _configFile.setForceLoginV2(true); |
| 109 | + _configFile.setPromptDeleteFiles(true); |
| 110 | + _configFile.setDeleteFilesThreshold(1); |
| 111 | + _configFile.setMonoIcons(true); |
| 112 | + _configFile.setAutomaticLogDir(true); |
| 113 | + _configFile.setLogDir(_temporaryDir.path()); |
| 114 | + _configFile.setLogDebug(true); |
| 115 | + _configFile.setLogExpire(72); |
| 116 | + _configFile.setLogFlush(true); |
| 117 | + _configFile.setCertificatePath(_temporaryDir.path()); |
| 118 | + _configFile.setCertificatePasswd("123456"); |
| 119 | + _configFile.setLaunchOnSystemStartup(true); |
| 120 | + _configFile.setServerHasValidSubscription(true); |
| 121 | + _configFile.setDesktopEnterpriseChannel("stable"); |
| 122 | + _configFile.setLanguage("pt"); |
| 123 | + settings.sync(); |
| 124 | + QVERIFY(_configFile.exists()); |
| 125 | + QScopedPointer<FakeQNAM> fakeQnam(new FakeQNAM({})); |
| 126 | + OCC::AccountPtr account = OCC::Account::create(); |
| 127 | + account->setDavUser("user"); |
| 128 | + account->setDavDisplayName("Nextcloud user"); |
| 129 | + account->setProxyType(QNetworkProxy::ProxyType::HttpProxy); |
| 130 | + account->setProxyUser("proxyuser"); |
| 131 | + account->setDownloadLimit(120); |
| 132 | + account->setUploadLimit(120); |
| 133 | + account->setDownloadLimitSetting(OCC::Account::AccountNetworkTransferLimitSetting::ManualLimit); |
| 134 | + account->setServerVersion("30"); |
| 135 | + account->setCredentials(new FakeCredentials{fakeQnam.data()}); |
| 136 | + account->setUrl(QUrl(("http://example.de"))); |
| 137 | + const auto accountState = OCC::AccountManager::instance()->addAccount(account); |
| 138 | + OCC::AccountManager::instance()->saveAccount(accountState->account()); |
| 139 | + OCC::FolderDefinition folderDefinition; |
| 140 | + folderDefinition.localPath = "/standardAppName"; |
| 141 | + folderDefinition.targetPath = "/"; |
| 142 | + folderDefinition.alias = standardAppName; |
| 143 | + _folderMan.reset({}); |
| 144 | + _folderMan.reset(new FolderMan{}); |
| 145 | + QVERIFY(_folderMan->addFolder(accountState, folderDefinition)); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + void initTestCase() |
| 150 | + { |
| 151 | + OCC::Logger::instance()->setLogFlush(true); |
| 152 | + OCC::Logger::instance()->setLogDebug(true); |
| 153 | + |
| 154 | + QStandardPaths::setTestModeEnabled(true); |
| 155 | + } |
| 156 | + |
| 157 | + // Upgrade - TODO: test running app with --confdir |
| 158 | + void testUpgrade() |
| 159 | + { |
| 160 | + // create Nextcloud config with older version |
| 161 | + setupStandarConfig("1.0.0"); |
| 162 | + const auto oldAppVersionNumber = QVersionNumber::fromString(_configFile.clientVersionString()); |
| 163 | + QVERIFY(_configFile.isUpgrade()); |
| 164 | + |
| 165 | + // backup old config |
| 166 | + const auto backupFilesList = _configFile.backupConfigFiles(); |
| 167 | + QCOMPARE_GE(backupFilesList.size(), 1); |
| 168 | + |
| 169 | + // successfully upgrade to new config |
| 170 | + const auto afterUpgradeVersionNumber = MIRALL_VERSION_STRING; |
| 171 | + _configFile.setClientVersionString(afterUpgradeVersionNumber); |
| 172 | + QVERIFY(MIRALL_VERSION_STRING == _configFile.clientVersionString()); |
| 173 | + |
| 174 | + QCOMPARE_GE(AccountManager::instance()->accounts().size(), 1); |
| 175 | + auto accounts = AccountManager::instance()->accounts().first()->settings(); |
| 176 | + QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 177 | + accounts->beginGroup(QLatin1String("Folders")); |
| 178 | + QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 179 | + accounts->endGroup(); |
| 180 | + } |
| 181 | + |
| 182 | + // From oC client to Nextcloud |
| 183 | + void testMigrationFromOctoNextcloud() |
| 184 | + { |
| 185 | + QTemporaryDir tempDir; |
| 186 | + QVERIFY(QDir(tempDir.path()).mkpath(legacyAppName)); |
| 187 | + const auto ocConfigFolder = QString(tempDir.path() + "/" + legacyAppName); |
| 188 | + const auto ocConfig = QString(ocConfigFolder + "/" + QString(legacyAppName).toLower() + ".cfg"); |
| 189 | + QFile ocConfigFile(ocConfig); |
| 190 | + QVERIFY(ocConfigFile.open(QFile::WriteOnly)); |
| 191 | + QCOMPARE_GE(ocConfigFile.write(legacyAppConfigContent, qstrlen(legacyAppConfigContent)), 0); |
| 192 | + ocConfigFile.close(); |
| 193 | + |
| 194 | + ConfigFile configFile; |
| 195 | + QVERIFY(QDir(tempDir.path()).mkpath(standardAppName)); |
| 196 | + const auto standardConfigFolder = QString(tempDir.path() + "/" + standardAppName); |
| 197 | + configFile.setConfDir(standardConfigFolder); |
| 198 | + |
| 199 | + // Nextcloud config file does not exist |
| 200 | + QVERIFY(!configFile.exists()); |
| 201 | + |
| 202 | + // owncloud config files exists |
| 203 | + configFile.findLegacyClientConfigFile(); |
| 204 | + QVERIFY(!configFile.discoveredLegacyConfigFile().isEmpty()); |
| 205 | + |
| 206 | + // _folderMan.reset({}); |
| 207 | + // _folderMan.reset(new FolderMan{}); |
| 208 | + |
| 209 | + // create accounts and folders from a legacy desktop client or for a new config file |
| 210 | + // QVERIFY(AccountManager::instance()->restore(configFile.configFileToRestore()) != AccountManager::AccountsRestoreFailure); |
| 211 | + // QCOMPARE_GE(FolderMan::instance()->setupFoldersMigration(), 1); |
| 212 | + // QVERIFY(configFile.configFile().contains("nextcloud")); |
| 213 | + // QCOMPARE_GE(AccountManager::instance()->accounts().size(), 1); |
| 214 | + |
| 215 | + // auto accounts = AccountManager::instance()->accounts().first()->settings(); |
| 216 | + // QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 217 | + // accounts->beginGroup(QLatin1String("Folders")); |
| 218 | + // QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 219 | + // accounts->endGroup(); |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + // From the standard Nextcloud client to a branded version |
| 224 | + void testMigrationFromNextcloudToBranded() |
| 225 | + { |
| 226 | + setupStandardConfigFolder(); |
| 227 | + |
| 228 | + QTemporaryDir tempDir; |
| 229 | + QVERIFY(QDir(tempDir.path()).mkpath(brandedAppName)); |
| 230 | + const auto brandedConfigFolder = QString(tempDir.path() + "/" + brandedAppName); |
| 231 | + const auto brandedConfig = QString(brandedConfigFolder + "/" + QString(brandedAppName).toLower() + ".cfg"); |
| 232 | + |
| 233 | + QFile::copy(_configFile.configFile(), QFileInfo(brandedConfig).filePath()); |
| 234 | + |
| 235 | + ConfigFile configFile; |
| 236 | + QVERIFY(QDir(tempDir.path()).mkpath(standardAppName)); |
| 237 | + const auto standardConfigFolder = QString(tempDir.path() + "/" + standardAppName); |
| 238 | + configFile.setConfDir(standardConfigFolder); |
| 239 | + |
| 240 | + // Nextcloud config file does not exist |
| 241 | + QVERIFY(!configFile.exists()); |
| 242 | + |
| 243 | + // owncloud config files exists |
| 244 | + configFile.findLegacyClientConfigFile(); |
| 245 | + QVERIFY(!configFile.discoveredLegacyConfigFile().isEmpty()); |
| 246 | + |
| 247 | + // _folderMan.reset({}); |
| 248 | + // _folderMan.reset(new FolderMan{}); |
| 249 | + |
| 250 | + // create accounts and folders from a legacy desktop client or for a new config file |
| 251 | + // QVERIFY(AccountManager::instance()->restore(configFile.configFileToRestore()) != AccountManager::AccountsRestoreFailure); |
| 252 | + // QCOMPARE_GE(FolderMan::instance()->setupFoldersMigration(), 1); |
| 253 | + // QVERIFY(configFile.configFile().contains("nextcloud")); |
| 254 | + // QCOMPARE_GE(AccountManager::instance()->accounts().size(), 1); |
| 255 | + |
| 256 | + // auto accounts = AccountManager::instance()->accounts().first()->settings(); |
| 257 | + // QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 258 | + // accounts->beginGroup(QLatin1String("Folders")); |
| 259 | + // QCOMPARE_GE(accounts->childGroups().size(), 1); |
| 260 | + // accounts->endGroup(); |
| 261 | + } |
| 262 | + |
| 263 | + // TODO: Downgrade |
| 264 | +}; |
| 265 | + |
| 266 | +QTEST_GUILESS_MAIN(TestMigration) |
| 267 | +#include "testmigration.moc" |
0 commit comments