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
|
test_description='Check filtered content'
. ./setup.sh
for prefix in exec lua
do
filter_prereq=
test "$prefix" = exec || filter_prereq=CGIT_LUA
test_expect_success $filter_prereq "the $prefix source filter gets the file name and fills the code cell" '
cgit_ok_url "filter-$prefix/tree/a%2bb" >tmp &&
grep "<code>a+b HELLO$" tmp &&
tr -d "\n" <tmp >flat.out &&
grep "<code>a+b HELLO</code>" flat.out
'
test_expect_success $filter_prereq "the $prefix about filter gets the readme name and the tree url" '
cgit_ok_url "filter-$prefix/about/" >tmp &&
grep "<div id=.about.>a+b /filter-$prefix/tree/ HELLO$" tmp
'
test_expect_success $filter_prereq "the $prefix commit filter rewrites the subject" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<div class=.commit-subject.>ADD A+B" tmp
'
test_expect_success $filter_prereq "the $prefix email filter gets the author's address and name" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<author@example.com> commit A U THOR <AUTHOR@EXAMPLE.COM>" tmp
'
test_expect_success $filter_prereq "the $prefix email filter gets the committer's address and name" '
cgit_ok_url "filter-$prefix/commit/" >tmp &&
grep "<committer@example.com> commit C O MITTER <COMMITTER@EXAMPLE.COM>" tmp
'
done
test_expect_success 'the blame page goes through the source filter too' '
cgitrc_copy blamerc enable-blame=1 &&
cgit_rc blamerc "url=filter-exec/blame/a%2bb" >tmp &&
grep "<td class=.linenumbers.>" tmp &&
grep "<code>a+b HELLO$" tmp
'
test_expect_success 'the about filter gets the tree url at the readme branch' '
cgitrc_repo aboutargsrc filtered readme=:a+b "about-filter=exec:$FILTER_DIRECTORY/dump.sh" "repo.path=$PWD/repos/filter/.git" &&
cgit_rc aboutargsrc "url=filtered/about/&h=1%2b2" >tmp &&
grep "<div id=.about.>a+b /filtered/tree/ ?h=1%2B2 HELLO" tmp
'
test_expect_success 'the site readme leaves the tree url empty' '
echo site >root.txt &&
cgitrc_repo siterc foo "about-filter=exec:$FILTER_DIRECTORY/dump.sh" "root-readme=$PWD/root.txt" &&
cgit_rc siterc "p=about" >tmp &&
grep "root.txt SITE" tmp
'
test_expect_success 'only a repository page hands filters its variables' '
write_script env.sh <<-\EOF &&
cat >/dev/null
echo "repo url: $CGIT_REPO_URL."
EOF
echo site >root.txt &&
cgitrc_repo envrc filtered readme=:a+b "about-filter=exec:$PWD/env.sh" "root-readme=$PWD/root.txt" "repo.path=$PWD/repos/filter/.git" &&
cgit_rc envrc "url=filtered/about/" >tmp &&
grep "repo url: filtered\." tmp &&
cgit_rc envrc "p=about" >tmp &&
grep "repo url: \." tmp
'
test_expect_success 'a content filter that exits non-zero still renders the page' '
write_script failing-filter <<-\EOF &&
cat
exit 3
EOF
cgitrc_repo failingrc foo "repo.commit-filter=exec:$PWD/failing-filter" &&
cgit_ok failingrc "url=foo/commit/" >tmp &&
test_grep "<div class=.commit-subject.>commit 5" tmp
'
test_expect_success 'a filter killed by a signal ends the request' '
write_script killed-filter <<-\EOF &&
cat >/dev/null
kill -9 $$
EOF
cgitrc_repo killedrc foo "auth-filter=exec:$PWD/killed-filter" &&
cgit_rc killedrc "url=foo/" >tmp 2>err &&
test $(grep -c "^Status:" tmp) = 1 &&
test_grep "^Status: 500" tmp &&
test_grep -F "Subprocess $PWD/killed-filter exited abnormally" err
'
test_expect_success 'a filter command with no prefix runs as exec' '
cgitrc_repo bareprefixrc foo "repo.commit-filter=$FILTER_DIRECTORY/dump.sh" &&
cgit_ok bareprefixrc "url=foo/commit/" >tmp &&
test_grep "<div class=.commit-subject.>COMMIT 5" tmp
'
test_expect_success 'a filter command with an unknown prefix ends the request' '
cgitrc_repo badprefixrc foo "repo.commit-filter=perl:$FILTER_DIRECTORY/dump.sh" &&
cgit_rc badprefixrc "url=foo/commit/" >tmp 2>err &&
test $(grep -c "^Status:" tmp) = 1 &&
test_grep "^Status: 500" tmp &&
test_grep -F "Invalid filter type: perl" err
'
check_lua_error() {
test $(grep -c "^Status:" tmp) = 1 &&
test_grep "^Status: 500" tmp &&
test_grep -F "Lua error in $1: " err
}
test_expect_success CGIT_LUA 'a failing lua filter still renders an error page' '
cat >broken.lua <<-\EOF &&
function filter_open(...) error("boom") end
function filter_write(str) end
function filter_close() return 0 end
EOF
cgitrc_repo brokenrc foo "auth-filter=lua:$PWD/broken.lua" &&
cgit_rc brokenrc "url=foo/commit" >tmp 2>err &&
check_lua_error "$PWD/broken.lua"
'
test_expect_success CGIT_LUA 'a lua filter raising a table is named by type' '
cat >tabled.lua <<-\EOF &&
function filter_open(...) error({}) end
function filter_write(str) end
function filter_close() return 0 end
EOF
cgitrc_repo tabledrc foo "auth-filter=lua:$PWD/tabled.lua" &&
cgit_rc tabledrc "url=foo/commit" >tmp 2>err &&
grep "Status: 500" tmp &&
grep "Lua error in .*tabled.lua: (error object is a table value)" err
'
test_expect_success CGIT_LUA 'a lua filter with a syntax error ends the request' '
cat >syntax.lua <<-\EOF &&
function filter_open(...) end
function filter_write(str) html(str end
function filter_close() return 0 end
EOF
cgitrc_repo syntaxrc foo "auth-filter=lua:$PWD/syntax.lua" &&
cgit_rc syntaxrc "url=foo/" >tmp 2>err &&
check_lua_error "$PWD/syntax.lua" &&
test_grep "syntax\.lua:2: " err
'
test_expect_success CGIT_LUA 'a lua filter whose script is missing ends the request' '
cgitrc_repo missingrc foo "auth-filter=lua:$PWD/no-such.lua" &&
cgit_rc missingrc "url=foo/" >tmp 2>err &&
check_lua_error "$PWD/no-such.lua" &&
test_grep -F "cannot open $PWD/no-such.lua" err
'
test_expect_success CGIT_LUA 'a lua filter that fails in filter_close ends the request' '
cat >close-error.lua <<-\EOF &&
function filter_open(...) end
function filter_write(str) end
function filter_close() error("boom") end
EOF
cgitrc_repo closeerrorrc foo "auth-filter=lua:$PWD/close-error.lua" &&
cgit_rc closeerrorrc "url=foo/" >tmp 2>err &&
check_lua_error "$PWD/close-error.lua"
'
test_expect_success CGIT_LUA 'a lua filter that fails in filter_write ends the request' '
cat >write-error.lua <<-\EOF &&
function filter_open(...) end
function filter_write(str) error("boom") end
function filter_close() return 0 end
EOF
cgitrc_repo writeerrorrc foo "auth-filter=lua:$PWD/write-error.lua" &&
printf "user=a" >post &&
(
export REQUEST_METHOD=POST CONTENT_LENGTH=6 &&
cgit_rc writeerrorrc "p=login" <post
) >tmp 2>err &&
check_lua_error "$PWD/write-error.lua"
'
test_expect_success CGIT_LUA 'a lua filter without filter_write ends the request' '
cat >no-write.lua <<-\EOF &&
function filter_open(...) end
function filter_close() return 0 end
EOF
cgitrc_repo nowriterc foo "auth-filter=lua:$PWD/no-write.lua" &&
printf "user=a" >post &&
(
export REQUEST_METHOD=POST CONTENT_LENGTH=6 &&
cgit_rc nowriterc "p=login" <post
) >tmp 2>err &&
check_lua_error "$PWD/no-write.lua"
'
test_expect_success CGIT_LUA 'a lua filter that writes while loading still renders' '
cat >loud.lua <<-\EOF &&
html("written while loading")
function filter_open(...) end
function filter_write(str) html(str) end
function filter_close() return 0 end
EOF
cgitrc_repo loudrc foo "repo.commit-filter=lua:$PWD/loud.lua" &&
cgit_ok loudrc "url=foo/commit" >tmp &&
grep "<div class=.commit-subject.>written while loading" tmp
'
test_expect_success CGIT_LUA 'git can trace before the filters are set up' '
CGIT_CONFIG="$PWD/cgitrc" GIT_TRACE2="$PWD/trace2.out" QUERY_STRING="url=foo/" cgit >tmp &&
grep "</html>" tmp &&
test -s trace2.out
'
test_expect_success CGIT_LUA 'a lua filter can encode an argument and include a file' '
echo "included <b>bold</b>" >include.txt &&
cat >apis.lua <<-EOF &&
function filter_open(...) end
function filter_write(str)
html_url_arg("a b&c")
html("|")
html_include("$PWD/include.txt")
end
function filter_close() return 0 end
EOF
cgitrc_repo apisrc foo "repo.commit-filter=lua:$PWD/apis.lua" &&
cgit_rc apisrc "url=foo/commit" >tmp &&
grep "a+b%26c|included <b>bold</b>" tmp
'
test_expect_success CGIT_LUA 'a lua filter prints where it runs' '
cat >print.lua <<-\EOF &&
function filter_open(...) end
function filter_write(str)
print("printed", 1, nil, true)
html(str)
end
function filter_close() return 0 end
EOF
cgitrc_repo printrc foo "repo.commit-filter=lua:$PWD/print.lua" &&
cgit_rc printrc "url=foo/commit" >tmp &&
tab=$(printf "\t") &&
grep "<div class=.commit-subject.>printed${tab}1${tab}nil${tab}true$" tmp &&
sed -n "/<\/html>/,\$p" tmp >tail &&
! grep printed tail
'
test_done
|