os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/safety.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/safety.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,93 @@
     1.4 +# 2005 January 11
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +# This file implements regression tests for SQLite library.  The
    1.15 +# focus of this file is testing the sqlite3SafetyOn and sqlite3SafetyOff
    1.16 +# functions.  Those routines are not strictly necessary - they are
    1.17 +# designed to detect misuse of the library.
    1.18 +#
    1.19 +# $Id: safety.test,v 1.4 2008/03/18 13:46:53 drh Exp $
    1.20 +
    1.21 +set testdir [file dirname $argv0]
    1.22 +source $testdir/tester.tcl
    1.23 +
    1.24 +ifcapable !debug {
    1.25 +  puts "Skipping safety tests since SQLITE_DEBUG is off"
    1.26 +  finish_test
    1.27 +  return
    1.28 +}
    1.29 +
    1.30 +# Return the UTF-8 representation of the supplied UTF-16 string $str. 
    1.31 +proc utf8 {str} {
    1.32 +  # If $str ends in two 0x00 0x00 bytes, knock these off before
    1.33 +  # converting to UTF-8 using TCL.
    1.34 +  binary scan $str \c* vals
    1.35 +  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
    1.36 +    set str [binary format \c* [lrange $vals 0 end-2]]
    1.37 +  }
    1.38 +
    1.39 +  set r [encoding convertfrom unicode $str]
    1.40 +  return $r
    1.41 +}
    1.42 +
    1.43 +
    1.44 +do_test safety-1.1 {
    1.45 +  set DB [sqlite3_connection_pointer db]
    1.46 +  db eval {CREATE TABLE t1(a)}
    1.47 +  sqlite_set_magic $DB SQLITE_MAGIC_BUSY
    1.48 +  catchsql {
    1.49 +    SELECT name FROM sqlite_master;
    1.50 +  }
    1.51 +} {1 {library routine called out of sequence}}
    1.52 +do_test safety-1.2 {
    1.53 +  sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    1.54 +  catchsql {
    1.55 +    SELECT name FROM sqlite_master
    1.56 +  }
    1.57 +} {0 t1}
    1.58 +
    1.59 +do_test safety-2.1 {
    1.60 +  proc safety_on {} "sqlite_set_magic $DB SQLITE_MAGIC_BUSY"
    1.61 +  db function safety_on safety_on
    1.62 +  catchsql {
    1.63 +    SELECT safety_on(), name FROM sqlite_master
    1.64 +  }
    1.65 +} {1 {library routine called out of sequence}}
    1.66 +ifcapable {utf16} {
    1.67 +  do_test safety-2.1.1 {
    1.68 +    utf8 [sqlite3_errmsg16 db]
    1.69 +  } {library routine called out of sequence}
    1.70 +}
    1.71 +do_test safety-2.2 {
    1.72 +  catchsql {
    1.73 +    SELECT 'hello'
    1.74 +  }
    1.75 +} {1 {library routine called out of sequence}}
    1.76 +do_test safety-2.3 {
    1.77 +  sqlite3_close $DB
    1.78 +} {SQLITE_MISUSE}
    1.79 +do_test safety-2.4 {
    1.80 +  sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    1.81 +  execsql {
    1.82 +    SELECT name FROM sqlite_master
    1.83 +  }
    1.84 +} {t1}
    1.85 +
    1.86 +do_test safety-3.1 {
    1.87 +  set rc [catch {
    1.88 +    db eval {SELECT name FROM sqlite_master} {
    1.89 +      sqlite_set_magic $DB SQLITE_MAGIC_BUSY
    1.90 +    }
    1.91 +  } msg]
    1.92 +  lappend rc $msg
    1.93 +} {1 {library routine called out of sequence}}
    1.94 +sqlite_set_magic $DB SQLITE_MAGIC_OPEN
    1.95 +
    1.96 +finish_test