Fossil SCM

fossil-scm / test / th1main.test
Blame History Raw 1187 lines
1
#
2
# Copyright (c) 2026 Dan Mestas
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the Simplified BSD License (also
6
# known as the "2-Clause License" or "FreeBSD License".)
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but without any warranty; without even the implied warranty of
10
# merchantability or fitness for a particular purpose.
11
#
12
############################################################################
13
#
14
# Fossil's own TH1 commands and the template renderer: src/th_main.c.
15
# Scripts are run with "fossil test-th-eval", templates with
16
# "fossil test-th-render" and "fossil test-th-source", and a ticket page
17
# that holds TH1 is asked for through "fossil http --in".
18
#
19
20
# The first sections run at every level, and the rest need
21
# -assurance stress.
22
test_setup
23
24
write_file a.txt "hello\n"
25
write_file f83.txt "line 83\n"
26
write_file f100.txt "line 100\n"
27
fossil add a.txt f83.txt f100.txt
28
fossil commit -m "three files"
29
fossil user new u1 "" pw1
30
set th1mainRepo [file join [pwd] .rep.fossil]
31
32
# Run SCRIPT with "fossil test-th-eval" and the options in ARGS. Returns
33
# what fossil printed, without the final newline.
34
#
35
# A build with TH1 hooks runs th1-setup and opens the checkout before the
36
# command itself; these tests are of the commands, so they turn hooks off.
37
fossil test-th-eval "hasfeature th1Hooks"
38
set th1mainNoHook [expr {[string trim $RESULT] eq "1" ? "--no-th-hook" : ""}]
39
40
proc th1main_eval {script args} {
41
fossil_raw -seconds 60 test-th-eval {*}$::th1mainNoHook {*}$args $script
42
return [string trimright $::RESULT \n]
43
}
44
45
# Test NAME passes if ACTUAL is as EXPECTED says. MODE is "eq" for an
46
# exact value, "re" for a regular expression, or "has" for a list of
47
# strings that must all be there (one that begins with "!" must not).
48
#
49
proc th1main_check {name actual mode expected {constraints ""}} {
50
switch -- $mode {
51
eq { test_eq $name $actual $expected $constraints }
52
re { test_match $name $actual $expected $constraints }
53
has { test $name {[holds_strings $actual $expected]} $constraints }
54
}
55
}
56
57
# Run each row of TABLE: a test name, options for test-th-eval, a
58
# script, a mode and an expected value as th1main_check takes them.
59
#
60
proc th1main_table {table} {
61
foreach {name opts script mode expected} $table {
62
set got [th1main_eval $script {*}$opts]
63
th1main_check th1main-$name $got $mode $expected
64
}
65
}
66
67
###############################################################################
68
# Every command refuses a call with too few or too many arguments. Each
69
# row is a call and the usage text of the error.
70
71
foreach {th1mainName th1mainCall th1mainUsage} {
72
dir-few {dir} {dir CHECKIN ?GLOB? ?DETAILS?}
73
dir-many {dir a b 1 d} {dir CHECKIN ?GLOB? ?DETAILS?}
74
httpize-few {httpize} {httpize STRING}
75
httpize-many {httpize a b} {httpize STRING}
76
output-few {enable_output} {enable_output [LABEL] BOOLEAN}
77
output-many {enable_output a 1 c} {enable_output [LABEL] BOOLEAN}
78
nonce-many {nonce a} {nonce}
79
puts-few {puts} {puts STRING}
80
puts-many {puts a b} {puts STRING}
81
html-few {html} {puts STRING}
82
redirect-few {redirect} {redirect URL ?withMethod?}
83
redirect-many {redirect a 1 c} {redirect URL ?withMethod?}
84
insertcsrf-many {insertCsrf a} {insertCsrf}
85
verifycsrf-many {verifyCsrf a} {verifyCsrf}
86
login-few {verifyLogin a} {verifyLogin userName password}
87
login-many {verifyLogin a b c} {verifyLogin userName password}
88
markdown-few {markdown} {markdown STRING}
89
markdown-many {markdown a b} {markdown STRING}
90
wiki-few {wiki} {wiki STRING}
91
wiki-many {wiki a b} {wiki STRING}
92
decorate-few {decorate} {wiki STRING}
93
assoc-few {wiki_assoc a} {wiki_assoc STRING STRING}
94
assoc-many {wiki_assoc a b c} {wiki_assoc STRING STRING}
95
htmlize-few {htmlize} {htmlize STRING}
96
htmlize-many {htmlize a b} {htmlize STRING}
97
encode64-few {encode64} {encode64 STRING}
98
encode64-many {encode64 a b} {encode64 STRING}
99
hascap-few {hascap} {hascap STRING ...}
100
anoncap-few {anoncap} {hascap STRING ...}
101
capexpr-few {capexpr} {capexpr EXPR}
102
capexpr-many {capexpr a b} {capexpr EXPR}
103
searchable-few {searchable} {hascap STRING ...}
104
hasfeature-few {hasfeature} {hasfeature STRING}
105
hasfeature-many {hasfeature a b} {hasfeature STRING}
106
tclready-many {tclReady a} {tclReady}
107
anycap-few {anycap} {anycap STRING}
108
anycap-many {anycap a b} {anycap STRING}
109
combobox-few {combobox a b} {combobox NAME TEXT-LIST NUMLINES}
110
combobox-many {combobox a b 1 d} {combobox NAME TEXT-LIST NUMLINES}
111
copybtn-few {copybtn a 0}
112
{copybtn TARGETID FLIPPED TEXT ?COPYLENGTH?}
113
copybtn-many {copybtn a 0 c 1 e}
114
{copybtn TARGETID FLIPPED TEXT ?COPYLENGTH?}
115
linecount-few {linecount a 1} {linecount STRING MAX MIN}
116
linecount-many {linecount a 1 2 3} {linecount STRING MAX MIN}
117
repository-many {repository 1 2} {repository ?BOOLEAN?}
118
checkout-many {checkout 1 2} {checkout ?BOOLEAN?}
119
trace-few {trace} {trace STRING}
120
trace-many {trace a b} {trace STRING}
121
state-few {globalState} {globalState NAME ?DEFAULT?}
122
state-many {globalState a b c} {globalState NAME ?DEFAULT?}
123
getparam-few {getParameter} {getParameter NAME ?DEFAULT?}
124
getparam-many {getParameter a b c} {getParameter NAME ?DEFAULT?}
125
setparam-few {setParameter a} {setParameter NAME VALUE}
126
setparam-many {setParameter a b c} {setParameter NAME VALUE}
127
reinit-many {reinitialize 1 2} {reinitialize ?FLAGS?}
128
render-few {render} {render STRING}
129
render-many {render a b} {render STRING}
130
defheader-many {defHeader a} {defHeader}
131
header-few {styleHeader} {styleHeader TITLE}
132
header-many {styleHeader a b} {styleHeader TITLE}
133
footer-many {styleFooter a} {styleFooter}
134
script-many {styleScript a b} {styleScript ?BUILTIN_NAME?}
135
submenu-few {submenu link a} {submenu link LABEL URL}
136
submenu-many {submenu link a b c} {submenu link LABEL URL}
137
submenu-word {submenu lin a b} {submenu link LABEL URL}
138
submenu-longer {submenu links a b} {submenu link LABEL URL}
139
requestjs-few {builtin_request_js} {builtin_request_js NAME}
140
requestjs-many {builtin_request_js a b} {builtin_request_js NAME}
141
artifact-few {artifact} {artifact ID ?FILENAME?}
142
artifact-many {artifact a b c} {artifact ID ?FILENAME?}
143
headerline-few {cgiHeaderLine} {cgiHeaderLine line}
144
headerline-many {cgiHeaderLine a b} {cgiHeaderLine line}
145
uvcontent-few {unversioned content} {unversioned content FILENAME}
146
uvcontent-many {unversioned content a b} {unversioned content FILENAME}
147
uvlist-many {unversioned list a} {unversioned list}
148
taint-few {taint} {STRING}
149
taint-many {taint a b} {STRING}
150
untaint-few {untaint} {STRING}
151
untaint-many {untaint a b} {STRING}
152
randhex-many {randhex 1 2} {randhex ?N?}
153
query-few {query a} {query SQL CODE}
154
query-many {query a b c} {query SQL CODE}
155
query-option-many {query -nocomplain a b c} {query SQL CODE}
156
setting-few {setting} {setting ?-strict? ?--? name}
157
setting-many {setting a b c d} {setting ?-strict? ?--? name}
158
setting-two {setting -- a b} {setting ?-strict? ?--? name}
159
setting-strict-two {setting -strict -- a b} {setting ?-strict? ?--? name}
160
glob-few {glob_match a}
161
{glob_match ?-one? ?--? patternList string}
162
glob-many {glob_match -one -- a b c}
163
{glob_match ?-one? ?--? patternList string}
164
glob-three {glob_match a b c}
165
{glob_match ?-one? ?--? patternList string}
166
regexp-few {regexp a} {regexp ?-nocase? ?--? exp string}
167
regexp-many {regexp -nocase -- a b c} {regexp ?-nocase? ?--? exp string}
168
regexp-three {regexp -- a b c} {regexp ?-nocase? ?--? exp string}
169
regexp-one {regexp -nocase -- a} {regexp ?-nocase? ?--? exp string}
170
http-few {http}
171
{http ?-asynchronous? ?--? url ?payload?}
172
http-many {http -asynchronous -- a b c}
173
{http ?-asynchronous? ?--? url ?payload?}
174
http-none {http --}
175
{http ?-asynchronous? ?--? url ?payload?}
176
http-three {http -- a b c}
177
{http ?-asynchronous? ?--? url ?payload?}
178
capture-few {captureTh1} {captureTh1 STRING}
179
capture-many {captureTh1 a b} {captureTh1 STRING}
180
} {
181
test_eq th1main-args-$th1mainName [th1main_eval $th1mainCall] \
182
"TH_ERROR: wrong # args: should be \"$th1mainUsage\""
183
}
184
185
if {[test_assurance] ne "stress"} {
186
test_skip "the tests after the first sections need -assurance stress"
187
}
188
189
###############################################################################
190
# What the commands return and write. A script that writes ends with
191
# "set x {}" so that only what it wrote is printed.
192
193
th1main_table {
194
httpize {} {httpize {a b/c?d&e=f%"<>}} eq {a+b%2Fc%3Fd%26e%3Df%25%22%3C%3E}
195
htmlize {} {htmlize {<a href="x">&'</a>}}
196
eq {&lt;a href=&quot;x&quot;&gt;&amp;&#39;&lt;/a&gt;}
197
encode64 {} {encode64 {hello, world}} eq aGVsbG8sIHdvcmxk
198
encode64-empty {} {encode64 {}} eq {}
199
markdown {} {markdown "# Title\n\nbody *text*"}
200
has {{<div class="markdown">} {<p>body <em>text</em></p>}}
201
markdown-title {} {lindex [markdown "# Title\n\nbody *text*"] 0} eq Title
202
markdown-empty {} {lindex [markdown {}] 0} eq {}
203
204
puts-escapes {} {puts "<b>&"; html "<i>"; set x {}} eq {&lt;b&gt;&amp;<i>}
205
html-tainted {} {html [taint <i>]}
206
eq {TH_ERROR: tainted output string: "<i>"}
207
puts-tainted {} {puts [taint <i>]; set x {}} eq {&lt;i&gt;}
208
html-empty {} {html {}; set x done} eq done
209
untaint {} {string is tainted [untaint [taint abc]]} eq 0
210
taint-length {} {string length [taint abc]} eq 3
211
212
hasfeature-markdown {} {hasfeature markdown} eq 1
213
hasfeature-case {} {hasfeature MARKDOWN} eq 1
214
hasfeature-unknown {} {hasfeature nosuch} eq 0
215
hasfeature-empty {} {hasfeature {}} eq 0
216
hasfeature-longer {} {hasfeature markdownx} eq 0
217
tclready {} {tclReady} eq 0
218
219
anycap-empty {} {anycap {}} eq 0
220
anycap-first {--set-user-caps} {anycap sz} eq 1
221
anycap-second {--set-user-caps} {anycap zs} eq 1
222
anycap-none {} {anycap sz} eq 0
223
hascap-empty {} {hascap {}} eq 1
224
hascap-all {--set-user-caps} {hascap s x} eq 1
225
hascap-none {} {hascap s} eq 0
226
anoncap-all {--set-anon-caps} {anoncap s x} eq 1
227
anoncap-user-only {--set-user-caps} {anoncap s} eq 0
228
capexpr-empty {} {capexpr {}} eq 0
229
capexpr-star {} {capexpr {*}} eq 1
230
capexpr-not {} {capexpr {!s}} eq 1
231
capexpr-not-held {--set-user-caps} {capexpr {!s}} eq 0
232
capexpr-anon {} {capexpr {@s}} eq 0
233
capexpr-anon-held {--set-anon-caps} {capexpr {@s}} eq 1
234
capexpr-second {} {capexpr {zz !zz}} eq 1
235
capexpr-held {--set-user-caps} {capexpr {z sx}} eq 1
236
237
linecount-max {} {linecount "a\nb\nc\nd" 3 1} eq 3
238
linecount-min {} {linecount "a\nb" 10 5} eq 5
239
linecount-between {} {linecount "a\nb\nc" 10 1} eq 3
240
linecount-empty {} {linecount {} 0 0} eq 0
241
linecount-negative {} {linecount "a\nb\nc" -5 -9} eq -5
242
linecount-widest {} {linecount "a\nb\nc" 2147483647 -2147483648} eq 3
243
linecount-bad-max {} {linecount x a 1}
244
eq {TH_ERROR: expected integer, got: "a"}
245
linecount-bad-min {} {linecount x 1 b}
246
eq {TH_ERROR: expected integer, got: "b"}
247
linecount-too-big {} {linecount x 99999999999 1} eq 1
248
249
combobox {} {set nm b; combobox nm {a b <c>} 3; set x {}}
250
eq {<select id="nm" name="nm" size="3"><option value="a">a</option><option value="b" selected="selected">b</option><option value="&lt;c&gt;">&lt;c&gt;</option></select>}
251
combobox-same-length {} {set nm x; combobox nm {a b} 1; set x {}}
252
eq {<select id="nm" name="nm" size="1"><option value="a">a</option><option value="b">b</option></select>}
253
combobox-other-length {} {set nm bb; combobox nm {a b} 1; set x {}}
254
eq {<select id="nm" name="nm" size="1"><option value="a">a</option><option value="b">b</option></select>}
255
combobox-no-variable {} {combobox {n"m} {a} 1; set x {}}
256
eq {<select id="n&quot;m" name="n&quot;m" size="1"><option value="a">a</option></select>}
257
combobox-no-values {} {set nm x; combobox nm {} 1; set x {}}
258
eq {<select id="nm" name="nm" size="1"></select>}
259
combobox-bad-lines {} {combobox nm {a b} x}
260
eq {TH_ERROR: expected integer, got: "x"}
261
262
output-off {} {enable_output 0; puts hidden; html hidden;
263
combobox nm {a b} 1; copybtn id 0 text; wiki {[x]}; decorate {[x]}
264
enable_output 1; puts shown; set x {}} eq shown
265
output-label {} {enable_output lbl 0; puts hidden; enable_output lbl 1
266
puts shown; set x {}} eq shown
267
output-bad {} {enable_output lbl xyz}
268
eq {TH_ERROR: expected integer, got: "xyz"}
269
output-off-error {} {enable_output 0; render {<th1>error shown</th1>}
270
set x {}} has {{ERROR: shown}}
271
272
copybtn {} {copybtn cid 0 text; set x {}}
273
has {{data-copytarget="cid"} {data-copylength="0"}
274
{<span id="cid">text</span>} !copy-button-flipped}
275
copybtn-flipped {} {copybtn cid 1 {<t>} 3; set x {}}
276
has {{data-copylength="3"} copy-button-flipped
277
{<span id="cid">&lt;t&gt;</span>}}
278
copybtn-bad-flipped {} {copybtn cid x text}
279
eq {TH_ERROR: expected integer, got: "x"}
280
copybtn-bad-length {} {copybtn cid 0 text x}
281
eq {TH_ERROR: expected integer, got: "x"}
282
283
repository {} {repository} re {/\.rep\.fossil$}
284
repository-0 {} {repository 0} re {/\.rep\.fossil$}
285
repository-1 {} {repository 1} re {/\.rep\.fossil$}
286
repository-bad {} {repository x} eq {TH_ERROR: expected integer, got: "x"}
287
checkout {} {checkout} re {/th1main\.test/$}
288
checkout-0 {} {checkout 0} re {/th1main\.test/$}
289
checkout-1 {} {checkout 1} re {/th1main\.test/$}
290
checkout-bad {} {checkout x} eq {TH_ERROR: expected integer, got: "x"}
291
292
state-unknown {} {globalState nosuch}
293
eq {TH_ERROR: unsupported global state: nosuch}
294
state-checkout {} {globalState checkout dflt} re {/th1main\.test/$}
295
state-case {} {globalState CHECKOUT} re {/th1main\.test/$}
296
state-longer {} {globalState checkouts}
297
eq {TH_ERROR: unsupported global state: checkouts}
298
state-configuration {--open-config} {globalState configuration dflt}
299
re {fossil}
300
state-executable {} {globalState executable dflt} re {fossil[^/]*$}
301
state-flags {} {globalState flags} eq 0
302
state-flags-config {--open-config} {expr {[globalState flags] & 0x600}}
303
eq 1024
304
state-log {} {globalState log dflt} eq dflt
305
state-log-set {--errorlog th1main-errors.log} {globalState log dflt}
306
re {th1main-errors\.log$}
307
state-repository {} {globalState repository dflt} re {/\.rep\.fossil$}
308
state-top {} {globalState top dflt} eq dflt
309
state-user {} {globalState user dflt} eq dflt
310
state-vfs {} {globalState vfs dflt} eq dflt
311
state-vfs-set {--vfs unix-none} {globalState vfs dflt} eq unix-none
312
313
reinit {} {reinitialize; globalState flags} eq 0
314
reinit-reset {} {reinitialize 4; globalState flags} eq 4
315
reinit-setup {} {reinitialize 8; globalState flags} eq 8
316
reinit-no-tcl {} {reinitialize 31; globalState flags} eq 29
317
reinit-bad {} {reinitialize x} eq {TH_ERROR: expected integer, got: "x"}
318
reinit-keeps-procs {} {proc p {} {return kept}; reinitialize 4; p} eq kept
319
320
render {} {set v 5; render {a $v <th1>puts [expr {$v+1}]</th1> b}
321
set x {}} eq {a 5 6 b}
322
render-error {} {render {<th1>error inner</th1>}; puts after}
323
eq {<hr><p class="thmainError">ERROR: inner</p>TH_ERROR: }
324
submenu-no-label {} {submenu link {} /url}
325
eq {TH_ERROR: link's LABEL is empty}
326
submenu-no-url {} {submenu link label {}} eq {TH_ERROR: link's URL is empty}
327
defheader {} {defHeader} has {<html> {$<title>}}
328
329
dir {} {dir tip} eq {a.txt f100.txt f83.txt}
330
dir-glob {} {dir tip {f*}} eq {f100.txt f83.txt}
331
dir-glob-none {} {dir tip nomatch} eq {}
332
dir-details {} {dir tip {a*} 1}
333
re {^\{a\.txt 6 \{\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\}\}$}
334
dir-details-0 {} {dir tip {a*} 0} eq a.txt
335
dir-details-bad {} {dir tip * x} eq {TH_ERROR: expected integer, got: "x"}
336
dir-unknown {} {dir nosuch} eq {}
337
dir-empty-name {} {dir {}} eq {}
338
339
artifact-file {} {artifact tip a.txt} eq hello
340
artifact-checkin {} {artifact tip} has {{C three\sfiles} {F a.txt }}
341
artifact-unknown {} {artifact nosuch} eq {TH_ERROR: name not found}
342
artifact-empty-name {} {artifact {}} eq {TH_ERROR: }
343
artifact-unknown-checkin {} {artifact nosuch a.txt}
344
eq {TH_ERROR: manifest not found}
345
artifact-not-a-checkin {} {artifact [lindex [artifact tip] 6] a.txt}
346
re {^TH_ERROR: }
347
artifact-unknown-file {} {artifact tip nosuch}
348
eq {TH_ERROR: file name not found in manifest}
349
350
glob-list {} {glob_match {*.txt,*.c} a.c} eq 2
351
glob-list-none {} {glob_match {*.txt,*.c} a.h} eq 0
352
glob-empty {} {glob_match {} x}
353
eq {TH_ERROR: unable to create glob from pattern list}
354
glob-one {} {glob_match -one {*.c} a.c} eq 1
355
glob-one-none {} {glob_match -one {*.c} a.h} eq 0
356
glob-one-dashes {} {glob_match -one -- -one -one} eq 1
357
glob-dashes {} {glob_match -- {*.c} a.h} eq 0
358
359
regexp {} {regexp {^a+b$} aaab} eq 1
360
regexp-case {} {regexp {^a+b$} AAAB} eq 0
361
regexp-nocase {} {regexp -nocase {^a+b$} AAAB} eq 1
362
regexp-dashes {} {regexp -nocase -- -nocase -NOCASE} eq 1
363
regexp-empty {} {regexp {} {}} eq 1
364
regexp-unclosed {} {regexp {((a} x} eq {TH_ERROR: unmatched '('}
365
regexp-bracket {} {regexp {[} x} eq {TH_ERROR: unclosed '['}
366
regexp-too-big {} {regexp [string repeat {(a|b)*} 2000] x}
367
eq {TH_ERROR: REGEXP pattern too big}
368
369
setting {} {setting autosync} re {^(on|off|0|1)?$}
370
setting-unknown {} {setting th1main-no-such-setting} eq {}
371
setting-strict-unknown {} {setting -strict th1main-no-such-setting}
372
eq {TH_ERROR: no value for setting "th1main-no-such-setting"}
373
374
capture {} {captureTh1 {puts <b>; html <i>}} eq {&lt;b&gt;<i>}
375
capture-error {} {captureTh1 {puts lost; error boom}} eq {TH_ERROR: boom}
376
capture-return {} {captureTh1 {return 9}} eq {TH_RETURN: 9}
377
capture-nested {} {puts a; set r [captureTh1 {puts b
378
puts [captureTh1 {puts c}]}]; puts d; set r} eq adbc
379
capture-render {} {set v <b>; captureTh1 {render {x<th1>puts y</th1>$<v>z}}}
380
eq {xy&lt;b&gt;z}
381
capture-render-error {} {captureTh1 {render {x$<nosuch>z}}} eq {TH_ERROR: }
382
capture-widgets {} {set nm a; captureTh1 {combobox nm {a} 1
383
copybtn cid 0 t}} has {{<select id="nm"} {<span id="cid">t</span>}}
384
385
login-good {} {verifyLogin u1 pw1} eq 1
386
login-wrong-password {} {verifyLogin u1 wrong} eq 0
387
login-unknown-user {} {verifyLogin nosuch pw1} eq 0
388
login-empty {} {verifyLogin {} {}} eq 0
389
390
date {} {date} re {^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$}
391
date-other-option {} {date -utc} re {^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$}
392
date-longer-option {} {date -localx} re {^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$}
393
date-same-length-option {} {date -locax}
394
re {^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$}
395
utime {} {utime} re {^\d+$}
396
stime {} {stime} re {^\d+$}
397
398
randhex {} {randhex} re {^[0-9a-f]{20}$}
399
randhex-3 {} {randhex 3} re {^[0-9a-f]{6}$}
400
randhex-0 {} {randhex 0} re {^[0-9a-f]{2}$}
401
randhex-negative {} {randhex -5} re {^[0-9a-f]{2}$}
402
randhex-49 {} {randhex 49} re {^[0-9a-f]{98}$}
403
randhex-bad {} {randhex x} eq {TH_ERROR: expected integer, got: "x"}
404
405
query {} {query {SELECT 1 AS a, 'x' AS b UNION ALL SELECT 2, 'y'} {
406
puts "$a$b "}; set x {}} eq {1x 2y }
407
query-variables {} {set v 7; set w {it's}
408
query {SELECT $v+1 AS a, $w='it''s' AS b, $nosuch IS NULL AS c,
409
:v IS NULL AS d, ?5 IS NULL AS e, ? IS NULL AS f} {
410
puts "$a/$b/$c/$d/$e/$f"}; set x {}} eq {8/1/1/1/1/1}
411
query-null-and-blob {} {query {SELECT NULL AS a, x'410042' AS b} {
412
puts "[string length $a]/[string length $b]"}; set x {}} eq 0/3
413
query-values-tainted {} {query {SELECT 'v' AS a} {
414
puts [string is tainted $a]}; set x {}} eq 1
415
query-column-names {} {query {SELECT 1 AS "", 2 AS "a(b", 3 AS "a b"} {
416
puts ran}; set x {}} eq ran
417
query-no-rows {} {query {SELECT 1 WHERE 0} {puts never}; set x none}
418
eq none
419
query-empty {} {query {} {puts never}; set x none} eq none
420
query-spaces {} {query { } {puts never}; set x none} eq none
421
query-semicolon {} {query {;} {puts never}; set x none} eq none
422
query-break {} {query {SELECT 1 AS a UNION ALL SELECT 2} {
423
puts $a; break}; set x {}} eq 12
424
query-continue {} {query {SELECT 1 AS a UNION ALL SELECT 2} {
425
puts $a; continue; puts never}; set x {}} eq 12
426
query-return {} {query {SELECT 1 AS a UNION ALL SELECT 2} {
427
puts $a; return 7}} eq {1TH_RETURN: 7}
428
query-error {} {query {SELECT 1 AS a UNION ALL SELECT 2} {
429
puts $a; error boom}} eq {1TH_ERROR: boom}
430
query-nested {} {query {SELECT 1 AS a} {query {SELECT 2 AS b} {
431
puts $a$b}}; set x {}} eq 12
432
query-bad-sql {} {query {SELECT nosuch} {puts never}}
433
eq {TH_ERROR: SQL error: no such column: nosuch}
434
query-bad-sql-quiet {} {query -nocomplain {SELECT nosuch} {puts never}
435
set x quiet} eq quiet
436
query-quiet {} {query -nocomplain {SELECT 4 AS a} {puts $a}; set x {}} eq 4
437
query-delete {} {query {DELETE FROM user} {puts never}}
438
eq {TH_ERROR: SQL error: not authorized}
439
query-delete-quiet {} {query -nocomplain {DELETE FROM user} {puts never}
440
set x quiet} eq quiet
441
query-update {} {query {UPDATE config SET value=1} {puts never}}
442
eq {TH_ERROR: SQL error: not authorized}
443
query-password {} {query {SELECT pw FROM user} {puts never}}
444
re {^TH_ERROR: SQL error: }
445
query-step-error {} {query {SELECT abs(-9223372036854775807-1) AS a} {
446
puts never}} eq {TH_ERROR: SQL error: integer overflow}
447
query-step-error-quiet {} {
448
query -nocomplain {SELECT abs(-9223372036854775807-1) AS a} {puts never}
449
set x quiet} eq quiet
450
query-option-as-sql {} {query -nocomplain {puts never}}
451
eq {TH_ERROR: SQL error: near "-": syntax error}
452
query-option-misspelt {} {query -nocomplaix {SELECT 1} {puts never}}
453
eq {TH_ERROR: wrong # args: should be "query SQL CODE"}
454
query-tainted {} {query [taint {SELECT 1}] {puts never}}
455
eq {TH_ERROR: tainted query SQL: "SELECT 1"}
456
query-undefined {} {query $nosuch {}} eq {TH_ERROR: no such variable: nosuch}
457
458
code-break {} {break} re {^TH_BREAK: }
459
code-continue {} {continue} re {^TH_CONTINUE: }
460
code-return {} {return x} eq {TH_RETURN: x}
461
code-return2 {} {return -code 5 x} eq {TH_RETURN2: x}
462
code-other {} {return -code 99 x} eq {TH1 return code 99: x}
463
}
464
465
test_eq th1main-capexpr-not-list [th1main_eval "capexpr \"\{\""] \
466
"TH_ERROR: Expected list, got: \"\{\""
467
468
# With capability "o" alone for the user and "h" alone for anonymous.
469
set env(TH1_TEST_USER_CAPS) o
470
set env(TH1_TEST_ANON_CAPS) h
471
th1main_table {
472
caps-hascap {--set-user-caps} {hascap o} eq 1
473
caps-hascap-first-missing {--set-user-caps} {hascap z o} eq 0
474
caps-hascap-last-missing {--set-user-caps} {hascap o z} eq 0
475
caps-hascap-one-word {--set-user-caps} {hascap oz} eq 0
476
caps-anycap-first {--set-user-caps} {anycap oz} eq 1
477
caps-anycap-second {--set-user-caps} {anycap zo} eq 1
478
caps-anycap-none {--set-user-caps} {anycap zh} eq 0
479
caps-anoncap {--set-anon-caps} {anoncap h} eq 1
480
caps-anoncap-missing {--set-anon-caps} {anoncap h o} eq 0
481
caps-capexpr-not {--set-user-caps} {capexpr {!o}} eq 0
482
caps-capexpr-not-other {--set-user-caps} {capexpr {!h}} eq 1
483
caps-capexpr-second {--set-user-caps} {capexpr {z o}} eq 1
484
caps-capexpr-both-needed {--set-user-caps} {capexpr {oz}} eq 0
485
caps-capexpr-anon {--set-anon-caps} {capexpr {@h}} eq 1
486
caps-capexpr-anon-missing {--set-anon-caps} {capexpr {@o}} eq 0
487
}
488
unset env(TH1_TEST_USER_CAPS) env(TH1_TEST_ANON_CAPS)
489
490
# The users still exist after the queries that tried to change them.
491
fossil sql "SELECT count(*) FROM user WHERE login='u1'"
492
test_eq th1main-query-users-kept [string trim $RESULT] 1
493
494
# hasfeature answers 1 for a feature the program is built with.
495
fossil version -vv
496
set th1mainVersion $RESULT
497
foreach {th1mainName th1mainOption} {
498
legacyMvRm FOSSIL_ENABLE_LEGACY_MV_RM
499
dynamicBuild FOSSIL_DYNAMIC_BUILD
500
json {JSON (API}
501
unicodeCmdLine UNICODE_COMMAND_LINE
502
th1Hooks FOSSIL_ENABLE_TH1_HOOKS
503
} {
504
set th1mainBuilt [expr {[string first $th1mainOption $th1mainVersion] >= 0}]
505
test_eq th1main-hasfeature-$th1mainName \
506
[th1main_eval "hasfeature $th1mainName"] $th1mainBuilt
507
}
508
509
# With timeline-utc off, "date -local" gives the time of the zone in TZ.
510
fossil settings timeline-utc 0
511
set th1mainSavedTz [expr {[info exists env(TZ)] ? $env(TZ) : ""}]
512
set env(TZ) XXX-5
513
set th1mainDates [split [th1main_eval {puts "[date]|[date -local]"; set x {}}] \
514
|]
515
if {$th1mainSavedTz eq ""} { unset env(TZ) } else \
516
{ set env(TZ) $th1mainSavedTz }
517
fossil settings timeline-utc 1
518
set th1mainDiff [expr {[clock scan [lindex $th1mainDates 1] -gmt 1]
519
- [clock scan [lindex $th1mainDates 0] -gmt 1]}]
520
test th1main-date-local {$th1mainDiff >= 17999 && $th1mainDiff <= 18001}
521
522
# A script given as the name of a file is read from the file.
523
write_file script.th1 "puts from-file; set x {}\n"
524
test_eq th1main-eval-file [th1main_eval script.th1] from-file
525
526
fossil_raw test-th-eval
527
test th1main-eval-usage {$CODE == 1 && [string match "Usage: *" $RESULT]}
528
fossil_raw test-th-eval a b
529
test th1main-eval-usage-many {$CODE == 1 && [string match "Usage: *" $RESULT]}
530
531
###############################################################################
532
# Unversioned files.
533
534
# In a repository that has never held an unversioned file, "unversioned
535
# list" gives an empty list and "unversioned content" a script error.
536
set th1mainGot [th1main_eval {unversioned list}]
537
test th1main-uv-list-none {$CODE == 0 && $th1mainGot eq ""}
538
set th1mainGot [th1main_eval {unversioned content nosuch}]
539
test th1main-uv-content-none \
540
{$CODE == 0 && [string match "TH_ERROR:*" $th1mainGot]}
541
542
write_file u.txt "data\n"
543
write_file v.txt "more\n"
544
fossil uv add u.txt v.txt
545
fossil uv rm v.txt
546
th1main_table {
547
uv-list {} {unversioned list} eq u.txt
548
uv-content {} {unversioned content u.txt} eq data
549
uv-content-unknown {} {unversioned content nosuch} re {^TH_ERROR: }
550
uv-unknown {} {unversioned nosuch}
551
eq {TH_ERROR: Expected sub-command, got: nosuch}
552
}
553
554
# knownBug: the content of a removed file is given as an empty string;
555
# "unversioned list" does not list the file.
556
test_match th1main-uv-content-removed \
557
[th1main_eval {unversioned content v.txt}] {^TH_ERROR: } knownBug
558
559
###############################################################################
560
# Calls that end fossil.
561
562
# The hashes of f83.txt and f100.txt begin with the same four digits.
563
# "artifact" given those digits is a script error, with or without a
564
# file name.
565
fossil sql "SELECT substr(uuid,1,4) FROM blob GROUP BY 1 HAVING count(*)>1"
566
set th1mainPrefix [string trim $RESULT]
567
test_eq th1main-ambiguous-setup $th1mainPrefix 2b06
568
set th1mainGot [th1main_eval "artifact $th1mainPrefix"]
569
test th1main-artifact-ambiguous \
570
{$CODE == 0 && $th1mainGot eq "TH_ERROR: ambiguous name"}
571
set th1mainGot [th1main_eval "artifact $th1mainPrefix a.txt"]
572
test th1main-artifact-ambiguous-checkin \
573
{$CODE == 0 && [string match "TH_ERROR: *" $th1mainGot]}
574
test_eq th1main-dir-ambiguous [th1main_eval "dir $th1mainPrefix"] {}
575
576
# "setting -strict" and "http -asynchronous" with nothing after the
577
# option, and "http" with an empty first argument, give a script error.
578
foreach {th1mainName th1mainCall th1mainUsage} {
579
setting-strict-alone {setting -strict} {setting ?-strict? ?--? name}
580
http-option-alone {http -asynchronous}
581
{http ?-asynchronous? ?--? url ?payload?}
582
http-empty {http {}} {http ?-asynchronous? ?--? url ?payload?}
583
} {
584
set th1mainGot [th1main_eval $th1mainCall]
585
test th1main-args-$th1mainName {$CODE == 0 && [string match \
586
"TH_ERROR: *" $th1mainGot]}
587
}
588
589
# combobox with a second argument that is not a list gives a script error.
590
set th1mainGot [th1main_eval "combobox nm \"\{\" 1"]
591
test th1main-combobox-not-list \
592
{$CODE == 0 && [string match "*TH_ERROR: *" $th1mainGot]}
593
594
# A query runs each of its statements once.
595
foreach {th1mainName th1mainSql} {
596
two {SELECT 1 AS a; SELECT 2 AS a}
597
space {SELECT 1 AS a; }
598
} {
599
fossil_raw -seconds 10 test-th-eval \
600
[join [list \
601
"set n 0; query {$th1mainSql} {set n \[expr {\$n+1}\]; if " \
602
"{\$n>50} {error endless}}; set n"] ""]
603
test th1main-query-statements-$th1mainName \
604
{$CODE == 0 && [string trim $RESULT] eq [dict get {two 2 space 1} \
605
$th1mainName]}
606
}
607
608
# builtin_request_js with a name that is not a built-in file gives a
609
# script error.
610
set th1mainGot [th1main_eval {builtin_request_js nosuch.js}]
611
test th1main-requestjs-unknown {$CODE == 0}
612
613
# randhex gives at most 50 bytes, 100 digits.
614
test_match th1main-randhex-50 [th1main_eval {randhex 50}] {^[0-9a-f]{100}$}
615
test th1main-randhex-50-exit {$CODE == 0}
616
test_match th1main-randhex-over [th1main_eval {randhex 1000}] {^[0-9a-f]{100}$}
617
618
# http with text that is not a URL gives a script error.
619
set th1mainGot [th1main_eval {http not-a-url}]
620
test th1main-http-not-url \
621
{$CODE == 0 && [string match "TH_ERROR: *" $th1mainGot]}
622
623
# A phantom: an artifact whose content the repository does not hold.
624
set th1mainPhantom \
625
abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789
626
fossil sql "INSERT INTO blob(rcvid,size,uuid) VALUES(0,-1,'$th1mainPhantom');
627
INSERT INTO phantom SELECT rid FROM blob WHERE size=-1;"
628
test_match th1main-artifact-phantom \
629
[th1main_eval "artifact $th1mainPhantom"] {^TH_ERROR: }
630
631
###############################################################################
632
# The http command. Only a URL that matches the th1-uri-regexp setting
633
# is allowed, and only an asynchronous request is implemented.
634
635
th1main_table {
636
http-ssh {} {http ssh://host/path}
637
eq {TH_ERROR: url must be http:// or https:// }
638
http-file {} {http file:///tmp/x}
639
eq {TH_ERROR: url must be http:// or https:// }
640
http-no-setting {} {http http://localhost/x} eq {TH_ERROR: url not allowed}
641
http-no-setting-async {} {http -asynchronous -- http://localhost/x}
642
eq {TH_ERROR: url not allowed}
643
}
644
fossil test-var-set th1-uri-regexp ""
645
test_eq th1main-http-empty-setting [th1main_eval {http http://localhost/x}] \
646
{TH_ERROR: url not allowed}
647
fossil settings th1-uri-regexp {((}
648
test_eq th1main-http-bad-setting [th1main_eval {http http://localhost/x}] \
649
{TH_ERROR: unmatched '('}
650
fossil settings th1-uri-regexp {^http://127\.0\.0\.1[:/]}
651
th1main_table {
652
http-other-host {} {http -asynchronous http://localhost/x}
653
eq {TH_ERROR: url not allowed}
654
http-synchronous {} {http http://127.0.0.1/x}
655
eq {TH_ERROR: synchronous requests are not yet implemented }
656
http-synchronous-post {} {http -- http://127.0.0.1/x payload}
657
eq {TH_ERROR: synchronous requests are not yet implemented }
658
}
659
660
# A server that keeps what each request sent in th1mainHeard.
661
proc th1main_accept {channel address port} {
662
fconfigure $channel -translation binary -blocking 0
663
fileevent $channel readable [list th1main_listen $channel]
664
}
665
proc th1main_listen {channel} {
666
append ::th1mainHeard [read $channel]
667
if {[eof $channel]} { close $channel }
668
}
669
if {![loopback_available]} {
670
test_skip_part "the http command needs the loopback network"
671
} else {
672
set th1mainServer [socket -server th1main_accept -myaddr 127.0.0.1 0]
673
set th1mainPort [lindex [fconfigure $th1mainServer -sockname] 2]
674
set th1mainUrl 127.0.0.1:$th1mainPort
675
676
# Run SCRIPT and give the server time to read the request.
677
proc th1main_http {script} {
678
set ::th1mainHeard ""
679
set got [th1main_eval $script]
680
after 300 {set ::th1mainWaited 1}
681
vwait ::th1mainWaited
682
return $got
683
}
684
685
set th1mainGot [th1main_http \
686
"http -asynchronous http://$th1mainUrl/dir/page?a=b; set x sent"]
687
test_eq th1main-http-get $th1mainGot sent
688
test th1main-http-get-request {[holds_strings $th1mainHeard [list \
689
"GET /dir/page?a=b HTTP/1.0\r\n" "Host: $th1mainUrl\r\n" \
690
"User-Agent: " "!Content-Length" "!Authorization"]]}
691
692
set th1mainGot [th1main_http \
693
[join [list \
694
"http -asynchronous -- http://$th1mainUrl/post {some " \
695
"payload}; set x sent"] ""]]
696
test_eq th1main-http-post $th1mainGot sent
697
test th1main-http-post-request {[holds_strings $th1mainHeard [list \
698
"POST /post HTTP/1.0\r\n" "Content-Length: 12\r\n\r\nsome payload" \
699
"Content-Type: application/x-www-form-urlencoded\r\n"]]}
700
701
# No path: the request is for "/".
702
th1main_http "http -asynchronous http://$th1mainUrl"
703
test th1main-http-no-path {[string match "GET / HTTP/1.0\r\n*" $th1mainHeard]}
704
705
# A user and a password in the URL are sent as basic authorization
706
# when the password begins with "#", written %23 in a URL.
707
fossil settings th1-uri-regexp {^http://}
708
th1main_http "http -asynchronous http://me:%23secret@$th1mainUrl/x"
709
test th1main-http-authorization {[holds_strings $th1mainHeard [list \
710
"Authorization: Basic [binary encode base64 me:secret]\r\n"]]}
711
th1main_http "http -asynchronous http://me:secret@$th1mainUrl/x"
712
test th1main-http-password-plain {[holds_strings $th1mainHeard \
713
{{GET /x HTTP/1.0} !Authorization}]}
714
th1main_http "http -asynchronous http://me@$th1mainUrl/x"
715
test th1main-http-user-only {[holds_strings $th1mainHeard \
716
{{GET /x HTTP/1.0} !Authorization}]}
717
718
close $th1mainServer
719
# Nothing listens on the port any more.
720
set th1mainGot [th1main_eval "http -asynchronous http://$th1mainUrl/x"]
721
test_match th1main-http-refused $th1mainGot {^TH_ERROR: .}
722
}
723
724
###############################################################################
725
# Tracing: --th-trace prints a log of what the commands did.
726
727
foreach {th1mainName th1mainOpts th1mainScript th1mainWanted} {
728
output {} {enable_output lbl 1} {{enable_output {lbl} -> 1<br>}}
729
hascap {--set-user-caps} {hascap s x} {{[hascap s x] => 1<br>}}
730
hascap-missing {} {hascap s x} {{[hascap s] => 0<br>}}
731
anycap {--set-user-caps} {anycap zs} {{[anycap zs] => 1<br>}}
732
searchable {} {searchable cdtw} {{[searchable cdtw] => 0<br>}}
733
hasfeature {} {hasfeature markdown} {{[hasfeature markdown] => 1<br>}}
734
tclready {} {tclReady} {{[tclReady] => 0<br>}}
735
trace {} {trace {a message}} {{a message}}
736
setting {} {setting th1main-no-such-setting}
737
{{[setting th1main-no-such-setting] => 0<br>}}
738
setting-strict {} {setting -strict th1main-no-such-setting}
739
{{[setting strict th1main-no-such-setting] => 1<br>}}
740
query {} {query {SELECT 1 AS a} {set y <$a>}}
741
{{query_eval {<pre>set y &lt;$a&gt;</pre>}<br>}
742
{[query_eval] => TH_OK {&lt;1&gt;}<br>}}
743
render {} {render {<th1>set y <b></th1>}}
744
{{render_eval {<pre>set y &lt;b&gt;</pre>}<br>}
745
{[render_eval] => TH_OK {&lt;b&gt;}<br>}}
746
init {} {reinitialize 12} {{th1-init 0x0 => 0x0<br>} {th1-init 0x0 => 0xc<br>}
747
{th1-setup {} => TH_OK<br>}}
748
header {--cgi} {styleHeader {A Title}} {{set title {A Title}<br>}
749
{maybe_set default_csp } {set project_name [taint }}
750
} {
751
set th1mainGot [th1main_eval $th1mainScript --th-trace {*}$th1mainOpts]
752
test th1main-trace-$th1mainName {[holds_strings $th1mainGot [concat \
753
{{------------------ BEGIN TRACE LOG ------------------}} \
754
$th1mainWanted]]}
755
}
756
757
###############################################################################
758
# Templates: text with TH1 between <th1> and </th1>, $name for the value
759
# of a variable as it is and $<name> for its value escaped for HTML.
760
761
# Render TEXT with "fossil test-th-render" and the options in ARGS.
762
proc th1main_render {text args} {
763
write_file template.th1 $text
764
fossil_raw -seconds 60 test-th-render {*}$args template.th1
765
return $::RESULT
766
}
767
768
set th1mainNoVar {no such variable: }
769
set th1mainErr {<hr><p class="thmainError">ERROR: }
770
foreach {th1mainName th1mainText th1mainExpected} [list \
771
plain {no markup at all} {no markup at all} \
772
empty {} {} \
773
script {a<th1>puts b</th1>c} abc \
774
script-upper {<TH1>puts 1</TH1><Th1>puts 2</tH1><tH1>puts 3</Th1>} 123 \
775
script-unclosed {a<th1>puts b} ab \
776
script-empty-unclosed {a<th1>} a \
777
script-half-closed {a<th1></th1} \
778
"a${th1mainErr}no such command: &lt;/th1</p>" \
779
not-tags {<t><th><th1 ><th2>x</t></th></th1 ></th2></th1x<xh1><tx1>} \
780
{<t><th><th1 ><th2>x</t></th></th1 ></th2></th1x<xh1><tx1>} \
781
end-tag-alone {a</th1>b} {a</th1>b} \
782
end-tag-look-alikes {<th1>set y {<x </x </tx </thx </th1x}; puts $y</th1>} \
783
{&lt;x &lt;/x &lt;/tx &lt;/thx &lt;/th1x} \
784
dollar-end {a$} {a$} \
785
dollar-bracket-end {a$<} {a$<} \
786
dollar-bracket-unclosed {a$<v b} {a$<v b} \
787
dollar-digit {a$1 $_ $: $:: $::1 $<1> ${v}} {a$1 $_ $: $:: $::1 $<1> ${v}} \
788
variables \
789
{<th1>set v {<b>&}; set ::g G; set v_2 two</th1>[$v][$<v>][$::g][$<::g>][$v_2]} \
790
{[<b>&][&lt;b&gt;&amp;][G][G][two]} \
791
variable-then-text {<th1>set v 1</th1>$v.$v-$<v>x} {1.1-1x} \
792
variable-unknown {a$nosuch b} \
793
"a${th1mainNoVar}nosuch${th1mainErr}no such variable: nosuch</p>" \
794
variable-unknown-escaped {a$<nosuch>b} \
795
"a${th1mainNoVar}nosuch${th1mainErr}no such variable: nosuch</p>" \
796
variable-unknown-then-known {<th1>set v 1</th1>a$nosuch b$v c} \
797
"a${th1mainNoVar}nosuch b1 c" \
798
variable-tainted {<th1>set t [taint <i>]</th1>[$t][$<t>]end} \
799
{[][&lt;i&gt;]end} \
800
error-plain {a<th1>error boom</th1>after} "a${th1mainErr}boom</p>" \
801
error-escaped {<th1>error <b></th1>} "${th1mainErr}&lt;b&gt;</p>" \
802
nested {<th1>set v 2; render {b$<v>|}; render {c$v}</th1>d} {b2|c2d} \
803
nested-error {<th1>render {b$<v>|}; puts never</th1>d} \
804
"b${th1mainNoVar}v${th1mainErr}no such variable: v</p>${th1mainErr}</p>" \
805
output-off \
806
{<th1>enable_output 0</th1>hidden $<v><th1>enable_output 1</th1>shown} shown \
807
] {
808
test_eq th1main-render-$th1mainName [th1main_render $th1mainText] \
809
$th1mainExpected
810
test th1main-render-$th1mainName-exit {$CODE == 0}
811
}
812
813
# A script that ends with break, continue or return ends the template;
814
# nothing of the block is written to the output.
815
foreach {th1mainName th1mainText} {
816
break {a<th1>break</th1>after}
817
continue {a<th1>continue</th1>after}
818
return {a<th1>return 5</th1>after}
819
} {
820
test_eq th1main-render-$th1mainName [th1main_render $th1mainText] a
821
}
822
823
# Text after a zero byte in the file is not rendered.
824
test_eq th1main-render-zero-byte [th1main_render "a\0b<th1>puts c</th1>"] a
825
826
set th1mainGot [th1main_render {<th1>puts hello</th1>} --cgi]
827
test th1main-render-cgi {[holds_strings $th1mainGot [list \
828
"Status: 200 OK\r\n" "Content-Length: 5\r\n" "\r\n\r\nhello"]]}
829
set th1mainGot [th1main_render {<th1>error boom</th1>} --http]
830
test th1main-render-http {[holds_strings $th1mainGot [list \
831
"HTTP/1.0 200 OK\r\n" "${th1mainErr}boom</p>"]]}
832
test_match th1main-render-open-config \
833
[th1main_render {<th1>puts [globalState configuration none]</th1>} \
834
--open-config] {fossil}
835
test_eq th1main-render-user-caps \
836
[th1main_render {<th1>puts [hascap s][anoncap s]</th1>} --set-user-caps] 10
837
test_eq th1main-render-anon-caps \
838
[th1main_render {<th1>puts [anoncap s]</th1>} --set-anon-caps] 1
839
set env(TH1_TEST_USER_CAPS) o
840
set env(TH1_TEST_ANON_CAPS) h
841
test_eq th1main-render-caps-from-environment \
842
[th1main_render \
843
{<th1>puts [hascap o][hascap s][anoncap h][anoncap s]</th1>} \
844
--set-user-caps --set-anon-caps] 1010
845
test_eq th1main-eval-caps-from-environment \
846
[th1main_eval {puts [hascap o][hascap s][anoncap h][anoncap s]; set x {}} \
847
--set-user-caps --set-anon-caps] 1010
848
unset env(TH1_TEST_USER_CAPS) env(TH1_TEST_ANON_CAPS)
849
set th1mainGot [th1main_render {<th1>trace seen</th1>} --th-trace]
850
test th1main-render-trace {[holds_strings $th1mainGot \
851
{{BEGIN TRACE LOG} seen {[render_eval] => TH_OK}}]}
852
fossil_raw test-th-render
853
test th1main-render-usage {$CODE == 1 && [string match "Usage: *" $RESULT]}
854
855
###############################################################################
856
# test-th-source runs a file as a script. It opens no repository of
857
# its own.
858
859
# Run TEXT with "fossil test-th-source" and the options in ARGS.
860
proc th1main_source {text args} {
861
write_file source.th1 $text
862
fossil_raw -seconds 60 test-th-source {*}$::th1mainNoHook {*}$args source.th1
863
return [string trimright $::RESULT \n]
864
}
865
866
test_eq th1main-source [th1main_source {puts a; set x b}] ab
867
test_eq th1main-source-error [th1main_source {error boom}] {TH_ERROR: boom}
868
test_eq th1main-source-no-result \
869
[th1main_source {puts a; set x b} --no-print-result] a
870
test_eq th1main-source-no-database \
871
[th1main_source {query {SELECT 1 AS a} {puts $a}}] \
872
{TH_ERROR: database is not open }
873
test_eq th1main-source-no-database-quiet \
874
[th1main_source \
875
{query -nocomplain {SELECT 1 AS a} {puts $a}; set x quiet}] \
876
quiet
877
test_eq th1main-source-no-repository [th1main_source {repository}] {}
878
test_match th1main-source-repository [th1main_source {repository 1}] \
879
{/\.rep\.fossil$}
880
test_match th1main-source-checkout [th1main_source {checkout 1}] \
881
{/th1main\.test/$}
882
test_eq th1main-source-checkout-not-opened [th1main_source {checkout 0}] {}
883
test_match th1main-source-open-config \
884
[th1main_source {globalState configuration none} --open-config] {fossil}
885
test_eq th1main-source-user-caps \
886
[th1main_source {puts [hascap s][anoncap s]; set x {}} --set-user-caps] 10
887
test_eq th1main-source-anon-caps \
888
[th1main_source {anoncap s} --set-anon-caps] 1
889
set env(TH1_TEST_USER_CAPS) o
890
set env(TH1_TEST_ANON_CAPS) h
891
test_eq th1main-source-caps-from-environment \
892
[th1main_source \
893
{puts [hascap o][hascap s][anoncap h][anoncap s]; set x {}} \
894
--set-user-caps --set-anon-caps] 1010
895
unset env(TH1_TEST_USER_CAPS) env(TH1_TEST_ANON_CAPS)
896
set th1mainGot [th1main_source {puts hello; set x {}} --cgi]
897
test th1main-source-cgi {[holds_strings $th1mainGot [list \
898
"Status: 200 OK\r\n" "\r\n\r\nhello"]]}
899
set th1mainGot [th1main_source {puts hello; set x {}} --http]
900
test th1main-source-http {[holds_strings $th1mainGot [list \
901
"HTTP/1.0 200 OK\r\n" "\r\n\r\nhello"]]}
902
fossil_raw test-th-source
903
test th1main-source-usage {$CODE == 1 && [string match "Usage: *" $RESULT]}
904
905
###############################################################################
906
# The th1-setup setting holds a script that runs before any other.
907
908
fossil settings th1-setup {proc from_setup {} {return made-by-setup}
909
if {[info exists ::runs]} {set ::runs 2} else {set ::runs 1}}
910
test_eq th1main-setup [th1main_eval {from_setup}] made-by-setup
911
test_eq th1main-setup-once [th1main_eval {set ::runs}] 1
912
# reinitialize with flag 8 runs the script again.
913
test_eq th1main-setup-again [th1main_eval {reinitialize 8; set ::runs}] 2
914
set th1mainGot [th1main_eval {from_setup} --th-trace]
915
test th1main-setup-trace {[holds_strings $th1mainGot [list \
916
"th1-setup \{proc from_setup \{\} \{return made-by-setup\}" \
917
"=> TH_OK<br>"]]}
918
919
fossil settings th1-setup {error {setup <failed>}}
920
test_eq th1main-setup-error [th1main_eval {set x after}] \
921
"ERROR: setup &lt;failed&gt;\nafter"
922
set th1mainGot [th1main_eval {set x after} --cgi]
923
test th1main-setup-error-cgi {[holds_strings $th1mainGot \
924
{{<hr><p class="thmainError">ERROR: setup &lt;failed&gt;</p>}}]}
925
set th1mainGot [th1main_eval {set x after} --th-trace]
926
test th1main-setup-error-trace {[holds_strings $th1mainGot \
927
{{th1-setup {error {setup &lt;failed&gt;}} => TH_ERROR<br>}}]}
928
fossil unset th1-setup
929
930
###############################################################################
931
# TH1 in a page of the web interface. The script of the "new ticket"
932
# page is replaced and the page asked for as a local administrator.
933
# The parameters of the request are variables of the script, tainted.
934
935
# Make SCRIPT the new-ticket page, send REQUEST to the server and return
936
# the reply. ARGS are more options for "fossil http".
937
#
938
proc th1main_request {script request args} {
939
global th1mainRepo
940
write_file page.th1 $script
941
fossil test-var-set ticket-newpage --file page.th1
942
write_file request.txt $request
943
file delete reply.txt
944
fossil_raw -seconds 60 http $th1mainRepo --in request.txt --out reply.txt \
945
--ipaddr 127.0.0.1 --localauth {*}$args
946
if {[file exists reply.txt]} { return [read_file reply.txt] }
947
return ""
948
}
949
950
# The same for a GET of /tktnew with the query string QUERY.
951
proc th1main_page {script {query ""} args} {
952
return [th1main_request $script [request_get /tktnew$query] {*}$args]
953
}
954
955
fossil fts-config enable cdtw
956
set th1mainGot [th1main_page {<th1>
957
puts "top=[globalState top none] user=[globalState user none]|"
958
puts "caps=[hascap s][anycap zs][anoncap s][capexpr {@s z}][capexpr {z s}]|"
959
puts "search=[searchable cdtw][searchable c d t w][searchable z][searchable c z][searchable z c][searchable {}]|"
960
puts "param=[getParameter p none][getParameter q none][getParameter q]|"
961
puts "tainted=[string is tainted [getParameter p]][string is tainted [getParameter q none]]|"
962
setParameter q set; puts "set=[getParameter q none]|"
963
puts "nonce=[nonce]|"
964
submenu link SubLabel /sublink
965
cgiHeaderLine "X-Th1main: yes\n"
966
wiki {[a] <b>wiki</b>}; decorate {[b] <i>decorated</i>}
967
copybtn cid 1 txt 7
968
builtin_request_js copybtn.js
969
styleScript copybtn.js
970
insertCsrf
971
</th1>|T=$<title>|END} "?p=one&title=%3Cu%3E"]
972
test_eq th1main-page-status [lindex [split $th1mainGot \r] 0] "HTTP/1.0 200 OK"
973
regexp {nonce=([0-9a-f]+)\|} $th1mainGot -> th1mainNonce
974
test th1main-page {[holds_strings $th1mainGot [list \
975
"top= user=" "!user=none" \
976
"caps=11111|" \
977
"search=110000|" \
978
"param=onenone|" \
979
"tainted=11|" \
980
"set=set|" \
981
"\nX-Th1main: yes\n" \
982
{>SubLabel</a>} {/sublink} \
983
{<b>wiki</b>} {[b] &lt;i&gt;decorated&lt;/i&gt;} \
984
{data-copylength="7"} \
985
{<input type="hidden" name="csrf" value="} \
986
"|T=&lt;u&gt;|END" \
987
"'nonce-$th1mainNonce'"]]}
988
989
# The wiki page that belongs to a branch is shown by wiki_assoc.
990
write_file trunk.md "About the trunk.\n"
991
fossil wiki create branch/trunk trunk.md --mimetype text/x-markdown
992
set th1mainGot [th1main_page {<th1>wiki_assoc branch trunk
993
wiki_assoc branch nosuch</th1>END}]
994
test th1main-page-wiki-assoc {[holds_strings $th1mainGot \
995
{{About the trunk.} END}]}
996
997
# styleScript without an argument renders the script of the skin, and
998
# with the name of no built-in file renders nothing.
999
set th1mainGot [th1main_page {S[<th1>styleScript nosuch.js</th1>]E}]
1000
test th1main-page-stylescript-unknown {[holds_strings $th1mainGot {{S[]E}}]}
1001
fossil test-var-set js {/* th1main skin script <th1>puts ran</th1> */}
1002
set th1mainGot [th1main_page {S[<th1>styleScript</th1>]E}]
1003
test th1main-page-stylescript-skin {[holds_strings $th1mainGot \
1004
{{S[/* th1main skin script ran */]E}}]}
1005
1006
# Redirects.
1007
set th1mainGot [th1main_page {<th1>redirect /elsewhere</th1>}]
1008
test th1main-page-redirect {[holds_strings $th1mainGot [list \
1009
"HTTP/1.0 302 " "\r\nLocation: http://x/elsewhere\r\n"]]}
1010
set th1mainGot [th1main_page {<th1>redirect /elsewhere 1</th1>}]
1011
test th1main-page-redirect-method {[holds_strings $th1mainGot [list \
1012
"HTTP/1.0 307 " "\r\nLocation: http://x/elsewhere\r\n"]]}
1013
set th1mainGot [th1main_page {<th1>redirect /elsewhere 0</th1>}]
1014
test th1main-page-redirect-no-method {[holds_strings $th1mainGot \
1015
{{HTTP/1.0 302 }}]}
1016
set th1mainGot [th1main_page {<th1>redirect /elsewhere x</th1>END}]
1017
test th1main-page-redirect-bad-method {[holds_strings $th1mainGot \
1018
{{HTTP/1.0 200 OK} {ERROR: expected integer, got: &quot;x&quot;}}]}
1019
1020
# A redirect outside a web request is refused, whatever the URL.
1021
th1main_table {
1022
redirect-no-request {} {redirect /elsewhere}
1023
eq {TH_ERROR: redirect is only allowed in a web request }
1024
redirect-no-request-http {} {redirect http://example.com/}
1025
eq {TH_ERROR: redirect is only allowed in a web request }
1026
}
1027
set th1mainGot [th1main_eval {redirect http://example.com/a} --cgi]
1028
test th1main-redirect-cgi-http {[holds_strings $th1mainGot [list \
1029
"Status: 302 " "Location: http://example.com/a\r\n"]]}
1030
set th1mainGot [th1main_eval {redirect https://example.com/a} --cgi]
1031
test th1main-redirect-cgi-https {[holds_strings $th1mainGot [list \
1032
"Status: 302 " "Location: https://example.com/a\r\n"]]}
1033
set th1mainGot [th1main_eval {redirect /relative} --cgi]
1034
test th1main-redirect-cgi-relative {[holds_strings $th1mainGot \
1035
{{redirect is only allowed in a web request} !Location}]}
1036
1037
# verifyCsrf lets through only a POST from the same site that carries
1038
# the token insertCsrf wrote.
1039
set th1mainGot [th1main_page {<th1>insertCsrf</th1>}]
1040
regexp {name="csrf" value="([^"]+)"} $th1mainGot -> th1mainToken
1041
set th1mainGot [th1main_page {<th1>verifyCsrf; puts verified</th1>}]
1042
test th1main-page-csrf-get {[holds_strings $th1mainGot \
1043
{{possible CSRF attack} !verified}]}
1044
proc th1main_post {script body args} {
1045
set headers "Host: x\r\nReferer: http://x/tktnew\r\n"
1046
append headers "Content-Type: application/x-www-form-urlencoded\r\n"
1047
append headers "Content-Length: [string length $body]\r\n"
1048
return [th1main_request $script \
1049
"POST /tktnew HTTP/1.1\r\n$headers\r\n$body" {*}$args]
1050
}
1051
set th1mainGot [th1main_post {<th1>verifyCsrf; puts verified</th1>} \
1052
"csrf=[string map {/ %2F} $th1mainToken]"]
1053
test th1main-page-csrf-post {[holds_strings $th1mainGot \
1054
{verified {!possible CSRF attack}}]}
1055
set th1mainGot [th1main_post {<th1>verifyCsrf; puts verified</th1>} \
1056
"csrf=wrong"]
1057
test th1main-page-csrf-wrong-token {[holds_strings $th1mainGot \
1058
{{possible CSRF attack} !verified}]}
1059
1060
# With --th-trace the log is at the foot of the page.
1061
set th1mainGot [th1main_page {<th1>set y 1</th1>} "?zzparam=zzvalue" --th-trace]
1062
test th1main-page-trace {[holds_strings $th1mainGot \
1063
{{BEGIN_TKTNEW<br>} {set zzparam [taint {zzvalue}]<br>}
1064
{maybe_set default_csp } {[render_eval] => TH_OK {1}<br>}}]}
1065
1066
###############################################################################
1067
# A tainted value used where it could do harm. The vuln-report setting
1068
# says what happens: the default "block" refuses the command, "log"
1069
# and "off" let it run, "fatal" shows an error page. All but "off"
1070
# write to the error log.
1071
1072
set th1mainTaintPage {A<th1>html "P=[getParameter p none]|"
1073
puts went-on</th1>|B=$title|C=$<title>|<th1>
1074
query "SELECT 1 AS n /* [getParameter p] */" {puts "n=$n"}
1075
puts went-on</th1>|END}
1076
set th1mainTaintQuery "?p=%3Ci%3E&title=%3Cu%3E"
1077
set th1mainLog [file join [pwd] th1main-taint.log]
1078
1079
# Ask for the page with the setting at VALUE. Returns the reply; what
1080
# fossil logged is in th1mainLogged.
1081
proc th1main_taint {value} {
1082
global th1mainTaintPage th1mainTaintQuery th1mainLog th1mainLogged
1083
if {$value eq ""} {
1084
fossil unset vuln-report
1085
} else {
1086
fossil settings vuln-report $value
1087
}
1088
file delete $th1mainLog
1089
set reply [th1main_page $th1mainTaintPage $th1mainTaintQuery \
1090
--errorlog $th1mainLog]
1091
set th1mainLogged ""
1092
if {[file exists $th1mainLog]} { set th1mainLogged [read_file $th1mainLog] }
1093
return $reply
1094
}
1095
1096
set th1mainGot [th1main_taint ""]
1097
test th1main-taint-block {[holds_strings $th1mainGot \
1098
{{ERROR: tainted output string: &quot;P=&lt;i&gt;|&quot;} !P=<i>
1099
!went-on !|END}]}
1100
test th1main-taint-block-log {[holds_strings $th1mainLogged \
1101
{{possible TH1 XSS vulnerability due to tainted output string: "P=<i>|"}}]}
1102
1103
set th1mainGot [th1main_taint log]
1104
test th1main-taint-log {[holds_strings $th1mainGot \
1105
{{AP=<i>|went-on|B=<u>|C=&lt;u&gt;|n=1went-on|END} !thmainError}]}
1106
test th1main-taint-log-log {[holds_strings $th1mainLogged \
1107
{{possible TH1 XSS vulnerability due to tainted output string: "P=<i>|"}
1108
{possible TH1 XSS vulnerability due to tainted inline variable: "title"}
1109
{possible TH1 SQL-injection vulnerability due to tainted query SQL: "SELECT 1 AS n /* <i> */"}}]}
1110
1111
set th1mainGot [th1main_taint off]
1112
test th1main-taint-off {[holds_strings $th1mainGot \
1113
{{AP=<i>|went-on|B=<u>|C=&lt;u&gt;|n=1went-on|END} !thmainError}]}
1114
test th1main-taint-off-log {![string match *vulnerability* $th1mainLogged]}
1115
1116
set th1mainGot [th1main_taint fatal]
1117
test th1main-taint-fatal {[holds_strings $th1mainGot \
1118
{{Configuration Error}
1119
{Error in a TH1 configuration script:}
1120
{tainted output string: "P=&lt;i&gt;|"} !|END}]}
1121
test th1main-taint-fatal-exit {$CODE == 1}
1122
test th1main-taint-fatal-log {[holds_strings $th1mainLogged \
1123
{{tainted output string: "P=<i>|"}}]}
1124
1125
# With "block", a tainted variable written as $name is left out and the
1126
# page goes on; a tainted query and a tainted redirect are refused.
1127
fossil unset vuln-report
1128
set th1mainGot [th1main_page {B=$title|C=$<title>|END} $th1mainTaintQuery]
1129
test th1main-taint-block-inline {[holds_strings $th1mainGot \
1130
{{B=|C=&lt;u&gt;|END}}]}
1131
set th1mainGot [th1main_page \
1132
{<th1>query "SELECT 1 AS n /* [getParameter p] */" {puts n=$n}</th1>END} \
1133
$th1mainTaintQuery]
1134
test th1main-taint-block-query {[holds_strings $th1mainGot \
1135
{{ERROR: tainted query SQL: &quot;SELECT 1 AS n /* &lt;i&gt; */&quot;} !n=1}]}
1136
set th1mainGot [th1main_page {<th1>redirect [getParameter u]</th1>END} \
1137
"?u=/elsewhere"]
1138
test th1main-taint-block-redirect {[holds_strings $th1mainGot \
1139
{{HTTP/1.0 200 OK}
1140
{ERROR: tainted redirect URL: &quot;/elsewhere&quot;} !Location:}]}
1141
fossil settings vuln-report off
1142
set th1mainGot [th1main_page {<th1>redirect [getParameter u]</th1>END} \
1143
"?u=/elsewhere"]
1144
test th1main-taint-off-redirect {[holds_strings $th1mainGot [list \
1145
"HTTP/1.0 302 " "\r\nLocation: http://x/elsewhere\r\n"]]}
1146
1147
###############################################################################
1148
# With no check-out and no repository.
1149
1150
fossil close --force
1151
file delete .rep.fossil
1152
th1main_table {
1153
none-repository {} {repository} eq {}
1154
none-repository-1 {} {repository 1} eq {}
1155
none-checkout-1 {} {checkout 1} eq {}
1156
none-state-checkout {} {globalState checkout dflt} eq dflt
1157
none-state-checkout-no-default {} {globalState checkout} eq {}
1158
none-state-repository {} {globalState repository dflt} eq dflt
1159
none-state-configuration {} {globalState configuration dflt} eq dflt
1160
none-dir {} {dir tip} eq {TH_ERROR: repository unavailable}
1161
none-artifact {} {artifact tip} eq {TH_ERROR: repository unavailable}
1162
none-artifact-file {} {artifact tip a.txt}
1163
eq {TH_ERROR: repository unavailable}
1164
none-header {} {styleHeader title} eq {TH_ERROR: repository unavailable}
1165
none-footer {} {styleFooter} eq {TH_ERROR: repository unavailable}
1166
none-script {} {styleScript} eq {TH_ERROR: repository unavailable}
1167
none-uv-list {} {unversioned list} eq {TH_ERROR: repository unavailable}
1168
none-uv-content {} {unversioned content u.txt}
1169
eq {TH_ERROR: repository unavailable}
1170
none-open-config {--open-config} {expr {[globalState flags] & 0x600}} eq 512
1171
}
1172
test_eq th1main-none-render-open-config \
1173
[th1main_render {<th1>puts [expr {[globalState flags] & 0x600}]</th1>} \
1174
--open-config] 512
1175
1176
# The configuration database cannot be opened when the home directory
1177
# cannot exist: the flag that says TH1 opened it stays off.
1178
set th1mainSavedHome $env(FOSSIL_HOME)
1179
set env(FOSSIL_HOME) [file join [pwd] a.txt home]
1180
test_eq th1main-none-open-config-no-home \
1181
[th1main_eval {expr {[globalState flags] & 0x600}} --open-config] 0
1182
set env(FOSSIL_HOME) $th1mainSavedHome
1183
1184
###############################################################################
1185
1186
test_cleanup
1187

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button