From 6eed3155e6a84f1b27b5340b45d6deb801fee42d Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 24 Aug 2015 09:32:27 -0700 Subject: Add simple test for git storage This just makes sure that writing data to git storage and reading it back gives you the same result. Without the fixed generation of initial dive site UUIDs this fails. Signed-off-by: Dirk Hohndel --- tests/testgitstorage.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/testgitstorage.cpp (limited to 'tests/testgitstorage.cpp') diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp new file mode 100644 index 000000000..abb4b2c59 --- /dev/null +++ b/tests/testgitstorage.cpp @@ -0,0 +1,37 @@ +#include "testgitstorage.h" +#include "dive.h" +#include "divelist.h" +#include "file.h" +#include "git2.h" +#include +#include + +void TestGitStorage::testGitStorageLocal() +{ + // test writing and reading back from local git storage + git_repository *repo; + git_libgit2_init(); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/SampleDivesV2.ssrf"), 0); + QString testDirName("./gittest"); + QDir testDir(testDirName); + QCOMPARE(testDir.removeRecursively(), true); + QCOMPARE(QDir().mkdir(testDirName), true); + QCOMPARE(git_repository_init(&repo, qPrintable(testDirName), false), 0); + QCOMPARE(save_dives(qPrintable(testDirName + "[test]")), 0); + QCOMPARE(save_dives("./SampleDivesV3.ssrf"), 0); + clear_dive_file_data(); + QCOMPARE(parse_file(qPrintable(testDirName + "[test]")), 0); + QCOMPARE(save_dives("./SampleDivesV3viagit.ssrf"), 0); + QFile org("./SampleDivesV3.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SampleDivesV3viagit.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); + clear_dive_file_data(); +} + +QTEST_MAIN(TestGitStorage) -- cgit v1.2.3-70-g09d2 From 092abe9b393d7cfdb1cf1d8012860591593210f3 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 24 Aug 2015 12:15:45 -0700 Subject: Cloud storage: add test for simple save / load from our test account This will fail if the proxy settings of the user running the test are incorrect. Signed-off-by: Dirk Hohndel --- tests/testgitstorage.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/testgitstorage.h | 1 + 2 files changed, 63 insertions(+) (limited to 'tests/testgitstorage.cpp') diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index abb4b2c59..08dbcaaa0 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -3,8 +3,12 @@ #include "divelist.h" #include "file.h" #include "git2.h" +#include "prefs-macros.h" #include #include +#include +#include +#include void TestGitStorage::testGitStorageLocal() { @@ -34,4 +38,62 @@ void TestGitStorage::testGitStorageLocal() clear_dive_file_data(); } +void TestGitStorage::testGitStorageCloud() +{ + // test writing and reading back from cloud storage + // first, setup the preferences an proxy information + prefs = default_prefs; + QCoreApplication::setOrganizationName("Subsurface"); + QCoreApplication::setOrganizationDomain("subsurface.hohndel.org"); + QCoreApplication::setApplicationName("Subsurface"); + QSettings s; + QVariant v; + s.beginGroup("Network"); + GET_INT_DEF("proxy_type", proxy_type, QNetworkProxy::DefaultProxy); + GET_TXT("proxy_host", proxy_host); + GET_INT("proxy_port", proxy_port); + GET_BOOL("proxy_auth", proxy_auth); + GET_TXT("proxy_user", proxy_user); + GET_TXT("proxy_pass", proxy_pass); + s.endGroup(); + s.beginGroup("CloudStorage"); + GET_TXT("cloud_base_url", cloud_base_url); + QString gitUrl(prefs.cloud_base_url); + if (gitUrl.right(1) != "/") + gitUrl += "/"; + prefs.cloud_git_url = strdup(qPrintable(gitUrl + "git")); + s.endGroup(); + prefs.cloud_storage_email_encoded = strdup("ssrftest@hohndel.org"); + prefs.cloud_storage_password = strdup("geheim"); + prefs.cloud_background_sync = true; + QNetworkProxy proxy; + proxy.setType(QNetworkProxy::ProxyType(prefs.proxy_type)); + proxy.setHostName(prefs.proxy_host); + proxy.setPort(prefs.proxy_port); + if (prefs.proxy_auth) { + proxy.setUser(prefs.proxy_user); + proxy.setPassword(prefs.proxy_pass); + } + QNetworkProxy::setApplicationProxy(proxy); + + // now connect to the ssrftest repository on the cloud server + // and repeat the same test as before with the local git storage + QString cloudTestRepo("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org[ssrftest@hohndel.org]"); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/SampleDivesV2.ssrf"), 0); + QCOMPARE(save_dives(qPrintable(cloudTestRepo)), 0); + clear_dive_file_data(); + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(save_dives("./SampleDivesV3viacloud.ssrf"), 0); + QFile org("./SampleDivesV3.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SampleDivesV3viacloud.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); + clear_dive_file_data(); +} + QTEST_MAIN(TestGitStorage) diff --git a/tests/testgitstorage.h b/tests/testgitstorage.h index b182d4a22..a701363e9 100644 --- a/tests/testgitstorage.h +++ b/tests/testgitstorage.h @@ -8,6 +8,7 @@ class TestGitStorage : public QObject Q_OBJECT private slots: void testGitStorageLocal(); + void testGitStorageCloud(); }; #endif // TESTGITSTORAGE_H -- cgit v1.2.3-70-g09d2 From 449ba2876fd8d41262b440270e804a2f5acfbc12 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 24 Aug 2015 13:59:20 -0700 Subject: Cloud storage: test offline operation All this really does is make sure that the fast forward works if the local cache has received updates that haven't made it to the server, yet. Signed-off-by: Dirk Hohndel --- git-access.c | 2 +- tests/testgitstorage.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/testgitstorage.h | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) (limited to 'tests/testgitstorage.cpp') diff --git a/git-access.c b/git-access.c index 36309fa42..9c5b703a2 100644 --- a/git-access.c +++ b/git-access.c @@ -45,7 +45,7 @@ git_branch_create(out, repo, branch_name, target, force) #endif -static char *get_local_dir(const char *remote, const char *branch) +char *get_local_dir(const char *remote, const char *branch) { SHA_CTX ctx; unsigned char hash[20]; diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index 08dbcaaa0..5bc7924af 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -96,4 +96,57 @@ void TestGitStorage::testGitStorageCloud() clear_dive_file_data(); } +// this is a local helper function in git-access.c +extern "C" char *get_local_dir(const char *remote, const char *branch); + +void TestGitStorage::testGitStorageCloudOfflineSync() +{ + // make a change to local cache repo (pretending that we did some offline changes) + // and then open the remote one again and check that things were propagated correctly + QString cloudTestRepo("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org[ssrftest@hohndel.org]"); + QString localCacheDir(get_local_dir("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org", "ssrftest@hohndel.org")); + QString localCacheRepo = localCacheDir + "[ssrftest@hohndel.org]"; + // read the local repo from the previous test and add dive 10 + QCOMPARE(parse_file(qPrintable(localCacheRepo)), 0); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/test10.xml"), 0); + // calling process_dive() sorts the table, but calling it with + // is_imported == true causes it to try to update the window title... let's not do that + process_dives(false, false); + // now save only to the local cache but not to the remote server + QCOMPARE(save_dives(qPrintable(localCacheRepo)), 0); + QCOMPARE(save_dives("./SampleDivesV3plus10local.ssrf"), 0); + clear_dive_file_data(); + // open the cloud storage and compare + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(save_dives("./SampleDivesV3plus10viacloud.ssrf"), 0); + QFile org("./SampleDivesV3plus10local.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SampleDivesV3plus10viacloud.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); + // write back out to cloud storage, move away the local cache, open again and compare + QCOMPARE(save_dives(qPrintable(cloudTestRepo)), 0); + clear_dive_file_data(); + QDir localCacheDirectory(localCacheDir); + QDir localCacheDirectorySave(localCacheDir + "save"); + QCOMPARE(localCacheDirectorySave.removeRecursively(), true); + QCOMPARE(localCacheDirectory.rename(localCacheDir, localCacheDir + "save"), true); + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(save_dives("./SampleDivesV3plus10fromcloud.ssrf"), 0); + org.close(); + org.open(QFile::ReadOnly); + QFile out2("./SampleDivesV3plus10fromcloud.ssrf"); + out2.open(QFile::ReadOnly); + QTextStream orgS2(&org); + QTextStream outS2(&out2); + readin = orgS2.readAll(); + written = outS2.readAll(); + QCOMPARE(readin, written); + clear_dive_file_data(); +} + QTEST_MAIN(TestGitStorage) diff --git a/tests/testgitstorage.h b/tests/testgitstorage.h index a701363e9..43c33c228 100644 --- a/tests/testgitstorage.h +++ b/tests/testgitstorage.h @@ -9,6 +9,7 @@ class TestGitStorage : public QObject private slots: void testGitStorageLocal(); void testGitStorageCloud(); + void testGitStorageCloudOfflineSync(); }; #endif // TESTGITSTORAGE_H -- cgit v1.2.3-70-g09d2 From 9f5a944107b7a8852adccd6da71dc94baa374bb5 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 24 Aug 2015 14:00:42 -0700 Subject: Cloud storage: first test case for a very simple merge - We add a dive while offline. - On a different computer (here simulated by a different local cache) we add a different file. - Now we go back to the previous local cache (the one where we added a different dive in the first step) and take that online (i.e., connect to cloud storage). Now both of the new dives should have been added to our data file. This is a rather trivial test with no conflict and a straight forward merge. We need to add a lot more test cases to make sure this works as expected and doesn't leave the user with a corrupt state. Ideally whatever happens, the user should never see an error... Signed-off-by: Dirk Hohndel --- tests/testgitstorage.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ tests/testgitstorage.h | 1 + 2 files changed, 48 insertions(+) (limited to 'tests/testgitstorage.cpp') diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index 5bc7924af..c262e27be 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -149,4 +149,51 @@ void TestGitStorage::testGitStorageCloudOfflineSync() clear_dive_file_data(); } +void TestGitStorage::testGitStorageCloudMerge() +{ + // now we need to mess with the local git repo to get an actual merge + // first we add another dive to the "moved away" repository, pretending we did + // another offline change there + QString cloudTestRepo("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org[ssrftest@hohndel.org]"); + QString localCacheDir(get_local_dir("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org", "ssrftest@hohndel.org")); + QString localCacheRepoSave = localCacheDir + "save[ssrftest@hohndel.org]"; + QCOMPARE(parse_file(qPrintable(localCacheRepoSave)), 0); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/test11.xml"), 0); + process_dives(false, false); + QCOMPARE(save_dives(qPrintable(localCacheRepoSave)), 0); + clear_dive_file_data(); + + // now we open the cloud storage repo and add a different dive to it + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/test12.xml"), 0); + process_dives(false, false); + QCOMPARE(save_dives(qPrintable(cloudTestRepo)), 0); + clear_dive_file_data(); + + // now we move the saved local cache into place and try to open the cloud repo + // -> this forces a merge + QDir localCacheDirectory(localCacheDir); + QCOMPARE(localCacheDirectory.removeRecursively(), true); + QDir localCacheDirectorySave(localCacheDir + "save"); + QCOMPARE(localCacheDirectory.rename(localCacheDir + "save", localCacheDir), true); + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(save_dives("./SapleDivesV3plus10-11-12-merged.ssrf"), 0); + clear_dive_file_data(); + QCOMPARE(parse_file("./SampleDivesV3plus10local.ssrf"), 0); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/test11.xml"), 0); + process_dives(false, false); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/test12.xml"), 0); + process_dives(false, false); + QCOMPARE(save_dives("./SapleDivesV3plus10-11-12.ssrf"), 0); + QFile org("./SapleDivesV3plus10-11-12-merged.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SapleDivesV3plus10-11-12.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); +} + QTEST_MAIN(TestGitStorage) diff --git a/tests/testgitstorage.h b/tests/testgitstorage.h index 43c33c228..93cf70177 100644 --- a/tests/testgitstorage.h +++ b/tests/testgitstorage.h @@ -10,6 +10,7 @@ private slots: void testGitStorageLocal(); void testGitStorageCloud(); void testGitStorageCloudOfflineSync(); + void testGitStorageCloudMerge(); }; #endif // TESTGITSTORAGE_H -- cgit v1.2.3-70-g09d2 From 72817ff47f8a990aa043603ebe2d3ead1ff811c9 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Mon, 24 Aug 2015 16:23:21 -0700 Subject: Cloud storage: second merge test, delete dive on one side, modify on other Delete a dive while offline, modify it from a different system while online. Then resync. Signed-off-by: Dirk Hohndel --- tests/testgitstorage.cpp | 128 +++++++++++++++++++++++++++++++++++------------ tests/testgitstorage.h | 2 + 2 files changed, 97 insertions(+), 33 deletions(-) (limited to 'tests/testgitstorage.cpp') diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index c262e27be..f5846bbd2 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -10,37 +10,11 @@ #include #include -void TestGitStorage::testGitStorageLocal() -{ - // test writing and reading back from local git storage - git_repository *repo; - git_libgit2_init(); - QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/SampleDivesV2.ssrf"), 0); - QString testDirName("./gittest"); - QDir testDir(testDirName); - QCOMPARE(testDir.removeRecursively(), true); - QCOMPARE(QDir().mkdir(testDirName), true); - QCOMPARE(git_repository_init(&repo, qPrintable(testDirName), false), 0); - QCOMPARE(save_dives(qPrintable(testDirName + "[test]")), 0); - QCOMPARE(save_dives("./SampleDivesV3.ssrf"), 0); - clear_dive_file_data(); - QCOMPARE(parse_file(qPrintable(testDirName + "[test]")), 0); - QCOMPARE(save_dives("./SampleDivesV3viagit.ssrf"), 0); - QFile org("./SampleDivesV3.ssrf"); - org.open(QFile::ReadOnly); - QFile out("./SampleDivesV3viagit.ssrf"); - out.open(QFile::ReadOnly); - QTextStream orgS(&org); - QTextStream outS(&out); - QString readin = orgS.readAll(); - QString written = outS.readAll(); - QCOMPARE(readin, written); - clear_dive_file_data(); -} +// this is a local helper function in git-access.c +extern "C" char *get_local_dir(const char *remote, const char *branch); -void TestGitStorage::testGitStorageCloud() +void TestGitStorage::testSetup() { - // test writing and reading back from cloud storage // first, setup the preferences an proxy information prefs = default_prefs; QCoreApplication::setOrganizationName("Subsurface"); @@ -76,7 +50,44 @@ void TestGitStorage::testGitStorageCloud() } QNetworkProxy::setApplicationProxy(proxy); - // now connect to the ssrftest repository on the cloud server + // now cleanup the cache dir in case there's something weird from previous runs + QString localCacheDir(get_local_dir("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org", "ssrftest@hohndel.org")); + QDir localCacheDirectory(localCacheDir); + QCOMPARE(localCacheDirectory.removeRecursively(), true); +} + +void TestGitStorage::testGitStorageLocal() +{ + // test writing and reading back from local git storage + git_repository *repo; + git_libgit2_init(); + QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/SampleDivesV2.ssrf"), 0); + QString testDirName("./gittest"); + QDir testDir(testDirName); + QCOMPARE(testDir.removeRecursively(), true); + QCOMPARE(QDir().mkdir(testDirName), true); + QCOMPARE(git_repository_init(&repo, qPrintable(testDirName), false), 0); + QCOMPARE(save_dives(qPrintable(testDirName + "[test]")), 0); + QCOMPARE(save_dives("./SampleDivesV3.ssrf"), 0); + clear_dive_file_data(); + QCOMPARE(parse_file(qPrintable(testDirName + "[test]")), 0); + QCOMPARE(save_dives("./SampleDivesV3viagit.ssrf"), 0); + QFile org("./SampleDivesV3.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SampleDivesV3viagit.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); + clear_dive_file_data(); +} + +void TestGitStorage::testGitStorageCloud() +{ + // test writing and reading back from cloud storage + // connect to the ssrftest repository on the cloud server // and repeat the same test as before with the local git storage QString cloudTestRepo("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org[ssrftest@hohndel.org]"); QCOMPARE(parse_file(SUBSURFACE_SOURCE "/dives/SampleDivesV2.ssrf"), 0); @@ -96,9 +107,6 @@ void TestGitStorage::testGitStorageCloud() clear_dive_file_data(); } -// this is a local helper function in git-access.c -extern "C" char *get_local_dir(const char *remote, const char *branch); - void TestGitStorage::testGitStorageCloudOfflineSync() { // make a change to local cache repo (pretending that we did some offline changes) @@ -194,6 +202,60 @@ void TestGitStorage::testGitStorageCloudMerge() QString readin = orgS.readAll(); QString written = outS.readAll(); QCOMPARE(readin, written); + clear_dive_file_data(); +} + +void TestGitStorage::testGitStorageCloudMerge2() +{ + // delete a dive offline + // edit the same dive in the cloud repo + // merge + QString cloudTestRepo("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org[ssrftest@hohndel.org]"); + QString localCacheDir(get_local_dir("https://cloud.subsurface-divelog.org/git/ssrftest@hohndel.org", "ssrftest@hohndel.org")); + QString localCacheRepo = localCacheDir + "[ssrftest@hohndel.org]"; + QCOMPARE(parse_file(qPrintable(localCacheRepo)), 0); + process_dives(false, false); + struct dive *dive = get_dive(1); + delete_single_dive(1); + QCOMPARE(save_dives("./SampleDivesMinus1.ssrf"), 0); + QCOMPARE(save_dives(qPrintable(localCacheRepo)), 0); + clear_dive_file_data(); + + // move the local cache away + { // scope for variables + QDir localCacheDirectory(localCacheDir); + QDir localCacheDirectorySave(localCacheDir + "save"); + QCOMPARE(localCacheDirectorySave.removeRecursively(), true); + QCOMPARE(localCacheDirectory.rename(localCacheDir, localCacheDir + "save"), true); + } + // now we open the cloud storage repo and modify that first dive + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + process_dives(false, false); + dive = get_dive(1); + free(dive->notes); + dive->notes = strdup("These notes have been modified by TestGitStorage"); + QCOMPARE(save_dives(qPrintable(cloudTestRepo)), 0); + clear_dive_file_data(); + + // now we move the saved local cache into place and try to open the cloud repo + // -> this forces a merge + QDir localCacheDirectory(localCacheDir); + QDir localCacheDirectorySave(localCacheDir + "save"); + QCOMPARE(localCacheDirectory.removeRecursively(), true); + QCOMPARE(localCacheDirectorySave.rename(localCacheDir + "save", localCacheDir), true); + + QCOMPARE(parse_file(qPrintable(cloudTestRepo)), 0); + QCOMPARE(save_dives("./SampleDivesMinus1-merged.ssrf"), 0); + QCOMPARE(save_dives(qPrintable(cloudTestRepo)), 0); + QFile org("./SampleDivesMinus1-merged.ssrf"); + org.open(QFile::ReadOnly); + QFile out("./SampleDivesMinus1.ssrf"); + out.open(QFile::ReadOnly); + QTextStream orgS(&org); + QTextStream outS(&out); + QString readin = orgS.readAll(); + QString written = outS.readAll(); + QCOMPARE(readin, written); } QTEST_MAIN(TestGitStorage) diff --git a/tests/testgitstorage.h b/tests/testgitstorage.h index 93cf70177..d5f69fc65 100644 --- a/tests/testgitstorage.h +++ b/tests/testgitstorage.h @@ -7,10 +7,12 @@ class TestGitStorage : public QObject { Q_OBJECT private slots: + void testSetup(); void testGitStorageLocal(); void testGitStorageCloud(); void testGitStorageCloudOfflineSync(); void testGitStorageCloudMerge(); + void testGitStorageCloudMerge2(); }; #endif // TESTGITSTORAGE_H -- cgit v1.2.3-70-g09d2