1// Copyright 2011 The Kyua Authors.
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/write_backend.hpp"
30
31#include <atf-c++.hpp>
32
33#include "store/exceptions.hpp"
34#include "store/metadata.hpp"
35#include "utils/datetime.hpp"
36#include "utils/env.hpp"
37#include "utils/fs/path.hpp"
38#include "utils/logging/operations.hpp"
39#include "utils/sqlite/database.hpp"
40#include "utils/sqlite/exceptions.hpp"
41#include "utils/sqlite/statement.ipp"
42
43namespace datetime = utils::datetime;
44namespace fs = utils::fs;
45namespace logging = utils::logging;
46namespace sqlite = utils::sqlite;
47
48
49ATF_TEST_CASE(detail__initialize__ok);
50ATF_TEST_CASE_HEAD(detail__initialize__ok)
51{
52    logging::set_inmemory();
53    set_md_var("require.files", store::detail::schema_file().c_str());
54}
55ATF_TEST_CASE_BODY(detail__initialize__ok)
56{
57    sqlite::database db = sqlite::database::in_memory();
58    const datetime::timestamp before = datetime::timestamp::now();
59    const store::metadata md = store::detail::initialize(db);
60    const datetime::timestamp after = datetime::timestamp::now();
61
62    ATF_REQUIRE(md.timestamp() >= before.to_seconds());
63    ATF_REQUIRE(md.timestamp() <= after.to_microseconds());
64    ATF_REQUIRE_EQ(store::detail::current_schema_version, md.schema_version());
65
66    // Query some known tables to ensure they were created.
67    db.exec("SELECT * FROM metadata");
68
69    // And now query some know values.
70    sqlite::statement stmt = db.create_statement(
71        "SELECT COUNT(*) FROM metadata");
72    ATF_REQUIRE(stmt.step());
73    ATF_REQUIRE_EQ(1, stmt.column_int(0));
74    ATF_REQUIRE(!stmt.step());
75}
76
77
78ATF_TEST_CASE_WITHOUT_HEAD(detail__initialize__missing_schema);
79ATF_TEST_CASE_BODY(detail__initialize__missing_schema)
80{
81    utils::setenv("KYUA_STOREDIR", "/non-existent");
82    store::detail::current_schema_version = 712;
83
84    sqlite::database db = sqlite::database::in_memory();
85    ATF_REQUIRE_THROW_RE(store::error,
86                         "Cannot read.*'/non-existent/schema_v712.sql'",
87                         store::detail::initialize(db));
88}
89
90
91ATF_TEST_CASE_WITHOUT_HEAD(detail__initialize__sqlite_error);
92ATF_TEST_CASE_BODY(detail__initialize__sqlite_error)
93{
94    utils::setenv("KYUA_STOREDIR", ".");
95    store::detail::current_schema_version = 712;
96
97    atf::utils::create_file("schema_v712.sql", "foo_bar_baz;\n");
98
99    sqlite::database db = sqlite::database::in_memory();
100    ATF_REQUIRE_THROW_RE(store::error, "Failed to initialize.*:.*foo_bar_baz",
101                         store::detail::initialize(db));
102}
103
104
105ATF_TEST_CASE_WITHOUT_HEAD(detail__schema_file__builtin);
106ATF_TEST_CASE_BODY(detail__schema_file__builtin)
107{
108    utils::unsetenv("KYUA_STOREDIR");
109    ATF_REQUIRE_EQ(fs::path(KYUA_STOREDIR) / "schema_v3.sql",
110                   store::detail::schema_file());
111}
112
113
114ATF_TEST_CASE_WITHOUT_HEAD(detail__schema_file__overriden);
115ATF_TEST_CASE_BODY(detail__schema_file__overriden)
116{
117    utils::setenv("KYUA_STOREDIR", "/tmp/test");
118    store::detail::current_schema_version = 123;
119    ATF_REQUIRE_EQ(fs::path("/tmp/test/schema_v123.sql"),
120                   store::detail::schema_file());
121}
122
123
124ATF_TEST_CASE(write_backend__open_rw__ok_if_empty);
125ATF_TEST_CASE_HEAD(write_backend__open_rw__ok_if_empty)
126{
127    logging::set_inmemory();
128    set_md_var("require.files", store::detail::schema_file().c_str());
129}
130ATF_TEST_CASE_BODY(write_backend__open_rw__ok_if_empty)
131{
132    {
133        sqlite::database db = sqlite::database::open(
134            fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);
135    }
136    store::write_backend backend = store::write_backend::open_rw(
137        fs::path("test.db"));
138    backend.database().exec("SELECT * FROM metadata");
139}
140
141
142ATF_TEST_CASE(write_backend__open_rw__error_if_not_empty);
143ATF_TEST_CASE_HEAD(write_backend__open_rw__error_if_not_empty)
144{
145    logging::set_inmemory();
146    set_md_var("require.files", store::detail::schema_file().c_str());
147}
148ATF_TEST_CASE_BODY(write_backend__open_rw__error_if_not_empty)
149{
150    {
151        sqlite::database db = sqlite::database::open(
152            fs::path("test.db"), sqlite::open_readwrite | sqlite::open_create);
153        store::detail::initialize(db);
154    }
155    ATF_REQUIRE_THROW_RE(store::error, "test.db already exists",
156                         store::write_backend::open_rw(fs::path("test.db")));
157}
158
159
160ATF_TEST_CASE(write_backend__open_rw__create_missing);
161ATF_TEST_CASE_HEAD(write_backend__open_rw__create_missing)
162{
163    logging::set_inmemory();
164    set_md_var("require.files", store::detail::schema_file().c_str());
165}
166ATF_TEST_CASE_BODY(write_backend__open_rw__create_missing)
167{
168    store::write_backend backend = store::write_backend::open_rw(
169        fs::path("test.db"));
170    backend.database().exec("SELECT * FROM metadata");
171}
172
173
174ATF_TEST_CASE(write_backend__close);
175ATF_TEST_CASE_HEAD(write_backend__close)
176{
177    logging::set_inmemory();
178    set_md_var("require.files", store::detail::schema_file().c_str());
179}
180ATF_TEST_CASE_BODY(write_backend__close)
181{
182    store::write_backend backend = store::write_backend::open_rw(
183        fs::path("test.db"));
184    backend.database().exec("SELECT * FROM metadata");
185    backend.close();
186    ATF_REQUIRE_THROW(utils::sqlite::error,
187                      backend.database().exec("SELECT * FROM metadata"));
188}
189
190
191ATF_INIT_TEST_CASES(tcs)
192{
193    ATF_ADD_TEST_CASE(tcs, detail__initialize__ok);
194    ATF_ADD_TEST_CASE(tcs, detail__initialize__missing_schema);
195    ATF_ADD_TEST_CASE(tcs, detail__initialize__sqlite_error);
196
197    ATF_ADD_TEST_CASE(tcs, detail__schema_file__builtin);
198    ATF_ADD_TEST_CASE(tcs, detail__schema_file__overriden);
199
200    ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__ok_if_empty);
201    ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__error_if_not_empty);
202    ATF_ADD_TEST_CASE(tcs, write_backend__open_rw__create_missing);
203    ATF_ADD_TEST_CASE(tcs, write_backend__close);
204}
205