| [25] | 1 | # Commands covered:  unload | 
|---|
 | 2 | # | 
|---|
 | 3 | # This file contains a collection of tests for one or more of the Tcl | 
|---|
 | 4 | # built-in commands.  Sourcing this file into Tcl runs the tests and | 
|---|
 | 5 | # generates output for errors.  No output means no errors were found. | 
|---|
 | 6 | # | 
|---|
 | 7 | # Copyright (c) 1995 Sun Microsystems, Inc. | 
|---|
 | 8 | # Copyright (c) 1998-1999 by Scriptics Corporation. | 
|---|
 | 9 | # Copyright (c) 2003-2004 by Georgios Petasis | 
|---|
 | 10 | # | 
|---|
 | 11 | # See the file "license.terms" for information on usage and redistribution | 
|---|
 | 12 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
 | 13 | # | 
|---|
 | 14 | # RCS: @(#) $Id: unload.test,v 1.6 2006/12/17 03:47:08 das Exp $ | 
|---|
 | 15 |  | 
|---|
 | 16 | if {[lsearch [namespace children] ::tcltest] == -1} { | 
|---|
 | 17 |     package require tcltest 2 | 
|---|
 | 18 |     namespace import -force ::tcltest::* | 
|---|
 | 19 | } | 
|---|
 | 20 |  | 
|---|
 | 21 | # Figure out what extension is used for shared libraries on this | 
|---|
 | 22 | # platform. | 
|---|
 | 23 | if {![info exists ext]} { | 
|---|
 | 24 |     set ext [info sharedlibextension] | 
|---|
 | 25 | } | 
|---|
 | 26 |  | 
|---|
 | 27 | # Tests require the existence of one of the DLLs in the dltest directory. | 
|---|
 | 28 | set testDir [file join [file dirname [info nameofexecutable]] dltest] | 
|---|
 | 29 | set x [file join $testDir pkgua$ext] | 
|---|
 | 30 | set dll "[file tail $x]Required" | 
|---|
 | 31 | testConstraint $dll [file readable $x] | 
|---|
 | 32 |  | 
|---|
 | 33 | # Tests also require that this DLL has not already been loaded. | 
|---|
 | 34 | set loaded "[file tail $x]Loaded" | 
|---|
 | 35 | set alreadyLoaded [info loaded] | 
|---|
 | 36 | testConstraint $loaded [expr {![string match *pkgua* $alreadyLoaded]}] | 
|---|
 | 37 |  | 
|---|
 | 38 | set alreadyTotalLoaded [info loaded] | 
|---|
 | 39 |  | 
|---|
 | 40 | # Certain tests require the 'teststaticpkg' command from tcltest | 
|---|
 | 41 | testConstraint teststaticpkg [llength [info commands teststaticpkg]] | 
|---|
 | 42 |  | 
|---|
 | 43 | # Basic tests: parameter testing... | 
|---|
 | 44 | test unload-1.1 {basic errors} {} { | 
|---|
 | 45 |     list [catch {unload} msg] $msg | 
|---|
 | 46 | } "1 {wrong \# args: should be \"unload ?switches? fileName ?packageName? ?interp?\"}" | 
|---|
 | 47 | test unload-1.2 {basic errors} {} { | 
|---|
 | 48 |     list [catch {unload a b c d} msg] $msg | 
|---|
 | 49 | } "1 {wrong \# args: should be \"unload ?switches? fileName ?packageName? ?interp?\"}" | 
|---|
 | 50 | test unload-1.3 {basic errors} {} { | 
|---|
 | 51 |     list [catch {unload a b foobar} msg] $msg | 
|---|
 | 52 | } {1 {could not find interpreter "foobar"}} | 
|---|
 | 53 | test unload-1.4 {basic errors} {} { | 
|---|
 | 54 |     list [catch {unload {}} msg] $msg | 
|---|
 | 55 | } {1 {must specify either file name or package name}} | 
|---|
 | 56 | test unload-1.5 {basic errors} {} { | 
|---|
 | 57 |     list [catch {unload {} {}} msg] $msg | 
|---|
 | 58 | } {1 {must specify either file name or package name}} | 
|---|
 | 59 | test unload-1.6 {basic errors} {} { | 
|---|
 | 60 |     list [catch {unload {} Unknown} msg] $msg | 
|---|
 | 61 | } {1 {package "Unknown" is loaded statically and cannot be unloaded}} | 
