1 // Copyright 2011 Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 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. 16 // 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. 28 29 #include "store/metadata.hpp" 30 31 #include <atf-c++.hpp> 32 33 #include "store/backend.hpp" 34 #include "store/exceptions.hpp" 35 #include "utils/logging/operations.hpp" 36 #include "utils/sqlite/database.hpp" 37 38 namespace logging = utils::logging; 39 namespace sqlite = utils::sqlite; 40 41 42 namespace { 43 44 45 /// Creates a test in-memory database. 46 /// 47 /// When using this function, you must define a 'require.files' property in this 48 /// case pointing to store::detail::schema_file(). 49 /// 50 /// The database created by this function mimics a real complete database, but 51 /// without any predefined values. I.e. for our particular case, the metadata 52 /// table is empty. 53 /// 54 /// \return A SQLite database instance. 55 static sqlite::database 56 create_database(void) 57 { 58 sqlite::database db = sqlite::database::in_memory(); 59 store::detail::initialize(db); 60 db.exec("DELETE FROM metadata"); 61 return db; 62 } 63 64 65 } // anonymous namespace 66 67 68 ATF_TEST_CASE(fetch_latest__ok); 69 ATF_TEST_CASE_HEAD(fetch_latest__ok) 70 { 71 logging::set_inmemory(); 72 set_md_var("require.files", store::detail::schema_file().c_str()); 73 } 74 ATF_TEST_CASE_BODY(fetch_latest__ok) 75 { 76 sqlite::database db = create_database(); 77 db.exec("INSERT INTO metadata (schema_version, timestamp) " 78 "VALUES (512, 5678)"); 79 db.exec("INSERT INTO metadata (schema_version, timestamp) " 80 "VALUES (256, 1234)"); 81 82 const store::metadata metadata = store::metadata::fetch_latest(db); 83 ATF_REQUIRE_EQ(5678L, metadata.timestamp()); 84 ATF_REQUIRE_EQ(512, metadata.schema_version()); 85 } 86 87 88 ATF_TEST_CASE(fetch_latest__empty_metadata); 89 ATF_TEST_CASE_HEAD(fetch_latest__empty_metadata) 90 { 91 logging::set_inmemory(); 92 set_md_var("require.files", store::detail::schema_file().c_str()); 93 } 94 ATF_TEST_CASE_BODY(fetch_latest__empty_metadata) 95 { 96 sqlite::database db = create_database(); 97 ATF_REQUIRE_THROW_RE(store::integrity_error, "metadata.*empty", 98 store::metadata::fetch_latest(db)); 99 } 100 101 102 ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_timestamp); 103 ATF_TEST_CASE_BODY(fetch_latest__no_timestamp) 104 { 105 sqlite::database db = sqlite::database::in_memory(); 106 db.exec("CREATE TABLE metadata (schema_version INTEGER)"); 107 db.exec("INSERT INTO metadata VALUES (3)"); 108 109 ATF_REQUIRE_THROW_RE(store::integrity_error, 110 "Invalid metadata.*timestamp", 111 store::metadata::fetch_latest(db)); 112 } 113 114 115 ATF_TEST_CASE_WITHOUT_HEAD(fetch_latest__no_schema_version); 116 ATF_TEST_CASE_BODY(fetch_latest__no_schema_version) 117 { 118 sqlite::database db = sqlite::database::in_memory(); 119 db.exec("CREATE TABLE metadata (timestamp INTEGER)"); 120 db.exec("INSERT INTO metadata VALUES (3)"); 121 122 ATF_REQUIRE_THROW_RE(store::integrity_error, 123 "Invalid metadata.*schema_version", 124 store::metadata::fetch_latest(db)); 125 } 126 127 128 ATF_TEST_CASE(fetch_latest__invalid_timestamp); 129 ATF_TEST_CASE_HEAD(fetch_latest__invalid_timestamp) 130 { 131 logging::set_inmemory(); 132 set_md_var("require.files", store::detail::schema_file().c_str()); 133 } 134 ATF_TEST_CASE_BODY(fetch_latest__invalid_timestamp) 135 { 136 sqlite::database db = create_database(); 137 db.exec("INSERT INTO metadata (schema_version, timestamp) " 138 "VALUES (3, 'foo')"); 139 140 ATF_REQUIRE_THROW_RE(store::integrity_error, 141 "timestamp.*invalid type", 142 store::metadata::fetch_latest(db)); 143 } 144 145 146 ATF_INIT_TEST_CASES(tcs) 147 { 148 ATF_ADD_TEST_CASE(tcs, fetch_latest__ok); 149 ATF_ADD_TEST_CASE(tcs, fetch_latest__empty_metadata); 150 ATF_ADD_TEST_CASE(tcs, fetch_latest__no_timestamp); 151 ATF_ADD_TEST_CASE(tcs, fetch_latest__no_schema_version); 152 ATF_ADD_TEST_CASE(tcs, fetch_latest__invalid_timestamp); 153 } 154