summaryrefslogtreecommitdiff
path: root/chromium/third_party/sqlite/src/ext/rbu/rbu11.test
blob: 5a4219b00152dfaf5e19d4767ec35aa96f1aaaff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 2015 February 16
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

if {![info exists testdir]} {
  set testdir [file join [file dirname [info script]] .. .. test]
}
source $testdir/tester.tcl
set ::testprefix rbu11


#--------------------------------------------------------------------
# Test that the xAccess() method of an rbu vfs handles queries other
# than SQLITE_ACCESS_EXISTS correctly. The test code below causes
# SQLite to call xAccess(SQLITE_ACCESS_READWRITE) on the directory
# path argument passed to "PRAGMA temp_store_directory".
#
do_test 1.1 {
  sqlite3rbu_create_vfs -default rbu ""
  reset_db
  catchsql { PRAGMA temp_store_directory = '/no/such/directory' }
} {1 {not a writable directory}}

do_test 1.2 {
  catchsql " PRAGMA temp_store_directory = '[pwd]' "
} {0 {}}

do_test 1.3 {
  catchsql " PRAGMA temp_store_directory = '' "
} {0 {}}

do_test 1.4 {
  db close
  sqlite3rbu_destroy_vfs rbu
} {}

#--------------------------------------------------------------------
# Try to trick rbu into operating on a database opened in wal mode.
#
reset_db
do_execsql_test 2.1 {
  CREATE TABLE t1(a PRIMARY KEY, b, c);
  INSERT INTO t1 VALUES(1, 2, 3);
  PRAGMA journal_mode = 'wal';
  CREATE TABLE t2(d PRIMARY KEY, e, f);
} {wal}

do_test 2.2 {
  db_save 
  db close

  forcedelete rbu.db
  sqlite3 dbo rbu.db
  dbo eval {
    CREATE TABLE data_t1(a, b, c, rbu_control);
    INSERT INTO data_t1 VALUES(4, 5, 6, 0);
    INSERT INTO data_t1 VALUES(7, 8, 9, 0);
  }
  dbo close

  db_restore 
  hexio_write test.db 18 0101
  file exists test.db-wal
} {1}

do_test 2.3 {
  sqlite3rbu rbu test.db rbu.db
  rbu step
} {SQLITE_ERROR}

do_test 2.4 {
  list [catch {rbu close} msg] $msg
} {1 {SQLITE_ERROR - cannot update wal mode database}}

#--------------------------------------------------------------------
# Test a constraint violation message with an unusual table name. 
# Specifically, one for which the first character is a codepoint
# smaller than 30 (character '0').
#
reset_db
do_execsql_test 3.1 {
  CREATE TABLE "(t1)"(a PRIMARY KEY, b, c);
  INSERT INTO "(t1)" VALUES(1, 2, 3);
  INSERT INTO "(t1)" VALUES(4, 5, 6);
}
db close

do_test 3.2 {
  forcedelete rbu.db
  sqlite3 dbo rbu.db
  dbo eval {
    CREATE TABLE "data_(t1)"(a, b, c, rbu_control);
    INSERT INTO "data_(t1)" VALUES(4, 8, 9, 0);
  }
  dbo close

  sqlite3rbu rbu test.db rbu.db
  rbu step
  rbu step
} {SQLITE_CONSTRAINT}

do_test 3.3 {
  list [catch {rbu close} msg] $msg
} {1 {SQLITE_CONSTRAINT - UNIQUE constraint failed: (t1).a}}

#--------------------------------------------------------------------
# Check that once an RBU update has been applied, attempting to apply
# it a second time is a no-op (as the state stored in the RBU database is
# "all steps completed").
#
reset_db
do_execsql_test 4.1 {
  CREATE TABLE "(t1)"(a, b, c, PRIMARY KEY(c, b, a));
  INSERT INTO "(t1)" VALUES(1, 2, 3);
  INSERT INTO "(t1)" VALUES(4, 5, 6);
}
db close

do_test 4.2 {
  forcedelete rbu.db
  sqlite3 dbo rbu.db
  dbo eval {
    CREATE TABLE "data_(t1)"(a, b, c, rbu_control);
    INSERT INTO "data_(t1)" VALUES(7, 8, 9, 0);
    INSERT INTO "data_(t1)" VALUES(1, 2, 3, 1);
  }
  dbo close

  sqlite3rbu rbu test.db rbu.db
  while {[rbu step]=="SQLITE_OK"} { }
  rbu close
} {SQLITE_DONE}

do_test 4.3 {
  sqlite3rbu rbu test.db rbu.db
  rbu step
} {SQLITE_DONE}

do_test 4.4 {
  rbu close
} {SQLITE_DONE}

do_test 4.5.1 {
  sqlite3 dbo rbu.db
  dbo eval { INSERT INTO rbu_state VALUES(100, 100) }
  dbo close
  sqlite3rbu rbu test.db rbu.db
  rbu step
} {SQLITE_CORRUPT}
do_test 4.5.2 {
  list [catch {rbu close} msg] $msg
} {1 SQLITE_CORRUPT}
do_test 4.5.3 {
  sqlite3 dbo rbu.db
  dbo eval { DELETE FROM rbu_state WHERE k = 100 }
  dbo close 
} {}

# Also, check that an invalid state value in the rbu_state table is
# detected and reported as corruption.
do_test 4.6.1 {
  sqlite3 dbo rbu.db
  dbo eval { UPDATE rbu_state SET v = v*-1 WHERE k = 1 }
  dbo close
  sqlite3rbu rbu test.db rbu.db
  rbu step
} {SQLITE_CORRUPT}
do_test 4.6.2 {
  list [catch {rbu close} msg] $msg
} {1 SQLITE_CORRUPT}
do_test 4.6.3 {
  sqlite3 dbo rbu.db
  dbo eval { UPDATE rbu_state SET v = v*-1 WHERE k = 1 }
  dbo close 
} {}

do_test 4.7.1 {
  sqlite3 dbo rbu.db
  dbo eval { UPDATE rbu_state SET v = 1 WHERE k = 1 }
  dbo eval { UPDATE rbu_state SET v = 'nosuchtable' WHERE k = 2 }
  dbo close
  sqlite3rbu rbu test.db rbu.db
  rbu step
} {SQLITE_ERROR}
do_test 4.7.2 {
  list [catch {rbu close} msg] $msg
} {1 {SQLITE_ERROR - rbu_state mismatch error}}

finish_test