|---|
 | 62 | test unload-1.7 {-nocomplain switch} {} { | 
|---|
 | 63 |     list [unload -nocomplain {} Unknown] | 
|---|
 | 64 | } {{}} | 
|---|
 | 65 |  | 
|---|
 | 66 | set pkgua_loaded {} | 
|---|
 | 67 | set pkgua_detached {} | 
|---|
 | 68 | set pkgua_unloaded {} | 
|---|
 | 69 | # Tests for loading/unloading in trusted (non-safe) interpreters... | 
|---|
 | 70 | test unload-2.1 {basic loading of non-unloadable package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 71 |     load [file join $testDir pkga$ext] | 
|---|
 | 72 |     list [pkga_eq abc def] [lsort [info commands pkga_*]] | 
|---|
 | 73 | } {0 {pkga_eq pkga_quote}} | 
|---|
 | 74 | test unload-2.2 {basic loading of unloadable package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 75 |     list $pkgua_loaded $pkgua_detached $pkgua_unloaded \ | 
|---|
 | 76 |             [load [file join $testDir pkgua$ext]] \ | 
|---|
 | 77 |             [pkgua_eq abc def] [lsort [info commands pkgua_*]] \ | 
|---|
 | 78 |             $pkgua_loaded $pkgua_detached $pkgua_unloaded | 
|---|
 | 79 | } {{} {} {} {} 0 {pkgua_eq pkgua_quote} . {} {}} | 
|---|
 | 80 | test unload-2.3 {basic unloading of non-unloadable package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 81 |     list [catch {unload [file join $testDir pkga$ext]} msg] \ | 
|---|
 | 82 |             [string map [list [file join $testDir pkga$ext] file] $msg] | 
|---|
 | 83 | } {1 {file "file" cannot be unloaded under a trusted interpreter}} | 
|---|
 | 84 | test unload-2.4 {basic unloading of unloadable package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 85 |     list $pkgua_loaded $pkgua_detached $pkgua_unloaded \ | 
|---|
 | 86 |             [unload [file join $testDir pkgua$ext]] \ | 
|---|
 | 87 |             [info commands pkgua_*] \ | 
|---|
 | 88 |             $pkgua_loaded $pkgua_detached $pkgua_unloaded | 
|---|
 | 89 | } {. {} {} {} {} . . .} | 
|---|
 | 90 | test unload-2.5 {reloading of unloaded package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 91 |     list $pkgua_loaded $pkgua_detached $pkgua_unloaded \ | 
|---|
 | 92 |             [load [file join $testDir pkgua$ext]] \ | 
|---|
 | 93 |             [pkgua_eq abc def] [lsort [info commands pkgua_*]] \ | 
|---|
 | 94 |             $pkgua_loaded $pkgua_detached $pkgua_unloaded | 
|---|
 | 95 | } {. . . {} 0 {pkgua_eq pkgua_quote} .. . .} | 
|---|
 | 96 | test unload-2.6 {basic unloading of re-loaded package, with guess for package name} [list $dll $loaded] { | 
|---|
 | 97 |     list $pkgua_loaded $pkgua_detached $pkgua_unloaded \ | 
|---|
 | 98 |             [unload [file join $testDir pkgua$ext]] \ | 
|---|
 | 99 |             [info commands pkgua_*] \ | 
|---|
 | 100 |             $pkgua_loaded $pkgua_detached $pkgua_unloaded | 
|---|
 | 101 | } {.. . . {} {} .. .. ..} | 
|---|
 | 102 |  | 
|---|
 | 103 | # Tests for loading/unloading in safe interpreters... | 
|---|
 | 104 | interp create -safe child | 
|---|
 | 105 | child eval { | 
|---|
 | 106 |     set pkgua_loaded {} | 
|---|
 | 107 |     set pkgua_detached {} | 
|---|
 | 108 |     set pkgua_unloaded {} | 
|---|
 | 109 | } | 
|---|
 | 110 | test unload-3.1 {basic loading of non-unloadable package in a safe interpreter, with package name conversion} \ | 
