summaryrefslogtreecommitdiffstats
path: root/contrib/tcl/tests/safe.test
blob: 702bf8d2bc5dc63a3a3f5f9a17931f5e7d360793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# safe.test --
#
# This file contains a collection of tests for security policies, safe Tcl,
# and using safe interpreters. Sourcing this file into tcl runs the tests
# and generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1995-1996 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# SCCS: @(#) safe.test 1.13 97/06/24 17:33:22

# NOTE: The tests in this file only pass if you invoke them from the
#	"tests" directory.

if {[string compare test [info procs test]] == 1} then {source defs}

foreach i [interp slaves] {
  interp delete $i
}

proc equiv {x} {return $x}

test safe-1.1 {creating interpreters, should have no aliases} {
    interp aliases
} ""
test safe-1.2 {creating interpreters, should have no aliases} {
    catch {tcl_safeDeleteInterp a}
    interp create a
    set l [a aliases]
    interp delete a
    set l
} ""
test safe-1.3 {creating safe interpreters, should have no aliases} {
    catch {tcl_safeDeleteInterp a}
    interp create a -safe
    set l [a aliases]
    interp delete a
    set l
} ""

test safe-2.1 {calling tcl_SafeInit is safe} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    catch {interp eval a exec ls} msg
    tcl_safeDeleteInterp a
    set msg
} {invalid command name "exec"}
test safe-2.2 {calling tcl_safeCreateInterp on trusted interp} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l [lsort [a aliases]]
    tcl_safeDeleteInterp a
    set l
} {exit file load source tclPkgUnknown}
test safe-2.3 {calling tcl_safeCreateInterp on trusted interp} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set x [interp eval a {source [file join $tcl_library init.tcl]}]
    tcl_safeDeleteInterp a
    set x
} ""
test safe-2.4 {calling tcl_safeCreateInterp on trusted interp} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    catch {set x \
		[interp eval a {source [file join $tcl_library init.tcl]}]} msg
    tcl_safeDeleteInterp a
    list $x $msg
} {{} {}}

test safe-3.1 {tcl_safeDeleteInterp} {
    catch {tcl_safeDeleteInterp a}
    interp create a
    tcl_safeDeleteInterp a
} ""
test safe-3.2 {tcl_safeDeleteInterp, indirectly} {
    catch {tcl_safeDeleteInterp a}
    interp create a
    a alias exit tcl_safeDeleteInterp a
    a eval exit
} ""
test safe-3.3 {tcl_safeDeleteInterp, state array} {
    catch {tcl_safeDeleteInterp a}
    set tclSafea(foo) 33
    tcl_safeDeleteInterp a
    catch {set tclSafea(foo)} msg
    set msg
} {can't read "tclSafea(foo)": no such variable}
test safe-3.4 {tcl_safeDeleteInterp, state array, indirectly} {
    catch {tcl_safeDeleteInterp a}
    set tclSafea(foo) 33
    tcl_safeCreateInterp a
    a eval exit
    catch {set tclSafea(foo)} msg
    set msg
} {can't read "tclSafea(foo)": no such variable}
test safe-3.5 {tcl_safeDeleteInterp} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    catch {tcl_safeCreateInterp a} msg
    set msg
} {interpreter named "a" already exists, cannot create}
test safe-3.6 {tcl_safeDeleteInterp, indirectly} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    a eval exit
} ""
test safe-3.7 {tcl_safeDeleteInterp, state array} {
    catch {tcl_safeDeleteInterp a}
    set tclSafea(foo) 33
    tcl_safeCreateInterp a
    tcl_safeDeleteInterp a
    catch {set tclSafea(foo)} msg
    set msg
} {can't read "tclSafea(foo)": no such variable}
test safe-3.8 {tcl_safeDeleteInterp, state array, indirectly} {
    catch {tcl_safeDeleteInterp a}
    set tclSafea(foo) 33
    tcl_safeCreateInterp a
    a eval exit
    catch {set tclSafea(foo)} msg
    set msg
} {can't read "tclSafea(foo)": no such variable}

# For the following tests, we need a policyPath; we assume that the
# test directory has a subdirectory policies, and we will use that.

# Save old value of tcl_PolicyPath so we can restore it once we are
# done with this test sequence:

set my_old_auto_path $auto_path
lappend auto_path [pwd]

test safe-4.1 {loading a policy from the main directory} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l [a eval {package require globalPolicy}]
    tcl_safeDeleteInterp a
    set l
} 1.0
test safe-4.2 {same, loading into safe interpreter} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l [a eval {package require globalPolicy}]
    tcl_safeDeleteInterp a
    set l
} 1.0
test safe-4.3 {loading a policy from a subdirectory} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l [a eval {package require policyA}]
    tcl_safeDeleteInterp a
    set l
} 1.0
test safe-4.4 {loading a policy, unloading, reloading -- clean} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    tcl_safeDeleteInterp a
    tcl_safeCreateInterp a
    lappend l [a eval {package require policyA}]
    tcl_safeDeleteInterp a
    set l
} {1.0 1.0}
test safe-4.5 {loading two policies - prevented} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [catch {a eval {package require policyB}} msg]
    lappend l $msg
    tcl_safeDeleteInterp a
    set l
} {1.0 1 {security policy policyA already loaded}}
test safe-4.6 {two interpreters can have different policies} {
    catch {tcl_safeDeleteInterp a}
    catch {tcl_safeDeleteInterp b}
    tcl_safeCreateInterp a
    tcl_safeCreateInterp b
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [b eval {package require policyB}]
    tcl_safeDeleteInterp a
    tcl_safeDeleteInterp b
    set l
} {1.0 1.0}
test safe-4.7 {safe, loading policy, unloading, reloading: clean} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    tcl_safeDeleteInterp a
    tcl_safeCreateInterp a
    lappend l [a eval {package require policyA}]
    tcl_safeDeleteInterp a
    set l
} {1.0 1.0}
test safe-4.8 {safe, loading two policies - prevented} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [catch {a eval {package require policyB}} msg]
    lappend l $msg
    tcl_safeDeleteInterp a
    set l
} {1.0 1 {security policy policyA already loaded}}
test safe-4.9 {safe, two interpreters have different policies} {
    catch {tcl_safeDeleteInterp a}
    catch {tcl_safeDeleteInterp b}
    tcl_safeCreateInterp a
    tcl_safeCreateInterp b
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [b eval {package require policyB}]
    tcl_safeDeleteInterp a
    tcl_safeDeleteInterp b
    set l
} {1.0 1.0}

