SQLite4
Check-in [9638da92b6]
Not logged in

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

Overview
SHA1 Hash:9638da92b694b9876743d7633991516e64774094
Date: 2014-02-17 18:39:40
User: dan
Comment:Add IO error tests for the bt backend.
Tags And Properties
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to main.mk

225
226
227
228
229
230
231

232
233
234
235
236
237
238
  sqlite4.h


# Source code to the test files.
#
TESTSRC = \
  $(TOP)/test/test_auth.c \

  $(TOP)/test/test_config.c \
  $(TOP)/test/test_func.c \
  $(TOP)/test/test_hexio.c \
  $(TOP)/test/test_kv.c \
  $(TOP)/test/test_kv2.c \
  $(TOP)/test/test_lsm.c \
  $(TOP)/test/test_main.c \







>







225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  sqlite4.h


# Source code to the test files.
#
TESTSRC = \
  $(TOP)/test/test_auth.c \
  $(TOP)/test/test_bt.c \
  $(TOP)/test/test_config.c \
  $(TOP)/test/test_func.c \
  $(TOP)/test/test_hexio.c \
  $(TOP)/test/test_kv.c \
  $(TOP)/test/test_kv2.c \
  $(TOP)/test/test_lsm.c \
  $(TOP)/test/test_main.c \

Changes to src/bt_lock.c

158
159
160
161
162
163
164
165



166
167
168
169
170
171
172
173
174
175
...
332
333
334
335
336
337
338

339




340
341
342
343
344
345
346
...
432
433
434
435
436
437
438

439
440
441
442
443
444
445
      case BT_LOCK_SHARED:
        if( nExcl ){
          rc = SQLITE4_BUSY;
        }else{
          if( nShared==0 ){
            rc = btLockSharedFile(p, iLock, BT_LOCK_SHARED);
          }
          if( rc==SQLITE4_OK ){



            p->mSharedLock |= mask;
            p->mExclLock &= ~mask;
          }
        }
        break;

      default:
        assert( eOp==BT_LOCK_EXCL );
        if( nExcl || nShared ){
          rc = SQLITE4_BUSY;
................................................................................
        p->pFd = p->pBtFile->pFd;
      }else{
        p->pBtFile = (BtFile*)sqlite4_malloc(pEnv, sizeof(BtFile));
        if( p->pBtFile ){
          int flags = BT_OPEN_DATABASE;
          p->pBtFile->pNext = 0;
          rc = pVfs->xOpen(pEnv, pVfs, pShared->zName, flags, &p->pFd);

          p->pBtFile->pFd = p->pFd;




        }else{
          rc = btErrorBkpt(SQLITE4_NOMEM);
        }
      }
    }

    if( rc==SQLITE4_OK ){
................................................................................
  sqlite4_mutex_enter(pShared->pClientMutex);
  assert( p->pBtFile->pNext==0 );
  p->pBtFile->pNext = pShared->pBtFile;
  pShared->pBtFile = p->pBtFile;
  sqlite4_mutex_leave(pShared->pClientMutex);

  btLockSharedDeref(p->pEnv, p->pVfs, pShared);

  return rc;
}

#ifndef NDEBUG
static void assertNoLockedSlots(BtLock *pLock){
  u32 mask = (1 << (BT_LOCK_READER0+BT_NREADER+1)) - (1 << BT_LOCK_READER0);
  assert( (pLock->mExclLock & mask)==0 );







|
>
>
>
|
|
<







 







>
|
>
>
>
>







 







>







158
159
160
161
162
163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
      case BT_LOCK_SHARED:
        if( nExcl ){
          rc = SQLITE4_BUSY;
        }else{
          if( nShared==0 ){
            rc = btLockSharedFile(p, iLock, BT_LOCK_SHARED);
          }
          /* If no error occurred, set the bit in the mask of SHARED locks
          ** held. Either way, clear the bit in the mask of EXCLUSIVE locks.
          ** The idea here is that when there is any uncertainty as to whether
          ** a lock is held, the corresponding bit is cleared.  */
          if( rc==SQLITE4_OK ) p->mSharedLock |= mask;
          p->mExclLock &= ~mask;

        }
        break;

      default:
        assert( eOp==BT_LOCK_EXCL );
        if( nExcl || nShared ){
          rc = SQLITE4_BUSY;
................................................................................
        p->pFd = p->pBtFile->pFd;
      }else{
        p->pBtFile = (BtFile*)sqlite4_malloc(pEnv, sizeof(BtFile));
        if( p->pBtFile ){
          int flags = BT_OPEN_DATABASE;
          p->pBtFile->pNext = 0;
          rc = pVfs->xOpen(pEnv, pVfs, pShared->zName, flags, &p->pFd);
          if( rc==SQLITE4_OK ){
            p->pBtFile->pFd = p->pFd;
          }else{
            sqlite4_free(pEnv, p->pBtFile);
            p->pBtFile = 0;
          }
        }else{
          rc = btErrorBkpt(SQLITE4_NOMEM);
        }
      }
    }

    if( rc==SQLITE4_OK ){
................................................................................
  sqlite4_mutex_enter(pShared->pClientMutex);
  assert( p->pBtFile->pNext==0 );
  p->pBtFile->pNext = pShared->pBtFile;
  pShared->pBtFile = p->pBtFile;
  sqlite4_mutex_leave(pShared->pClientMutex);

  btLockSharedDeref(p->pEnv, p->pVfs, pShared);
  p->pShared = 0;
  return rc;
}

