diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-04-08 14:24:03 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-04-08 14:55:47 +0200 |
commit | 4b66c490b41e8ea18428828707ed4327734f92dc (patch) | |
tree | 2dab5679b594fc1ff7c15a21e91b0f849c6d0b66 /test | |
parent | 7d58a41de5dbf1b24b8bad9a2a98c21a7bf75382 (diff) | |
download | qtlocation-mapboxgl-4b66c490b41e8ea18428828707ed4327734f92dc.tar.gz |
guard against invalid database files
Diffstat (limited to 'test')
-rw-r--r-- | test/storage/database.cpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/test/storage/database.cpp b/test/storage/database.cpp index 80cb956194..c60388be4a 100644 --- a/test/storage/database.cpp +++ b/test/storage/database.cpp @@ -6,8 +6,9 @@ #include <mbgl/storage/default/sqlite_cache.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> +#include <mbgl/util/io.hpp> -#include <fcntl.h> +#include <sys/file.h> class ScopedTest { public: @@ -66,6 +67,12 @@ void deleteFile(const char* name) { } } + +void writeFile(const char* name, const std::string& data) { + mbgl::util::write_file(name, data); +} + + TEST_F(Storage, DatabaseCreate) { using namespace mbgl; @@ -336,3 +343,40 @@ TEST_F(Storage, DatabaseDeleted) { // Explicitly delete the Cache now. cache.reset(); } + + + +TEST_F(Storage, DatabaseInvalid) { + using namespace mbgl; + + // Create a locked file. + createDir("test/fixtures/database"); + deleteFile("test/fixtures/database/invalid.db"); + writeFile("test/fixtures/database/invalid.db", "this is an invalid file"); + + auto cache = util::make_unique<SQLiteCache>("test/fixtures/database/invalid.db"); + + std::promise<void> promise; + + { + // Adds a file. + Log::setObserver(util::make_unique<FixtureLogObserver>()); + promise = {}; + auto response = std::make_shared<Response>(); + response->data = "Demo"; + cache->put({ Resource::Unknown, "mapbox://test" }, response, FileCache::Hint::Full); + cache->get({ Resource::Unknown, "mapbox://test" }, [&] (std::unique_ptr<Response> res) { + EXPECT_NE(nullptr, res.get()); + EXPECT_EQ("Demo", res->data); + promise.set_value(); + }); + promise.get_future().get(); + + auto observer = Log::removeObserver(); + auto flo = dynamic_cast<FixtureLogObserver*>(observer.get()); + EXPECT_EQ(1ul, flo->count({ EventSeverity::Warning, Event::Database, -1, "Trashing invalid database" })); + } + + // Explicitly delete the Cache now. + cache.reset(); +} |