1 |
578 |
markom |
#
|
2 |
|
|
# Tests for method/variable protection and access
|
3 |
|
|
# ----------------------------------------------------------------------
|
4 |
|
|
# AUTHOR: Michael J. McLennan
|
5 |
|
|
# Bell Labs Innovations for Lucent Technologies
|
6 |
|
|
# mmclennan@lucent.com
|
7 |
|
|
# http://www.tcltk.com/itcl
|
8 |
|
|
#
|
9 |
|
|
# RCS: $Id: protection.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
10 |
|
|
# ----------------------------------------------------------------------
|
11 |
|
|
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
12 |
|
|
# ======================================================================
|
13 |
|
|
# See the file "license.terms" for information on usage and
|
14 |
|
|
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
15 |
|
|
|
16 |
|
|
if {[string compare test [info procs test]] == 1} then {source defs}
|
17 |
|
|
|
18 |
|
|
# ----------------------------------------------------------------------
|
19 |
|
|
# Class members are protected by access restrictions
|
20 |
|
|
# ----------------------------------------------------------------------
|
21 |
|
|
test protect-1.1 {define a class with various protection levels} {
|
22 |
|
|
itcl::class test_pr {
|
23 |
|
|
public {
|
24 |
|
|
variable pubv "public var"
|
25 |
|
|
common pubc "public com"
|
26 |
|
|
method pubm {} {return "public method"}
|
27 |
|
|
method ovpubm {} {return "overloaded public method"}
|
28 |
|
|
proc pubp {} {return "public proc"}
|
29 |
|
|
}
|
30 |
|
|
protected {
|
31 |
|
|
variable prov "protected var"
|
32 |
|
|
common proc "protected com"
|
33 |
|
|
method prom {} {return "protected method"}
|
34 |
|
|
method ovprom {} {return "overloaded protected method"}
|
35 |
|
|
proc prop {} {return "protected proc"}
|
36 |
|
|
}
|
37 |
|
|
private {
|
38 |
|
|
variable priv "private var"
|
39 |
|
|
common pric "private com"
|
40 |
|
|
method prim {} {return "private method"}
|
41 |
|
|
method ovprim {} {return "overloaded private method"}
|
42 |
|
|
proc prip {} {return "private proc"}
|
43 |
|
|
}
|
44 |
|
|
method do {args} {eval $args}
|
45 |
|
|
}
|
46 |
|
|
} ""
|
47 |
|
|
|
48 |
|
|
test protect-1.2 {create an object to execute tests} {
|
49 |
|
|
test_pr #auto
|
50 |
|
|
} {test_pr0}
|
51 |
|
|
|
52 |
|
|
test protect-1.3a {public methods can be accessed from outside} {
|
53 |
|
|
list [catch {test_pr0 pubm} msg] $msg
|
54 |
|
|
} {0 {public method}}
|
55 |
|
|
|
56 |
|
|
test protect-1.3b {public methods can be accessed from inside} {
|
57 |
|
|
list [catch {test_pr0 do pubm} msg] $msg
|
58 |
|
|
} {0 {public method}}
|
59 |
|
|
|
60 |
|
|
test protect-1.4a {protected methods are blocked from outside} {
|
61 |
|
|
list [catch {test_pr0 prom} msg] $msg
|
62 |
|
|
} {1 {bad option "prom": should be one of...
|
63 |
|
|
test_pr0 cget -option
|
64 |
|
|
test_pr0 configure ?-option? ?value -option value...?
|
65 |
|
|
test_pr0 do ?arg arg ...?
|
66 |
|
|
test_pr0 isa className
|
67 |
|
|
test_pr0 ovpubm
|
68 |
|
|
test_pr0 pubm}}
|
69 |
|
|
|
70 |
|
|
test protect-1.4b {protected methods can be accessed from inside} {
|
71 |
|
|
list [catch {test_pr0 do prom} msg] $msg
|
72 |
|
|
} {0 {protected method}}
|
73 |
|
|
|
74 |
|
|
test protect-1.5a {private methods are blocked from outside} {
|
75 |
|
|
list [catch {test_pr0 prim} msg] $msg
|
76 |
|
|
} {1 {bad option "prim": should be one of...
|
77 |
|
|
test_pr0 cget -option
|
78 |
|
|
test_pr0 configure ?-option? ?value -option value...?
|
79 |
|
|
test_pr0 do ?arg arg ...?
|
80 |
|
|
test_pr0 isa className
|
81 |
|
|
test_pr0 ovpubm
|
82 |
|
|
test_pr0 pubm}}
|
83 |
|
|
|
84 |
|
|
test protect-1.5b {private methods can be accessed from inside} {
|
85 |
|
|
list [catch {test_pr0 do prim} msg] $msg
|
86 |
|
|
} {0 {private method}}
|
87 |
|
|
|
88 |
|
|
test protect-1.6a {public procs can be accessed from outside} {
|
89 |
|
|
list [catch {test_pr::pubp} msg] $msg
|
90 |
|
|
} {0 {public proc}}
|
91 |
|
|
|
92 |
|
|
test protect-1.6b {public procs can be accessed from inside} {
|
93 |
|
|
list [catch {test_pr0 do pubp} msg] $msg
|
94 |
|
|
} {0 {public proc}}
|
95 |
|
|
|
96 |
|
|
test protect-1.7a {protected procs are blocked from outside} {
|
97 |
|
|
list [catch {test_pr::prop} msg] $msg
|
98 |
|
|
} {1 {can't access "::test_pr::prop": protected function}}
|
99 |
|
|
|
100 |
|
|
test protect-1.7b {protected procs can be accessed from inside} {
|
101 |
|
|
list [catch {test_pr0 do prop} msg] $msg
|
102 |
|
|
} {0 {protected proc}}
|
103 |
|
|
|
104 |
|
|
test protect-1.8a {private procs are blocked from outside} {
|
105 |
|
|
list [catch {test_pr::prip} msg] $msg
|
106 |
|
|
} {1 {can't access "::test_pr::prip": private function}}
|
107 |
|
|
|
108 |
|
|
test protect-1.8b {private procs can be accessed from inside} {
|
109 |
|
|
list [catch {test_pr0 do prip} msg] $msg
|
110 |
|
|
} {0 {private proc}}
|
111 |
|
|
|
112 |
|
|
test protect-1.9a {public commons can be accessed from outside} {
|
113 |
|
|
list [catch {set test_pr::pubc} msg] $msg
|
114 |
|
|
} {0 {public com}}
|
115 |
|
|
|
116 |
|
|
test protect-1.9b {public commons can be accessed from inside} {
|
117 |
|
|
list [catch {test_pr0 do set pubc} msg] $msg
|
118 |
|
|
} {0 {public com}}
|
119 |
|
|
|
120 |
|
|
test protect-1.10 {protected commons can be accessed from inside} {
|
121 |
|
|
list [catch {test_pr0 do set proc} msg] $msg
|
122 |
|
|
} {0 {protected com}}
|
123 |
|
|
|
124 |
|
|
test protect-1.11 {private commons can be accessed from inside} {
|
125 |
|
|
list [catch {test_pr0 do set pric} msg] $msg
|
126 |
|
|
} {0 {private com}}
|
127 |
|
|
|
128 |
|
|
test protect-1.12a {object-specific variables require an access command} {
|
129 |
|
|
list [catch {set test_pr::pubv} msg] $msg
|
130 |
|
|
} {1 {can't read "test_pr::pubv": no such variable}}
|
131 |
|
|
|
132 |
|
|
test protect-1.12b {public variables can be accessed from inside} {
|
133 |
|
|
list [catch {test_pr0 do set pubv} msg] $msg
|
134 |
|
|
} {0 {public var}}
|
135 |
|
|
|
136 |
|
|
test protect-1.13a {object-specific variables require an access command} {
|
137 |
|
|
list [catch {set test_pr::prov} msg] $msg
|
138 |
|
|
} {1 {can't read "test_pr::prov": no such variable}}
|
139 |
|
|
|
140 |
|
|
test protect-1.13b {protected variables can be accessed from inside} {
|
141 |
|
|
list [catch {test_pr0 do set prov} msg] $msg
|
142 |
|
|
} {0 {protected var}}
|
143 |
|
|
|
144 |
|
|
test protect-1.14a {object-specific variables require an access command} {
|
145 |
|
|
list [catch {set test_pr::priv} msg] $msg
|
146 |
|
|
} {1 {can't read "test_pr::priv": no such variable}}
|
147 |
|
|
|
148 |
|
|
test protect-1.14b {private variables can be accessed from inside} {
|
149 |
|
|
list [catch {test_pr0 do set priv} msg] $msg
|
150 |
|
|
} {0 {private var}}
|
151 |
|
|
|
152 |
|
|
# ----------------------------------------------------------------------
|
153 |
|
|
# Access restrictions work properly with inheritance
|
154 |
|
|
# ----------------------------------------------------------------------
|
155 |
|
|
test protect-2.1 {define a derived class} {
|
156 |
|
|
itcl::class test_pr_derived {
|
157 |
|
|
inherit test_pr
|
158 |
|
|
method do {args} {eval $args}
|
159 |
|
|
|
160 |
|
|
public method ovpubm {} {return "specific public method"}
|
161 |
|
|
protected method ovprom {} {return "specific protected method"}
|
162 |
|
|
private method ovprim {} {return "specific private method"}
|
163 |
|
|
|
164 |
|
|
public method dpubm {} {return "pub (only in derived)"}
|
165 |
|
|
protected method dprom {} {return "pro (only in derived)"}
|
166 |
|
|
private method dprim {} {return "pri (only in derived)"}
|
167 |
|
|
}
|
168 |
|
|
} ""
|
169 |
|
|
|
170 |
|
|
test protect-2.2 {create an object to execute tests} {
|
171 |
|
|
test_pr_derived #auto
|
172 |
|
|
} {test_pr_derived0}
|
173 |
|
|
|
174 |
|
|
test protect-2.3 {public methods can be accessed from inside} {
|
175 |
|
|
list [catch {test_pr_derived0 do pubm} msg] $msg
|
176 |
|
|
} {0 {public method}}
|
177 |
|
|
|
178 |
|
|
test protect-2.4 {protected methods can be accessed from inside} {
|
179 |
|
|
list [catch {test_pr_derived0 do prom} msg] $msg
|
180 |
|
|
} {0 {protected method}}
|
181 |
|
|
|
182 |
|
|
test protect-2.5 {private methods are blocked} {
|
183 |
|
|
list [catch {test_pr_derived0 do prim} msg] $msg
|
184 |
|
|
} {1 {invalid command name "prim"}}
|
185 |
|
|
|
186 |
|
|
test protect-2.6 {public procs can be accessed from inside} {
|
187 |
|
|
list [catch {test_pr_derived0 do pubp} msg] $msg
|
188 |
|
|
} {0 {public proc}}
|
189 |
|
|
|
190 |
|
|
test protect-2.7 {protected procs can be accessed from inside} {
|
191 |
|
|
list [catch {test_pr_derived0 do prop} msg] $msg
|
192 |
|
|
} {0 {protected proc}}
|
193 |
|
|
|
194 |
|
|
test protect-2.8 {private procs are blocked} {
|
195 |
|
|
list [catch {test_pr_derived0 do prip} msg] $msg
|
196 |
|
|
} {1 {invalid command name "prip"}}
|
197 |
|
|
|
198 |
|
|
test protect-2.9 {public commons can be accessed from inside} {
|
199 |
|
|
list [catch {test_pr_derived0 do set pubc} msg] $msg
|
200 |
|
|
} {0 {public com}}
|
201 |
|
|
|
202 |
|
|
test protect-2.10 {protected commons can be accessed from inside} {
|
203 |
|
|
list [catch {test_pr_derived0 do set proc} msg] $msg
|
204 |
|
|
} {0 {protected com}}
|
205 |
|
|
|
206 |
|
|
test protect-2.11 {private commons are blocked} {
|
207 |
|
|
list [catch {test_pr_derived0 do set pric} msg] $msg
|
208 |
|
|
} {1 {can't read "pric": no such variable}}
|
209 |
|
|
|
210 |
|
|
test protect-2.12 {public variables can be accessed from inside} {
|
211 |
|
|
list [catch {test_pr_derived0 do set pubv} msg] $msg
|
212 |
|
|
} {0 {public var}}
|
213 |
|
|
|
214 |
|
|
test protect-2.13 {protected variables can be accessed from inside} {
|
215 |
|
|
list [catch {test_pr_derived0 do set prov} msg] $msg
|
216 |
|
|
} {0 {protected var}}
|
217 |
|
|
|
218 |
|
|
test protect-2.14 {private variables are blocked} {
|
219 |
|
|
list [catch {test_pr_derived0 do set priv} msg] $msg
|
220 |
|
|
} {1 {can't read "priv": no such variable}}
|
221 |
|
|
|
222 |
|
|
test protect-2.15 {can access overloaded public method} {
|
223 |
|
|
set cmd {namespace eval test_pr_derived {test_pr_derived0 ovpubm}}
|
224 |
|
|
list [catch $cmd msg] $msg
|
225 |
|
|
} {0 {specific public method}}
|
226 |
|
|
|
227 |
|
|
test protect-2.16 {can access overloaded public method} {
|
228 |
|
|
set cmd {namespace eval test_pr_derived {test_pr_derived0 ovprom}}
|
229 |
|
|
list [catch $cmd msg] $msg
|
230 |
|
|
} {0 {specific protected method}}
|
231 |
|
|
|
232 |
|
|
test protect-2.17 {can access overloaded private method} {
|
233 |
|
|
set cmd {namespace eval test_pr_derived {test_pr_derived0 ovprim}}
|
234 |
|
|
list [catch $cmd msg] $msg
|
235 |
|
|
} {0 {specific private method}}
|
236 |
|
|
|
237 |
|
|
test protect-2.18 {can access overloaded public method from base class} {
|
238 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 ovpubm}}
|
239 |
|
|
list [catch $cmd msg] $msg
|
240 |
|
|
} {0 {specific public method}}
|
241 |
|
|
|
242 |
|
|
test protect-2.19 {can access overloaded protected method from base class} {
|
243 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 ovprom}}
|
244 |
|
|
list [catch $cmd msg] $msg
|
245 |
|
|
} {0 {specific protected method}}
|
246 |
|
|
|
247 |
|
|
test protect-2.20 {*cannot* access overloaded private method from base class} {
|
248 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 ovprim}}
|
249 |
|
|
list [catch $cmd msg] $msg
|
250 |
|
|
} {1 {bad option "ovprim": should be one of...
|
251 |
|
|
test_pr_derived0 cget -option
|
252 |
|
|
test_pr_derived0 configure ?-option? ?value -option value...?
|
253 |
|
|
test_pr_derived0 do ?arg arg ...?
|
254 |
|
|
test_pr_derived0 dpubm
|
255 |
|
|
test_pr_derived0 isa className
|
256 |
|
|
test_pr_derived0 ovprom
|
257 |
|
|
test_pr_derived0 ovpubm
|
258 |
|
|
test_pr_derived0 prim
|
259 |
|
|
test_pr_derived0 prom
|
260 |
|
|
test_pr_derived0 pubm}}
|
261 |
|
|
|
262 |
|
|
test protect-2.21 {can access non-overloaded public method from base class} {
|
263 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 dpubm}}
|
264 |
|
|
list [catch $cmd msg] $msg
|
265 |
|
|
} {0 {pub (only in derived)}}
|
266 |
|
|
|
267 |
|
|
test protect-2.22 {*cannot* access non-overloaded protected method from base class} {
|
268 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 dprom}}
|
269 |
|
|
list [catch $cmd msg] $msg
|
270 |
|
|
} {1 {bad option "dprom": should be one of...
|
271 |
|
|
test_pr_derived0 cget -option
|
272 |
|
|
test_pr_derived0 configure ?-option? ?value -option value...?
|
273 |
|
|
test_pr_derived0 do ?arg arg ...?
|
274 |
|
|
test_pr_derived0 dpubm
|
275 |
|
|
test_pr_derived0 isa className
|
276 |
|
|
test_pr_derived0 ovprom
|
277 |
|
|
test_pr_derived0 ovpubm
|
278 |
|
|
test_pr_derived0 prim
|
279 |
|
|
test_pr_derived0 prom
|
280 |
|
|
test_pr_derived0 pubm}}
|
281 |
|
|
|
282 |
|
|
test protect-2.23 {*cannot* access non-overloaded private method from base class} {
|
283 |
|
|
set cmd {namespace eval test_pr {test_pr_derived0 dprim}}
|
284 |
|
|
list [catch $cmd msg] $msg
|
285 |
|
|
} {1 {bad option "dprim": should be one of...
|
286 |
|
|
test_pr_derived0 cget -option
|
287 |
|
|
test_pr_derived0 configure ?-option? ?value -option value...?
|
288 |
|
|
test_pr_derived0 do ?arg arg ...?
|
289 |
|
|
test_pr_derived0 dpubm
|
290 |
|
|
test_pr_derived0 isa className
|
291 |
|
|
test_pr_derived0 ovprom
|
292 |
|
|
test_pr_derived0 ovpubm
|
293 |
|
|
test_pr_derived0 prim
|
294 |
|
|
test_pr_derived0 prom
|
295 |
|
|
test_pr_derived0 pubm}}
|
296 |
|
|
|
297 |
|
|
eval namespace delete [find classes test_pr*]
|
298 |
|
|
|
299 |
|
|
# ----------------------------------------------------------------------
|
300 |
|
|
# Access restrictions don't mess up "info"
|
301 |
|
|
# ----------------------------------------------------------------------
|
302 |
|
|
test protect-3.1 {define a base class with private variables} {
|
303 |
|
|
itcl::class test_info_base {
|
304 |
|
|
private variable pribv "pribv-value"
|
305 |
|
|
private common pribc "pribc-value"
|
306 |
|
|
protected variable probv "probv-value"
|
307 |
|
|
protected common probc "probc-value"
|
308 |
|
|
public variable pubbv "pubbv-value"
|
309 |
|
|
public common pubbc "pubbc-value"
|
310 |
|
|
}
|
311 |
|
|
itcl::class test_info_derived {
|
312 |
|
|
inherit test_info_base
|
313 |
|
|
private variable pridv "pridv-value"
|
314 |
|
|
private common pridc "pridc-value"
|
315 |
|
|
}
|
316 |
|
|
} ""
|
317 |
|
|
|
318 |
|
|
test protect-3.2 {create an object to execute tests} {
|
319 |
|
|
test_info_derived #auto
|
320 |
|
|
} {test_info_derived0}
|
321 |
|
|
|
322 |
|
|
test protect-3.3 {all variables are reported} {
|
323 |
|
|
list [catch {test_info_derived0 info variable} msg] [lsort $msg]
|
324 |
|
|
} {0 {::test_info_base::pribc ::test_info_base::pribv ::test_info_base::probc ::test_info_base::probv ::test_info_base::pubbc ::test_info_base::pubbv ::test_info_derived::pridc ::test_info_derived::pridv ::test_info_derived::this}}
|
325 |
|
|
|
326 |
|
|
test protect-3.4 {private base class variables can be accessed} {
|
327 |
|
|
list [catch {test_info_derived0 info variable pribv} msg] $msg
|
328 |
|
|
} {0 {private variable ::test_info_base::pribv pribv-value pribv-value}}
|
329 |
|
|
|
330 |
|
|
test protect-3.5 {private base class commons can be accessed} {
|
331 |
|
|
list [catch {test_info_derived0 info variable pribc} msg] $msg
|
332 |
|
|
} {0 {private common ::test_info_base::pribc pribc-value pribc-value}}
|
333 |
|
|
|
334 |
|
|
test protect-3.6 {protected base class variables can be accessed} {
|
335 |
|
|
list [catch {test_info_derived0 info variable probv} msg] $msg
|
336 |
|
|
} {0 {protected variable ::test_info_base::probv probv-value probv-value}}
|
337 |
|
|
|
338 |
|
|
test protect-3.7 {protected base class commons can be accessed} {
|
339 |
|
|
list [catch {test_info_derived0 info variable probc} msg] $msg
|
340 |
|
|
} {0 {protected common ::test_info_base::probc probc-value probc-value}}
|
341 |
|
|
|
342 |
|
|
test protect-3.8 {public base class variables can be accessed} {
|
343 |
|
|
list [catch {test_info_derived0 info variable pubbv} msg] $msg
|
344 |
|
|
} {0 {public variable ::test_info_base::pubbv pubbv-value {} pubbv-value}}
|
345 |
|
|
|
346 |
|
|
test protect-3.9 {public base class commons can be accessed} {
|
347 |
|
|
list [catch {test_info_derived0 info variable pubbc} msg] $msg
|
348 |
|
|
} {0 {public common ::test_info_base::pubbc pubbc-value pubbc-value}}
|
349 |
|
|
|
350 |
|
|
test protect-3.10 {private derived class variables can be accessed} {
|
351 |
|
|
list [catch {test_info_derived0 info variable pridv} msg] $msg
|
352 |
|
|
} {0 {private variable ::test_info_derived::pridv pridv-value pridv-value}}
|
353 |
|
|
|
354 |
|
|
test protect-3.11 {private derived class commons can be accessed} {
|
355 |
|
|
list [catch {test_info_derived0 info variable pridc} msg] $msg
|
356 |
|
|
} {0 {private common ::test_info_derived::pridc pridc-value pridc-value}}
|
357 |
|
|
|
358 |
|
|
test protect-3.12 {private base class variables can't be accessed from class} {
|
359 |
|
|
list [catch {
|
360 |
|
|
namespace eval test_info_derived {info variable pribv}
|
361 |
|
|
} msg] $msg
|
362 |
|
|
} {1 {cannot access object-specific info without an object context}}
|
363 |
|
|
|
364 |
|
|
test protect-3.13 {private base class commons can be accessed from class} {
|
365 |
|
|
list [catch {
|
366 |
|
|
namespace eval test_info_derived {info variable pribc}
|
367 |
|
|
} msg] $msg
|
368 |
|
|
} {0 {private common ::test_info_base::pribc pribc-value pribc-value}}
|
369 |
|
|
|
370 |
|
|
eval namespace delete [find classes test_info*]
|