#ifndef NDEBUG
static void assertNoLockedSlots(BtLock *pLock){
  u32 mask = (1 << (BT_LOCK_READER0+BT_NREADER+1)) - (1 << BT_LOCK_READER0);
  assert( (pLock->mExclLock & mask)==0 );

Changes to src/bt_pager.c

494
495
496
497
498
499
500
501





502
503
504
505
506
507
508
    p->nFile = strlen(p->zFile);
    rc = sqlite4BtLockConnect((BtLock*)p, btRecover);
    if( rc==SQLITE4_OK && p->pLog==0 ){
      rc = sqlite4BtLogOpen(p, 0, &p->pLog);
    }
  }

  assert( (rc==SQLITE4_OK)==(p->btl.pFd!=0 && p->pLog!=0) );





  return rc;
}

/*
** Open a read-transaction.
*/
static int btOpenReadTransaction(BtPager *p){







|
>
>
>
>
>







494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
    p->nFile = strlen(p->zFile);
    rc = sqlite4BtLockConnect((BtLock*)p, btRecover);
    if( rc==SQLITE4_OK && p->pLog==0 ){
      rc = sqlite4BtLogOpen(p, 0, &p->pLog);
    }
  }

  if( rc!=SQLITE4_OK ){
    sqlite4BtLockDisconnect((BtLock*)p, btCheckpoint, btCleanup);
    sqlite4BtLogClose(p->pLog, 0);
    p->pLog = 0;
  }

  return rc;
}

/*
** Open a read-transaction.
*/
static int btOpenReadTransaction(BtPager *p){

Changes to test/fault1.test

11
12
13
14
15
16
17

18
19




20






21






22

23
24

25
26
27
28

#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl
set ::testprefix fault1



execsql { SELECT *  FROM  sqlite_master }











do_faultsim_test 1.0 -faults oom* -body {






  execsql { SELECT * FROM sqlite_master }

} -test {
  faultsim_test_result {0 {}} 

}

finish_test









>

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

|
>




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

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl
set ::testprefix fault1

db close


proc open_test_db {} { 
  sqlite4 db test.db
  btenv testenv
  testenv attach db
}

proc close_test_db {} { 
  db close
  testenv delete
}

do_faultsim_test 2.0 -prep {
  forcedelete test.db test.db-wal
  open_test_db
} -body {
  execsql {
    CREATE TABLE t1(x,y);
    INSERT INTO t1 VALUES('abc', 'def');
    SELECT * FROM t1;
  }
} -test {
  faultsim_test_result {0 {abc def}}
  close_test_db
}

finish_test


Changes to test/malloc_common.tcl

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
...
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
]
  
# Transient and persistent IO errors:
#
set FAULTSIM(ioerr-transient) [list        \
  -injectstart   {ioerr_injectstart 0}     \
  -injectstop    ioerr_injectstop          \
  -injecterrlist {{1 {disk I/O error}}}    \
]
set FAULTSIM(ioerr-persistent) [list       \
  -injectstart   {ioerr_injectstart 1}     \
  -injectstop    ioerr_injectstop          \
  -injecterrlist {{1 {disk I/O error}}}    \
]