|---|
 | 111 |         [list $dll $loaded] { | 
|---|
 | 112 |     catch {rename pkgb_sub {}} | 
|---|
 | 113 |     load [file join $testDir pkgb$ext] pKgB child | 
|---|
 | 114 |     list [child eval pkgb_sub 44 13] [catch {child eval pkgb_unsafe} msg] $msg \ | 
|---|
 | 115 |          [catch {pkgb_sub 12 10} msg2] $msg2 | 
|---|
 | 116 | } {31 1 {invalid command name "pkgb_unsafe"} 1 {invalid command name "pkgb_sub"}} | 
|---|
 | 117 | test unload-3.2 {basic loading of unloadable package in a safe interpreter, with package name conversion} \ | 
|---|
 | 118 |         [list $dll $loaded] { | 
|---|
 | 119 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 120 |             [load [file join $testDir pkgua$ext] pKgUA child] \ | 
|---|
 | 121 |             [child eval pkgua_eq abc def] \ | 
|---|
 | 122 |             [lsort [child eval info commands pkgua_*]] \ | 
|---|
 | 123 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 124 | } {{{} {} {}} {} 0 {pkgua_eq pkgua_quote} {. {} {}}} | 
|---|
 | 125 | test unload-3.3 {unloading of a package that has never been loaded from a safe interpreter} \ | 
|---|
 | 126 |         [list $dll $loaded] { | 
|---|
 | 127 |     list [catch {unload [file join $testDir pkga$ext] {} child} msg] \ | 
|---|
 | 128 |          [string map [list [file join $testDir pkga$ext] file] $msg] | 
|---|
 | 129 | } {1 {file "file" has never been loaded in this interpreter}} | 
|---|
 | 130 | test unload-3.4 {basic unloading of a non-unloadable package from a safe interpreter, with guess for package name} \ | 
|---|
 | 131 |         [list $dll $loaded] { | 
|---|
 | 132 |     list [catch {unload [file join $testDir pkgb$ext] {} child} msg] \ | 
|---|
 | 133 |          [string map [list [file join $testDir pkgb$ext] file] $msg] | 
|---|
 | 134 | } {1 {file "file" cannot be unloaded under a safe interpreter}} | 
|---|
 | 135 | test unload-3.5 {basic unloading of an unloadable package from a safe interpreter, with guess for package name} \ | 
|---|
 | 136 |         [list $dll $loaded] { | 
|---|
 | 137 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 138 |             [unload [file join $testDir pkgua$ext] {} child] \ | 
|---|
 | 139 |             [child eval info commands pkgua_*] \ | 
|---|
 | 140 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 141 | } {{. {} {}} {} {} {. . .}} | 
|---|
 | 142 | test unload-3.6 {reloading of unloaded package in a safe interpreter, with guess for package name} \ | 
|---|
 | 143 |         [list $dll $loaded] { | 
|---|
 | 144 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 145 |             [load [file join $testDir pkgua$ext] {} child] \ | 
|---|
 | 146 |             [child eval pkgua_eq abc def] \ | 
|---|
 | 147 |             [lsort [child eval info commands pkgua_*]] \ | 
|---|
 | 148 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 149 | } {{. . .} {} 0 {pkgua_eq pkgua_quote} {.. . .}} | 
|---|
 | 150 | test unload-3.7 {basic unloading of re-loaded package from a safe interpreter, with package name conversion} \ | 
|---|
 | 151 |         [list $dll $loaded] { | 
|---|
 | 152 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 153 |             [unload [file join $testDir pkgua$ext] pKgUa child] \ | 
|---|
 | 154 |             [child eval info commands pkgua_*] \ | 
|---|
 | 155 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 156 | } {{.. . .} {} {} {.. .. ..}} | 
|---|
 | 157 |  | 
|---|
 | 158 | # Tests for loading/unloading of a package among multiple interpreters... | 
|---|
 | 159 | interp create child-trusted | 
|---|
 | 160 | child-trusted eval { | 
|---|
 | 161 |     set pkgua_loaded {} | 
|---|
 | 162 |     set pkgua_detached {} | 
|---|
 | 163 |     set pkgua_unloaded {} | 
|---|
 | 164 | } | 
|---|
 | 165 | ## Load package in main trusted interpreter... | 
|---|
 | 166 | test unload-4.1 {loading of unloadable package in trusted interpreter, with guess for package name} \ | 
