]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add test infrastructure (and some tests) to inject faults into os_unix.c using the...
authordan <dan@noemail.net>
Mon, 28 Mar 2011 19:10:06 +0000 (19:10 +0000)
committerdan <dan@noemail.net>
Mon, 28 Mar 2011 19:10:06 +0000 (19:10 +0000)
FossilOrigin-Name: 0e1d20dfaeea5ab285d16aa4b8f02b61b1d2bc10

main.mk
manifest
manifest.uuid
src/tclsqlite.c
src/test_syscall.c [new file with mode: 0644]
test/malloc_common.tcl
test/sysfault.test [new file with mode: 0644]

diff --git a/main.mk b/main.mk
index 29374c546fb2e155f49aed3c87c8d024a4aaff9b..4cebf846ef46fbf469a930d2ebe52e71512c2af6 100644 (file)
--- a/main.mk
+++ b/main.mk
@@ -253,6 +253,7 @@ TESTSRC = \
   $(TOP)/src/test_server.c \
   $(TOP)/src/test_stat.c \
   $(TOP)/src/test_superlock.c \
+  $(TOP)/src/test_syscall.c \
   $(TOP)/src/test_tclvar.c \
   $(TOP)/src/test_thread.c \
   $(TOP)/src/test_vfs.c \
index d485703eda0838611bc770b8ce111448953dec40..60cb55d88b301139b76e21175e1ba1eaa4551b63 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Minor\schange\sto\ssqlite3Utf8Read()\sto\smake\sconsistent\swith\sREAD_UTF8()\susage\sand\savoid\simplementation\sdefined\susages\sof\s<<.\s\s\nAdded\ssome\sadditional\sUTF-8\stest\scases.
-D 2011-03-24T17:43:18.990
+C Add\stest\sinfrastructure\s(and\ssome\stests)\sto\sinject\sfaults\sinto\sos_unix.c\susing\sthe\snew\sxSetSystemCall\sinterface.
+D 2011-03-28T19:10:07
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 27701a1653595a1f2187dc61c8117e00a6c1d50f
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -101,7 +101,7 @@ F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de
 F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024
 F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895
 F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8
-F main.mk 54190fab7cdba523e311c274c95ea480f32abfb5
+F main.mk 7e4d4d0433c9cbfd906c6451a7cc50310a8f4555
 F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a
 F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f
 F mkextw.sh 4123480947681d9b434a5e7b1ee08135abe409ac
@@ -184,7 +184,7 @@ F src/sqliteInt.h f8f1d00a22c98fd3f2fbc94da74eeb880879f89f
 F src/sqliteLimit.h a17dcd3fb775d63b64a43a55c54cb282f9726f44
 F src/status.c 4997380fbb915426fef9e500b4872e79c99267fc
 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
-F src/tclsqlite.c 879bf8a23d99fc0e99d9177fe1b48896bc796d65
+F src/tclsqlite.c b020ebf3b4af58cae7875e217efd7ac22f485713
 F src/test1.c 9020310c7617234b33fd1c3064f89524db25f290
 F src/test2.c 80d323d11e909cf0eb1b6fbb4ac22276483bcf31
 F src/test3.c 056093cfef69ff4227a6bdb9108564dc7f45e4bc
@@ -220,6 +220,7 @@ F src/test_schema.c 8c06ef9ddb240c7a0fcd31bc221a6a2aade58bf0
 F src/test_server.c bbba05c144b5fc4b52ff650a4328027b3fa5fcc6
 F src/test_stat.c f682704b5d1ba8e1d4e7e882a6d7922e2dcf066c
 F src/test_superlock.c 2b97936ca127d13962c3605dbc9a4ef269c424cd
+F src/test_syscall.c 0f5131994a813d75a9e63746b08fc940bd272bdc
 F src/test_tclvar.c f4dc67d5f780707210d6bb0eb6016a431c04c7fa
 F src/test_thread.c bedd05cad673dba53326f3aa468cc803038896c0
 F src/test_vfs.c 2ed8853c1e51ac6f9ea091f7ce4e0d618bba8b86
@@ -559,7 +560,7 @@ F test/mallocH.test 79b65aed612c9b3ed2dcdaa727c85895fd1bfbdb
 F test/mallocI.test a88c2b9627c8506bf4703d8397420043a786cdb6
 F test/mallocJ.test b5d1839da331d96223e5f458856f8ffe1366f62e
 F test/mallocK.test d79968641d1b70d88f6c01bdb9a7eb4a55582cc9
