1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c++.hpp>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
34*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
35*11be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
36*11be35a1SLionel Sambuc #include "utils/sqlite/test_utils.hpp"
37*11be35a1SLionel Sambuc #include "utils/sqlite/transaction.hpp"
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc namespace fs = utils::fs;
40*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
41*11be35a1SLionel Sambuc 
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(in_memory);
ATF_TEST_CASE_BODY(in_memory)44*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(in_memory)
45*11be35a1SLionel Sambuc {
46*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
47*11be35a1SLionel Sambuc     create_test_table(raw(db));
48*11be35a1SLionel Sambuc     verify_test_table(raw(db));
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc     ATF_REQUIRE(!fs::exists(fs::path(":memory:")));
51*11be35a1SLionel Sambuc }
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open__readonly__ok);
ATF_TEST_CASE_BODY(open__readonly__ok)55*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open__readonly__ok)
56*11be35a1SLionel Sambuc {
57*11be35a1SLionel Sambuc     {
58*11be35a1SLionel Sambuc         ::sqlite3* db;
59*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_open_v2("test.db", &db,
60*11be35a1SLionel Sambuc             SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL));
61*11be35a1SLionel Sambuc         create_test_table(db);
62*11be35a1SLionel Sambuc         ::sqlite3_close(db);
63*11be35a1SLionel Sambuc     }
64*11be35a1SLionel Sambuc     {
65*11be35a1SLionel Sambuc         sqlite::database db = sqlite::database::open(fs::path("test.db"),
66*11be35a1SLionel Sambuc             sqlite::open_readonly);
67*11be35a1SLionel Sambuc         verify_test_table(raw(db));
68*11be35a1SLionel Sambuc     }
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open__readonly__fail);
ATF_TEST_CASE_BODY(open__readonly__fail)73*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open__readonly__fail)
74*11be35a1SLionel Sambuc {
75*11be35a1SLionel Sambuc     REQUIRE_API_ERROR("sqlite3_open_v2",
76*11be35a1SLionel Sambuc         sqlite::database::open(fs::path("missing.db"), sqlite::open_readonly));
77*11be35a1SLionel Sambuc     ATF_REQUIRE(!fs::exists(fs::path("missing.db")));
78*11be35a1SLionel Sambuc }
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc 
81*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(open__create__ok);
ATF_TEST_CASE_BODY(open__create__ok)82*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open__create__ok)
83*11be35a1SLionel Sambuc {
84*11be35a1SLionel Sambuc     {
85*11be35a1SLionel Sambuc         sqlite::database db = sqlite::database::open(fs::path("test.db"),
86*11be35a1SLionel Sambuc             sqlite::open_readwrite | sqlite::open_create);
87*11be35a1SLionel Sambuc         ATF_REQUIRE(fs::exists(fs::path("test.db")));
88*11be35a1SLionel Sambuc         create_test_table(raw(db));
89*11be35a1SLionel Sambuc     }
90*11be35a1SLionel Sambuc     {
91*11be35a1SLionel Sambuc         ::sqlite3* db;
92*11be35a1SLionel Sambuc         ATF_REQUIRE_EQ(SQLITE_OK, ::sqlite3_open_v2("test.db", &db,
93*11be35a1SLionel Sambuc             SQLITE_OPEN_READONLY, NULL));
94*11be35a1SLionel Sambuc         verify_test_table(db);
95*11be35a1SLionel Sambuc         ::sqlite3_close(db);
96*11be35a1SLionel Sambuc     }
97*11be35a1SLionel Sambuc }
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc ATF_TEST_CASE(open__create__fail);
ATF_TEST_CASE_HEAD(open__create__fail)101*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(open__create__fail)
102*11be35a1SLionel Sambuc {
103*11be35a1SLionel Sambuc     set_md_var("require.user", "unprivileged");
104*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(open__create__fail)105*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(open__create__fail)
106*11be35a1SLionel Sambuc {
107*11be35a1SLionel Sambuc     fs::mkdir(fs::path("protected"), 0555);
108*11be35a1SLionel Sambuc     REQUIRE_API_ERROR("sqlite3_open_v2",
109*11be35a1SLionel Sambuc         sqlite::database::open(fs::path("protected/test.db"),
110*11be35a1SLionel Sambuc                                sqlite::open_readwrite | sqlite::open_create));
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 
114*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(temporary);
ATF_TEST_CASE_BODY(temporary)115*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(temporary)
116*11be35a1SLionel Sambuc {
117*11be35a1SLionel Sambuc     // We could validate if files go to disk by setting the temp_store_directory
118*11be35a1SLionel Sambuc     // PRAGMA to a subdirectory of pwd, and then ensuring the subdirectory is
119*11be35a1SLionel Sambuc     // not empty.  However, there does not seem to be a way to force SQLite to
120*11be35a1SLionel Sambuc     // unconditionally write the temporary database to disk (even with
121*11be35a1SLionel Sambuc     // temp_store = FILE), so this scenary is hard to reproduce.
122*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::temporary();
123*11be35a1SLionel Sambuc     create_test_table(raw(db));
124*11be35a1SLionel Sambuc     verify_test_table(raw(db));
125*11be35a1SLionel Sambuc }
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(close);
ATF_TEST_CASE_BODY(close)129*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(close)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
132*11be35a1SLionel Sambuc     db.close();
133*11be35a1SLionel Sambuc     // The destructor for the database will run now.  If it does a second close,
134*11be35a1SLionel Sambuc     // we may crash, so let's see if we don't.
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(copy);
ATF_TEST_CASE_BODY(copy)139*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(copy)
140*11be35a1SLionel Sambuc {
141*11be35a1SLionel Sambuc     sqlite::database db1 = sqlite::database::in_memory();
142*11be35a1SLionel Sambuc     {
143*11be35a1SLionel Sambuc         sqlite::database db2 = sqlite::database::in_memory();
144*11be35a1SLionel Sambuc         create_test_table(raw(db2));
145*11be35a1SLionel Sambuc         db1 = db2;
146*11be35a1SLionel Sambuc         verify_test_table(raw(db1));
147*11be35a1SLionel Sambuc     }
148*11be35a1SLionel Sambuc     // db2 went out of scope.  If the destruction is not properly managed, the
149*11be35a1SLionel Sambuc     // memory of db1 may have been invalidated and this would not work.
150*11be35a1SLionel Sambuc     verify_test_table(raw(db1));
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(exec__ok);
ATF_TEST_CASE_BODY(exec__ok)155*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec__ok)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
158*11be35a1SLionel Sambuc     db.exec(create_test_table_sql);
159*11be35a1SLionel Sambuc     verify_test_table(raw(db));
160*11be35a1SLionel Sambuc }
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc 
163*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(exec__fail);
ATF_TEST_CASE_BODY(exec__fail)164*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(exec__fail)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
167*11be35a1SLionel Sambuc     REQUIRE_API_ERROR("sqlite3_exec",
168*11be35a1SLionel Sambuc                       db.exec("SELECT * FROM test"));
169*11be35a1SLionel Sambuc     REQUIRE_API_ERROR("sqlite3_exec",
170*11be35a1SLionel Sambuc                       db.exec("CREATE TABLE test (col INTEGER PRIMARY KEY);"
171*11be35a1SLionel Sambuc                               "FOO BAR"));
172*11be35a1SLionel Sambuc     db.exec("SELECT * FROM test");
173*11be35a1SLionel Sambuc }
174*11be35a1SLionel Sambuc 
175*11be35a1SLionel Sambuc 
176*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_statement__ok);
ATF_TEST_CASE_BODY(create_statement__ok)177*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_statement__ok)
178*11be35a1SLionel Sambuc {
179*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
180*11be35a1SLionel Sambuc     sqlite::statement stmt = db.create_statement("SELECT 3");
181*11be35a1SLionel Sambuc     // Statement testing happens in statement_test.  We are only interested here
182*11be35a1SLionel Sambuc     // in ensuring that the API call exists and runs.
183*11be35a1SLionel Sambuc }
184*11be35a1SLionel Sambuc 
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(begin_transaction);
ATF_TEST_CASE_BODY(begin_transaction)187*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(begin_transaction)
188*11be35a1SLionel Sambuc {
189*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
190*11be35a1SLionel Sambuc     sqlite::transaction stmt = db.begin_transaction();
191*11be35a1SLionel Sambuc     // Transaction testing happens in transaction_test.  We are only interested
192*11be35a1SLionel Sambuc     // here in ensuring that the API call exists and runs.
193*11be35a1SLionel Sambuc }
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(create_statement__fail);
ATF_TEST_CASE_BODY(create_statement__fail)197*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(create_statement__fail)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
200*11be35a1SLionel Sambuc     REQUIRE_API_ERROR("sqlite3_prepare_v2",
201*11be35a1SLionel Sambuc                       db.create_statement("SELECT * FROM missing"));
202*11be35a1SLionel Sambuc }
203*11be35a1SLionel Sambuc 
204*11be35a1SLionel Sambuc 
205*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(last_insert_rowid);
ATF_TEST_CASE_BODY(last_insert_rowid)206*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(last_insert_rowid)
207*11be35a1SLionel Sambuc {
208*11be35a1SLionel Sambuc     sqlite::database db = sqlite::database::in_memory();
209*11be35a1SLionel Sambuc     db.exec("CREATE TABLE test (a INTEGER PRIMARY KEY, b INTEGER)");
210*11be35a1SLionel Sambuc     db.exec("INSERT INTO test VALUES (723, 5)");
211*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(723, db.last_insert_rowid());
212*11be35a1SLionel Sambuc     db.exec("INSERT INTO test VALUES (145, 20)");
213*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(145, db.last_insert_rowid());
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)217*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
218*11be35a1SLionel Sambuc {
219*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, in_memory);
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open__readonly__ok);
222*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open__readonly__fail);
223*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open__create__ok);
224*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, open__create__fail);
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, temporary);
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, close);
229*11be35a1SLionel Sambuc 
230*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, copy);
231*11be35a1SLionel Sambuc 
232*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exec__ok);
233*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, exec__fail);
234*11be35a1SLionel Sambuc 
235*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, begin_transaction);
236*11be35a1SLionel Sambuc 
237*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, create_statement__ok);
238*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, create_statement__fail);
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, last_insert_rowid);
241*11be35a1SLionel Sambuc }
242