os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/malloc4.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/malloc4.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,194 @@
     1.4 +# 2005 November 30
     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 +#
    1.15 +# This file contains tests to ensure that the library handles malloc() failures
    1.16 +# correctly. The emphasis in this file is on sqlite3_column_XXX() APIs.
    1.17 +#
    1.18 +# $Id: malloc4.test,v 1.10 2008/02/18 22:24:58 drh Exp $
    1.19 +
    1.20 +#---------------------------------------------------------------------------
    1.21 +# NOTES ON EXPECTED BEHAVIOUR
    1.22 +#
    1.23 +# [193] When a memory allocation failure occurs during sqlite3_column_name(),
    1.24 +#       sqlite3_column_name16(), sqlite3_column_decltype(), or
    1.25 +#       sqlite3_column_decltype16() the function shall return NULL.
    1.26 +#
    1.27 +#---------------------------------------------------------------------------
    1.28 +
    1.29 +set testdir [file dirname $argv0]
    1.30 +source $testdir/tester.tcl
    1.31 +source $testdir/malloc_common.tcl
    1.32 +
    1.33 +# Only run these tests if memory debugging is turned on.
    1.34 +if {!$MEMDEBUG} {
    1.35 +   puts "Skipping malloc4 tests: not compiled with -DSQLITE_MEMDEBUG..."
    1.36 +   finish_test
    1.37 +   return
    1.38 +}
    1.39 +
    1.40 +ifcapable !utf16 {
    1.41 +  finish_test
    1.42 +  return
    1.43 +}
    1.44 +
    1.45 +proc do_stmt_test {id sql} {
    1.46 +  set ::sql $sql
    1.47 +  set go 1
    1.48 +  for {set n 0} {$go} {incr n} {
    1.49 +    set testid "malloc4-$id.$n"
    1.50 +
    1.51 +    # Prepare the statement
    1.52 +    do_test ${testid}.1 {
    1.53 +      set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
    1.54 +      expr [string length $::STMT] > 0
    1.55 +    } {1}
    1.56 +
    1.57 +    # Set the Nth malloc() to fail.
    1.58 +    sqlite3_memdebug_fail $n -repeat 0
    1.59 +
    1.60 +    # Test malloc failure in the _name(), _name16(), decltype() and
    1.61 +    # decltype16() APIs. Calls that occur after the malloc() failure should
    1.62 +    # return NULL. No error is raised though.
    1.63 +    #
    1.64 +    # ${testid}.2.1 - Call _name()
    1.65 +    # ${testid}.2.2 - Call _name16()
    1.66 +    # ${testid}.2.3 - Call _name()
    1.67 +    # ${testid}.2.4 - Check that the return values of the above three calls are
    1.68 +    #                 consistent with each other and with the simulated
    1.69 +    #                 malloc() failures.
    1.70 +    #
    1.71 +    # Because the code that implements the _decltype() and _decltype16() APIs
    1.72 +    # is the same as the _name() and _name16() implementations, we don't worry
    1.73 +    # about explicitly testing them.
    1.74 +    #
    1.75 +    do_test ${testid}.2.1 {
    1.76 +      set mf1 [expr [sqlite3_memdebug_pending] < 0]
    1.77 +      set ::name8  [sqlite3_column_name $::STMT 0]
    1.78 +      set mf2 [expr [sqlite3_memdebug_pending] < 0]
    1.79 +      expr {$mf1 == $mf2 || $::name8 == ""}
    1.80 +    } {1}
    1.81 +    do_test ${testid}.2.2 {
    1.82 +      set mf1 [expr [sqlite3_memdebug_pending] < 0]
    1.83 +      set ::name16 [sqlite3_column_name16 $::STMT 0]
    1.84 +      set ::name16 [encoding convertfrom unicode $::name16]
    1.85 +      set ::name16 [string range $::name16 0 end-1]
    1.86 +      set mf2 [expr [sqlite3_memdebug_pending] < 0]
    1.87 +      expr {$mf1 == $mf2 || $::name16 == ""}
    1.88 +    } {1}
    1.89 +    do_test ${testid}.2.3 {
    1.90 +      set mf1 [expr [sqlite3_memdebug_pending] < 0]
    1.91 +      set ::name8_2 [sqlite3_column_name $::STMT 0]
    1.92 +      set mf2 [expr [sqlite3_memdebug_pending] < 0]
    1.93 +      expr {$mf1 == $mf2 || $::name8_2 == ""}
    1.94 +    } {1}
    1.95 +    set ::mallocFailed [expr [sqlite3_memdebug_pending] < 0]
    1.96 +    do_test ${testid}.2.4 {
    1.97 +      expr {
    1.98 +        $::name8 == $::name8_2 && $::name16 == $::name8 && !$::mallocFailed ||
    1.99 +        $::name8 == $::name8_2 && $::name16 == "" &&        $::mallocFailed ||
   1.100 +        $::name8 == $::name16 && $::name8_2 == "" &&        $::mallocFailed ||
   1.101 +        $::name8_2 == $::name16 && $::name8 == "" &&        $::mallocFailed
   1.102 +      }
   1.103 +    } {1}
   1.104 +
   1.105 +    # Step the statement so that we can call _text() and _text16().  Before
   1.106 +    # running sqlite3_step(), make sure that malloc() is not about to fail.
   1.107 +    # Memory allocation failures that occur within sqlite3_step() are tested
   1.108 +    # elsewhere.
   1.109 +    set mf [sqlite3_memdebug_pending]
   1.110 +    sqlite3_memdebug_fail -1
   1.111 +    do_test ${testid}.3 {
   1.112 +      sqlite3_step $::STMT
   1.113 +    } {SQLITE_ROW}
   1.114 +    sqlite3_memdebug_fail $mf
   1.115 +
   1.116 +    # Test for malloc() failures within _text() and _text16().
   1.117 +    #
   1.118 +    do_test ${testid}.4.1 {
   1.119 +      set ::text8 [sqlite3_column_text $::STMT 0]
   1.120 +      set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed]
   1.121 +      expr {$mf==0 || $::text8 == ""}
   1.122 +    } {1}
   1.123 +    do_test ${testid}.4.2 {
   1.124 +      set ::text16 [sqlite3_column_text16 $::STMT 0]
   1.125 +      set ::text16 [encoding convertfrom unicode $::text16]
   1.126 +      set ::text16 [string range $::text16 0 end-1]
   1.127 +      set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed]
   1.128 +      expr {$mf==0 || $::text16 == ""}
   1.129 +    } {1}
   1.130 +    do_test ${testid}.4.3 {
   1.131 +      set ::text8_2 [sqlite3_column_text $::STMT 0]
   1.132 +      set mf [expr [sqlite3_memdebug_pending] < 0 && !$::mallocFailed]
   1.133 +      expr {$mf==0 || $::text8_2 == "" || ($::text16 == "" && $::text8 != "")}
   1.134 +    } {1}
   1.135 +
   1.136 +    # Test for malloc() failures within _int(), _int64() and _real(). The only
   1.137 +    # way this can occur is if the string has to be translated from UTF-16 to
   1.138 +    # UTF-8 before being converted to a numeric value.
   1.139 +    do_test ${testid}.4.4.1 {
   1.140 +      set mf [sqlite3_memdebug_pending]
   1.141 +      sqlite3_memdebug_fail -1
   1.142 +      sqlite3_column_text16 $::STMT 0
   1.143 +      sqlite3_memdebug_fail $mf
   1.144 +      sqlite3_column_int $::STMT 0
   1.145 +    } {0}
   1.146 +    do_test ${testid}.4.5 {
   1.147 +      set mf [sqlite3_memdebug_pending]
   1.148 +      sqlite3_memdebug_fail -1
   1.149 +      sqlite3_column_text16 $::STMT 0
   1.150 +      sqlite3_memdebug_fail $mf
   1.151 +      sqlite3_column_int64 $::STMT 0
   1.152 +    } {0}
   1.153 +
   1.154 +    do_test ${testid}.4.6 {
   1.155 +      set mf [sqlite3_memdebug_pending]
   1.156 +      sqlite3_memdebug_fail -1
   1.157 +      sqlite3_column_text16 $::STMT 0
   1.158 +      sqlite3_memdebug_fail $mf
   1.159 +      sqlite3_column_double $::STMT 0
   1.160 +    } {0.0}
   1.161 +
   1.162 +    set mallocFailedAfterStep [expr \
   1.163 +      [sqlite3_memdebug_pending] < 0 && !$::mallocFailed
   1.164 +    ]
   1.165 +
   1.166 +    sqlite3_memdebug_fail -1
   1.167 +    # Test that if a malloc() failed the next call to sqlite3_step() returns
   1.168 +    # SQLITE_ERROR. If malloc() did not fail, it should return SQLITE_DONE.
   1.169 +    #
   1.170 +    do_test ${testid}.5 {
   1.171 +      sqlite3_step $::STMT
   1.172 +    } [expr {$mallocFailedAfterStep ? "SQLITE_ERROR" : "SQLITE_DONE"}]
   1.173 +
   1.174 +    do_test ${testid}.6 {
   1.175 +      sqlite3_finalize $::STMT
   1.176 +    } [expr {$mallocFailedAfterStep ? "SQLITE_NOMEM" : "SQLITE_OK"}]
   1.177 +
   1.178 +    if {$::mallocFailed == 0 && $mallocFailedAfterStep == 0} {
   1.179 +      sqlite3_memdebug_fail -1
   1.180 +      set go 0
   1.181 +    }
   1.182 +  }
   1.183 +}
   1.184 +
   1.185 +execsql {
   1.186 +  CREATE TABLE tbl(
   1.187 +    the_first_reasonably_long_column_name that_also_has_quite_a_lengthy_type
   1.188 +  );
   1.189 +  INSERT INTO tbl VALUES(
   1.190 +    'An extra long string. Far too long to be stored in NBFS bytes.'
   1.191 +  );
   1.192 +}
   1.193 +
   1.194 +do_stmt_test 1 "SELECT * FROM tbl"
   1.195 +
   1.196 +sqlite3_memdebug_fail -1
   1.197 +finish_test