os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/misuse.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2002 May 10
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.
sl@0
    12
#
sl@0
    13
# This file implements tests for the SQLITE_MISUSE detection logic.
sl@0
    14
# This test file leaks memory and file descriptors.
sl@0
    15
#
sl@0
    16
# $Id: misuse.test,v 1.11 2006/01/03 00:33:50 drh Exp $
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
proc catchsql2 {sql} {
sl@0
    22
  set r [
sl@0
    23
    catch {
sl@0
    24
      set res [list]
sl@0
    25
      db eval $sql data {
sl@0
    26
        if { $res==[list] } {
sl@0
    27
          foreach f $data(*) {lappend res $f}
sl@0
    28
        }
sl@0
    29
        foreach f $data(*) {lappend res $data($f)}
sl@0
    30
      }
sl@0
    31
      set res
sl@0
    32
    } msg
sl@0
    33
  ]
sl@0
    34
  lappend r $msg
sl@0
    35
}
sl@0
    36
sl@0
    37
sl@0
    38
# Make sure the test logic works
sl@0
    39
#
sl@0
    40
do_test misuse-1.1 {
sl@0
    41
  db close
sl@0
    42
  catch {file delete -force test2.db}
sl@0
    43
  catch {file delete -force test2.db-journal}
sl@0
    44
  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
sl@0
    45
  execsql {
sl@0
    46
    CREATE TABLE t1(a,b);
sl@0
    47
    INSERT INTO t1 VALUES(1,2);
sl@0
    48
  }
sl@0
    49
  catchsql2 {
sl@0
    50
    SELECT * FROM t1
sl@0
    51
  }
sl@0
    52
} {0 {a b 1 2}}
sl@0
    53
do_test misuse-1.2 {
sl@0
    54
  catchsql2 {
sl@0
    55
    SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
sl@0
    56
  }
sl@0
    57
} {1 {no such function: x_coalesce}}
sl@0
    58
do_test misuse-1.3 {
sl@0
    59
  sqlite3_create_function $::DB
sl@0
    60
  catchsql2 {
sl@0
    61
    SELECT x_coalesce(NULL,a) AS 'xyz' FROM t1
sl@0
    62
  }
sl@0
    63
} {0 {xyz 1}}
sl@0
    64
sl@0
    65
# Use the x_sqlite_exec() SQL function to simulate the effect of two
sl@0
    66
# threads trying to use the same database at the same time.
sl@0
    67
#
sl@0
    68
# It used to be prohibited to invoke sqlite_exec() from within a function,
sl@0
    69
# but that has changed.  The following tests used to cause errors but now
sl@0
    70
# they do not.
sl@0
    71
#
sl@0
    72
ifcapable {utf16} {
sl@0
    73
  do_test misuse-1.4 {
sl@0
    74
    catchsql2 {
sl@0
    75
       SELECT x_sqlite_exec('SELECT * FROM t1') AS xyz;
sl@0
    76
    } 
sl@0
    77
  } {0 {xyz {1 2}}}
sl@0
    78
}
sl@0
    79
do_test misuse-1.5 {
sl@0
    80
  catchsql2 {SELECT * FROM t1}
sl@0
    81
} {0 {a b 1 2}}
sl@0
    82
do_test misuse-1.6 {
sl@0
    83
  catchsql {
sl@0
    84
    SELECT * FROM t1
sl@0
    85
  }
sl@0
    86
} {0 {1 2}}
sl@0
    87
sl@0
    88
# Attempt to register a new SQL function while an sqlite_exec() is active.
sl@0
    89
#
sl@0
    90
do_test misuse-2.1 {
sl@0
    91
  db close
sl@0
    92
  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
sl@0
    93
  execsql {
sl@0
    94
    SELECT * FROM t1
sl@0
    95
  }
sl@0
    96
} {1 2}
sl@0
    97
do_test misuse-2.2 {
sl@0
    98
  catchsql2 {SELECT * FROM t1}
sl@0
    99
} {0 {a b 1 2}}
sl@0
   100
sl@0
   101
# We used to disallow creating new function from within an exec().
sl@0
   102
# But now this is acceptable.
sl@0
   103
do_test misuse-2.3 {
sl@0
   104
  set v [catch {
sl@0
   105
    db eval {SELECT * FROM t1} {} {
sl@0
   106
      sqlite3_create_function $::DB
sl@0
   107
    }
sl@0
   108
  } msg]
sl@0
   109
  lappend v $msg
sl@0
   110
} {0 {}}
sl@0
   111
do_test misuse-2.4 {
sl@0
   112
  catchsql2 {SELECT * FROM t1}
sl@0
   113
} {0 {a b 1 2}}
sl@0
   114
