Index: ext/zonefile/zonefile.c ================================================================== --- ext/zonefile/zonefile.c +++ ext/zonefile/zonefile.c @@ -2311,26 +2311,25 @@ if( pTab->pIdToName==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pIdToName, &pTab->base.zErrMsg, "SELECT filename FROM %Q.'%q_shadow_file' WHERE fileid=?", pTab->zDb, pTab->zName ); - if( rc!=SQLITE_OK ){ - zonefileTransferError(pCtx); - return SQLITE_ERROR; - } - } - - iFileid = sqlite3_column_int64(pCsr->pSelect,1); - sqlite3_bind_int64(pTab->pIdToName, 1, iFileid); - if( SQLITE_ROW==sqlite3_step(pTab->pIdToName) ){ - *pzFile = (const char*)sqlite3_column_text(pTab->pIdToName, 0); - }else{ - rc = sqlite3_reset(pTab->pIdToName); - if( rc==SQLITE_OK ){ - rc = SQLITE_CORRUPT_VTAB; - }else{ - zonefileTransferError(pCtx); + if( rc==SQLITE_ERROR ) zonefileTransferError(pCtx); + } + + if( rc==SQLITE_OK ){ + iFileid = sqlite3_column_int64(pCsr->pSelect,1); + sqlite3_bind_int64(pTab->pIdToName, 1, iFileid); + if( SQLITE_ROW==sqlite3_step(pTab->pIdToName) ){ + *pzFile = (const char*)sqlite3_column_text(pTab->pIdToName, 0); + }else{ + rc = sqlite3_reset(pTab->pIdToName); + if( rc==SQLITE_OK ){ + rc = SQLITE_CORRUPT_VTAB; + }else{ + zonefileTransferError(pCtx); + } } } return rc; } Index: ext/zonefile/zonefile1.test ================================================================== --- ext/zonefile/zonefile1.test +++ ext/zonefile/zonefile1.test @@ -229,16 +229,20 @@ CREATE VIRTUAL TABLE cc USING zonefile; INSERT INTO cc_files(filename,ekey) VALUES('test.zonefile','0123456789'); SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {1 1 1} -do_execsql_test 3.2 { +do_execsql_test 3.2.1 { DELETE FROM cc_files; INSERT INTO cc_files(filename,ekey) VALUES('test.zonefile','abcdefghij'); SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {0 0 0} +do_execsql_test 3.2.2 { + SELECT rowid,sz FROM cc; +} {1000 44 1001 55 1002 66} + do_execsql_test 3.3 { UPDATE cc_files SET ekey = '0123456789'; SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {1 1 1} @@ -254,10 +258,15 @@ do_execsql_test 3.6 { SELECT ekey FROM cc_files } {{}} +forcedelete test.zonefile +do_catchsql_test 3.7 { + SELECT * FROM cc; +} {1 {failed to open file "test.zonefile" for reading}} + #------------------------------------------------------------------------- # Check that a file that uses an unknown compression method is handled # correctly (an error is returned). # reset_db @@ -557,17 +566,66 @@ 4 "k < 55" 5 "k LIKE '1%'" 6 "k BETWEEN 10 AND 20" 7 "k > 100 AND k < 200" } { - do_execsql_test 11.$tn.1 [subst { + do_execsql_test 11.1.$tn.1 [subst { SELECT count(*) FROM nm WHERE $cond }] [db one "SELECT count(*) FROM data WHERE $cond"] - do_execsql_test 11.$tn.2 [subst { + do_execsql_test 11.1.$tn.2 [subst { SELECT count(*) FROM nm WHERE $cond AND v!=(SELECT v FROM data WHERE k=nm.k); }] 0 } + +close [open test1.zonefile w+] +do_catchsql_test 11.2.1 { + SELECT * FROM nm WHERE k=24; +} {1 {SQL logic error}} +forcedelete test1.zonefile +do_catchsql_test 11.2.2 { + SELECT * FROM nm WHERE k=24; +} {1 {failed to open file "test1.zonefile" for reading}} + +do_catchsql_test 11.3.1 { + DELETE FROM nm_shadow_file; + SELECT * FROM nm WHERE k=24; +} {1 {database disk image is malformed}} +do_catchsql_test 11.3.2 { + DROP TABLE nm_shadow_file; + SELECT * FROM nm WHERE k=24; +} {1 {no such table: main.nm_shadow_file}} +db close +sqlite3 db test.db +load_static_extension db zonefile +do_catchsql_test 11.3.3 { + SELECT * FROM nm WHERE k=24; +} {1 {no such table: main.nm_shadow_file}} + +#------------------------------------------------------------------------- +# +reset_db +load_static_extension db zonefile +do_execsql_test 11.0 { + CREATE TABLE data(k INTEGER PRIMARY KEY, frame, idx, v BLOB); + INSERT INTO data VALUES(1, 1, -1, randomblob(200)); + INSERT INTO data VALUES(2, 2, -1, randomblob(200)); + INSERT INTO data VALUES(3, 3, -1, randomblob(200)); + SELECT zonefile_write('test.zonefile', 'data', + '{"encryptionType":"xor","encryptionKey":"pass"}' + ); + + CREATE VIRTUAL TABLE nm USING zonefile(cachesize=2); + INSERT INTO nm_files(filename,ekey) VALUES('test.zonefile','pass'); +} {{}} + +set i 0 +foreach id {1 2 3 2 3 1} { + do_execsql_test 11.1.$i { + SELECT data.v=nm.v FROM data,nm WHERE data.k=$id AND nm.k=$id + } 1 + incr i +} finish_test Index: ext/zonefile/zonefilefault.test ================================================================== --- ext/zonefile/zonefilefault.test +++ ext/zonefile/zonefilefault.test @@ -44,12 +44,16 @@ set sql { SELECT zonefile_write('test.zonefile', 'tt', '{"compressionTypeContent":"zstd_global_dict"}' ); } - +set HAVE_ZSTD 0 if {[catch { db eval $sql }]==0} { + set HAVE_ZSTD 1 +} + +if {$HAVE_ZSTD} { do_faultsim_test 1.2 -faults oom* -prep { sqlite3 db test.db load_static_extension db zonefile } -body { execsql { @@ -80,11 +84,11 @@ INSERT INTO tt VALUES(7, -1, -1, randomblob(100)); INSERT INTO tt VALUES(8, -1, -1, randomblob(100)); INSERT INTO tt VALUES(9, -1, -1, randomblob(100)); CREATE VIRTUAL TABLE ttz USING zonefile; } -if {[catch { db eval $sql }]==0} { +if {$HAVE_ZSTD} { faultsim_save_and_close do_faultsim_test 1.4 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { @@ -149,18 +153,90 @@ } -test { faultsim_test_result {0 {}} } #------------------------------------------------------------------------- +# reset_db faultsim_save_and_close -do_faultsim_test 3 -faults oom* -prep { +do_faultsim_test 3.1 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { CREATE VIRTUAL TABLE t1 USING zonefile(cachesize=5) } } -test { faultsim_test_result {0 {}} } + +faultsim_save_and_close +do_faultsim_test 3.2 -faults oom* -prep { + faultsim_restore_and_reopen + load_static_extension db zonefile +} -body { + execsql { DROP TABLE t1 } +} -test { + faultsim_test_result {0 {}} +} + +#------------------------------------------------------------------------- +# +reset_db +load_static_extension db zonefile +do_execsql_test 4.0 { + CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); + INSERT INTO zz VALUES(1, -1, -1, randomblob(100)); + INSERT INTO zz VALUES(2, -1, -1, randomblob(100)); + INSERT INTO zz VALUES(3, -1, -1, randomblob(100)); + SELECT zonefile_write('test.zonefile', 'zz'); + CREATE VIRTUAL TABLE zone USING zonefile; + INSERT INTO zone_files(filename) VALUES('test.zonefile'); +} {{}} + +faultsim_save_and_close +do_faultsim_test 4.1 -faults oom* -prep { + faultsim_restore_and_reopen + load_static_extension db zonefile +} -body { + execsql { SELECT v IS NULL FROM zone WHERE k = 2 } +} -test { + faultsim_test_result {0 0} +} + + +if {$HAVE_ZSTD} { + set params { + {"encryptionType":"xor","encryptionKey":"pass", + "compressionTypeContent":"zstd_global_dict" + } + } +} else { + set params { {"encryptionType":"xor","encryptionKey":"pass" } } +} +do_execsql_test 4.2 { + SELECT zonefile_write('test.zonefile', 'zz', $params); + CREATE VIRTUAL TABLE zone2 USING zonefile; + INSERT INTO zone2_files(filename,ekey) VALUES('test.zonefile','pass'); +} {{}} + +faultsim_save_and_close +do_faultsim_test 4.3 -faults oom* -prep { + faultsim_restore_and_reopen + load_static_extension db zonefile + execsql { UPDATE zone2_files SET ekey='pass' } +} -body { + execsql { SELECT v IS NULL FROM zone2 WHERE k = 2 } +} -test { + faultsim_test_result {0 0} +} + +#------------------------------------------------------------------------- +reset_db +do_faultsim_test 4.3 -faults oom* -prep { + faultsim_restore_and_reopen +} -body { + load_static_extension db zonefile +} -test { + faultsim_test_result {0 {}} {1 {initialization of zonefile failed: }} +} finish_test