1
// Copyright 2008, Google Inc.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are met:
6
// 1. Redistributions of source code must retain the above copyright notice,
7
// this list of conditions and the following disclaimer.
8
// 2. Redistributions in binary form must reproduce the above copyright notice,
9
// this list of conditions and the following disclaimer in the documentation
10
// and/or other materials provided with the distribution.
11
// 3. Neither the name of Google Inc. nor the names of its contributors may be
12
// used to endorse or promote products derived from this software without
13
// specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include "gears/database/database_utils_test.h"
28
#include "gears/base/common/file.h"
29
#include "gears/base/common/paths.h"
30
#include "gears/base/common/permissions_db.h"
31
#include "gears/base/common/scoped_token.h"
32
#include "gears/database/database_utils.h"
34
// Hack together a scoped sqlite3 * so that TEST_ASSERT() failures
35
// dont't leave the database wedged.
36
typedef DECLARE_SCOPED_TRAITS(sqlite3*, sqlite3_close, NULL)
38
typedef scoped_token<sqlite3*, sqlite3Traits> scoped_sqlite3_handle;
40
bool TestDatabaseUtilsAll(std::string16 *error) {
41
// TODO(aa): Refactor into a common location for all the internal tests.
43
#define TEST_ASSERT(b) \
46
LOG(("TestDatabaseUtilsAll - failed (%d)\n", __LINE__)); \
48
*error += STRING16(L"TestDatabaseUtilsAll - failed. "); \
53
// TODO(shess): I would like it if this code could clear out the
54
// existing PermissionsDB database name mappings, both to prevent
55
// growth of cruft in PermissionsDB, and to make sure we're testing
56
// cases such as no mappings. For now, this code manually deletes
57
// the database files, because that has the biggest potential for
58
// issues (too many files in a directory). That will work as long
59
// as assertions aren't failing...
61
PermissionsDB *permissions = PermissionsDB::GetDB();
62
TEST_ASSERT(permissions);
64
// Get us an origin to work with.
66
foo.InitFromUrl(STRING16(L"http://unittest.foo.example.com"));
67
permissions->SetPermission(foo,
68
PermissionsDB::PERMISSION_LOCAL_DATA,
69
PermissionsDB::PERMISSION_ALLOWED);
71
// Delete any existing database file.
72
const char16 *kFooDatabaseName = STRING16(L"poison_test");
73
std::string16 basename;
74
TEST_ASSERT(permissions->GetDatabaseBasename(foo, kFooDatabaseName,
77
std::string16 data_dir;
78
TEST_ASSERT(GetDataDirectory(foo, &data_dir));
80
std::string16 filename(data_dir);
81
filename += kPathSeparator;
83
File::Delete(filename.c_str());
85
// Open a couple handles to the database.
86
scoped_sqlite3_handle db, db2;
87
TEST_ASSERT(OpenSqliteDatabase(kFooDatabaseName, foo, NULL,
88
as_out_parameter(db)));
89
TEST_ASSERT(OpenSqliteDatabase(kFooDatabaseName, foo, NULL,
90
as_out_parameter(db2)));
92
// Test that the database works.
93
const char *kInsertSql("INSERT INTO t VALUES ('x')");
94
TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), "CREATE TABLE t (c TEXT)",
96
TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), kInsertSql,
99
// This should have the side effect of poisoning the database.
100
TEST_ASSERT(SQLITE_CORRUPT == SqlitePoisonIfCorrupt(db.get(),
103
// Should map SQLITE_NOTADB to SQLITE_CORRUPT.
104
TEST_ASSERT(SQLITE_CORRUPT == SqlitePoisonIfCorrupt(db.get(),
108
TEST_ASSERT(SQLITE_NOTADB == sqlite3_exec(db.get(), kInsertSql,
110
TEST_ASSERT(SQLITE_OK == sqlite3_close(db.release()));
112
// Should also fail on the other handle.
113
// TODO(shess): This is a problem. I get SQLITE_ERROR, which will
114
// NOT provide the desired SQLITE_CORRUPT signal to close the
115
// database and re-open.
116
TEST_ASSERT(SQLITE_ERROR == sqlite3_exec(db2.get(), kInsertSql,
118
TEST_ASSERT(SQLITE_OK == sqlite3_close(db2.release()));
120
// At this point we should still see the old name, because only
121
// OpenSqliteDatabase() initiates the PermissionsDB change.
122
std::string16 current_basename;
123
TEST_ASSERT(permissions->GetDatabaseBasename(foo, kFooDatabaseName,
125
TEST_ASSERT(basename == current_basename);
127
// Open the database again. It should work.
128
TEST_ASSERT(OpenSqliteDatabase(kFooDatabaseName, foo, NULL,
129
as_out_parameter(db)));
131
// The basename should be different.
132
TEST_ASSERT(permissions->GetDatabaseBasename(foo, kFooDatabaseName,
134
TEST_ASSERT(basename != current_basename);
136
// Delete the poisoned database now that we're done with it.
138
filename += kPathSeparator;
139
filename += basename;
140
TEST_ASSERT(File::Delete(filename.c_str()));
142
// The table we created now shouldn't be there.
143
TEST_ASSERT(SQLITE_ERROR == sqlite3_exec(db.get(), kInsertSql,
146
// The database should work just fine.
147
TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), "CREATE TABLE t (c TEXT)",
149
TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), kInsertSql,
152
TEST_ASSERT(SQLITE_OK == sqlite3_close(db.release()));
154
// Delete our new database file so we don't accumulate things in the
155
// filesystem over many runs.
157
filename += kPathSeparator;
158
filename += current_basename;
159
TEST_ASSERT(File::Delete(filename.c_str()));
161
LOG(("TestPermissionsDBAll - passed\n"));