|---|
 | 167 |         [list $dll $loaded] { | 
|---|
 | 168 |     list [list $pkgua_loaded $pkgua_detached $pkgua_unloaded] \ | 
|---|
 | 169 |             [load [file join $testDir pkgua$ext]] \ | 
|---|
 | 170 |             [pkgua_eq abc def] [lsort [info commands pkgua_*]] \ | 
|---|
 | 171 |             [list $pkgua_loaded $pkgua_detached $pkgua_unloaded] | 
|---|
 | 172 | } {{.. .. ..} {} 0 {pkgua_eq pkgua_quote} {... .. ..}} | 
|---|
 | 173 | ## Load package in child-safe interpreter... | 
|---|
 | 174 | test unload-4.2 {basic loading of unloadable package in a safe interpreter, with package name conversion} \ | 
|---|
 | 175 |         [list $dll $loaded] { | 
|---|
 | 176 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 177 |             [load [file join $testDir pkgua$ext] pKgUA child] \ | 
|---|
 | 178 |             [child eval pkgua_eq abc def] \ | 
|---|
 | 179 |             [lsort [child eval info commands pkgua_*]] \ | 
|---|
 | 180 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 181 | } {{.. .. ..} {} 0 {pkgua_eq pkgua_quote} {... .. ..}} | 
|---|
 | 182 | ## Load package in child-trusted interpreter... | 
|---|
 | 183 | test unload-4.3 {basic loading of unloadable package in a second trusted interpreter, with package name conversion} \ | 
|---|
 | 184 |         [list $dll $loaded] { | 
|---|
 | 185 |     list [child-trusted eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 186 |             [load [file join $testDir pkgua$ext] pkguA child-trusted] \ | 
|---|
 | 187 |             [child-trusted eval pkgua_eq abc def] \ | 
|---|
 | 188 |             [lsort [child-trusted eval info commands pkgua_*]] \ | 
|---|
 | 189 |             [child-trusted eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 190 | } {{{} {} {}} {} 0 {pkgua_eq pkgua_quote} {. {} {}}} | 
|---|
 | 191 | ## Unload the package from the main trusted interpreter... | 
|---|
 | 192 | test unload-4.4 {basic unloading of unloadable package from trusted interpreter, with guess for package name} \ | 
|---|
 | 193 |         [list $dll $loaded] { | 
|---|
 | 194 |     list [list $pkgua_loaded $pkgua_detached $pkgua_unloaded] \ | 
|---|
 | 195 |             [unload [file join $testDir pkgua$ext]] \ | 
|---|
 | 196 |             [info commands pkgua_*] \ | 
|---|
 | 197 |             [list $pkgua_loaded $pkgua_detached $pkgua_unloaded] | 
|---|
 | 198 | } {{... .. ..} {} {} {... ... ..}} | 
|---|
 | 199 | ## Unload the package from the child safe interpreter... | 
|---|
 | 200 | test unload-4.5 {basic unloading of unloadable package from a safe interpreter, with guess for package name} \ | 
|---|
 | 201 |         [list $dll $loaded] { | 
|---|
 | 202 |     list [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 203 |             [unload [file join $testDir pkgua$ext] {} child] \ | 
|---|
 | 204 |             [child eval info commands pkgua_*] \ | 
|---|
 | 205 |             [child eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 206 | } {{... .. ..} {} {} {... ... ..}} | 
|---|
 | 207 | ## Unload the package from the child trusted interpreter... | 
|---|
 | 208 | test unload-4.6 {basic unloading of unloadable package from a safe interpreter, with guess for package name} \ | 
|---|
 | 209 |         [list $dll $loaded] { | 
|---|
 | 210 |     list [child-trusted eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] \ | 
|---|
 | 211 |             [unload [file join $testDir pkgua$ext] {} child-trusted] \ | 
|---|
 | 212 |             [child-trusted eval info commands pkgua_*] \ | 
|---|
 | 213 |             [child-trusted eval {list $pkgua_loaded $pkgua_detached $pkgua_unloaded}] | 
|---|
 | 214 | } {{. {} {}} {} {} {. . .}} | 
|---|
 | 215 |  | 
|---|
 | 216 | # cleanup | 
|---|
 | 217 | interp delete child | 
|---|
 | 218 | interp delete child-trusted | 
|---|
 | 219 | unset ext | 
|---|
 | 220 | ::tcltest::cleanupTests | 
|---|
 | 221 | return | 
|---|