# SQLITE4_FULL errors (always persistent):
#
set FAULTSIM(full) [list                   \
  -injectinstall   fullerr_injectinstall   \
  -injectstart     fullerr_injectstart     \
  -injectstop      fullerr_injectstop      \
  -injecterrlist   {{1 {database or disk is full}}} \
  -injectuninstall fullerr_injectuninstall \
]

# Transient and persistent SHM errors:
#
set FAULTSIM(shmerr-transient) [list       \
  -injectinstall   shmerr_injectinstall    \
  -injectstart     {shmerr_injectstart 0}  \
  -injectstop      shmerr_injectstop       \
  -injecterrlist   {{1 {disk I/O error}}}  \
  -injectuninstall shmerr_injectuninstall  \
]
set FAULTSIM(shmerr-persistent) [list      \
  -injectinstall   shmerr_injectinstall    \
  -injectstart     {shmerr_injectstart 1}  \
  -injectstop      shmerr_injectstop       \
  -injecterrlist   {{1 {disk I/O error}}}  \
  -injectuninstall shmerr_injectuninstall  \
]

# Transient and persistent CANTOPEN errors:
#
set FAULTSIM(cantopen-transient) [list       \
  -injectinstall   cantopen_injectinstall    \
  -injectstart     {cantopen_injectstart 0}  \
  -injectstop      cantopen_injectstop       \
  -injecterrlist   {{1 {unable to open database file}}}  \
  -injectuninstall cantopen_injectuninstall  \
]
set FAULTSIM(cantopen-persistent) [list      \
  -injectinstall   cantopen_injectinstall    \
  -injectstart     {cantopen_injectstart 1}  \
  -injectstop      cantopen_injectstop       \
  -injecterrlist   {{1 {unable to open database file}}}  \
  -injectuninstall cantopen_injectuninstall  \
]



#--------------------------------------------------------------------------
# Usage do_faultsim_test NAME ?OPTIONS...? 
#
................................................................................
  return $nFault
}

# The following procs are used as [do_one_faultsim_test] callbacks when 
# injecting IO error faults into test cases.
#
proc ioerr_injectstart {persist iFail} {
  set ::sqlite_io_error_persist $persist
  set ::sqlite_io_error_pending $iFail
}
proc ioerr_injectstop {} {
  set sv $::sqlite_io_error_hit
  set ::sqlite_io_error_persist 0
  set ::sqlite_io_error_pending 0
  set ::sqlite_io_error_hardhit 0
  set ::sqlite_io_error_hit     0
  set ::sqlite_io_error_pending 0
  return $sv

}

# The following procs are used as [do_one_faultsim_test] callbacks when 
# injecting shared-memory related error faults into test cases.
#
proc shmerr_injectinstall {} {
  testvfs shmfault -default true







|




|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
<


<
<
<
<
<
<
<
>







37
38
39
40
41
42
43
44
45
46
47
48
49












































50
51
52
53
54
55
56
...
139
140
141
142
143
144
145
146

147
148







149
150
151
152
153
154
155
156
]
  
# Transient and persistent IO errors:
#
set FAULTSIM(ioerr-transient) [list        \
  -injectstart   {ioerr_injectstart 0}     \
  -injectstop    ioerr_injectstop          \
  -injecterrlist {{1 {disk I/O error}} {1 {unable to open database file}}} \
]
set FAULTSIM(ioerr-persistent) [list       \
  -injectstart   {ioerr_injectstart 1}     \
  -injectstop    ioerr_injectstop          \
  -injecterrlist {{1 {disk I/O error}} {1 {unable to open database file}}} \












































]



#--------------------------------------------------------------------------
# Usage do_faultsim_test NAME ?OPTIONS...? 
#
................................................................................
  return $nFault
}

# The following procs are used as [do_one_faultsim_test] callbacks when 
# injecting IO error faults into test cases.
#
proc ioerr_injectstart {persist iFail} {
  testenv ioerr $iFail $persist

}
proc ioerr_injectstop {} {







  testenv ioerr 0 0
}