-F test/malloc_common.tcl 660b82ab528521cc4a48ff6df05ca3b6a00d47c5
+F test/malloc_common.tcl 50d0ed21eed0ae9548b58935bd29ac89a05a54fa
 F test/manydb.test b3d3bc4c25657e7f68d157f031eb4db7b3df0d3c
 F test/mem5.test c6460fba403c5703141348cd90de1c294188c68f
 F test/memdb.test 0825155b2290e900264daaaf0334b6dfe69ea498
@@ -671,6 +672,7 @@ F test/subselect.test d24fd8757daf97dafd2e889c73ea4c4272dcf4e4
 F test/substr.test 18f57c4ca8a598805c4d64e304c418734d843c1a
 F test/superlock.test 5d7a4954b0059c903f82c7b67867bc5451a7c082
 F test/sync.test ded6b39d8d8ca3c0c5518516c6371b3316d3e3a3
+F test/sysfault.test 63144f0000167f12676adccee2a0db45eb5c2314
 F test/table.test 04ba066432430657712d167ebf28080fe878d305
 F test/tableapi.test 2674633fa95d80da917571ebdd759a14d9819126
 F test/tclsqlite.test 8c154101e704170c2be10f137a5499ac2c6da8d3
@@ -916,7 +918,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 69fe0c873d702ef1d781453ee6ac2b1fb77fce48
-R 3655844fccb71eb3fdafa07a1f475145
-U shaneh
-Z 4c379d73c236b4115e9dab5017415240
+P 7173b3929fae4e678223b0e978a2da7fa50a9005
+R cf394f283c387056ed9e49d666ce9c76
+U dan
+Z 649f6859769bccf6e81a35338176b4bb
index 44925bdb50559344411275ec29f2403bfaf61514..afecceee565fd8aac9922b1db5018aa93fe1f317 100644 (file)
@@ -1 +1 @@
-7173b3929fae4e678223b0e978a2da7fa50a9005
\ No newline at end of file
+0e1d20dfaeea5ab285d16aa4b8f02b61b1d2bc10
\ No newline at end of file
index 57f38d78d165b1966fad76a5d993607b7416f3e1..88bc8bb5dde9aa1aadf591d5726b43a7576fb393 100644 (file)
@@ -3581,6 +3581,7 @@ static void init_all(Tcl_Interp *interp){
     extern int Sqlitequota_Init(Tcl_Interp*);
     extern int Sqlitemultiplex_Init(Tcl_Interp*);
     extern int SqliteSuperlock_Init(Tcl_Interp*);
+    extern int SqlitetestSyscall_Init(Tcl_Interp*);
 
 #ifdef SQLITE_ENABLE_ZIPVFS
     extern int Zipvfs_Init(Tcl_Interp*);
@@ -3618,6 +3619,7 @@ static void init_all(Tcl_Interp *interp){
     Sqlitequota_Init(interp);
     Sqlitemultiplex_Init(interp);
     SqliteSuperlock_Init(interp);
+    SqlitetestSyscall_Init(interp);
 
     Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);
 
diff --git a/src/test_syscall.c b/src/test_syscall.c
new file mode 100644 (file)
index 0000000..ba70f64
--- /dev/null
@@ -0,0 +1,523 @@
+/*
+** 2011 March 28
+**
+** 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.
+**
+*************************************************************************
+**
+** The code in this file implements a Tcl interface used to test error
+** handling in the os_unix.c module. Wrapper functions that support fault
+** injection are registered as the low-level OS functions using the 
+** xSetSystemCall() method of the VFS. The Tcl interface is as follows:
+**
+**
+**   test_syscall install LIST
+**     Install wrapper functions for all system calls in argument LIST.
+**     LIST must be a list consisting of zero or more of the following
+**     literal values:
+**
+**         open        close      access   getcwd   stat      fstat    
+**         ftruncate   fcntl      read     pread    pread64   write
+**         pwrite      pwrite64   fchmod   fallocate
+**
+**   test_syscall uninstall
+**     Uninstall all wrapper functions.
+**
+**   test_syscall fault ?COUNT PERSIST?
+**     If [test_syscall fault] is invoked without the two arguments, fault
+**     injection is disabled. Otherwise, fault injection is configured to
+**     cause a failure on the COUNT'th next call to a system call with a
+**     wrapper function installed. A COUNT value of 1 means fail the next
+**     system call. 
+** 
+**     Argument PERSIST is interpreted as a boolean. If true, the all
+**     system calls following the initial failure also fail. Otherwise, only
+**     the single transient failure is injected.
+**
+**   test_syscall errno CALL ERRNO
+**     Set the value that the global "errno" is set to following a fault
+**     in call CALL. Argument CALL must be one of the system call names
+**     listed above (under [test_syscall install]). ERRNO is a symbolic
+**     name (i.e. "EACCES"). Not all errno codes are supported. Add extra
+**     to the aErrno table in function test_syscall_errno() below as 
+**     required.
+*/
+
+#include "sqlite3.h"
+#include "tcl.h"
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#ifdef SQLITE_OS_UNIX
+
+#include <sys/types.h>
+#include <errno.h>
+
+static struct TestSyscallGlobal {
+  int bPersist;                   /* 1 for persistent errors, 0 for transient */
+  int nCount;                     /* Fail after this many more calls */
+  int nFail;                      /* Number of failures that have occurred */
+} gSyscall = { 0, 0 };
+
+static int ts_open(const char *, int, int);
+static int ts_close(int fd);
+static int ts_access(const char *zPath, int mode);
+static char *ts_getcwd(char *zPath, size_t nPath);
+static int ts_stat(const char *zPath, struct stat *p);
+static int ts_fstat(int fd, struct stat *p);
+static int ts_ftruncate(int fd, off_t n);
+static int ts_fcntl(int fd, int cmd, ... );
+static int ts_read(int fd, void *aBuf, size_t nBuf);
+static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off);
+static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off);
+static int ts_write(int fd, const void *aBuf, size_t nBuf);
+static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off);
+static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off);
+static int ts_fchmod(int fd, mode_t mode);
+static int ts_fallocate(int fd, off_t off, off_t len);
+
+
+struct TestSyscallArray {
+  const char *zName;
+  sqlite3_syscall_ptr xTest;
+  sqlite3_syscall_ptr xOrig;
+  int default_errno;              /* Default value for errno following errors */
+  int custom_errno;               /* Current value for errno if error */
+} aSyscall[] = {
+  /*  0 */ { "open",      (sqlite3_syscall_ptr)ts_open,      0, EACCES, 0 },
+  /*  1 */ { "close",     (sqlite3_syscall_ptr)ts_close,     0, 0, 0 },
+  /*  2 */ { "access",    (sqlite3_syscall_ptr)ts_access,    0, 0, 0 },
+  /*  3 */ { "getcwd",    (sqlite3_syscall_ptr)ts_getcwd,    0, 0, 0 },
+  /*  4 */ { "stat",      (sqlite3_syscall_ptr)ts_stat,      0, 0, 0 },
+  /*  5 */ { "fstat",     (sqlite3_syscall_ptr)ts_fstat,     0, 0, 0 },
+  /*  6 */ { "ftruncate", (sqlite3_syscall_ptr)ts_ftruncate, 0, 0, 0 },
+  /*  7 */ { "fcntl",     (sqlite3_syscall_ptr)ts_fcntl,     0, 0, 0 },
+  /*  8 */ { "read",      (sqlite3_syscall_ptr)ts_read,      0, 0, 0 },
+  /*  9 */ { "pread",     (sqlite3_syscall_ptr)ts_pread,     0, 0, 0 },
+  /* 10 */ { "pread64",   (sqlite3_syscall_ptr)ts_pread64,   0, 0, 0 },
+  /* 11 */ { "write",     (sqlite3_syscall_ptr)ts_write,     0, 0, 0 },
+  /* 12 */ { "pwrite",    (sqlite3_syscall_ptr)ts_pwrite,    0, 0, 0 },
+  /* 13 */ { "pwrite64",  (sqlite3_syscall_ptr)ts_pwrite64,  0, 0, 0 },
+  /* 14 */ { "fchmod",    (sqlite3_syscall_ptr)ts_fchmod,    0, 0, 0 },
+  /* 15 */ { "fallocate", (sqlite3_syscall_ptr)ts_fallocate, 0, 0, 0 },
+           { 0, 0, 0, 0, 0 }
+};
+
+#define orig_open      ((int(*)(const char *, int, int))aSyscall[0].xOrig)
+#define orig_close     ((int(*)(int))aSyscall[1].xOrig)
+#define orig_access    ((int(*)(const char*,int))aSyscall[2].xOrig)
+#define orig_getcwd    ((char*(*)(char*,size_t))aSyscall[3].xOrig)
+#define orig_stat      ((int(*)(const char*,struct stat*))aSyscall[4].xOrig)
+#define orig_fstat     ((int(*)(int,struct stat*))aSyscall[5].xOrig)
+#define orig_ftruncate ((int(*)(int,off_t))aSyscall[6].xOrig)
+#define orig_fcntl     ((int(*)(int,int,...))aSyscall[7].xOrig)
+#define orig_read      ((ssize_t(*)(int,void*,size_t))aSyscall[8].xOrig)
+#define orig_pread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].xOrig)
+#define orig_pread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].xOrig)
+#define orig_write     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].xOrig)
+#define orig_pwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
+                       aSyscall[12].xOrig)
+#define orig_pwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
+                       aSyscall[13].xOrig)
+#define orig_fchmod    ((int(*)(int,mode_t))aSyscall[14].xOrig)
+#define orig_fallocate ((int(*)(int,off_t,off_t))aSyscall[15].xOrig)
+
+
+/*
+** This function is called exactly once from within each invocation of a
+** system call wrapper in this file. It returns 1 if the function should
+** fail, or 0 if it should succeed.
+*/
+static int tsIsFail(void){
+  gSyscall.nCount--;
+  if( gSyscall.nCount==0 || (gSyscall.nFail && gSyscall.bPersist) ){
+    gSyscall.nFail++;
+    return 1;
+  }
+  return 0;
+}
+
+/*
+** Return the current error-number value for function zFunc. zFunc must be
+** the name of a system call in the aSyscall[] table.
+**
+** Usually, the current error-number is the value that errno should be set
+** to if the named system call fails. The exception is "fallocate". See 
+** comments above the implementation of ts_fallocate() for details.
+*/
+static int tsErrno(const char *zFunc){
+  int i;
+  int nFunc = strlen(zFunc);
+  for(i=0; aSyscall[i].zName; i++){
+    if( strlen(aSyscall[i].zName)!=nFunc ) continue;
+    if( memcmp(aSyscall[i].zName, zFunc, nFunc) ) continue;
+    return aSyscall[i].custom_errno;
+  }
+
+  assert(0);
+  return 0;
+}
+
+/*
+** A wrapper around tsIsFail(). If tsIsFail() returns non-zero, set the
+** value of errno before returning.
+*/ 
+static int tsIsFailErrno(const char *zFunc){
+  if( tsIsFail() ){
+    errno = tsErrno(zFunc);
+    return 1;
+  }
+  return 0;
+}
+
+/*
+** A wrapper around open().
+*/
+static int ts_open(const char *zFile, int flags, int mode){
+  if( tsIsFailErrno("open") ){
+    return -1;
+  }
+  return orig_open(zFile, flags, mode);
+}
+
+/*
+** A wrapper around close().
+*/
+static int ts_close(int fd){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_close(fd);
+}
+
+/*
+** A wrapper around access().
+*/
+static int ts_access(const char *zPath, int mode){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_access(zPath, mode);
+}
+
+/*
+** A wrapper around getcwd().
+*/
+static char *ts_getcwd(char *zPath, size_t nPath){
+  if( tsIsFail() ){
+    return NULL;
+  }
+  return orig_getcwd(zPath, nPath);
+}
+
+/*
+** A wrapper around stat().
+*/
+static int ts_stat(const char *zPath, struct stat *p){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_stat(zPath, p);
+}
+
+/*
+** A wrapper around fstat().
+*/
+static int ts_fstat(int fd, struct stat *p){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_fstat(fd, p);
+}
+
+/*
+** A wrapper around ftruncate().
+*/
+static int ts_ftruncate(int fd, off_t n){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_ftruncate(fd, n);
+}
+
+/*
+** A wrapper around fcntl().
+*/
+static int ts_fcntl(int fd, int cmd, ... ){
+  va_list ap;
+  void *pArg;
+  if( tsIsFail() ){
+    return -1;
+  }
+  va_start(ap, cmd);
+  pArg = va_arg(ap, void *);
+  return orig_fcntl(fd, cmd, pArg);
+}
+
+/*
+** A wrapper around read().
+*/
+static int ts_read(int fd, void *aBuf, size_t nBuf){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_read(fd, aBuf, nBuf);
+}
+
+/*
+** A wrapper around pread().
+*/
+static int ts_pread(int fd, void *aBuf, size_t nBuf, off_t off){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_pread(fd, aBuf, nBuf, off);
+}
+
+/*
+** A wrapper around pread64().
+*/
+static int ts_pread64(int fd, void *aBuf, size_t nBuf, off_t off){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_pread64(fd, aBuf, nBuf, off);
+}
+
+/*
+** A wrapper around write().
+*/
+static int ts_write(int fd, const void *aBuf, size_t nBuf){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_write(fd, aBuf, nBuf);
+}
+
+/*
+** A wrapper around pwrite().
+*/
+static int ts_pwrite(int fd, const void *aBuf, size_t nBuf, off_t off){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_pwrite(fd, aBuf, nBuf, off);
+}
+
+/*
+** A wrapper around pwrite64().
+*/
+static int ts_pwrite64(int fd, const void *aBuf, size_t nBuf, off_t off){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_pwrite64(fd, aBuf, nBuf, off);
+}
+
+/*
+** A wrapper around fchmod().
+*/
+static int ts_fchmod(int fd, mode_t mode){
+  if( tsIsFail() ){
+    return -1;
+  }
+  return orig_fchmod(fd, mode);
+}
+
+/*
+** A wrapper around fallocate().
+**
+** SQLite assumes that the fallocate() function is compatible with
+** posix_fallocate(). According to the Linux man page (2009-09-30):
+**
+**   posix_fallocate() returns  zero on success, or an error number on
+**   failure. Note that errno is not set.
+*/
+static int ts_fallocate(int fd, off_t off, off_t len){
+  if( tsIsFail() ){
+    return tsErrno("fallocate");
+  }
+  return orig_fallocate(fd, off, len);
+}
+
+static int test_syscall_install(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  sqlite3_vfs *pVfs; 
+  int nElem;
+  int i;
+  Tcl_Obj **apElem;
+
+  if( objc!=3 ){
+    Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL-LIST");
+    return TCL_ERROR;
+  }
+  if( Tcl_ListObjGetElements(interp, objv[2], &nElem, &apElem) ){
+    return TCL_ERROR;
+  }
+  pVfs = sqlite3_vfs_find(0);
+
+  for(i=0; i<nElem; i++){
+    int iCall;
+    int rc = Tcl_GetIndexFromObjStruct(interp, 
+        apElem[i], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
+    );
+    if( rc ) return rc;
+    if( aSyscall[iCall].xOrig==0 ){
+      aSyscall[iCall].xOrig = pVfs->xGetSystemCall(pVfs, aSyscall[iCall].zName);
+      pVfs->xSetSystemCall(pVfs, aSyscall[iCall].zName, aSyscall[iCall].xTest);
+    }
+    aSyscall[iCall].custom_errno = aSyscall[iCall].default_errno;
+  }
+
+  return TCL_OK;
+}
+
+static int test_syscall_uninstall(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  sqlite3_vfs *pVfs; 
+  int i;
+
+  if( objc!=2 ){
+    Tcl_WrongNumArgs(interp, 2, objv, "");
+    return TCL_ERROR;
+  }
+
+  pVfs = sqlite3_vfs_find(0);
+  for(i=0; aSyscall[i].zName; i++){
+    if( aSyscall[i].xOrig ){
+      pVfs->xSetSystemCall(pVfs, aSyscall[i].zName, aSyscall[i].xOrig);
+      aSyscall[i].xOrig = 0;
+    }
+  }
+  return TCL_OK;
+}
+
+static int test_syscall_fault(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  int nCount = 0;
+  int bPersist = 0;
+
+  if( objc!=2 && objc!=4 ){
+    Tcl_WrongNumArgs(interp, 2, objv, "?COUNT PERSIST?");
+    return TCL_ERROR;
+  }
+
+  if( objc==4 ){
+    if( Tcl_GetIntFromObj(interp, objv[2], &nCount)
+     || Tcl_GetBooleanFromObj(interp, objv[3], &bPersist)
+    ){
+      return TCL_ERROR;
+    }
+  }
+
+  Tcl_SetObjResult(interp, Tcl_NewIntObj(gSyscall.nFail));
+  gSyscall.nCount = nCount;
+  gSyscall.bPersist = bPersist;
+  gSyscall.nFail = 0;
+  return TCL_OK;
+}
+
+static int test_syscall_errno(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  int iCall;
+  int iErrno;
+  int rc;
+
+  struct Errno {
+    const char *z;
+    int i;
+  } aErrno[] = {
+    { "EACCES", EACCES },
+    { 0, 0 }
+  };
+
+  if( objc!=4 ){
+    Tcl_WrongNumArgs(interp, 2, objv, "SYSCALL ERRNO");
+    return TCL_ERROR;
+  }
+
+  rc = Tcl_GetIndexFromObjStruct(interp, 
+      objv[2], aSyscall, sizeof(aSyscall[0]), "system-call", 0, &iCall
+  );
+  if( rc!=TCL_OK ) return rc;
+  rc = Tcl_GetIndexFromObjStruct(interp, 
+      objv[3], aErrno, sizeof(aErrno[0]), "errno", 0, &iErrno
+  );
+  if( rc!=TCL_OK ) return rc;
+
+  aSyscall[iCall].custom_errno = aErrno[iErrno].i;
+  return TCL_OK;
+}
+
+static int test_syscall(
+  void * clientData,
+  Tcl_Interp *interp,
+  int objc,
+  Tcl_Obj *CONST objv[]
+){
+  struct SyscallCmd {
+    const char *zName;
+    Tcl_ObjCmdProc *xCmd;
+  } aCmd[] = {
+    { "fault",     test_syscall_fault },
+    { "install",   test_syscall_install },
+    { "uninstall", test_syscall_uninstall },
+    { "errno",     test_syscall_errno },
+    { 0, 0 }
+  };
+  int iCmd;
+  int rc;
+
+  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, &iCmd
+  );
+  if( rc!=TCL_OK ) return rc;
+  return aCmd[iCmd].xCmd(clientData, interp, objc, objv);
+}
+
+int SqlitetestSyscall_Init(Tcl_Interp *interp){
+  struct SyscallCmd {
+    const char *zName;
+    Tcl_ObjCmdProc *xCmd;
+  } aCmd[] = {
+    { "test_syscall",     test_syscall},
+  };
+  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;
+}
+#else
+int SqlitetestSyscall_Init(Tcl_Interp *interp){
+  return TCL_OK;
+}
+#endif
+
index 379b970747da5e5762b14e0ed2b4e03c684947f7..e7f615648b406357a16612ef3067a774261fb326 100644 (file)
@@ -117,8 +117,8 @@ proc do_faultsim_test {name args} {
   set DEFAULT(-prep)          ""
   set DEFAULT(-body)          ""
   set DEFAULT(-test)          ""
-  set DEFAULT(-install)          ""
-  set DEFAULT(-uninstall)          ""
+  set DEFAULT(-install)       ""
+  set DEFAULT(-uninstall)     ""
 
   fix_testname name
 
diff --git a/test/sysfault.test b/test/sysfault.test
new file mode 100644 (file)
index 0000000..2d0d8b3
--- /dev/null
@@ -0,0 +1,70 @@
+# 2011 March 28
+#
+# 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.
+#
+#***********************************************************************
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/lock_common.tcl
+source $testdir/malloc_common.tcl
+
+if {[llength [info commands test_syscall]]==0} {
+  finish_test
+  return
+} 
+
+set testprefix sysfault
+
+set FAULTSIM(vfsfault-transient) [list             \
+  -injectinstall   vfsfault_install                \
+  -injectstart     vfsfault_injectstart_t          \
+  -injectstop      vfsfault_injectstop             \
+  -injecterrlist   {}                              \
+  -injectuninstall {test_syscall uninstall}        \
+]
+set FAULTSIM(vfsfault-persistent) [list            \
+  -injectinstall   vfsfault_install                \
+  -injectstart     vfsfault_injectstart_p          \
+  -injectstop      vfsfault_injectstop             \
+  -injecterrlist   {}                              \
+  -injectuninstall {test_syscall uninstall}        \
+]
+
+proc vfsfault_injectstart_t {iFail} { test_syscall fault $iFail 0 }
+proc vfsfault_injectstart_p {iFail} { test_syscall fault $iFail 1 }
+proc vfsfault_injectstop    {}      { test_syscall fault }
+
+faultsim_save_and_close
+
+proc vfsfault_install {} { 
+  test_syscall install {open getcwd}
+}
+
+do_faultsim_test 1 -faults vfsfault-* -prep {
+  faultsim_restore
+} -body {
+  sqlite3 db test.db
+  db eval {
+    CREATE TABLE t1(a, b);
+    INSERT INTO t1 VALUES(1, 2);
+    PRAGMA journal_mode = WAL;
+    INSERT INTO t1 VALUES(3, 4);
+    SELECT * FROM t1;
+    CREATE TEMP TABLE t2(x);
+    INSERT INTO t2 VALUES('y');
+  }
+} -test {
+  faultsim_test_result {0 {wal 1 2 3 4}}       \
+    {1 {unable to open database file}}         \
+    {1 {attempt to write a readonly database}}
+}
+
+finish_test
+