diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2018-06-13 15:50:23 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2018-08-14 17:03:46 -0700 |
commit | fa7b70945fe6927536869d7dd66de0834d1e8002 (patch) | |
tree | abd25881038ef21467e9a4d56dc6918f1a690a43 /test | |
parent | a3b6fc149895a7f4c29acd6bba70546edec41438 (diff) | |
download | qtlocation-mapboxgl-fa7b70945fe6927536869d7dd66de0834d1e8002.tar.gz |
[core] harden OfflineDatabase
Diffstat (limited to 'test')
-rw-r--r-- | test/fixtures/offline_database/corrupt-delayed.db | bin | 0 -> 19456 bytes | |||
-rw-r--r-- | test/fixtures/offline_database/corrupt-immediate.db | bin | 0 -> 4096 bytes | |||
-rw-r--r-- | test/src/mbgl/test/fixture_log_observer.cpp | 6 | ||||
-rw-r--r-- | test/src/mbgl/test/sqlite3_test_fs.cpp | 320 | ||||
-rw-r--r-- | test/src/mbgl/test/sqlite3_test_fs.hpp | 39 | ||||
-rw-r--r-- | test/storage/offline_database.test.cpp | 592 | ||||
-rw-r--r-- | test/storage/offline_download.test.cpp | 79 | ||||
-rw-r--r-- | test/storage/sqlite.test.cpp | 6 |
8 files changed, 850 insertions, 192 deletions
diff --git a/test/fixtures/offline_database/corrupt-delayed.db b/test/fixtures/offline_database/corrupt-delayed.db Binary files differnew file mode 100644 index 0000000000..04989dbf36 --- /dev/null +++ b/test/fixtures/offline_database/corrupt-delayed.db diff --git a/test/fixtures/offline_database/corrupt-immediate.db b/test/fixtures/offline_database/corrupt-immediate.db Binary files differnew file mode 100644 index 0000000000..8909c402b2 --- /dev/null +++ b/test/fixtures/offline_database/corrupt-immediate.db diff --git a/test/src/mbgl/test/fixture_log_observer.cpp b/test/src/mbgl/test/fixture_log_observer.cpp index d8a4b9edce..d768c0284a 100644 --- a/test/src/mbgl/test/fixture_log_observer.cpp +++ b/test/src/mbgl/test/fixture_log_observer.cpp @@ -32,7 +32,9 @@ bool FixtureLog::Observer::onRecord(EventSeverity severity, const std::string& msg) { std::lock_guard<std::mutex> lock(messagesMutex); - messages.emplace_back(severity, event, code, msg); + if (severity != EventSeverity::Debug) { + messages.emplace_back(severity, event, code, msg); + } return true; } @@ -48,7 +50,7 @@ size_t FixtureLog::Observer::count(const Message& message, bool substring) const size_t message_count = 0; for (const auto& msg : messages) { - if (msg.matches(message, substring)) { + if (!msg.checked && msg.matches(message, substring)) { message_count++; msg.checked = true; } diff --git a/test/src/mbgl/test/sqlite3_test_fs.cpp b/test/src/mbgl/test/sqlite3_test_fs.cpp new file mode 100644 index 0000000000..16d411faff --- /dev/null +++ b/test/src/mbgl/test/sqlite3_test_fs.cpp @@ -0,0 +1,320 @@ +#ifndef __QT__ // Qt doesn't expose SQLite VFS + +#include <mbgl/test/sqlite3_test_fs.hpp> + +#include <sqlite3.h> + +#include <stdexcept> +#include <cstdio> +#include <cstring> +#include <cstdlib> +#include <cassert> + +static bool sqlite3_test_fs_debug = false; +static bool sqlite3_test_fs_io = true; +static bool sqlite3_test_fs_file_open = true; +static bool sqlite3_test_fs_file_create = true; +static int64_t sqlite3_test_fs_read_limit = -1; +static int64_t sqlite3_test_fs_write_limit = -1; + +struct File { + sqlite3_file base; + sqlite3_file* real; +}; + +static int sqlite3_test_fs_close(sqlite3_file* pFile) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: close(%p)\n", pFile); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + const int rc = file->real->pMethods->xClose(file->real); + if (rc == SQLITE_OK) { + sqlite3_free((void*)file->base.pMethods); + file->base.pMethods = 0; + } + return rc; +} + +static int sqlite3_test_fs_read(sqlite3_file* pFile, void* zBuf, int iAmt, sqlite3_int64 iOfst) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: read(%p, amount=%d, offset=%lld)\n", pFile, iAmt, iOfst); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + if (sqlite3_test_fs_read_limit >= 0) { + if (iAmt > sqlite3_test_fs_read_limit) { + iAmt = 0; + return SQLITE_IOERR; + } + sqlite3_test_fs_read_limit -= iAmt; + } + File* file = (File*)pFile; + return file->real->pMethods->xRead(file->real, zBuf, iAmt, iOfst); +} + +static int sqlite3_test_fs_write(sqlite3_file* pFile, const void* zBuf, int iAmt, sqlite3_int64 iOfst) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: write(%p, amount=%d, offset=%lld)\n", pFile, iAmt, iOfst); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + if (sqlite3_test_fs_write_limit >= 0) { + if (iAmt > sqlite3_test_fs_write_limit) { + iAmt = 0; + return SQLITE_FULL; + } + sqlite3_test_fs_write_limit -= iAmt; + } + File* file = (File*)pFile; + return file->real->pMethods->xWrite(file->real, zBuf, iAmt, iOfst); +} + +static int sqlite3_test_fs_truncate(sqlite3_file* pFile, sqlite3_int64 size) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: truncate(%p, size=%lld)\n", pFile, size); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xTruncate(file->real, size); +} + +static int sqlite3_test_fs_sync(sqlite3_file* pFile, int flags) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: sync(%p, flags=%d)\n", pFile, flags); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xSync(file->real, flags); +} + +static int sqlite3_test_fs_file_size(sqlite3_file* pFile, sqlite3_int64* pSize) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: file_size(%p)\n", pFile); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xFileSize(file->real, pSize); +} + +static int sqlite3_test_fs_lock(sqlite3_file* pFile, int eLock) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: lock(%p, %d)\n", pFile, eLock); + } + File* file = (File*)pFile; + return file->real->pMethods->xLock(file->real, eLock); +} + +static int sqlite3_test_fs_unlock(sqlite3_file* pFile, int eLock) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: unlock(%p, %d)\n", pFile, eLock); + } + File* file = (File*)pFile; + return file->real->pMethods->xUnlock(file->real, eLock); +} + +static int sqlite3_test_fs_check_reserved_lock(sqlite3_file* pFile, int* pResOut) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: check_reserved_lock(%p)\n", pFile); + } + File* file = (File*)pFile; + return file->real->pMethods->xCheckReservedLock(file->real, pResOut); +} + +static int sqlite3_test_fs_file_control(sqlite3_file* pFile, int op, void* pArg) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: file_control(%p, op=%d)\n", pFile, op); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xFileControl(file->real, op, pArg); +} + +static int sqlite3_test_fs_sector_size(sqlite3_file* pFile) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: sector_size(%p)\n", pFile); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xSectorSize(file->real); +} + +static int sqlite3_test_fs_device_characteristics(sqlite3_file* pFile) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: device_characteristics(%p)\n", pFile); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + File* file = (File*)pFile; + return file->real->pMethods->xDeviceCharacteristics(file->real); +} + +static int sqlite3_test_fs_open(sqlite3_vfs* vfs, const char* zName, sqlite3_file* pFile, int flags, int* pOutFlags) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: open(name=%s, flags=%d) -> %p\n", zName, flags, pFile); + } + if (!sqlite3_test_fs_io) { + pFile->pMethods = NULL; + return SQLITE_AUTH; + } + if (!sqlite3_test_fs_file_open) { + pFile->pMethods = NULL; + return SQLITE_CANTOPEN; + } + + File* file = (File*)pFile; + sqlite3_vfs* unix_fs = (sqlite3_vfs*)vfs->pAppData; + file->real = (sqlite3_file*)&file[1]; + + if (!sqlite3_test_fs_file_create) { + int res; + const int result = unix_fs->xAccess(vfs, zName, SQLITE_ACCESS_EXISTS, &res); + if (result != SQLITE_OK) { + pFile->pMethods = NULL; + return result; + } + if (res != 1) { + pFile->pMethods = NULL; + return SQLITE_CANTOPEN; + } + } + + const int status = unix_fs->xOpen(unix_fs, zName, file->real, flags, pOutFlags); + if (file->real->pMethods) { + sqlite3_io_methods* methods = (sqlite3_io_methods*)sqlite3_malloc(sizeof(sqlite3_io_methods)); + memset(methods, 0, sizeof(sqlite3_io_methods)); + methods->iVersion = 1; + methods->xClose = sqlite3_test_fs_close; + methods->xRead = sqlite3_test_fs_read; + methods->xWrite = sqlite3_test_fs_write; + methods->xTruncate = sqlite3_test_fs_truncate; + methods->xSync = sqlite3_test_fs_sync; + methods->xFileSize = sqlite3_test_fs_file_size; + methods->xLock = sqlite3_test_fs_lock; + methods->xUnlock = sqlite3_test_fs_unlock; + methods->xCheckReservedLock = sqlite3_test_fs_check_reserved_lock; + methods->xFileControl = sqlite3_test_fs_file_control; + methods->xSectorSize = sqlite3_test_fs_sector_size; + methods->xDeviceCharacteristics = sqlite3_test_fs_device_characteristics; + pFile->pMethods = methods; + } + return status; +} + +static int sqlite3_test_fs_delete(sqlite3_vfs* vfs, const char *zPath, int dirSync) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: delete(name=%s, sync=%d)\n", zPath, dirSync); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + sqlite3_vfs* unix_fs = (sqlite3_vfs*)vfs->pAppData; + return unix_fs->xDelete(unix_fs, zPath, dirSync); +} + +static int sqlite3_test_fs_access(sqlite3_vfs* vfs, const char *zPath, int flags, int *pResOut) { + if (sqlite3_test_fs_debug) { + fprintf(stderr, "SQLite3: access(name=%s, flags=%d)\n", zPath, flags); + } + if (!sqlite3_test_fs_io) { + return SQLITE_AUTH; + } + sqlite3_vfs* unix_fs = (sqlite3_vfs*)vfs->pAppData; + return unix_fs->xAccess(unix_fs, zPath, flags, pResOut); +} + +namespace mbgl { +namespace test { + +SQLite3TestFS::SQLite3TestFS() { + sqlite3_vfs* unix_fs = sqlite3_vfs_find("unix"); + if (unix_fs == 0) { + abort(); + } + + sqlite3_vfs* test_fs = (sqlite3_vfs*)sqlite3_malloc(sizeof(sqlite3_vfs)); + if (test_fs == 0) { + abort(); + } + memset(test_fs, 0, sizeof(sqlite3_vfs)); + test_fs->iVersion = 1; + test_fs->szOsFile = unix_fs->szOsFile + sizeof(File); + test_fs->mxPathname = unix_fs->mxPathname; + test_fs->zName = "test_fs"; + test_fs->pAppData = unix_fs; + test_fs->xOpen = sqlite3_test_fs_open; + test_fs->xDelete = sqlite3_test_fs_delete; + test_fs->xAccess = sqlite3_test_fs_access; + test_fs->xFullPathname = unix_fs->xFullPathname; + test_fs->xDlOpen = unix_fs->xDlOpen; + test_fs->xDlError = unix_fs->xDlError; + test_fs->xDlSym = unix_fs->xDlSym; + test_fs->xDlClose = unix_fs->xDlClose; + test_fs->xRandomness = unix_fs->xRandomness; + test_fs->xSleep = unix_fs->xSleep; + test_fs->xCurrentTime = unix_fs->xCurrentTime; + test_fs->xGetLastError = unix_fs->xGetLastError; + + sqlite3_vfs_register(test_fs, 0); +} + +SQLite3TestFS::~SQLite3TestFS() { + reset(); + sqlite3_vfs* test_fs = sqlite3_vfs_find("test_fs"); + if (test_fs) { + sqlite3_vfs_unregister(test_fs); + } +} + +void SQLite3TestFS::setDebug(bool value) { + sqlite3_test_fs_debug = value; +} + +void SQLite3TestFS::allowIO(bool value) { + sqlite3_test_fs_io = value; +} + +void SQLite3TestFS::allowFileOpen(bool value) { + sqlite3_test_fs_file_open = value; +} + +void SQLite3TestFS::allowFileCreate(bool value) { + sqlite3_test_fs_file_create = value; +} + +void SQLite3TestFS::setReadLimit(int64_t value) { + sqlite3_test_fs_read_limit = value; +} + +void SQLite3TestFS::setWriteLimit(int64_t value) { + sqlite3_test_fs_write_limit = value; +} + +void SQLite3TestFS::reset() { + setDebug(false); + allowIO(true); + allowFileOpen(true); + allowFileCreate(true); + setReadLimit(-1); + setWriteLimit(-1); +} + +} // namespace test +} // namespace mbgl + +#endif // __QT__ diff --git a/test/src/mbgl/test/sqlite3_test_fs.hpp b/test/src/mbgl/test/sqlite3_test_fs.hpp new file mode 100644 index 0000000000..00351f49ac --- /dev/null +++ b/test/src/mbgl/test/sqlite3_test_fs.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include <cstdint> + +namespace mbgl { +namespace test { + +class SQLite3TestFS { +public: + SQLite3TestFS(); + ~SQLite3TestFS(); + + // When enabled, the VFS will log all I/O operations to stdout. + void setDebug(bool); + + // Allow any type of I/O. Will fail with SQLITE_AUTH if set to false. This is useful to simulate + // scenarios where the OS blocks an entire app's I/O, e.g. when it's in the background. + void allowIO(bool); + + // Allow files to be opened. Will fail with SQLITE_CANTOPEN if set to false. + void allowFileOpen(bool); + + // Allow files to be created. Will fail with SQLITE_CANTOPEN if set to false. + void allowFileCreate(bool); + + // Allow N bytes to be read, then fail reads with SQLITE_IOERR. -1 == unlimited + // This limit is global, not per file. + void setReadLimit(int64_t); + + // Allow N bytes to be written, then fail writes with SQLITE_FULL. -1 == unlimited + // This limit is global, not per file. + void setWriteLimit(int64_t); + + // Reset all restrictions. + void reset(); +}; + +} // namespace test +} // namespace mbgl diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index 000f24e1cd..de40d8caf7 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -1,5 +1,6 @@ #include <mbgl/test/util.hpp> #include <mbgl/test/fixture_log_observer.hpp> +#include <mbgl/test/sqlite3_test_fs.hpp> #include <mbgl/storage/offline_database.hpp> #include <mbgl/storage/resource.hpp> @@ -13,51 +14,235 @@ using namespace std::literals::string_literals; using namespace mbgl; +using mapbox::sqlite::ResultCode; static constexpr const char* filename = "test/fixtures/offline_database/offline.db"; +#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers. +static constexpr const char* filename_test_fs = "file:test/fixtures/offline_database/offline.db?vfs=test_fs"; +#endif -TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) { - FixtureLog log; +static void deleteDatabaseFiles() { + // Delete leftover journaling files as well. util::deleteFile(filename); + util::deleteFile(filename + "-wal"s); + util::deleteFile(filename + "-journal"s); +} + +static FixtureLog::Message error(ResultCode code, const char* message) { + return { EventSeverity::Error, Event::Database, static_cast<int64_t>(code), message }; +} + +static FixtureLog::Message warning(ResultCode code, const char* message) { + return { EventSeverity::Warning, Event::Database, static_cast<int64_t>(code), message }; +} + +static int databasePageCount(const std::string& path) { + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); + mapbox::sqlite::Statement stmt{ db, "pragma page_count" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get<int>(0); +} + +static int databaseUserVersion(const std::string& path) { + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); + mapbox::sqlite::Statement stmt{ db, "pragma user_version" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get<int>(0); +} + +static std::string databaseJournalMode(const std::string& path) { + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); + mapbox::sqlite::Statement stmt{ db, "pragma journal_mode" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get<std::string>(0); +} +static int databaseSyncMode(const std::string& path) { + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); + mapbox::sqlite::Statement stmt{ db, "pragma synchronous" }; + mapbox::sqlite::Query query{ stmt }; + query.run(); + return query.get<int>(0); +} + +static std::vector<std::string> databaseTableColumns(const std::string& path, const std::string& name) { + mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); + const auto sql = std::string("pragma table_info(") + name + ")"; + mapbox::sqlite::Statement stmt{ db, sql.c_str() }; + mapbox::sqlite::Query query{ stmt }; + std::vector<std::string> columns; + while (query.run()) { + columns.push_back(query.get<std::string>(1)); + } + return columns; +} + +namespace fixture { + +const Resource resource{ Resource::Style, "mapbox://test" }; +const Resource tile = Resource::tile("mapbox://test", 1, 0, 0, 0, Tileset::Scheme::XYZ); +const Response response = [] { + Response res; + res.data = std::make_shared<std::string>("first"); + return res; +}(); + +} // namespace + +TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) { + FixtureLog log; + deleteDatabaseFiles(); OfflineDatabase db(filename); EXPECT_FALSE(bool(db.get({ Resource::Unknown, "mapbox://test" }))); EXPECT_EQ(0u, log.uncheckedCount()); } +#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers. +TEST(OfflineDatabase, TEST_REQUIRES_WRITE(CreateFail)) { + FixtureLog log; + deleteDatabaseFiles(); + test::SQLite3TestFS fs; + + // Opening the database will fail because our mock VFS returns a SQLITE_CANTOPEN error because + // it is not allowed to create the file. The OfflineDatabase object should handle this gracefully + // and treat it like an empty cache that can't be written to. + fs.allowFileCreate(false); + OfflineDatabase db(filename_test_fs); + EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't open database: unable to open database file"))); + + EXPECT_EQ(0u, log.uncheckedCount()); + + // We can try to insert things into the cache, but since the cache database isn't open, it won't be stored. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(false, uint64_t(0)), db.put(res, fixture::response)); + EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't write resource: unable to open database file"))); + EXPECT_EQ(0u, log.uncheckedCount()); + } + + // We can also still "query" the database even though it is not open, and we will always get an empty result. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_FALSE(bool(db.get(res))); + EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't update timestamp: unable to open database file"))); + EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't read resource: unable to open database file"))); + EXPECT_EQ(0u, log.uncheckedCount()); + } + + // Now, we're "freeing up" some space on the disk, and try to insert and query again. This time, we should + // be opening the datbase, creating the schema, and writing the data so that we can retrieve it again. + fs.allowFileCreate(true); + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response)); + auto result = db.get(res); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } + + // Next, set the file system to read only mode and try to read the data again. While we can't + // write anymore, we should still be able to read, and the query that tries to update the last + // accessed timestamp may fail without crashing. + fs.allowFileCreate(false); + fs.setWriteLimit(0); + for (const auto& res : { fixture::resource, fixture::tile }) { + auto result = db.get(res); + EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't update timestamp: unable to open database file"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } + fs.setDebug(false); + + // We're allowing SQLite to create a journal file, but restrict the number of bytes it + // can write so that it can start writing the journal file, but eventually fails during the + // timestamp update. + fs.allowFileCreate(true); + fs.setWriteLimit(8192); + for (const auto& res : { fixture::resource, fixture::tile }) { + auto result = db.get(res); + EXPECT_EQ(1u, log.count(warning(ResultCode::Full, "Can't update timestamp: database or disk is full"))); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } + + // Lastly, we're disabling all I/O to simulate a backgrounded app that is restricted from doing + // any disk I/O at all. + fs.setWriteLimit(-1); + fs.allowIO(false); + for (const auto& res : { fixture::resource, fixture::tile }) { + // First, try reading. + auto result = db.get(res); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't update timestamp: authorization denied"))); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't read resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + EXPECT_FALSE(result); + + // Now try inserting. + EXPECT_EQ(std::make_pair(false, uint64_t(0)), db.put(res, fixture::response)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't write resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + } + + // Allow deleting the database. + fs.reset(); +} +#endif // __QT__ + TEST(OfflineDatabase, TEST_REQUIRES_WRITE(SchemaVersion)) { FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); { mapbox::sqlite::Database db = mapbox::sqlite::Database::open(filename, mapbox::sqlite::ReadWriteCreate); + db.setBusyTimeout(Milliseconds(1000)); db.exec("PRAGMA user_version = 1"); } + { + OfflineDatabase db(filename); + } + + EXPECT_EQ(6, databaseUserVersion(filename)); + OfflineDatabase db(filename); + // Now try inserting and reading back to make sure we have a valid database. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response)); + EXPECT_EQ(0u, log.uncheckedCount()); + auto result = db.get(res); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } EXPECT_EQ(0u, log.uncheckedCount()); } TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Invalid)) { FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::write_file(filename, "this is an invalid file"); OfflineDatabase db(filename); - -#ifndef __QT__ - // Only non-Qt platforms are setting a logger on the SQLite object. // Checking two possibilities for the error string because it apparently changes between SQLite versions. - EXPECT_EQ(1u, - log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::NotADB), - "statement aborts at 1: [PRAGMA user_version] file is encrypted or is not a database" }, true) + - log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::NotADB), - "statement aborts at 1: [PRAGMA user_version] file is not a database" }, true)); -#endif - EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" })); - EXPECT_EQ(0u, log.uncheckedCount()); + EXPECT_EQ(1u, log.count(error(ResultCode::NotADB, "Can't open database: file is encrypted or is not a database"), true) + + log.count(error(ResultCode::NotADB, "Can't open database: file is not a database"), true)); + EXPECT_EQ(1u, log.count(warning(static_cast<ResultCode>(-1), "Removing existing incompatible offline database"))); + + // Now try inserting and reading back to make sure we have a valid database. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response)); + EXPECT_EQ(0u, log.uncheckedCount()); + auto result = db.get(res); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } } TEST(OfflineDatabase, PutDoesNotStoreConnectionErrors) { @@ -118,7 +303,7 @@ TEST(OfflineDatabase, PutResource) { TEST(OfflineDatabase, TEST_REQUIRES_WRITE(GetResourceFromOfflineRegion)) { FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::copyFile(filename, "test/fixtures/offline_database/satellite_test.db"); OfflineDatabase db(filename, mapbox::sqlite::ReadOnly); @@ -228,14 +413,15 @@ TEST(OfflineDatabase, CreateRegion) { OfflineDatabase db(":memory:"); OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata {{ 1, 2, 3 }}; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); - EXPECT_EQ(definition.styleURL, region.getDefinition().styleURL); - EXPECT_EQ(definition.bounds, region.getDefinition().bounds); - EXPECT_EQ(definition.minZoom, region.getDefinition().minZoom); - EXPECT_EQ(definition.maxZoom, region.getDefinition().maxZoom); - EXPECT_EQ(definition.pixelRatio, region.getDefinition().pixelRatio); - EXPECT_EQ(metadata, region.getMetadata()); + EXPECT_EQ(definition.styleURL, region->getDefinition().styleURL); + EXPECT_EQ(definition.bounds, region->getDefinition().bounds); + EXPECT_EQ(definition.minZoom, region->getDefinition().minZoom); + EXPECT_EQ(definition.maxZoom, region->getDefinition().maxZoom); + EXPECT_EQ(definition.pixelRatio, region->getDefinition().pixelRatio); + EXPECT_EQ(metadata, region->getMetadata()); EXPECT_EQ(0u, log.uncheckedCount()); } @@ -245,10 +431,11 @@ TEST(OfflineDatabase, UpdateMetadata) { OfflineDatabase db(":memory:"); OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata {{ 1, 2, 3 }}; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); OfflineRegionMetadata newmetadata {{ 4, 5, 6 }}; - db.updateMetadata(region.getID(), newmetadata); + db.updateMetadata(region->getID(), newmetadata); EXPECT_EQ(db.listRegions().at(0).getMetadata(), newmetadata); EXPECT_EQ(0u, log.uncheckedCount()); @@ -260,11 +447,12 @@ TEST(OfflineDatabase, ListRegions) { OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata {{ 1, 2, 3 }}; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); std::vector<OfflineRegion> regions = db.listRegions(); ASSERT_EQ(1u, regions.size()); - EXPECT_EQ(region.getID(), regions.at(0).getID()); + EXPECT_EQ(region->getID(), regions.at(0).getID()); EXPECT_EQ(definition.styleURL, regions.at(0).getDefinition().styleURL); EXPECT_EQ(definition.bounds, regions.at(0).getDefinition().bounds); EXPECT_EQ(definition.minZoom, regions.at(0).getDefinition().minZoom); @@ -281,14 +469,16 @@ TEST(OfflineDatabase, GetRegionDefinition) { OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata {{ 1, 2, 3 }}; - OfflineRegion region = db.createRegion(definition, metadata); - OfflineRegionDefinition result = db.getRegionDefinition(region.getID()); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); + auto result = db.getRegionDefinition(region->getID()); + ASSERT_TRUE(result); - EXPECT_EQ(definition.styleURL, result.styleURL); - EXPECT_EQ(definition.bounds, result.bounds); - EXPECT_EQ(definition.minZoom, result.minZoom); - EXPECT_EQ(definition.maxZoom, result.maxZoom); - EXPECT_EQ(definition.pixelRatio, result.pixelRatio); + EXPECT_EQ(definition.styleURL, result->styleURL); + EXPECT_EQ(definition.bounds, result->bounds); + EXPECT_EQ(definition.minZoom, result->minZoom); + EXPECT_EQ(definition.maxZoom, result->maxZoom); + EXPECT_EQ(definition.pixelRatio, result->pixelRatio); EXPECT_EQ(0u, log.uncheckedCount()); } @@ -298,15 +488,16 @@ TEST(OfflineDatabase, DeleteRegion) { OfflineDatabase db(":memory:"); OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata {{ 1, 2, 3 }}; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); Response response; response.noContent = true; - db.putRegionResource(region.getID(), Resource::style("http://example.com/"), response); - db.putRegionResource(region.getID(), Resource::tile("http://example.com/", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), response); + db.putRegionResource(region->getID(), Resource::style("http://example.com/"), response); + db.putRegionResource(region->getID(), Resource::tile("http://example.com/", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), response); - db.deleteRegion(std::move(region)); + db.deleteRegion(std::move(*region)); ASSERT_EQ(0u, db.listRegions().size()); @@ -318,38 +509,35 @@ TEST(OfflineDatabase, CreateRegionInfiniteMaxZoom) { OfflineDatabase db(":memory:"); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; OfflineRegionMetadata metadata; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); - EXPECT_EQ(0, region.getDefinition().minZoom); - EXPECT_EQ(INFINITY, region.getDefinition().maxZoom); + EXPECT_EQ(0, region->getDefinition().minZoom); + EXPECT_EQ(INFINITY, region->getDefinition().maxZoom); EXPECT_EQ(0u, log.uncheckedCount()); } TEST(OfflineDatabase, TEST_REQUIRES_WRITE(ConcurrentUse)) { FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); OfflineDatabase db1(filename); EXPECT_EQ(0u, log.uncheckedCount()); OfflineDatabase db2(filename); - Resource resource { Resource::Style, "http://example.com/" }; - Response response; - response.noContent = true; - std::thread thread1([&] { for (auto i = 0; i < 100; i++) { - db1.put(resource, response); - EXPECT_TRUE(bool(db1.get(resource))); + db1.put(fixture::resource, fixture::response); + EXPECT_TRUE(bool(db1.get(fixture::resource))); } }); std::thread thread2([&] { for (auto i = 0; i < 100; i++) { - db2.put(resource, response); - EXPECT_TRUE(bool(db2.get(resource))); + db2.put(fixture::resource, fixture::response); + EXPECT_TRUE(bool(db2.get(fixture::resource))); } }); @@ -411,13 +599,14 @@ TEST(OfflineDatabase, PutRegionResourceDoesNotEvict) { FixtureLog log; OfflineDatabase db(":memory:", 1024 * 100); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; - OfflineRegion region = db.createRegion(definition, OfflineRegionMetadata()); + auto region = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(region); Response response; response.data = randomString(1024); for (uint32_t i = 1; i <= 100; i++) { - db.putRegionResource(region.getID(), Resource::style("http://example.com/"s + util::toString(i)), response); + db.putRegionResource(region->getID(), Resource::style("http://example.com/"s + util::toString(i)), response); } EXPECT_TRUE(bool(db.get(Resource::style("http://example.com/1")))); @@ -448,32 +637,36 @@ TEST(OfflineDatabase, GetRegionCompletedStatus) { OfflineDatabase db(":memory:"); OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata; - OfflineRegion region = db.createRegion(definition, metadata); + auto region = db.createRegion(definition, metadata); + ASSERT_TRUE(region); - OfflineRegionStatus status1 = db.getRegionCompletedStatus(region.getID()); - EXPECT_EQ(0u, status1.completedResourceCount); - EXPECT_EQ(0u, status1.completedResourceSize); - EXPECT_EQ(0u, status1.completedTileCount); - EXPECT_EQ(0u, status1.completedTileSize); + auto status1 = db.getRegionCompletedStatus(region->getID()); + ASSERT_TRUE(status1); + EXPECT_EQ(0u, status1->completedResourceCount); + EXPECT_EQ(0u, status1->completedResourceSize); + EXPECT_EQ(0u, status1->completedTileCount); + EXPECT_EQ(0u, status1->completedTileSize); Response response; response.data = std::make_shared<std::string>("data"); - uint64_t styleSize = db.putRegionResource(region.getID(), Resource::style("http://example.com/"), response); + uint64_t styleSize = db.putRegionResource(region->getID(), Resource::style("http://example.com/"), response); - OfflineRegionStatus status2 = db.getRegionCompletedStatus(region.getID()); - EXPECT_EQ(1u, status2.completedResourceCount); - EXPECT_EQ(styleSize, status2.completedResourceSize); - EXPECT_EQ(0u, status2.completedTileCount); - EXPECT_EQ(0u, status2.completedTileSize); + auto status2 = db.getRegionCompletedStatus(region->getID()); + ASSERT_TRUE(status2); + EXPECT_EQ(1u, status2->completedResourceCount); + EXPECT_EQ(styleSize, status2->completedResourceSize); + EXPECT_EQ(0u, status2->completedTileCount); + EXPECT_EQ(0u, status2->completedTileSize); - uint64_t tileSize = db.putRegionResource(region.getID(), Resource::tile("http://example.com/", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), response); + uint64_t tileSize = db.putRegionResource(region->getID(), Resource::tile("http://example.com/", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), response); - OfflineRegionStatus status3 = db.getRegionCompletedStatus(region.getID()); - EXPECT_EQ(2u, status3.completedResourceCount); - EXPECT_EQ(styleSize + tileSize, status3.completedResourceSize); - EXPECT_EQ(1u, status3.completedTileCount); - EXPECT_EQ(tileSize, status3.completedTileSize); + auto status3 = db.getRegionCompletedStatus(region->getID()); + ASSERT_TRUE(status3); + EXPECT_EQ(2u, status3->completedResourceCount); + EXPECT_EQ(styleSize + tileSize, status3->completedResourceSize); + EXPECT_EQ(1u, status3->completedTileCount); + EXPECT_EQ(tileSize, status3->completedTileSize); EXPECT_EQ(0u, log.uncheckedCount()); } @@ -482,21 +675,22 @@ TEST(OfflineDatabase, HasRegionResource) { FixtureLog log; OfflineDatabase db(":memory:", 1024 * 100); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; - OfflineRegion region = db.createRegion(definition, OfflineRegionMetadata()); + auto region = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(region); - EXPECT_FALSE(bool(db.hasRegionResource(region.getID(), Resource::style("http://example.com/1")))); - EXPECT_FALSE(bool(db.hasRegionResource(region.getID(), Resource::style("http://example.com/20")))); + EXPECT_FALSE(bool(db.hasRegionResource(region->getID(), Resource::style("http://example.com/1")))); + EXPECT_FALSE(bool(db.hasRegionResource(region->getID(), Resource::style("http://example.com/20")))); Response response; response.data = randomString(1024); for (uint32_t i = 1; i <= 100; i++) { - db.putRegionResource(region.getID(), Resource::style("http://example.com/"s + util::toString(i)), response); + db.putRegionResource(region->getID(), Resource::style("http://example.com/"s + util::toString(i)), response); } - EXPECT_TRUE(bool(db.hasRegionResource(region.getID(), Resource::style("http://example.com/1")))); - EXPECT_TRUE(bool(db.hasRegionResource(region.getID(), Resource::style("http://example.com/20")))); - EXPECT_EQ(1024, *(db.hasRegionResource(region.getID(), Resource::style("http://example.com/20")))); + EXPECT_TRUE(bool(db.hasRegionResource(region->getID(), Resource::style("http://example.com/1")))); + EXPECT_TRUE(bool(db.hasRegionResource(region->getID(), Resource::style("http://example.com/20")))); + EXPECT_EQ(1024, *(db.hasRegionResource(region->getID(), Resource::style("http://example.com/20")))); EXPECT_EQ(0u, log.uncheckedCount()); } @@ -505,7 +699,8 @@ TEST(OfflineDatabase, HasRegionResourceTile) { FixtureLog log; OfflineDatabase db(":memory:", 1024 * 100); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; - OfflineRegion region = db.createRegion(definition, OfflineRegionMetadata()); + auto region = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(region); Resource resource { Resource::Tile, "http://example.com/" }; resource.tileData = Resource::TileData { @@ -519,15 +714,16 @@ TEST(OfflineDatabase, HasRegionResourceTile) { response.data = std::make_shared<std::string>("first"); - EXPECT_FALSE(bool(db.hasRegionResource(region.getID(), resource))); - db.putRegionResource(region.getID(), resource, response); - EXPECT_TRUE(bool(db.hasRegionResource(region.getID(), resource))); - EXPECT_EQ(5, *(db.hasRegionResource(region.getID(), resource))); + EXPECT_FALSE(bool(db.hasRegionResource(region->getID(), resource))); + db.putRegionResource(region->getID(), resource, response); + EXPECT_TRUE(bool(db.hasRegionResource(region->getID(), resource))); + EXPECT_EQ(5, *(db.hasRegionResource(region->getID(), resource))); - OfflineRegion anotherRegion = db.createRegion(definition, OfflineRegionMetadata()); - EXPECT_LT(region.getID(), anotherRegion.getID()); - EXPECT_TRUE(bool(db.hasRegionResource(anotherRegion.getID(), resource))); - EXPECT_EQ(5, *(db.hasRegionResource(anotherRegion.getID(), resource))); + auto anotherRegion = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(anotherRegion); + EXPECT_LT(region->getID(), anotherRegion->getID()); + EXPECT_TRUE(bool(db.hasRegionResource(anotherRegion->getID(), resource))); + EXPECT_EQ(5, *(db.hasRegionResource(anotherRegion->getID(), resource))); EXPECT_EQ(0u, log.uncheckedCount()); @@ -539,8 +735,10 @@ TEST(OfflineDatabase, OfflineMapboxTileCount) { OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 }; OfflineRegionMetadata metadata; - OfflineRegion region1 = db.createRegion(definition, metadata); - OfflineRegion region2 = db.createRegion(definition, metadata); + auto region1 = db.createRegion(definition, metadata); + ASSERT_TRUE(region1); + auto region2 = db.createRegion(definition, metadata); + ASSERT_TRUE(region2); Resource nonMapboxTile = Resource::tile("http://example.com/", 1.0, 0, 0, 0, Tileset::Scheme::XYZ); Resource mapboxTile1 = Resource::tile("mapbox://tiles/1", 1.0, 0, 0, 0, Tileset::Scheme::XYZ); @@ -553,27 +751,27 @@ TEST(OfflineDatabase, OfflineMapboxTileCount) { EXPECT_EQ(0u, db.getOfflineMapboxTileCount()); // Count stays the same after putting a non-tile resource. - db.putRegionResource(region1.getID(), Resource::style("http://example.com/"), response); + db.putRegionResource(region1->getID(), Resource::style("http://example.com/"), response); EXPECT_EQ(0u, db.getOfflineMapboxTileCount()); // Count stays the same after putting a non-Mapbox tile. - db.putRegionResource(region1.getID(), nonMapboxTile, response); + db.putRegionResource(region1->getID(), nonMapboxTile, response); EXPECT_EQ(0u, db.getOfflineMapboxTileCount()); // Count increases after putting a Mapbox tile not used by another region. - db.putRegionResource(region1.getID(), mapboxTile1, response); + db.putRegionResource(region1->getID(), mapboxTile1, response); EXPECT_EQ(1u, db.getOfflineMapboxTileCount()); // Count stays the same after putting a Mapbox tile used by another region. - db.putRegionResource(region2.getID(), mapboxTile1, response); + db.putRegionResource(region2->getID(), mapboxTile1, response); EXPECT_EQ(1u, db.getOfflineMapboxTileCount()); // Count stays the same after putting a Mapbox tile used by the same region. - db.putRegionResource(region2.getID(), mapboxTile1, response); + db.putRegionResource(region2->getID(), mapboxTile1, response); EXPECT_EQ(1u, db.getOfflineMapboxTileCount()); // Count stays the same after deleting a region when the tile is still used by another region. - db.deleteRegion(std::move(region2)); + db.deleteRegion(std::move(*region2)); EXPECT_EQ(1u, db.getOfflineMapboxTileCount()); // Count stays the same after the putting a non-offline Mapbox tile. @@ -581,11 +779,11 @@ TEST(OfflineDatabase, OfflineMapboxTileCount) { EXPECT_EQ(1u, db.getOfflineMapboxTileCount()); // Count increases after putting a pre-existing, but non-offline Mapbox tile. - db.putRegionResource(region1.getID(), mapboxTile2, response); + db.putRegionResource(region1->getID(), mapboxTile2, response); EXPECT_EQ(2u, db.getOfflineMapboxTileCount()); // Count decreases after deleting a region when the tiles are not used by other regions. - db.deleteRegion(std::move(region1)); + db.deleteRegion(std::move(*region1)); EXPECT_EQ(0u, db.getOfflineMapboxTileCount()); EXPECT_EQ(0u, log.uncheckedCount()); @@ -596,7 +794,8 @@ TEST(OfflineDatabase, BatchInsertion) { FixtureLog log; OfflineDatabase db(":memory:", 1024 * 100); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; - OfflineRegion region = db.createRegion(definition, OfflineRegionMetadata()); + auto region = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(region); Response response; response.data = randomString(1024); @@ -607,7 +806,7 @@ TEST(OfflineDatabase, BatchInsertion) { } OfflineRegionStatus status; - db.putRegionResources(region.getID(), resources, status); + db.putRegionResources(region->getID(), resources, status); for (uint32_t i = 1; i <= 100; i++) { EXPECT_TRUE(bool(db.get(Resource::style("http://example.com/"s + util::toString(i))))); @@ -621,7 +820,8 @@ TEST(OfflineDatabase, BatchInsertionMapboxTileCountExceeded) { OfflineDatabase db(":memory:", 1024 * 100); db.setOfflineMapboxTileCountLimit(1); OfflineRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0 }; - OfflineRegion region = db.createRegion(definition, OfflineRegionMetadata()); + auto region = db.createRegion(definition, OfflineRegionMetadata()); + ASSERT_TRUE(region); Response response; response.data = randomString(1024); @@ -633,68 +833,26 @@ TEST(OfflineDatabase, BatchInsertionMapboxTileCountExceeded) { OfflineRegionStatus status; try { - db.putRegionResources(region.getID(), resources, status); + db.putRegionResources(region->getID(), resources, status); EXPECT_FALSE(true); } catch (const MapboxTileLimitExceededException&) { // Expected } - EXPECT_EQ(status.completedTileCount, 1u); - EXPECT_EQ(status.completedResourceCount, 2u); - EXPECT_EQ(db.getRegionCompletedStatus(region.getID()).completedTileCount, 1u); - EXPECT_EQ(db.getRegionCompletedStatus(region.getID()).completedResourceCount, 2u); + EXPECT_EQ(0u, status.completedTileCount); + EXPECT_EQ(0u, status.completedResourceCount); + const auto completedStatus = db.getRegionCompletedStatus(region->getID()); + ASSERT_TRUE(completedStatus); + EXPECT_EQ(1u, completedStatus->completedTileCount); + EXPECT_EQ(2u, completedStatus->completedResourceCount); EXPECT_EQ(0u, log.uncheckedCount()); } -static int databasePageCount(const std::string& path) { - mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt{ db, "pragma page_count" }; - mapbox::sqlite::Query query{ stmt }; - query.run(); - return query.get<int>(0); -} - -static int databaseUserVersion(const std::string& path) { - mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt{ db, "pragma user_version" }; - mapbox::sqlite::Query query{ stmt }; - query.run(); - return query.get<int>(0); -} - -static std::string databaseJournalMode(const std::string& path) { - mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt{ db, "pragma journal_mode" }; - mapbox::sqlite::Query query{ stmt }; - query.run(); - return query.get<std::string>(0); -} - -static int databaseSyncMode(const std::string& path) { - mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); - mapbox::sqlite::Statement stmt{ db, "pragma synchronous" }; - mapbox::sqlite::Query query{ stmt }; - query.run(); - return query.get<int>(0); -} - -static std::vector<std::string> databaseTableColumns(const std::string& path, const std::string& name) { - mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly); - const auto sql = std::string("pragma table_info(") + name + ")"; - mapbox::sqlite::Statement stmt{ db, sql.c_str() }; - mapbox::sqlite::Query query{ stmt }; - std::vector<std::string> columns; - while (query.run()) { - columns.push_back(query.get<std::string>(1)); - } - return columns; -} - TEST(OfflineDatabase, MigrateFromV2Schema) { // v2.db is a v2 database containing a single offline region with a small number of resources. FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::copyFile(filename, "test/fixtures/offline_database/v2.db"); { @@ -715,7 +873,7 @@ TEST(OfflineDatabase, MigrateFromV2Schema) { TEST(OfflineDatabase, MigrateFromV3Schema) { // v3.db is a v3 database, migrated from v2. FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::copyFile(filename, "test/fixtures/offline_database/v3.db"); { @@ -734,7 +892,7 @@ TEST(OfflineDatabase, MigrateFromV3Schema) { TEST(OfflineDatabase, MigrateFromV4Schema) { // v4.db is a v4 database, migrated from v2 & v3. This database used `journal_mode = WAL` and `synchronous = NORMAL`. FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::copyFile(filename, "test/fixtures/offline_database/v4.db"); { @@ -760,7 +918,7 @@ TEST(OfflineDatabase, MigrateFromV4Schema) { TEST(OfflineDatabase, MigrateFromV5Schema) { // v5.db is a v5 database, migrated from v2, v3 & v4. FixtureLog log; - util::deleteFile(filename); + deleteDatabaseFiles(); util::copyFile(filename, "test/fixtures/offline_database/v5.db"); { @@ -808,3 +966,133 @@ TEST(OfflineDatabase, DowngradeSchema) { EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" })); EXPECT_EQ(0u, log.uncheckedCount()); } + +TEST(OfflineDatabase, CorruptDatabaseOnOpen) { + FixtureLog log; + util::deleteFile(filename); + util::copyFile(filename, "test/fixtures/offline_database/corrupt-immediate.db"); + + // This database is corrupt in a way that will prevent opening the database. + OfflineDatabase db(filename); + EXPECT_EQ(1u, log.count(error(ResultCode::Corrupt, "Can't open database: database disk image is malformed"), true)); + EXPECT_EQ(1u, log.count(warning(static_cast<ResultCode>(-1), "Removing existing incompatible offline database"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + // Now try inserting and reading back to make sure we have a valid database. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response)); + EXPECT_EQ(0u, log.uncheckedCount()); + auto result = db.get(res); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } +} + +TEST(OfflineDatabase, CorruptDatabaseOnQuery) { + FixtureLog log; + util::deleteFile(filename); + util::copyFile(filename, "test/fixtures/offline_database/corrupt-delayed.db"); + + // This database is corrupt in a way that won't manifest itself until we start querying it, + // so just opening it will not cause an error. + OfflineDatabase db(filename); + + // Just opening this corrupt database should not have produced an error yet, since + // PRAGMA user_version still succeeds with this database. + EXPECT_EQ(0u, log.uncheckedCount()); + + // The first request fails because the database is corrupt and has to be recreated. + EXPECT_EQ(nullopt, db.get(fixture::tile)); + EXPECT_EQ(1u, log.count(error(ResultCode::Corrupt, "Can't read resource: database disk image is malformed"), true)); + EXPECT_EQ(1u, log.count(warning(static_cast<ResultCode>(-1), "Removing existing incompatible offline database"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + // Now try inserting and reading back to make sure we have a valid database. + for (const auto& res : { fixture::resource, fixture::tile }) { + EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response)); + EXPECT_EQ(0u, log.uncheckedCount()); + auto result = db.get(res); + EXPECT_EQ(0u, log.uncheckedCount()); + ASSERT_TRUE(result && result->data); + EXPECT_EQ("first", *result->data); + } +} + +#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers. +TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DisallowedIO)) { + FixtureLog log; + deleteDatabaseFiles(); + test::SQLite3TestFS fs; + + OfflineDatabase db(filename_test_fs); + EXPECT_EQ(0u, log.uncheckedCount()); + + // First, create a region object so that we can try deleting it later. + OfflineTilePyramidRegionDefinition definition( + "mapbox://style", LatLngBounds::hull({ 37.66, -122.57 }, { 37.83, -122.32 }), 0, 8, 2); + auto region = db.createRegion(definition, {}); + ASSERT_TRUE(region); + + // Now forbid any type of IO on the database and test that none of the calls crashes. + fs.allowIO(false); + + EXPECT_EQ(nullopt, db.get(fixture::resource)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't update timestamp: authorization denied"))); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't read resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(std::make_pair(false, uint64_t(0)), db.put(fixture::resource, fixture::response)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't write resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + const auto regions = db.listRegions(); + EXPECT_TRUE(regions.empty()); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't list regions: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.createRegion(definition, {})); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't create region: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.updateMetadata(region->getID(), {})); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't update region metadata: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.getRegionResource(region->getID(), fixture::resource)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't update timestamp: authorization denied"))); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't read region resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.hasRegionResource(region->getID(), fixture::resource)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't query region resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(0u, db.putRegionResource(region->getID(), fixture::resource, fixture::response)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't write region resource: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + OfflineRegionStatus status; + db.putRegionResources(region->getID(), { std::make_tuple(fixture::resource, fixture::response) }, status); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't write region resources: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.getRegionDefinition(region->getID())); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't load region: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(nullopt, db.getRegionCompletedStatus(region->getID())); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't get region status: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + db.deleteRegion(std::move(*region)); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't delete region: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + EXPECT_EQ(std::numeric_limits<uint64_t>::max(), db.getOfflineMapboxTileCount()); + EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't get offline Mapbox tile count: authorization denied"))); + EXPECT_EQ(0u, log.uncheckedCount()); + + fs.reset(); +} +#endif // __QT__ diff --git a/test/storage/offline_download.test.cpp b/test/storage/offline_download.test.cpp index 57780eba40..e87ad6c370 100644 --- a/test/storage/offline_download.test.cpp +++ b/test/storage/offline_download.test.cpp @@ -42,7 +42,7 @@ public: OfflineDatabase db { ":memory:" }; std::size_t size = 0; - OfflineRegion createRegion() { + optional<OfflineRegion> createRegion() { OfflineRegionDefinition definition { "", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 1.0 }; OfflineRegionMetadata metadata; return db.createRegion(definition, metadata); @@ -60,9 +60,10 @@ public: TEST(OfflineDownload, NoSubresources) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -100,9 +101,10 @@ TEST(OfflineDownload, NoSubresources) { TEST(OfflineDownload, InlineSource) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -140,9 +142,10 @@ TEST(OfflineDownload, InlineSource) { TEST(OfflineDownload, GeoJSONSource) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -175,9 +178,10 @@ TEST(OfflineDownload, GeoJSONSource) { TEST(OfflineDownload, Activate) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -250,9 +254,10 @@ TEST(OfflineDownload, Activate) { TEST(OfflineDownload, DoesNotFloodTheFileSourceWithRequests) { FakeFileSource fileSource; OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, fileSource); @@ -272,9 +277,10 @@ TEST(OfflineDownload, DoesNotFloodTheFileSourceWithRequests) { TEST(OfflineDownload, GetStatusNoResources) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); OfflineRegionStatus status = download.getStatus(); @@ -289,9 +295,10 @@ TEST(OfflineDownload, GetStatusNoResources) { TEST(OfflineDownload, GetStatusStyleComplete) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -311,9 +318,10 @@ TEST(OfflineDownload, GetStatusStyleComplete) { TEST(OfflineDownload, GetStatusStyleAndSourceComplete) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -337,9 +345,10 @@ TEST(OfflineDownload, GetStatusStyleAndSourceComplete) { TEST(OfflineDownload, RequestError) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -365,9 +374,10 @@ TEST(OfflineDownload, RequestError) { TEST(OfflineDownload, RequestErrorsAreRetried) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -398,9 +408,10 @@ TEST(OfflineDownload, RequestErrorsAreRetried) { TEST(OfflineDownload, TileCountLimitExceededNoTileResponse) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -440,9 +451,10 @@ TEST(OfflineDownload, TileCountLimitExceededNoTileResponse) { TEST(OfflineDownload, TileCountLimitExceededWithTileResponse) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -494,9 +506,10 @@ TEST(OfflineDownload, TileCountLimitExceededWithTileResponse) { TEST(OfflineDownload, WithPreviouslyExistingTile) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -528,9 +541,10 @@ TEST(OfflineDownload, WithPreviouslyExistingTile) { TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -556,7 +570,7 @@ TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) { test.loop.run(); OfflineDownload redownload( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); @@ -595,9 +609,10 @@ TEST(OfflineDownload, ReactivatePreviouslyCompletedDownload) { TEST(OfflineDownload, Deactivate) { OfflineTest test; - OfflineRegion region = test.createRegion(); + auto region = test.createRegion(); + ASSERT_TRUE(region); OfflineDownload download( - region.getID(), + region->getID(), OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0), test.db, test.fileSource); diff --git a/test/storage/sqlite.test.cpp b/test/storage/sqlite.test.cpp index 22958c8bed..cdbb7f26d7 100644 --- a/test/storage/sqlite.test.cpp +++ b/test/storage/sqlite.test.cpp @@ -38,12 +38,6 @@ TEST(SQLite, TEST_REQUIRES_WRITE(TryOpen)) { auto result = mapbox::sqlite::Database::tryOpen("test/fixtures/offline_database/foobar123.db", mapbox::sqlite::ReadOnly); ASSERT_TRUE(result.is<mapbox::sqlite::Exception>()); ASSERT_EQ(result.get<mapbox::sqlite::Exception>().code, mapbox::sqlite::ResultCode::CantOpen); - -#ifndef __QT__ - // Only non-Qt platforms are setting a logger on the SQLite object. - EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "cannot open file" }, true)); - EXPECT_EQ(1u, log.count({ EventSeverity::Info, Event::Database, static_cast<int64_t>(mapbox::sqlite::ResultCode::CantOpen), "No such file or directory" }, true)); -#endif EXPECT_EQ(0u, log.uncheckedCount()); } |