Debian Codegraph @

testsuite/lib/test_simple.exp

raw
1 # test_simple.exp
2 #
3 # Simple commands for running user shell commands and checking their outputs.
4 # Also provided commands for generic string value and boolean value checks.
5
6 # run_cmd_2way CMD STDOUT_VAR STDERR_VAR
7 # * CMD is a string value for the user shell command to run.
8 # * STDOUT_VAR is the (output) variable name (without the `$` prefix!) to hold
9 # the stdout output.
10 # * STDERR_VAR is the (output) variable name (without the `$` prefix!) to hold
11 # the stderr output.
12 # Returns the exit code number
13 # TODO add timeout protection
14
15 proc run_cmd_2way { cmd stdout_var stderr_var } {
16 upvar 1 $stdout_var stdout
17 upvar 1 $stderr_var stderr
18
19 send_log "executing: $cmd\n"
20
21 set pipe [open "| sh -c {$cmd}" r]
22 set stdout [read $pipe]
23 set exit_code 0
24 if {[catch {close $pipe} stderr] != 0} {
25 if {$stderr ne "" && [string index $stderr end] ne "\n"} {
26 # Alas. close() automatically remove the trailing newline, so we
27 # have to add it back here...
28 append stderr "\n"
29 }
30 global errorCode
31 if {[lindex $errorCode 0] eq "CHILDSTATUS"} {
32 set exit_code [lindex $errorCode 2]
33 }
34 }
35
36 return $exit_code
37 }
38
39 # run_cmd_2way_as_root CMD STDOUT_VAR STDERR_VAR
40 # similar to run_cmd_2way but runs the command as root.
41
42 proc run_cmd_2way_as_root { cmd stdout_var stderr_var } {
43 set effective_uid [exec /usr/bin/id -u]
44
45 if {$effective_uid != 0} {
46 set cmd "sudo $cmd"
47 }
48
49 upvar 1 $stdout_var tmp_stdout
50 upvar 1 $stderr_var tmp_stderr
51
52 return [run_cmd_2way $cmd tmp_stdout tmp_stderr]
53 }
54
55 # like TEST_NAME GOT REGEX_PATTERN REGEX_OPTS
56 # The test passes when the TARGET_STR string matches the regex in REGEX_PATTERN.
57 # * TEST_NAME is a string describing the current check.
58 # * GOT is the target string to be checked.
59 # * REGEX_PATTERN is a tcl regular expression string to be matched against.
60 # * REGEX_OPTS is optional regex options like -expanded and -nocase.
61 # Returns 1 when the check passes, 0 otherwise.
62
63 proc like { test_name target regex regex_opts } {
64 regsub -all -- {\n} $regex {\n} escaped_regex
65 if {[regexp {*}$regex_opts -- $regex $target]} {
66 pass "${test_name}: matches regex \"$escaped_regex\""
67 return 1
68 }
69 fail "${test_name}: fails to match regex \"$escaped_regex\": got \"$target\""
70 return 0
71 }
72
73 # unlike TEST_NAME GOT REGEX_PATTERN REGEX_OPTS
74 # The test passes when the GOT string does NOT match the regex in
75 # REGEX_PATTERN.
76 # * TEST_NAME is a string describing the current check.
77 # * GOT is the target string to be checked.
78 # * REGEX_PATTERN is a tcl regular expression string to be matched against.
79 # * REGEX_OPTS is optional regex options like -expanded and -nocase.
80 # Returns 1 when the check passes, 0 otherwise.
81
82 proc unlike { test_name got regex regex_opts } {
83 regsub -all -- {\n} $regex {\n} escaped_regex
84 if {[regexp {*}$regex_opts -- $regex $got]} {
85 fail "${test_name}: should NOT match regex \"$escaped_regex\" but matches: got \"$got\""
86 return 0
87 }
88 pass "${test_name}: matches regex \"$escaped_regex\""
89 return 1
90 }
91
92 # is TEST_NAME GOT EXPECTED
93 # The test passes when the GOT string is (exactly) equal to the string EXPECTED.
94 # * TEST_NAME is a string describing the current check.
95 # * GOT is the target string to be checked.
96 # * EXPECTED is the expected string to be matched.
97 # Returns 1 when the check passes, 0 otherwise.
98
99 proc is { test_name got expected } {
100 regsub -all -- {\n} $expected {\n} escaped_exp
101 if {$got eq $expected} {
102 pass "${test_name}: string is \"$escaped_exp\""
103 return 1
104 }
105 fail "${test_name}: string should be \"$escaped_exp\", but got \"$got\""
106 return 0
107 }
108
109 # isnt TEST_NAME GOT EXPECTED
110 # The test passes when the GOT string is NOT equal to the string EXPECTED.
111 # * TEST_NAME is a string describing the current check.
112 # * GOT is the target string to be checked.
113 # * EXPECTED is the expected string to be matched.
114 # Returns 1 when the check passes, 0 otherwise.
115
116 proc isnt { test_name got expected } {
117 regsub -all -- {\n} $expected {\n} escaped_exp
118 if {$got eq $expected} {
119 fail "${test_name}: string should NOT be \"$escaped_exp\", but got \"$got\""
120 return 0
121 }
122 pass "${test_name}: string should NOT be \"$escaped_exp\""
123 return 1
124 }
125
126 # ok TEST_NAME RESULT
127 # The test passes when RESULT is a true value in Tcl.
128 # * TEST_NAME is a string describing the current check.
129 # * RESULT is the boolean value to be checked
130 # Returns 1 when the check passes, 0 otherwise.
131
132 proc ok { test_name result } {
133 regsub -all -- {\n} $expected {\n} escaped_exp
134 if {! $result} {
135 fail "${test_name}: not ok: $result"
136 return 0
137 }
138 pass "${test_name}: ok: $result"
139 return 1
140 }
141
142 # nok TEST_NAME RESULT
143 # The test passes when RESULT is a *false* value in Tcl.
144 # * TEST_NAME is a string describing the current check.
145 # * RESULT is the boolean value to be checked
146 # Returns 1 when the check passes, 0 otherwise.
147
148 proc nok { test_name result } {
149 regsub -all -- {\n} $expected {\n} escaped_exp
150 if {$result} {
151 fail "${test_name}: should NOT be ok but is ok: $result"
152 return 0
153 }
154 pass "${test_name}: sould NOT be ok: $result"
155 return 1
156 }
157
158 # process_template_file TEMPLATE_FILE OUT_FILE BIN_FILE ERR_VAR
159 # Processes the template file specified by the TEMPLATE_FILE argument,
160 # expands special macro variables in the template file, and genetates the final
161 # .stp file specified by the OUT_FILE argument.
162 # The following macro variables are supported:
163 # * $^PWD for the current working directory
164 # * $^ADDR_NAME for the hex address (without the 0x prefix) for the symbol
165 # named NAME by inspecting the binary target program file BIN_FILE via the
166 # nm utility.
167 # Returns 1 when all macro variables are expanded successfully; 0 otherwise.
168 # In case of error, the error message will be returned in the variable with
169 # the name specified by the 4th argument.
170
171 proc process_template_file { template_file out_file bin_file err_var } {
172 upvar 1 $err_var err
173
174 set in [open $template_file r]
175 set src [read $in]
176 close $in
177
178 set cwd [pwd]
179 regsub -all -- {\$\^PWD\y} $src $cwd src
180
181 set matches [regexp -all -inline -- {\$\^ADDR_([_a-zA-Z]\w*)} $src]
182
183 set nsyms 0
184 if {[llength $matches] > 0} {
185 array set names {}
186 foreach {match, name} $matches {
187 if {! [info exists names($name)]} {
188 incr nsyms
189 set names($name) 1
190 }
191 }
192
193 set nm_fh [open "| nm --defined-only ./a.out" r]
194 set hits 0
195 while {[gets $nm_fh line] >= 0} {
196 if {[regexp -- {^\s*([0-9a-f]+)\s+[DTBdtb]\s+([_a-zA-Z]\w*)\s*$} $line \
197 match addr name]} {
198 if {! [info exists names($name)]} {
199 continue
200 }
201 unset names($name)
202 send_log "found symbol '$name' at addr $addr in the nm output\n"
203 regsub -all -- "\\$\\^ADDR_$name\\y" $src $addr src
204 if {[incr hits] == $nsyms} {
205 break
206 }
207 }
208 }
209
210 if {[catch {close $nm_fh} nm_err] != 0} {
211 global errorCode
212 if {[lindex $errorCode 0] eq "CHILDSTATUS"} {
213 if {[lindex $errorCode 2] != 0} {
214 set err "failed to run nm: $nm_err"
215 return 0
216 }
217 }
218 }
219
220 if {$hits < $nsyms} {
221 set unmatched_names [array names names]
222 if {[llength $unmatched_names] == 1} {
223 set sym [lindex $unmatched_names 0]
224 set err "symbol '$sym' not found in binary file $bin_file"
225 return 0
226 }
227
228 set sym_list [join [lmap e [array names names] { expr {"'$e'"} }] ", "]
229 set err "symbols $sym_list not found in binary file $bin_file"
230 return 0
231 }
232 }
233
234 set out [open $out_file w+]
235 puts -nonewline $out $src
236 close $out
237
238 return 1
239 }