]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improve test coverage of ota code a bit.
authordan <dan@noemail.net>
Tue, 17 Feb 2015 20:49:42 +0000 (20:49 +0000)
committerdan <dan@noemail.net>
Tue, 17 Feb 2015 20:49:42 +0000 (20:49 +0000)
FossilOrigin-Name: a438fa6c9ad2fb1d78ac747172d07455d6381387

ext/ota/otafault.test
ext/ota/sqlite3ota.c
manifest
manifest.uuid

index a76d0a008e95b626d200e249278d7f51861e55c7..efc66673d615c250265915982fcb76bd24852a9d 100644 (file)
@@ -17,164 +17,171 @@ source $testdir/tester.tcl
 source $testdir/malloc_common.tcl
 set ::testprefix otafault
 
-do_test 1.1 {
-  forcedelete ota.db
-  execsql {
-    PRAGMA encoding = utf16;
+proc copy_if_exists {src target} {
+  if {[file exists $src]} {
+    forcecopy $src $target
+  } else {
+    forcedelete $target
+  }
+}
+
+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);
-    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);
+  } {SELECT * FROM t1} {1 1 1   3 three 3   4 4 4}
+
+  2 {
+    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');
 
+    CREATE TABLE ota.data_t2(a, b, c, ota_control);
     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);
+  } {SELECT * FROM t2} {a a a   c see c     d d d}
+
+  3 {
+    CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
+    CREATE TABLE t2(a PRIMARY KEY, b, c) WITHOUT ROWID;
+    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
-} {}
-
-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
-      }]
+
+  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}}
+    }
   
-      if {$res != $expected} {
-        puts ""
-        puts "res: $res"
-        puts "exp: $expected"
-        error "data not as expected!"
+    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
+    catch {db close}
+    sqlite3_shutdown
+    sqlite3_config_lookaside {*}$lookaside_config
+    sqlite3_initialize
+    autoinstall_test_functions
 
-proc copy_if_exists {src target} {
-  if {[file exists $src]} {
-    forcecopy $src $target
-  } else {
-    forcedelete $target
-  }
-}
-
-for {set iStep 0} {$iStep<=21} {incr iStep} {
-
-  forcedelete test.db-journal test.db-wal ota.db-journal ota.db-wal
 
-  copy_if_exists test.db.bak test.db
-  copy_if_exists ota.db.bak ota.db
-
-  sqlite3ota ota test.db ota.db
-  for {set x 0} {$x < $::iStep} {incr x} { ota step }
-  ota close
-
-  copy_if_exists test.db test.db.bak.2
-  copy_if_exists test.db-wal test.db.bak.2-wal
-  copy_if_exists test.db-oal test.db.bak.2-oal
-  copy_if_exists ota.db ota.db.bak.2
+  }
 
-  do_faultsim_test 3.$iStep -faults oom-trans* -prep {
-    catch { db close }
+  for {set iStep 0} {$iStep<=21} {incr iStep} {
+  
     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.2-oal test.db-oal
-    copy_if_exists ota.db.bak.2  ota.db
-  } -body {
+  
+    copy_if_exists test.db.bak test.db
+    copy_if_exists ota.db.bak 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
-  } -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} {
-      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
-      }]
+    copy_if_exists test.db test.db.bak.2
+    copy_if_exists test.db-wal test.db.bak.2-wal
+    copy_if_exists test.db-oal test.db.bak.2-oal
+    copy_if_exists ota.db ota.db.bak.2
   