test safe-5.1 {unloading runs policy cleanup code} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyC}]
    tcl_safeDeleteInterp a
    set l  ;# the cleanup side-effects the global variable "l"
} {1.0 bye}

# For the following tests we need an auto_path that has the policies and
# packages directories in it.

lappend auto_path [file join [pwd] policies] \
	[file join [pwd] policies packages]

proc findPackage {i n} {
    set l [$i eval {package names}]
    if {[lsearch $l $n] > -1} {
	return 1
    }
    return 0
}

test safe-6.1 {loading packages still works} {
    catch {tcl_safeDeleteInterp a}
    interp create a
    set l ""
    a eval [list set auto_path $auto_path]   
    lappend l [a eval {package require packageA 1.0}]
    lappend l [a eval hoohum]
    lappend l [a eval info proc hoohum]
    tcl_safeDeleteInterp a
    set l
} {1.0 bazooka hoohum}
test safe-6.2 {tcl_safeCreateInterp, loading packages} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require packageA 1.0}]
    lappend l [a eval hoohum]
    lappend l [a eval info proc hoohum]
    tcl_safeDeleteInterp a
    set l
} {1.0 bazooka hoohum}
test safe-6.3 {policies vs packages} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [a eval {package require packageA}]
    lappend l [findPackage a policyA]
    lappend l [findPackage a packageA]
    lappend l [findPackage a hohum]
    tcl_safeDeleteInterp a
    set l
} {1.0 1.0 1 1 0}
test safe-6.4 {policies vs packages} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [a eval {package require packageA}]
    lappend l [findPackage a Tcl]
    lappend l [findPackage a policyA]
    lappend l [findPackage a hohum]
    tcl_safeDeleteInterp a
    set l
} {1.0 1.0 1 1 0}
test safe-6.5 {policies vs packages vs policies} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set l ""
    lappend l [a eval {package require policyA}]
    lappend l [a eval {package require packageA}]
    catch {a eval {package require policyB}} msg
    lappend l $msg
    lappend l [findPackage a Tcl]
    lappend l [findPackage a policyA]
    lappend l [findPackage a policyB]
    tcl_safeDeleteInterp a
    set l
} {1.0 1.0 {security policy policyA already loaded} 1 1 0}

# The following test checks whether the definition of tcl_endOfWord can be
# obtained from auto_loading.

test safe-7.1 {test auto-loading in safe interpreters} {
    catch {tcl_safeDeleteInterp a}
    tcl_safeCreateInterp a
    set r [catch {interp eval a {tcl_endOfWord "" 0}} msg]
    tcl_safeDeleteInterp a
    list $r $msg
} {0 -1}

# Restore settings to what they were before this file was sourced:

set auto_path $my_old_auto_path
unset my_old_auto_path

# set auto_path $old_auto_path
# unset old_auto_path
OpenPOWER on IntegriCloud