/ Check-in [1764ade2]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Test edge cases in the zonefile module. Fix a broken error message in the same.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | zonefile
Files: files | file ages | folders
SHA3-256: 1764ade22b52eba0226ae2e6e837a1b0967023eabd7d50e9f87c5e7042ea2f12
User & Date: dan 2018-02-24 08:26:21
Context
2018-02-26
07:58
Add extra parameter to zonefileCodecCreate() to indicate whether the new object will be used for mock-encryption or mock-decryption. check-in: 231832c4 user: dan tags: zonefile
2018-02-24
08:26
Test edge cases in the zonefile module. Fix a broken error message in the same. check-in: 1764ade2 user: dan tags: zonefile
2018-02-23
21:01
Fix a problem with handling "k >= ?" constraints in the zonefile module. check-in: 9a99afaf user: dan tags: zonefile
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/zonefile/zonefile.c.

2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321

2322
2323
2324
2325
2326
2327
2328
2329
2330
2331

2332
2333
2334
2335
2336
2337
2338
  i64 iFileid;

  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);

    }
  }

  return rc;
}

static void zonefileReleaseFile(ZonefileCsr *pCsr){







<
|
<
|
|
<
>
|
|
|
|
|
|
|
|
|
|
>







2309
2310
2311
2312
2313
2314
2315

2316

2317
2318

2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
  i64 iFileid;

  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_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;
}

static void zonefileReleaseFile(ZonefileCsr *pCsr){

Changes to ext/zonefile/zonefile1.test.

227
228
229
230
231
232
233
234
235
236
237
238
239




240
241
242
243
244
245
246
...
252
253
254
255
256
257
258





259
260
261
262
263
264
265
...
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

















































572
573

do_execsql_test 3.1 {
  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 {
  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.3 {
  UPDATE cc_files SET ekey = '0123456789';
  SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k)
} {1 1 1}

close [open test.zonefile w+]
do_catchsql_test 3.4 {
................................................................................
  SELECT header FROM cc_files
} {1 {failed to open file "test.zonefile" for reading}}

do_execsql_test 3.6 {
  SELECT ekey FROM cc_files
} {{}}






#-------------------------------------------------------------------------
# Check that a file that uses an unknown compression method is handled
# correctly (an error is returned).
#
reset_db
load_static_extension db zonefile
do_execsql_test 4.0 {
................................................................................
  2   "k >= 100"
  3   "k <= 100"
  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 {
    SELECT count(*) FROM nm WHERE $cond
  }] [db one "SELECT count(*) FROM data WHERE $cond"]

  do_execsql_test 11.$tn.2 [subst {
    SELECT count(*) FROM nm WHERE $cond AND
      v!=(SELECT v FROM data WHERE k=nm.k);
  }] 0
}


















































finish_test








|





>
>
>
>







 







>
>
>
>
>







 







|



|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
...
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

do_execsql_test 3.1 {
  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.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}

close [open test.zonefile w+]
do_catchsql_test 3.4 {
................................................................................
  SELECT header FROM cc_files
} {1 {failed to open file "test.zonefile" for reading}}

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
load_static_extension db zonefile
do_execsql_test 4.0 {
................................................................................
  2   "k >= 100"
  3   "k <= 100"
  4   "k < 55"
  5   "k LIKE '1%'"
  6   "k BETWEEN 10 AND 20"
  7   "k > 100 AND k < 200"
} {
  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.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

Changes to ext/zonefile/zonefilefault.test.

42
43
44
45
46
47
48
49

50




51
52
53
54
55
56
57
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164







































































165
166
}

set sql {
    SELECT zonefile_write('test.zonefile', 'tt',
        '{"compressionTypeContent":"zstd_global_dict"}'
    );
}


if {[catch { db eval $sql }]==0} {




  do_faultsim_test 1.2 -faults oom* -prep {
    sqlite3 db test.db
    load_static_extension db zonefile
  } -body {
    execsql {
      SELECT zonefile_write('test.zonefile', 'tt',
          '{"compressionTypeContent":"zstd_global_dict"}'
................................................................................
  INSERT INTO tt VALUES(5, -1, -1, randomblob(100));
  INSERT INTO tt VALUES(6, -1, -1, randomblob(100));
  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} {
  faultsim_save_and_close
  do_faultsim_test 1.4 -faults oom* -prep {
    faultsim_restore_and_reopen
    load_static_extension db zonefile
  } -body {
    execsql {
      SELECT zonefile_write('test.zonefile', 'tt',
................................................................................
} -body {
  execsql { DELETE FROM zz_files }
} -test {
  faultsim_test_result {0 {}}
}

#-------------------------------------------------------------------------

reset_db
faultsim_save_and_close
do_faultsim_test 3 -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 {}}
}








































































finish_test








<
>

>
>
>
>







 







|







 







>


|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


42
43
44
45
46
47
48

49
50
51
52
53
54
55
56
57
58
59
60
61
..
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}

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 {
      SELECT zonefile_write('test.zonefile', 'tt',
          '{"compressionTypeContent":"zstd_global_dict"}'
................................................................................
  INSERT INTO tt VALUES(5, -1, -1, randomblob(100));
  INSERT INTO tt VALUES(6, -1, -1, randomblob(100));
  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 {$HAVE_ZSTD} {
  faultsim_save_and_close
  do_faultsim_test 1.4 -faults oom* -prep {
    faultsim_restore_and_reopen
    load_static_extension db zonefile
  } -body {
    execsql {
      SELECT zonefile_write('test.zonefile', 'tt',
................................................................................
} -body {
  execsql { DELETE FROM zz_files }
} -test {
  faultsim_test_result {0 {}}
}

#-------------------------------------------------------------------------
#
reset_db
faultsim_save_and_close
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