persistentstorage/sqlite3api/TEST/TclScript/safety.test
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 # 2005 January 11
       
     2 #
       
     3 # The author disclaims copyright to this source code.  In place of
       
     4 # a legal notice, here is a blessing:
       
     5 #
       
     6 #    May you do good and not evil.
       
     7 #    May you find forgiveness for yourself and forgive others.
       
     8 #    May you share freely, never taking more than you give.
       
     9 #
       
    10 #***********************************************************************
       
    11 # This file implements regression tests for SQLite library.  The
       
    12 # focus of this file is testing the sqlite3SafetyOn and sqlite3SafetyOff
       
    13 # functions.  Those routines are not strictly necessary - they are
       
    14 # designed to detect misuse of the library.
       
    15 #
       
    16 # $Id: safety.test,v 1.4 2008/03/18 13:46:53 drh Exp $
       
    17 
       
    18 set testdir [file dirname $argv0]
       
    19 source $testdir/tester.tcl
       
    20 
       
    21 ifcapable !debug {
       
    22   puts "Skipping safety tests since SQLITE_DEBUG is off"
       
    23   finish_test
       
    24   return
       
    25 }
       
    26 
       
    27 # Return the UTF-8 representation of the supplied UTF-16 string $str. 
       
    28 proc utf8 {str} {
       
    29   # If $str ends in two 0x00 0x00 bytes, knock these off before
       
    30   # converting to UTF-8 using TCL.
       
    31   binary scan $str \c* vals
       
    32   if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
       
    33     set str [binary format \c* [lrange $vals 0 end-2]]
       
    34   }
       
    35 
       
    36   set r [encoding convertfrom unicode $str]
       
    37   return $r
       
    38 }
       
    39 
       
    40 
       
    41 do_test safety-1.1 {
       
    42   set DB [sqlite3_connection_pointer db]
       
    43   db eval {CREATE TABLE t1(a)}
       
    44   sqlite_set_magic $DB SQLITE_MAGIC_BUSY
       
    45   catchsql {
       
    46     SELECT name FROM sqlite_master;
       
    47   }
       
    48 } {1 {library routine called out of sequence}}
       
    49 do_test safety-1.2 {
       
    50   sqlite_set_magic $DB SQLITE_MAGIC_OPEN
       
    51   catchsql {
       
    52     SELECT name FROM sqlite_master
       
    53   }
       
    54 } {0 t1}
       
    55 
       
    56 do_test safety-2.1 {
       
    57   proc safety_on {} "sqlite_set_magic $DB SQLITE_MAGIC_BUSY"
       
    58   db function safety_on safety_on
       
    59   catchsql {
       
    60     SELECT safety_on(), name FROM sqlite_master
       
    61   }
       
    62 } {1 {library routine called out of sequence}}
       
    63 ifcapable {utf16} {
       
    64   do_test safety-2.1.1 {
       
    65     utf8 [sqlite3_errmsg16 db]
       
    66   } {library routine called out of sequence}
       
    67 }
       
    68 do_test safety-2.2 {
       
    69   catchsql {
       
    70     SELECT 'hello'
       
    71   }
       
    72 } {1 {library routine called out of sequence}}
       
    73 do_test safety-2.3 {
       
    74   sqlite3_close $DB
       
    75 } {SQLITE_MISUSE}
       
    76 do_test safety-2.4 {
       
    77   sqlite_set_magic $DB SQLITE_MAGIC_OPEN
       
    78   execsql {
       
    79     SELECT name FROM sqlite_master
       
    80   }
       
    81 } {t1}
       
    82 
       
    83 do_test safety-3.1 {
       
    84   set rc [catch {
       
    85     db eval {SELECT name FROM sqlite_master} {
       
    86       sqlite_set_magic $DB SQLITE_MAGIC_BUSY
       
    87     }
       
    88   } msg]
       
    89   lappend rc $msg
       
    90 } {1 {library routine called out of sequence}}
       
    91 sqlite_set_magic $DB SQLITE_MAGIC_OPEN
       
    92 
       
    93 finish_test