Path: blob/main/contrib/kyua/store/metadata_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/metadata.hpp"2930#include <atf-c++.hpp>3132#include "store/exceptions.hpp"33#include "store/write_backend.hpp"34#include "utils/fs/path.hpp"35#include "utils/logging/operations.hpp"36#include "utils/sqlite/database.hpp"3738namespace logging = utils::logging;39namespace sqlite = utils::sqlite;404142namespace {434445/// Creates a test in-memory database.46///47/// When using this function, you must define a 'require.files' property in this48/// case pointing to store::detail::schema_file().49///50/// The database created by this function mimics a real complete database, but51/// without any predefined values. I.e. for our particular case, the metadata52/// table is empty.53///54/// \return A SQLite database instance.55static sqlite::database56create_database(void)57{58sqlite::database db = sqlite::database::in_memory();59store::detail::initialize(db);60db.exec("DELETE FROM metadata");61return db;62}636465} // anonymous namespace666768ATF_TEST_CASE(fetch_latest__ok);69ATF_TEST_CASE_HEAD(fetch_latest__ok)70{71logging::set_inmemory();72set_md_var("require.files", store::detail::schema_file().c_str());73}74ATF_TEST_CASE_BODY(fetch_latest__ok)75{76sqlite::database db = create_database();77db.exec("INSERT INTO metadata (schema_version, timestamp) "78"VALUES (512, 5678)");79db.exec("INSERT INTO metadata (schema_version, timestamp) "80"VALUES (256, 1234)");8182const store::metadata metadata = store::metadata::fetch_latest(db);83ATF_REQUIRE_EQ(5678L, metadata.timestamp());84ATF_REQUIRE_EQ(512, metadata.schema_version());85}868788ATF_TEST_CASE(fetch_latest__empty_metadata);89ATF_TEST_CASE_HEAD(fetch_latest__empty_metadata)90{91logging::set_inmemory();92set_md_var("require.files", store::detail::schema_file().c_str());93}94ATF_TEST_CASE_BODY(fetch_latest__empty_metadata)95{96sqlite::database db = create_database();97ATF_REQUIRE_THROW_RE(store::integrity_error, "metadata.*empty",98store::metadata::fetch_latest(db));99}100101102ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_timestamp);103ATF_TEST_CASE_BODY(fetch_latest__no_timestamp)104{105sqlite::database db = sqlite::database::in_memory();106db.exec("CREATE TABLE metadata (schema_version INTEGER)");107db.exec("INSERT INTO metadata VALUES (3)");108109ATF_REQUIRE_THROW_RE(store::integrity_error,110"Invalid metadata.*timestamp",111store::metadata::fetch_latest(db));112}113114115ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_schema_version);116ATF_TEST_CASE_BODY(fetch_latest__no_schema_version)117{118sqlite::database db = sqlite::database::in_memory();119db.exec("CREATE TABLE metadata (timestamp INTEGER)");120db.exec("INSERT INTO metadata VALUES (3)");121122ATF_REQUIRE_THROW_RE(store::integrity_error,123"Invalid metadata.*schema_version",124store::metadata::fetch_latest(db));125}126127128ATF_TEST_CASE(fetch_latest__invalid_timestamp);129ATF_TEST_CASE_HEAD(fetch_latest__invalid_timestamp)130{131logging::set_inmemory();132set_md_var("require.files", store::detail::schema_file().c_str());133}134ATF_TEST_CASE_BODY(fetch_latest__invalid_timestamp)135{136sqlite::database db = create_database();137db.exec("INSERT INTO metadata (schema_version, timestamp) "138"VALUES (3, 'foo')");139140ATF_REQUIRE_THROW_RE(store::integrity_error,141"timestamp.*invalid type",142store::metadata::fetch_latest(db));143}144145146ATF_INIT_TEST_CASES(tcs)147{148ATF_ADD_TEST_CASE(tcs, fetch_latest__ok);149ATF_ADD_TEST_CASE(tcs, fetch_latest__empty_metadata);150ATF_ADD_TEST_CASE(tcs, fetch_latest__no_timestamp);151ATF_ADD_TEST_CASE(tcs, fetch_latest__no_schema_version);152ATF_ADD_TEST_CASE(tcs, fetch_latest__invalid_timestamp);153}154155156