# The following procs are used as [do_one_faultsim_test] callbacks when 
# injecting shared-memory related error faults into test cases.
#
proc shmerr_injectinstall {} {
  testvfs shmfault -default true

Changes to test/select1.test

12
13
14
15
16
17
18



19
20
21
22
23
24
25
# focus of this file is testing the SELECT statement.
#
# $Id: select1.test,v 1.70 2009/05/28 01:00:56 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl




# Try to select on a non-existant table.
#
do_test select1-1.1 {
  set v [catch {execsql {SELECT * FROM test1}} msg]
  lappend v $msg
} {1 {no such table: test1}}








>
>
>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# focus of this file is testing the SELECT statement.
#
# $Id: select1.test,v 1.70 2009/05/28 01:00:56 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

btenv BT
BT attach db

# Try to select on a non-existant table.
#
do_test select1-1.1 {
  set v [catch {execsql {SELECT * FROM test1}} msg]
  lappend v $msg
} {1 {no such table: test1}}

Changes to test/testInt.h

69
70
71
72
73
74
75



76
77

sqlite4_mm *test_mm_debug(sqlite4_mm *p);
sqlite4_mm *test_mm_faultsim(sqlite4_mm *p);

/* test_num.c */
int Sqlitetest_num_init(Tcl_Interp *interp);




#endif








>
>
>


69
70
71
72
73
74
75
76
77
78
79
80

sqlite4_mm *test_mm_debug(sqlite4_mm *p);
sqlite4_mm *test_mm_faultsim(sqlite4_mm *p);

/* test_num.c */
int Sqlitetest_num_init(Tcl_Interp *interp);

/* test_bt.c */
int SqlitetestBt_Init(Tcl_Interp *interp);

#endif

Added test/test_bt.c























































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
** 2014 February 17
**
** 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.
**
*************************************************************************
*/

#include <tcl.h>
#include "bt.h"
#include "sqlite4.h"
#include "testInt.h"
#include <assert.h>
#include <string.h>

typedef sqlite4_int64 i64;


typedef struct BtTestEnv BtTestEnv;
struct BtTestEnv {
  bt_env base;                    /* The wrapper environment */
  sqlite4_env *pSqlEnv;           /* SQLite environment */
  bt_env *pEnv;                   /* Underlying environment object */

  /* IO error injection parameters */
  int nIoerrCnt;                  /* Error countdown */
  int bIoerrPersist;              /* True for persistent errors */
  int nIoerrInjected;             /* Number of errors injected */
};

typedef struct BtTestFile BtTestFile;
struct BtTestFile {
  BtTestEnv *pTestEnv;            /* Test environment */
  bt_file *pFile;                 /* Underlying file object */
};

/*
** Return true if an IO error should be injected. False otherwise.
*/
static int testInjectIoerr(BtTestEnv *p){
  if( (p->nIoerrCnt==0 && p->bIoerrPersist)
   || (p->nIoerrCnt>0 && (--p->nIoerrCnt)==0)
  ){
    p->nIoerrInjected++;
    return 1;
  }
  return 0;
}

/*
** The bt_env.xOpen method.
*/
static int testBtOsOpen(
  sqlite4_env *pSqlEnv,
  bt_env *pEnv,
  const char *zFile,
  int flags,
  bt_file **ppFile
){
  BtTestEnv *p = (BtTestEnv*)pEnv;
  BtTestFile *pNew = 0;
  int rc;

  if( testInjectIoerr(p) ){
    rc = SQLITE4_CANTOPEN;
  }else{
    pNew = ckalloc(sizeof(BtTestFile));
    memset(pNew, 0, sizeof(BtTestFile));
    pNew->pTestEnv = p;
    rc = p->pEnv->xOpen(pSqlEnv, p->pEnv, zFile, flags, &pNew->pFile);
    if( rc!=SQLITE4_OK ){
      ckfree(pNew);
      pNew = 0;
    }
  }
  *ppFile = (bt_file*)pNew;
  return rc;
}

static int testBtOsSize(bt_file *pFile, i64 *pnByte){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_FSTAT;
  }else{
    rc = pEnv->xSize(pTestFile->pFile, pnByte);
  }
  return rc;
}

static int testBtOsWrite(
  bt_file *pFile,                 /* File to write to */
  i64 iOff,                       /* Offset to write to */
  void *pData,                    /* Write data from this buffer */
  int nData                       /* Bytes of data to write */
){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_WRITE;
  }else{
    rc = pEnv->xWrite(pTestFile->pFile, iOff, pData, nData);
  }
  return rc;
}

static int testBtOsTruncate(
  bt_file *pFile,                 /* File to write to */
  i64 nSize                       /* Size to truncate file to */
){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_TRUNCATE;
  }else{
    rc = pEnv->xTruncate(pTestFile->pFile, nSize);
  }
  return rc;
}

