1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "store/metadata.hpp"
31 #include <atf-c++.hpp>
33 #include "store/exceptions.hpp"
34 #include "store/write_backend.hpp"
35 #include "utils/fs/path.hpp"
36 #include "utils/logging/operations.hpp"
37 #include "utils/sqlite/database.hpp"
39 namespace logging = utils::logging;
40 namespace sqlite = utils::sqlite;
46 /// Creates a test in-memory database.
48 /// When using this function, you must define a 'require.files' property in this
49 /// case pointing to store::detail::schema_file().
51 /// The database created by this function mimics a real complete database, but
52 /// without any predefined values. I.e. for our particular case, the metadata
55 /// \return A SQLite database instance.
56 static sqlite::database
59 sqlite::database db = sqlite::database::in_memory();
60 store::detail::initialize(db);
61 db.exec("DELETE FROM metadata");
66 } // anonymous namespace
69 ATF_TEST_CASE(fetch_latest__ok);
70 ATF_TEST_CASE_HEAD(fetch_latest__ok)
72 logging::set_inmemory();
73 set_md_var("require.files", store::detail::schema_file().c_str());
75 ATF_TEST_CASE_BODY(fetch_latest__ok)
77 sqlite::database db = create_database();
78 db.exec("INSERT INTO metadata (schema_version, timestamp) "
79 "VALUES (512, 5678)");
80 db.exec("INSERT INTO metadata (schema_version, timestamp) "
81 "VALUES (256, 1234)");
83 const store::metadata metadata = store::metadata::fetch_latest(db);
84 ATF_REQUIRE_EQ(5678L, metadata.timestamp());
85 ATF_REQUIRE_EQ(512, metadata.schema_version());
89 ATF_TEST_CASE(fetch_latest__empty_metadata);
90 ATF_TEST_CASE_HEAD(fetch_latest__empty_metadata)
92 logging::set_inmemory();
93 set_md_var("require.files", store::detail::schema_file().c_str());
95 ATF_TEST_CASE_BODY(fetch_latest__empty_metadata)
97 sqlite::database db = create_database();
98 ATF_REQUIRE_THROW_RE(store::integrity_error, "metadata.*empty",
99 store::metadata::fetch_latest(db));
103 ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_timestamp);
104 ATF_TEST_CASE_BODY(fetch_latest__no_timestamp)
106 sqlite::database db = sqlite::database::in_memory();
107 db.exec("CREATE TABLE metadata (schema_version INTEGER)");
108 db.exec("INSERT INTO metadata VALUES (3)");
110 ATF_REQUIRE_THROW_RE(store::integrity_error,
111 "Invalid metadata.*timestamp",
112 store::metadata::fetch_latest(db));
116 ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_schema_version);
117 ATF_TEST_CASE_BODY(fetch_latest__no_schema_version)
119 sqlite::database db = sqlite::database::in_memory();
120 db.exec("CREATE TABLE metadata (timestamp INTEGER)");
121 db.exec("INSERT INTO metadata VALUES (3)");
123 ATF_REQUIRE_THROW_RE(store::integrity_error,
124 "Invalid metadata.*schema_version",
125 store::metadata::fetch_latest(db));
129 ATF_TEST_CASE(fetch_latest__invalid_timestamp);
130 ATF_TEST_CASE_HEAD(fetch_latest__invalid_timestamp)
132 logging::set_inmemory();
133 set_md_var("require.files", store::detail::schema_file().c_str());
135 ATF_TEST_CASE_BODY(fetch_latest__invalid_timestamp)
137 sqlite::database db = create_database();
138 db.exec("INSERT INTO metadata (schema_version, timestamp) "
139 "VALUES (3, 'foo')");
141 ATF_REQUIRE_THROW_RE(store::integrity_error,
142 "timestamp.*invalid type",
143 store::metadata::fetch_latest(db));
147 ATF_INIT_TEST_CASES(tcs)
149 ATF_ADD_TEST_CASE(tcs, fetch_latest__ok);
150 ATF_ADD_TEST_CASE(tcs, fetch_latest__empty_metadata);
151 ATF_ADD_TEST_CASE(tcs, fetch_latest__no_timestamp);
152 ATF_ADD_TEST_CASE(tcs, fetch_latest__no_schema_version);
153 ATF_ADD_TEST_CASE(tcs, fetch_latest__invalid_timestamp);