Path: blob/main/contrib/kyua/store/read_backend_test.cpp
39478 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "store/read_backend.hpp"2930#include <atf-c++.hpp>3132#include "store/exceptions.hpp"33#include "store/metadata.hpp"34#include "store/write_backend.hpp"35#include "utils/fs/operations.hpp"36#include "utils/fs/path.hpp"37#include "utils/logging/operations.hpp"38#include "utils/sqlite/database.hpp"39#include "utils/sqlite/exceptions.hpp"4041namespace fs = utils::fs;42namespace logging = utils::logging;43namespace sqlite = utils::sqlite;444546ATF_TEST_CASE_WITHOUT_HEAD(detail__open_and_setup__ok);47ATF_TEST_CASE_BODY(detail__open_and_setup__ok)48{49{50sqlite::database db = sqlite::database::open(51fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);52db.exec("CREATE TABLE one (foo INTEGER PRIMARY KEY AUTOINCREMENT);");53db.exec("CREATE TABLE two (foo INTEGER REFERENCES one);");54db.close();55}5657sqlite::database db = store::detail::open_and_setup(58fs::path("test.db"), sqlite::open_readwrite);59db.exec("INSERT INTO one (foo) VALUES (12);");60// Ensure foreign keys have been enabled.61db.exec("INSERT INTO two (foo) VALUES (12);");62ATF_REQUIRE_THROW(sqlite::error,63db.exec("INSERT INTO two (foo) VALUES (34);"));64}656667ATF_TEST_CASE_WITHOUT_HEAD(detail__open_and_setup__missing_file);68ATF_TEST_CASE_BODY(detail__open_and_setup__missing_file)69{70ATF_REQUIRE_THROW_RE(store::error, "Cannot open 'missing.db': ",71store::detail::open_and_setup(fs::path("missing.db"),72sqlite::open_readonly));73ATF_REQUIRE(!fs::exists(fs::path("missing.db")));74}757677ATF_TEST_CASE(read_backend__open_ro__ok);78ATF_TEST_CASE_HEAD(read_backend__open_ro__ok)79{80logging::set_inmemory();81set_md_var("require.files", store::detail::schema_file().c_str());82}83ATF_TEST_CASE_BODY(read_backend__open_ro__ok)84{85{86sqlite::database db = sqlite::database::open(87fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);88store::detail::initialize(db);89}90store::read_backend backend = store::read_backend::open_ro(91fs::path("test.db"));92backend.database().exec("SELECT * FROM metadata");93}949596ATF_TEST_CASE_WITHOUT_HEAD(read_backend__open_ro__missing_file);97ATF_TEST_CASE_BODY(read_backend__open_ro__missing_file)98{99ATF_REQUIRE_THROW_RE(store::error, "Cannot open 'missing.db': ",100store::read_backend::open_ro(fs::path("missing.db")));101ATF_REQUIRE(!fs::exists(fs::path("missing.db")));102}103104105ATF_TEST_CASE(read_backend__open_ro__integrity_error);106ATF_TEST_CASE_HEAD(read_backend__open_ro__integrity_error)107{108logging::set_inmemory();109set_md_var("require.files", store::detail::schema_file().c_str());110}111ATF_TEST_CASE_BODY(read_backend__open_ro__integrity_error)112{113{114sqlite::database db = sqlite::database::open(115fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);116store::detail::initialize(db);117db.exec("DELETE FROM metadata");118}119ATF_REQUIRE_THROW_RE(store::integrity_error, "metadata.*empty",120store::read_backend::open_ro(fs::path("test.db")));121}122123124ATF_TEST_CASE(read_backend__close);125ATF_TEST_CASE_HEAD(read_backend__close)126{127logging::set_inmemory();128set_md_var("require.files", store::detail::schema_file().c_str());129}130ATF_TEST_CASE_BODY(read_backend__close)131{132store::write_backend::open_rw(fs::path("test.db")); // Create database.133store::read_backend backend = store::read_backend::open_ro(134fs::path("test.db"));135backend.database().exec("SELECT * FROM metadata");136backend.close();137ATF_REQUIRE_THROW(utils::sqlite::error,138backend.database().exec("SELECT * FROM metadata"));139}140141142ATF_INIT_TEST_CASES(tcs)143{144ATF_ADD_TEST_CASE(tcs, detail__open_and_setup__ok);145ATF_ADD_TEST_CASE(tcs, detail__open_and_setup__missing_file);146147ATF_ADD_TEST_CASE(tcs, read_backend__open_ro__ok);148ATF_ADD_TEST_CASE(tcs, read_backend__open_ro__missing_file);149ATF_ADD_TEST_CASE(tcs, read_backend__open_ro__integrity_error);150ATF_ADD_TEST_CASE(tcs, read_backend__close);151}152153154