static int testBtOsRead(
  bt_file *pFile,                 /* File to read from */
  i64 iOff,                       /* Offset to read from */
  void *pData,                    /* Read data into this buffer */
  int nData                       /* Bytes of data to read */
){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_READ;
  }else{
    rc = pEnv->xRead(pTestFile->pFile, iOff, pData, nData);
  }
  return rc;
}

static int testBtOsSync(bt_file *pFile){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_FSYNC;
  }else{
    rc = pEnv->xSync(pTestFile->pFile);
  }
  return rc;
}

static int testBtOsSectorSize(bt_file *pFile){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int res;

  res = pEnv->xSectorSize(pTestFile->pFile);
  return res;
}

static int testBtOsFullpath(
  sqlite4_env *pSqlEnv,
  bt_env *pEnv,
  const char *zName,
  char **pzOut
){
  BtTestEnv *p = (BtTestEnv*)pEnv;
  int rc;

  if( testInjectIoerr(p) ){
    rc = SQLITE4_IOERR;
  }else{
    rc = p->pEnv->xFullpath(pSqlEnv, p->pEnv, zName, pzOut);
  }
  return rc;
}

static int testBtOsUnlink(
  sqlite4_env *pSqlEnv, 
  bt_env *pEnv, 
  const char *zFile
){
  BtTestEnv *p = (BtTestEnv*)pEnv;
  int rc;

  if( testInjectIoerr(p) ){
    rc = SQLITE4_IOERR_DIR_FSYNC;
  }else{
    rc = p->pEnv->xUnlink(pSqlEnv, p->pEnv, zFile);
  }
  return rc;
}

int testBtOsLock(bt_file *pFile, int iLock, int eType){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_LOCK;
  }else{
    rc = pEnv->xLock(pTestFile->pFile, iLock, eType);
  }
  return rc;
}

int testBtOsTestLock(bt_file *pFile, int iLock, int nLock, int eType){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR;
  }else{
    rc = pEnv->xTestLock(pTestFile->pFile, iLock, nLock, eType);
  }
  return rc;
}

int testBtOsShmMap(bt_file *pFile, int iChunk, int sz, void **ppShm){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  if( testInjectIoerr(pTestFile->pTestEnv) ){
    rc = SQLITE4_IOERR_SHMMAP;
  }else{
    rc = pEnv->xShmMap(pTestFile->pFile, iChunk, sz, ppShm);
  }
  return rc;
}

void testBtOsShmBarrier(bt_file *pFile){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;

  pEnv->xShmBarrier(pTestFile->pFile);
}

int testBtOsShmUnmap(bt_file *pFile, int bDelete){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  rc = pEnv->xShmUnmap(pTestFile->pFile, bDelete);
  return rc;
}

static int testBtOsClose(bt_file *pFile){
  BtTestFile *pTestFile = (BtTestFile*)pFile;
  bt_env *pEnv = pTestFile->pTestEnv->pEnv;
  int rc;

  rc = pEnv->xClose(pTestFile->pFile);
  ckfree(pTestFile);
  return rc;
}

/*
** Destructor for object created by tcl [btenv] command.
*/
static void test_btenv_del(void *ctx){
  BtTestEnv *p = (BtTestEnv*)ctx;
  ckfree(p);
}