do_test misuse-2.5 {
sl@0
   115
  catchsql {
sl@0
   116
    SELECT * FROM t1
sl@0
   117
  }
sl@0
   118
} {0 {1 2}}
sl@0
   119
sl@0
   120
# Attempt to register a new SQL aggregate while an sqlite_exec() is active.
sl@0
   121
#
sl@0
   122
do_test misuse-3.1 {
sl@0
   123
  db close
sl@0
   124
  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
sl@0
   125
  execsql {
sl@0
   126
    SELECT * FROM t1
sl@0
   127
  }
sl@0
   128
} {1 2}
sl@0
   129
do_test misuse-3.2 {
sl@0
   130
  catchsql2 {SELECT * FROM t1}
sl@0
   131
} {0 {a b 1 2}}
sl@0
   132
sl@0
   133
# We used to disallow creating new function from within an exec().
sl@0
   134
# But now this is acceptable.
sl@0
   135
do_test misuse-3.3 {
sl@0
   136
  set v [catch {
sl@0
   137
    db eval {SELECT * FROM t1} {} {
sl@0
   138
      sqlite3_create_aggregate $::DB
sl@0
   139
    }
sl@0
   140
  } msg]
sl@0
   141
  lappend v $msg
sl@0
   142
} {0 {}}
sl@0
   143
do_test misuse-3.4 {
sl@0
   144
  catchsql2 {SELECT * FROM t1}
sl@0
   145
} {0 {a b 1 2}}
sl@0
   146
do_test misuse-3.5 {
sl@0
   147
  catchsql {
sl@0
   148
    SELECT * FROM t1
sl@0
   149
  }
sl@0
   150
} {0 {1 2}}
sl@0
   151
sl@0
   152
# Attempt to close the database from an sqlite_exec callback.
sl@0
   153
#
sl@0
   154
# Update for v3: The db cannot be closed because there are active
sl@0
   155
# VMs. The sqlite3_close call would return SQLITE_BUSY.
sl@0
   156
do_test misuse-4.1 {
sl@0
   157
  db close
sl@0
   158
  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
sl@0
   159
  execsql {
sl@0
   160
    SELECT * FROM t1
sl@0
   161
  }
sl@0
   162
} {1 2}
sl@0
   163
do_test misuse-4.2 {
sl@0
   164
  catchsql2 {SELECT * FROM t1}
sl@0
   165
} {0 {a b 1 2}}
sl@0
   166
do_test misuse-4.3 {
sl@0
   167
  set v [catch {
sl@0
   168
    db eval {SELECT * FROM t1} {} {
sl@0
   169
      set r [sqlite3_close $::DB]
sl@0
   170
    }
sl@0
   171
  } msg]
sl@0
   172
  lappend v $msg $r
sl@0
   173
} {0 {} SQLITE_BUSY}
sl@0
   174
do_test misuse-4.4 {
sl@0
   175
  # Flush the TCL statement cache here, otherwise the sqlite3_close() will
sl@0
   176
  # fail because there are still un-finalized() VDBEs.
sl@0
   177
  db cache flush
sl@0
   178
  sqlite3_close $::DB
sl@0
   179
  catchsql2 {SELECT * FROM t1}
sl@0
   180
} {1 {library routine called out of sequence}}
sl@0
   181
do_test misuse-4.5 {
sl@0
   182
  catchsql {
sl@0
   183
    SELECT * FROM t1
sl@0
   184
  }
sl@0
   185
} {1 {library routine called out of sequence}}
sl@0
   186
sl@0
   187
# Attempt to use a database after it has been closed.
sl@0
   188
#
sl@0
   189
do_test misuse-5.1 {
sl@0
   190
  db close
sl@0
   191
  sqlite3 db test2.db; set ::DB [sqlite3_connection_pointer db]
sl@0
   192
  execsql {
sl@0
   193
    SELECT * FROM t1
sl@0
   194
  }
sl@0
   195
} {1 2}
sl@0
   196
do_test misuse-5.2 {
sl@0
   197
  catchsql2 {SELECT * FROM t1}
sl@0
   198
} {0 {a b 1 2}}
sl@0
   199
do_test misuse-5.3 {
sl@0
   200
  db close
sl@0
   201
  set r [catch {
sl@0
   202
    sqlite3_prepare $::DB {SELECT * FROM t1} -1 TAIL
sl@0
   203
  } msg]
sl@0
   204
  lappend r $msg
sl@0
   205
} {1 {(21) library routine called out of sequence}}
sl@0
   206
sl@0
   207
finish_test