~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to gears/database/database_utils_test.cc

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2008, Google Inc.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without
 
4
// modification, are permitted provided that the following conditions are met:
 
5
//
 
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.
 
14
//
 
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.
 
25
 
 
26
#include "gears/database/database_utils_test.h"
 
27
 
 
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"
 
33
 
 
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)
 
37
    sqlite3Traits;
 
38
typedef scoped_token<sqlite3*, sqlite3Traits> scoped_sqlite3_handle;
 
39
 
 
40
bool TestDatabaseUtilsAll(std::string16 *error) {
 
41
// TODO(aa): Refactor into a common location for all the internal tests.
 
42
#undef TEST_ASSERT
 
43
#define TEST_ASSERT(b) \
 
44
{ \
 
45
  if (!(b)) { \
 
46
    LOG(("TestDatabaseUtilsAll - failed (%d)\n", __LINE__)); \
 
47
    assert(error); \
 
48
    *error += STRING16(L"TestDatabaseUtilsAll - failed. "); \
 
49
    return false; \
 
50
  } \
 
51
}
 
52
 
 
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...
 
60
 
 
61
  PermissionsDB *permissions = PermissionsDB::GetDB();
 
62
  TEST_ASSERT(permissions);
 
63
 
 
64
  // Get us an origin to work with.
 
65
  SecurityOrigin foo;
 
66
  foo.InitFromUrl(STRING16(L"http://unittest.foo.example.com"));
 
67
  permissions->SetPermission(foo,
 
68
                             PermissionsDB::PERMISSION_LOCAL_DATA,
 
69
                             PermissionsDB::PERMISSION_ALLOWED);
 
70
 
 
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,
 
75
                                               &basename));
 
76
 
 
77
  std::string16 data_dir;
 
78
  TEST_ASSERT(GetDataDirectory(foo, &data_dir));
 
79
 
 
80
  std::string16 filename(data_dir);
 
81
  filename += kPathSeparator;
 
82
  filename += basename;
 
83
  File::Delete(filename.c_str());
 
84
 
 
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)));
 
91
 
 
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)",
 
95
                                        NULL, NULL, NULL));
 
96
  TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), kInsertSql,
 
97
                                        NULL, NULL, NULL));
 
98
 
 
99
  // This should have the side effect of poisoning the database.
 
100
  TEST_ASSERT(SQLITE_CORRUPT == SqlitePoisonIfCorrupt(db.get(),
 
101
                                                      SQLITE_CORRUPT));
 
102
 
 
103
  // Should map SQLITE_NOTADB to SQLITE_CORRUPT.
 
104
  TEST_ASSERT(SQLITE_CORRUPT == SqlitePoisonIfCorrupt(db.get(),
 
105
                                                      SQLITE_NOTADB));
 
106
 
 
107
  // Should fail now.
 
108
  TEST_ASSERT(SQLITE_NOTADB == sqlite3_exec(db.get(), kInsertSql,
 
109
                                            NULL, NULL, NULL));
 
110
  TEST_ASSERT(SQLITE_OK == sqlite3_close(db.release()));
 
111
 
 
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,
 
117
                                           NULL, NULL, NULL));
 
118
  TEST_ASSERT(SQLITE_OK == sqlite3_close(db2.release()));
 
119
 
 
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,
 
124
                                               &current_basename));
 
125
  TEST_ASSERT(basename == current_basename);
 
126
 
 
127
  // Open the database again.  It should work.
 
128
  TEST_ASSERT(OpenSqliteDatabase(kFooDatabaseName, foo, NULL,
 
129
                                 as_out_parameter(db)));
 
130
 
 
131
  // The basename should be different.
 
132
  TEST_ASSERT(permissions->GetDatabaseBasename(foo, kFooDatabaseName,
 
133
                                               &current_basename));
 
134
  TEST_ASSERT(basename != current_basename);
 
135
 
 
136
  // Delete the poisoned database now that we're done with it.
 
137
  filename = data_dir;
 
138
  filename += kPathSeparator;
 
139
  filename += basename;
 
140
  TEST_ASSERT(File::Delete(filename.c_str()));
 
141
 
 
142
  // The table we created now shouldn't be there.
 
143
  TEST_ASSERT(SQLITE_ERROR == sqlite3_exec(db.get(), kInsertSql,
 
144
                                           NULL, NULL, NULL));
 
145
 
 
146
  // The database should work just fine.
 
147
  TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), "CREATE TABLE t (c TEXT)",
 
148
                                        NULL, NULL, NULL));
 
149
  TEST_ASSERT(SQLITE_OK == sqlite3_exec(db.get(), kInsertSql,
 
150
                                        NULL, NULL, NULL));
 
151
 
 
152
  TEST_ASSERT(SQLITE_OK == sqlite3_close(db.release()));
 
153
 
 
154
  // Delete our new database file so we don't accumulate things in the
 
155
  // filesystem over many runs.
 
156
  filename = data_dir;
 
157
  filename += kPathSeparator;
 
158
  filename += current_basename;
 
159
  TEST_ASSERT(File::Delete(filename.c_str()));
 
160
 
 
161
  LOG(("TestPermissionsDBAll - passed\n"));
 
162
  return true;
 
163
}