/*
** Tcl command: BTENV method ...
*/
static int test_btenv_cmd(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  BtTestEnv *p = (BtTestEnv*)clientData;

  enum BtenvCmdSymbol {
    BTC_ATTACH,
    BTC_DELETE,
    BTC_IOERR,
  };
  struct BtenvCmd {
    const char *zOpt;
    int eOpt;
    int nArg;
    const char *zErr;
  } aCmd[] = {
    { "attach", BTC_ATTACH, 3, "DBCMD" },
    { "delete", BTC_DELETE, 2, "" },
    { "ioerr",  BTC_IOERR,  4, "COUNT PERSISTENT" },
    { 0, 0 }
  };
  int rc = TCL_OK;
  int iOpt;

  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "sub-command ...");
    return TCL_ERROR;
  }
  rc = Tcl_GetIndexFromObjStruct(
      interp, objv[1], aCmd, sizeof(aCmd[0]), "sub-command", 0, &iOpt
  );
  if( rc!=TCL_OK ) return rc;

  if( objc!=aCmd[iOpt].nArg ){
    Tcl_WrongNumArgs(interp, 2, objv, aCmd[iOpt].zErr);
    return TCL_ERROR;
  }

  switch( aCmd[iOpt].eOpt ){
    case BTC_ATTACH: {
      if( p->pEnv ){
        Tcl_AppendResult(interp, "object has already been attached to db", 0);
        rc = TCL_ERROR;
      }else{
        sqlite4 *db = 0;
        rc = sqlite4TestDbHandle(interp, objv[2], &db);
        if( rc==TCL_OK ){
          void *pArg = (void*)(&p->pEnv);
          rc = sqlite4_kvstore_control(db, "main", BT_CONTROL_GETVFS, pArg);
          if( rc==SQLITE4_NOTFOUND ){
            Tcl_AppendResult(interp, "not a bt database", 0);
            rc = TCL_ERROR;
          }else{
            sqlite4_kvstore_control(db, "main", BT_CONTROL_SETVFS, (void*)p);
          }
        }
      }
      break;
    }

    case BTC_DELETE: {
      Tcl_DeleteCommand(interp, Tcl_GetString(objv[0]));
      break;
    }

    case BTC_IOERR: {
      int ret = p->nIoerrInjected;
      if( Tcl_GetIntFromObj(interp, objv[2], &p->nIoerrCnt)
       || Tcl_GetBooleanFromObj(interp, objv[3], &p->bIoerrPersist)
      ){
        rc = TCL_ERROR;
      }else{
        Tcl_SetObjResult(interp, Tcl_NewIntObj(ret));
      }
      
      break;
    }

    default:
      assert( 0 );
      break;
  }

  return rc;
}

/*
** Tcl command: btenv NAME
*/
static int test_btenv(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  BtTestEnv *p;
  const char *zName;

  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "NAME");
    return TCL_ERROR;
  }
  zName = Tcl_GetString(objv[1]);

  p = ckalloc(sizeof(BtTestEnv));
  memset(p, 0, sizeof(BtTestEnv));

  p->base.xFullpath = testBtOsFullpath;
  p->base.xOpen = testBtOsOpen;
  p->base.xSize = testBtOsSize;
  p->base.xRead = testBtOsRead;
  p->base.xWrite = testBtOsWrite;
  p->base.xTruncate = testBtOsTruncate;
  p->base.xSync = testBtOsSync;
  p->base.xSectorSize = testBtOsSectorSize;
  p->base.xClose = testBtOsClose;
  p->base.xUnlink = testBtOsUnlink;
  p->base.xLock = testBtOsLock;
  p->base.xTestLock = testBtOsTestLock;
  p->base.xShmMap = testBtOsShmMap;
  p->base.xShmBarrier = testBtOsShmBarrier;
  p->base.xShmUnmap = testBtOsShmUnmap;

  Tcl_CreateObjCommand(interp, zName, test_btenv_cmd, (void*)p, test_btenv_del);
  Tcl_SetObjResult(interp, Tcl_NewStringObj(zName, -1));
  return TCL_OK;
}

int SqlitetestBt_Init(Tcl_Interp *interp){
  struct SyscallCmd {
    const char *zName;
    Tcl_ObjCmdProc *xCmd;
  } aCmd[] = {
    { "btenv",                  test_btenv },
  };
  int i;

  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xCmd, 0, 0);
  }
  return TCL_OK;
}


Changes to test/test_main.c

4147
4148
4149
4150
4151
4152
4153

4154
4155
4156
4157
4158
4159
4160
  return TCL_OK;
}

void sqlite4TestInit(Tcl_Interp *interp){
  Sqlitetest_auth_init(interp);
  Sqlitetest_num_init(interp);
  Sqlitetest_func_Init(interp);

}

/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest1_Init(Tcl_Interp *interp){
  extern int sqlite4_search_count;







>







4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
  return TCL_OK;
}

void sqlite4TestInit(Tcl_Interp *interp){
  Sqlitetest_auth_init(interp);
  Sqlitetest_num_init(interp);
  Sqlitetest_func_Init(interp);
  SqlitetestBt_Init(interp);
}

/*
** Register commands with the TCL interpreter.
*/
int Sqlitetest1_Init(Tcl_Interp *interp){
  extern int sqlite4_search_count;