-      if {$res != $expected} {
-        puts ""
-        puts "res: $res"
-        puts "exp: $expected"
-        error "data not as expected!"
+    do_faultsim_test 3.$tn.$iStep -faults $::f -prep {
+      catch { db close }
+      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.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
index 54d4fca3f84d94443c7f3c21a601b759680b5506..b4a475bc0a07f475afa30c268d65f9a72ec376c4 100644 (file)
@@ -573,86 +573,83 @@ static char *otaStrndup(const char *zStr, int *pRc){
 **     return OTA_PK_NONE
 **   }
 */
-static int otaTableType(
-  sqlite3 *db,
+static void otaTableType(
+  sqlite3ota *p,
   const char *zTab,
   int *peType,
   int *piPk
 ){
-  sqlite3_stmt *pStmt = 0;
-  int rc = SQLITE_OK;
-  int rc2;
-  char *zSql = 0;
+  /*
+  ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
+  ** 1) PRAGMA index_list = ?
+  ** 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;
   *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%%')"
-          "  FROM main.sqlite_master"
-          " WHERE name=%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;
-  if( sqlite3_step(pStmt)!=SQLITE_ROW ){
-     goto otaTableType_end;                    /* no such table */
-  }
-  if( sqlite3_column_int(pStmt,0) ){
+          "  FROM sqlite_master"
+          " WHERE name=%Q", zTab
+  ));
+  if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
+    /* Either an error, or no such table. */
+    goto otaTableType_end;
+  }
+  if( sqlite3_column_int(aStmt[0], 0) ){
     *peType = OTA_PK_VTAB;                     /* virtual table */
     goto otaTableType_end;
   }
-  rc = sqlite3_finalize(pStmt);
-  if( rc ) return rc;
-  zSql = sqlite3_mprintf("PRAGMA index_list=%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;
-  while( sqlite3_step(pStmt)==SQLITE_ROW ){
-    const u8 *zOrig = sqlite3_column_text(pStmt,3);
-    if( zOrig && zOrig[0]=='p' ){
-      zSql = sqlite3_mprintf("SELECT rootpage FROM main.sqlite_master"
-                             " WHERE name=%Q", sqlite3_column_text(pStmt,1));
-      if( zSql==0 ){ rc = SQLITE_NOMEM; goto otaTableType_end; }
-      break;
+
+  p->rc = prepareFreeAndCollectError(p->db, &aStmt[1], &p->zErrmsg, 
+    sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
+  );
+  if( p->rc ) goto otaTableType_end;
+  while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
+    const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
+    const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
+    if( zOrig && zIdx && zOrig[0]=='p' ){
+      p->rc = prepareFreeAndCollectError(p->db, &aStmt[2], &p->zErrmsg, 
+          sqlite3_mprintf(
+            "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
+      ));
+      if( p->rc==SQLITE_OK ){
+        if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
+          *piPk = sqlite3_column_int(aStmt[2], 0);
+          *peType = OTA_PK_EXTERNAL;
+        }else{
+          *peType = OTA_PK_WITHOUT_ROWID;
+        }
+      }
+      goto otaTableType_end;
     }
   }
-  rc = sqlite3_finalize(pStmt);
-  pStmt = 0;
-  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 ){
+
+  p->rc = prepareFreeAndCollectError(p->db, &aStmt[3], &p->zErrmsg, 
+    sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
+  );
+  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 */
-        break;
+        goto otaTableType_end;
       }
     }
+    *peType = OTA_PK_NONE;
   }
 
-otaTableType_end:
-  sqlite3_free(zSql);
-  rc2 = sqlite3_finalize(pStmt);
-  return rc ? rc : rc2;
+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. */
     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;
 
     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);
     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) ){
         /* int iCid = sqlite3_column_int(pXInfo, 0); */
         const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
@@ -1119,6 +1116,25 @@ static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
   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){
   if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
     int tnum = pIter->iPkTnum;    /* Root page of PK index */
index 3ebd08f2b8c35f67e542efa735da9ce95fbc41a1..66b0b3d8291b488048e87757c595f6b60ad9f5c6 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sextra\stests\sand\sfixes\sfor\sota.
-D 2015-02-16T21:13:19.665
+C Improve\stest\scoverage\sof\sota\scode\sa\sbit.
+D 2015-02-17T20:49:42.756
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 6b9e7677829aa94b9f30949656e27312aefb9a46
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -135,8 +135,8 @@ F ext/ota/ota7.test 1fe2c5761705374530e29f70c39693076028221a
 F ext/ota/ota8.test cd70e63a0c29c45c0906692827deafa34638feda
 F ext/ota/ota9.test d3eee95dd836824d07a22e5efcdb7bf6e869358b
 F ext/ota/otaA.test ef4bfa8cfd4ed814ae86f7457b64aa2f18c90171
-F ext/ota/otafault.test fd3d4d9b9ed3cbe4461cde602e40f50825e0ee2a
-F ext/ota/sqlite3ota.c f378ebb435ce79bb5060907c3bc994c5b48cbb85
+F ext/ota/otafault.test 0c7565f69e5e379a5ebdcaea4056f0c69da1becf
+F ext/ota/sqlite3ota.c 0c6cb4cea1a9231bc488e9c84da201b796437af6
 F ext/ota/sqlite3ota.h 1cc7201086fe65a36957740381485a24738c4077
 F ext/ota/test_ota.c 5dd58e4e6eb3ae7b471566616d44b701971bce88
 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
@@ -1256,7 +1256,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 62dc1fffc38cb157c15105098749b6dd0198eb84
-R 26fc1fcf1b321dc2a4f4bbbaf0fa84e9
+P e0b7151962fedbcac975f2216fd6b33b995a8945
+R 13b826f0fe6f2960ec7719bf830cc36a
 U dan
-Z bb700675d8726f14df483f7f803a4d01
+Z 295fff9fbb7cdd54355b757b10496bab
index be38d8cb9a203a59c730a6c95044947503c08c67..3de9df01220d1ae443c101ff9f907f7beb922bf6 100644 (file)
@@ -1 +1 @@
-e0b7151962fedbcac975f2216fd6b33b995a8945
\ No newline at end of file
+a438fa6c9ad2fb1d78ac747172d07455d6381387
\ No newline at end of file