1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Improve test coverage of ota code a bit.

FossilOrigin-Name: a438fa6c9ad2fb1d78ac747172d07455d6381387
This commit is contained in:
dan
2015-02-17 20:49:42 +00:00
parent f5fe23909d
commit eadc772beb
4 changed files with 234 additions and 211 deletions

View File

@@ -17,102 +17,6 @@ source $testdir/tester.tcl
source $testdir/malloc_common.tcl source $testdir/malloc_common.tcl
set ::testprefix otafault set ::testprefix otafault
do_test 1.1 {
forcedelete ota.db
execsql {
PRAGMA encoding = utf16;
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
CREATE INDEX t1cb ON t1(c, b);
INSERT INTO t1 VALUES(1, 1, 1);
INSERT INTO t1 VALUES(2, 2, 2);
INSERT INTO t1 VALUES(3, 3, 3);
CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
CREATE INDEX t2cb ON t1(c, b);
INSERT INTO t2 VALUES('a', 'a', 'a');
INSERT INTO t2 VALUES('b', 'b', 'b');
INSERT INTO t2 VALUES('c', 'c', 'c');
ATTACH 'ota.db' AS ota;
CREATE TABLE ota.data_t1(a, b, c, ota_control);
CREATE TABLE ota.data_t2(a, b, c, ota_control);
INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
INSERT INTO data_t1 VALUES(3, 'three', NULL, '.x.');
INSERT INTO data_t1 VALUES(4, 4, 4, 0);
INSERT INTO data_t2 VALUES('b', NULL, NULL, 1);
INSERT INTO data_t2 VALUES('c', 'see', NULL, '.x.');
INSERT INTO data_t2 VALUES('d', 'd', 'd', 0);
}
db close
forcecopy test.db test.db.bak
forcecopy ota.db ota.db.bak
} {}
sqlite3_shutdown
set lookaside_config [sqlite3_config_lookaside 0 0]
sqlite3_initialize
autoinstall_test_functions
foreach {tn f reslist} {
1 oom-tra* {
{0 SQLITE_DONE}
{1 {SQLITE_NOMEM - out of memory}}
{1 SQLITE_NOMEM}
{1 SQLITE_IOERR_NOMEM}
{1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
}
2 ioerr-* {
{0 SQLITE_DONE}
{1 {SQLITE_IOERR - disk I/O error}}
{1 SQLITE_IOERR}
{1 SQLITE_IOERR_WRITE}
{1 SQLITE_IOERR_READ}
{1 SQLITE_IOERR_FSYNC}
{1 {SQLITE_ERROR - SQL logic error or missing database}}
{1 {SQLITE_ERROR - unable to open database: ota.db}}
{1 {SQLITE_IOERR - unable to open database: ota.db}}
}
} {
do_faultsim_test 2 -faults $::f -prep {
catch { db close }
forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
forcecopy test.db.bak test.db
forcecopy ota.db.bak ota.db
} -body {
sqlite3ota ota test.db ota.db
while {[ota step]=="SQLITE_OK"} {}
ota close
} -test {
faultsim_test_result {*}$::reslist
if {$testrc==0} {
sqlite3 db test.db
faultsim_integrity_check
set res [db eval {
SELECT * FROM t1 UNION ALL SELECT * FROM t2;
}]
set expected [list {*}{
1 1 1 3 three 3 4 4 4
a a a c see c d d d
}]
if {$res != $expected} {
puts ""
puts "res: $res"
puts "exp: $expected"
error "data not as expected!"
}
}
}
}
catch {db close}
sqlite3_shutdown
sqlite3_config_lookaside {*}$lookaside_config
sqlite3_initialize
autoinstall_test_functions
proc copy_if_exists {src target} { proc copy_if_exists {src target} {
if {[file exists $src]} { if {[file exists $src]} {
forcecopy $src $target forcecopy $src $target
@@ -121,60 +25,163 @@ proc copy_if_exists {src target} {
} }
} }
for {set iStep 0} {$iStep<=21} {incr iStep} { foreach {tn2 setup sql expect} {
1 {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
CREATE INDEX t1cb ON t1(c, b);
INSERT INTO t1 VALUES(1, 1, 1);
INSERT INTO t1 VALUES(2, 2, 2);
INSERT INTO t1 VALUES(3, 3, 3);
forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal CREATE TABLE ota.data_t1(a, b, c, ota_control);
INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
INSERT INTO data_t1 VALUES(3, 'three', NULL, '.x.');
INSERT INTO data_t1 VALUES(4, 4, 4, 0);
} {SELECT * FROM t1} {1 1 1 3 three 3 4 4 4}
copy_if_exists test.db.bak test.db 2 {
copy_if_exists ota.db.bak ota.db CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
CREATE INDEX t2cb ON t2(c, b);
INSERT INTO t2 VALUES('a', 'a', 'a');
INSERT INTO t2 VALUES('b', 'b', 'b');
INSERT INTO t2 VALUES('c', 'c', 'c');
sqlite3ota ota test.db ota.db CREATE TABLE ota.data_t2(a, b, c, ota_control);
for {set x 0} {$x < $::iStep} {incr x} { ota step } INSERT INTO data_t2 VALUES('b', NULL, NULL, 1);
ota close INSERT INTO data_t2 VALUES('c', 'see', NULL, '.x.');
INSERT INTO data_t2 VALUES('d', 'd', 'd', 0);
} {SELECT * FROM t2} {a a a c see c d d d}
copy_if_exists test.db test.db.bak.2 3 {
copy_if_exists test.db-wal test.db.bak.2-wal CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
copy_if_exists test.db-oal test.db.bak.2-oal CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
copy_if_exists ota.db ota.db.bak.2 CREATE INDEX t1cb ON t1(c, b);
CREATE INDEX t2cb ON t2(c, b);
CREATE TABLE ota.data_t1(a, b, c, ota_control);
CREATE TABLE ota.data_t2(a, b, c, ota_control);
INSERT INTO data_t1 VALUES(1, 2, 3, 0);
INSERT INTO data_t2 VALUES(4, 5, 6, 0);
} {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {1 2 3 4 5 6}
} {
catch {db close}
forcedelete ota.db test.db
sqlite3 db test.db
execsql {
PRAGMA encoding = utf16;
ATTACH 'ota.db' AS ota;
}
execsql $setup
db close
forcecopy test.db test.db.bak
forcecopy ota.db ota.db.bak
foreach {tn f reslist} {
1 oom-tra* {
{0 SQLITE_DONE}
{1 {SQLITE_NOMEM - out of memory}}
{1 SQLITE_NOMEM}
{1 SQLITE_IOERR_NOMEM}
{1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
}
2 ioerr-* {
{0 SQLITE_DONE}
{1 {SQLITE_IOERR - disk I/O error}}
{1 SQLITE_IOERR}
{1 SQLITE_IOERR_WRITE}
{1 SQLITE_IOERR_READ}
{1 SQLITE_IOERR_FSYNC}
{1 {SQLITE_ERROR - SQL logic error or missing database}}
{1 {SQLITE_ERROR - unable to open database: ota.db}}
{1 {SQLITE_IOERR - unable to open database: ota.db}}
}
} {
catch {db close}
sqlite3_shutdown
set lookaside_config [sqlite3_config_lookaside 0 0]
sqlite3_initialize
autoinstall_test_functions
do_faultsim_test 2.$tn2 -faults $::f -prep {
catch { db close }
forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
forcecopy test.db.bak test.db
forcecopy ota.db.bak ota.db
} -body {
sqlite3ota ota test.db ota.db
while {[ota step]=="SQLITE_OK"} {}
ota close
} -test {
faultsim_test_result {*}$::reslist
if {$testrc==0} {
sqlite3 db test.db
faultsim_integrity_check
set res [db eval $::sql]
if {$res != [list {*}$::expect]} {
puts ""
puts "res: $res"
puts "exp: $expect"
error "data not as expected!"
}
}
}
catch {db close}
sqlite3_shutdown
sqlite3_config_lookaside {*}$lookaside_config
sqlite3_initialize
autoinstall_test_functions
}
for {set iStep 0} {$iStep<=21} {incr iStep} {
do_faultsim_test 3.$iStep -faults oom-trans* -prep {
catch { db close }
forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
copy_if_exists test.db.bak.2 test.db
copy_if_exists test.db.bak.2-wal test.db-wal copy_if_exists test.db.bak test.db
copy_if_exists test.db.bak.2-oal test.db-oal copy_if_exists ota.db.bak ota.db
copy_if_exists ota.db.bak.2 ota.db
} -body {
sqlite3ota ota test.db ota.db sqlite3ota ota test.db ota.db
while {[ota step] == "SQLITE_OK"} {} for {set x 0} {$x < $::iStep} {incr x} { ota step }
ota close ota close
} -test {
faultsim_test_result {0 SQLITE_DONE} \
{1 {SQLITE_NOMEM - out of memory}} \
{1 SQLITE_NOMEM} \
{1 SQLITE_IOERR_NOMEM} \
{1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}}
if {$testrc==0} { copy_if_exists test.db test.db.bak.2
sqlite3 db test.db copy_if_exists test.db-wal test.db.bak.2-wal
faultsim_integrity_check copy_if_exists test.db-oal test.db.bak.2-oal
set res [db eval { copy_if_exists ota.db ota.db.bak.2
SELECT * FROM t1 UNION ALL SELECT * FROM t2;
}]
set expected [list {*}{
1 1 1 3 three 3 4 4 4
a a a c see c d d d
}]
if {$res != $expected} { do_faultsim_test 3.$tn.$iStep -faults $::f -prep {
puts "" catch { db close }
puts "res: $res" forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
puts "exp: $expected" copy_if_exists test.db.bak.2 test.db
error "data not as expected!" copy_if_exists test.db.bak.2-wal test.db-wal
copy_if_exists test.db.bak.2-oal test.db-oal
copy_if_exists ota.db.bak.2 ota.db
} -body {
sqlite3ota ota test.db ota.db
while {[ota step] == "SQLITE_OK"} {}
ota close
} -test {
faultsim_test_result {*}$::reslist
if {$testrc==0} {
sqlite3 db test.db
faultsim_integrity_check
set res [db eval $::sql]
if {$res != [list {*}$::expect]} {
puts ""
puts "res: $res"
puts "exp: $expected"
error "data not as expected!"
}
} }
} }
} }
} }
finish_test finish_test

View File

@@ -573,86 +573,83 @@ static char *otaStrndup(const char *zStr, int *pRc){
** return OTA_PK_NONE ** return OTA_PK_NONE
** } ** }
*/ */
static int otaTableType( static void otaTableType(
sqlite3 *db, sqlite3ota *p,
const char *zTab, const char *zTab,
int *peType, int *peType,
int *piPk int *piPk
){ ){
sqlite3_stmt *pStmt = 0; /*
int rc = SQLITE_OK; ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
int rc2; ** 1) PRAGMA index_list = ?
char *zSql = 0; ** 2) SELECT count(*) FROM sqlite_master where name=%Q
** 3) PRAGMA table_info = ?
*/
sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
*peType = OTA_PK_NOTABLE; *peType = OTA_PK_NOTABLE;
*piPk = 0; *piPk = 0;
zSql = sqlite3_mprintf(
assert( p->rc==SQLITE_OK );
p->rc = prepareFreeAndCollectError(p->db, &aStmt[0], &p->zErrmsg,
sqlite3_mprintf(
"SELECT (sql LIKE 'create virtual%%')" "SELECT (sql LIKE 'create virtual%%')"
" FROM main.sqlite_master" " FROM sqlite_master"
" WHERE name=%Q", zTab); " WHERE name=%Q", zTab
if( zSql==0 ) return SQLITE_NOMEM; ));
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
sqlite3_free(zSql); /* Either an error, or no such table. */
zSql = 0; goto otaTableType_end;
if( pStmt==0 ) goto otaTableType_end;
if( sqlite3_step(pStmt)!=SQLITE_ROW ){
goto otaTableType_end; /* no such table */
} }
if( sqlite3_column_int(pStmt,0) ){ if( sqlite3_column_int(aStmt[0], 0) ){
*peType = OTA_PK_VTAB; /* virtual table */ *peType = OTA_PK_VTAB; /* virtual table */
goto otaTableType_end; goto otaTableType_end;
} }
rc = sqlite3_finalize(pStmt);
if( rc ) return rc; p->rc = prepareFreeAndCollectError(p->db, &aStmt[1], &p->zErrmsg,
zSql = sqlite3_mprintf("PRAGMA index_list=%Q",zTab); sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
if( zSql==0 ) return SQLITE_NOMEM; );
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); if( p->rc ) goto otaTableType_end;
sqlite3_free(zSql); while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
zSql = 0; const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
if( pStmt==0 ) goto otaTableType_end; const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
while( sqlite3_step(pStmt)==SQLITE_ROW ){ if( zOrig && zIdx && zOrig[0]=='p' ){
const u8 *zOrig = sqlite3_column_text(pStmt,3); p->rc = prepareFreeAndCollectError(p->db, &aStmt[2], &p->zErrmsg,
if( zOrig && zOrig[0]=='p' ){ sqlite3_mprintf(
zSql = sqlite3_mprintf("SELECT rootpage FROM main.sqlite_master" "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
" WHERE name=%Q", sqlite3_column_text(pStmt,1)); ));
if( zSql==0 ){ rc = SQLITE_NOMEM; goto otaTableType_end; } if( p->rc==SQLITE_OK ){
break; if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
} *piPk = sqlite3_column_int(aStmt[2], 0);
} *peType = OTA_PK_EXTERNAL;
rc = sqlite3_finalize(pStmt); }else{
pStmt = 0; *peType = OTA_PK_WITHOUT_ROWID;
if( rc ) return rc; }
if( zSql ){
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
zSql = 0;
if( pStmt==0 ) goto otaTableType_end;
if( sqlite3_step(pStmt)==SQLITE_ROW ){
*piPk = sqlite3_column_int(pStmt, 0);
*peType = OTA_PK_EXTERNAL; /* external PK index */
}else{
*peType = OTA_PK_WITHOUT_ROWID; /* WITHOUT ROWID table */
}
}else{
zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
if( zSql==0 ) return SQLITE_NOMEM;
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
zSql = 0;
if( pStmt==0 ) goto otaTableType_end;
*peType = OTA_PK_NONE; /* (default) implicit ROWID */
while( sqlite3_step(pStmt)==SQLITE_ROW ){
if( sqlite3_column_int(pStmt,5)>0 ){
*peType = OTA_PK_IPK; /* explicit IPK column */
break;
} }
goto otaTableType_end;
} }
} }
otaTableType_end: p->rc = prepareFreeAndCollectError(p->db, &aStmt[3], &p->zErrmsg,
sqlite3_free(zSql); sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
rc2 = sqlite3_finalize(pStmt); );
return rc ? rc : rc2; if( p->rc==SQLITE_OK ){
while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
if( sqlite3_column_int(aStmt[3],5)>0 ){
*peType = OTA_PK_IPK; /* explicit IPK column */
goto otaTableType_end;
}
}
*peType = OTA_PK_NONE;
}
otaTableType_end: {
int i;
for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
int rc2 = sqlite3_finalize(aStmt[i]);
if( p->rc==SQLITE_OK ) p->rc = rc2;
}
}
} }
@@ -676,7 +673,7 @@ static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
/* Figure out the type of table this step will deal with. */ /* Figure out the type of table this step will deal with. */
assert( pIter->eType==0 ); assert( pIter->eType==0 );
p->rc = otaTableType(p->db, pIter->zTbl, &pIter->eType, &pIter->iPkTnum); otaTableType(p, pIter->zTbl, &pIter->eType, &pIter->iPkTnum);
if( p->rc ) return p->rc; if( p->rc ) return p->rc;
assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
@@ -1103,7 +1100,7 @@ static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
rc = sqlite3_finalize(pXList); rc = sqlite3_finalize(pXList);
if( p->rc==SQLITE_OK ) p->rc = rc; if( p->rc==SQLITE_OK ) p->rc = rc;
while( p->rc==SQLITE_OK && pXInfo && SQLITE_ROW==sqlite3_step(pXInfo) ){ while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
if( sqlite3_column_int(pXInfo, 5) ){ if( sqlite3_column_int(pXInfo, 5) ){
/* int iCid = sqlite3_column_int(pXInfo, 0); */ /* int iCid = sqlite3_column_int(pXInfo, 0); */
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2); const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
@@ -1119,6 +1116,25 @@ static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
return z; return z;
} }
/*
** This function creates the second imposter table used when writing to
** a table b-tree where the table has an external primary key. If the
** iterator passed as the second argument does not currently point to
** a table (not index) with an external primary key, this function is a
** no-op.
**
** Assuming the iterator does point to a table with an external PK, this
** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
** used to access that PK index. For example, if the target table is
** declared as follows:
**
** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
**
** then the imposter table schema is:
**
** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
**
*/
static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){ static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){ if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
int tnum = pIter->iPkTnum; /* Root page of PK index */ int tnum = pIter->iPkTnum; /* Root page of PK index */

View File

@@ -1,5 +1,5 @@
C Add\sextra\stests\sand\sfixes\sfor\sota. C Improve\stest\scoverage\sof\sota\scode\sa\sbit.
D 2015-02-16T21:13:19.665 D 2015-02-17T20:49:42.756
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 6b9e7677829aa94b9f30949656e27312aefb9a46 F Makefile.in 6b9e7677829aa94b9f30949656e27312aefb9a46
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -135,8 +135,8 @@ F ext/ota/ota7.test 1fe2c5761705374530e29f70c39693076028221a
F ext/ota/ota8.test cd70e63a0c29c45c0906692827deafa34638feda F ext/ota/ota8.test cd70e63a0c29c45c0906692827deafa34638feda
F ext/ota/ota9.test d3eee95dd836824d07a22e5efcdb7bf6e869358b F ext/ota/ota9.test d3eee95dd836824d07a22e5efcdb7bf6e869358b
F ext/ota/otaA.test ef4bfa8cfd4ed814ae86f7457b64aa2f18c90171 F ext/ota/otaA.test ef4bfa8cfd4ed814ae86f7457b64aa2f18c90171
F ext/ota/otafault.test fd3d4d9b9ed3cbe4461cde602e40f50825e0ee2a F ext/ota/otafault.test 0c7565f69e5e379a5ebdcaea4056f0c69da1becf
F ext/ota/sqlite3ota.c f378ebb435ce79bb5060907c3bc994c5b48cbb85 F ext/ota/sqlite3ota.c 0c6cb4cea1a9231bc488e9c84da201b796437af6
F ext/ota/sqlite3ota.h 1cc7201086fe65a36957740381485a24738c4077 F ext/ota/sqlite3ota.h 1cc7201086fe65a36957740381485a24738c4077
F ext/ota/test_ota.c 5dd58e4e6eb3ae7b471566616d44b701971bce88 F ext/ota/test_ota.c 5dd58e4e6eb3ae7b471566616d44b701971bce88
F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
@@ -1256,7 +1256,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 62dc1fffc38cb157c15105098749b6dd0198eb84 P e0b7151962fedbcac975f2216fd6b33b995a8945
R 26fc1fcf1b321dc2a4f4bbbaf0fa84e9 R 13b826f0fe6f2960ec7719bf830cc36a
U dan U dan
Z bb700675d8726f14df483f7f803a4d01 Z 295fff9fbb7cdd54355b757b10496bab

View File

@@ -1 +1 @@
e0b7151962fedbcac975f2216fd6b33b995a8945 a438fa6c9ad2fb1d78ac747172d07455d6381387