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
|
test_description='Validate cache'
. ./setup.sh
request_all_pages() {
cgit_url "" &&
for repo in foo bar
do
for page in "" /refs /tree /log /diff /patch
do
cgit_url "$repo$page" || return 1
done
done
}
set_cache_size() {
sed -e "s/^cache-size=.*$/cache-size=$1/" cgitrc >cgitrc.tmp &&
mv -f cgitrc.tmp cgitrc
}
test_expect_success 'verify cache-size=0' '
rm -f cache/* &&
set_cache_size 0 &&
request_all_pages &&
ls cache >output &&
test_line_count = 0 output
'
test_expect_success 'verify cache-size=1' '
rm -f cache/* &&
set_cache_size 1 &&
request_all_pages &&
ls cache >output &&
test_line_count = 1 output
'
test_expect_success 'verify cache-size=1021' '
rm -f cache/* &&
set_cache_size 1021 &&
request_all_pages &&
ls cache >output &&
test_line_count = 13 output &&
cgit_url "foo/ls_cache" >output.full &&
strip_headers <output.full >output &&
test_line_count = 13 output &&
cgit_url "foo/ls_cache" >output.second &&
test_cmp output.full output.second
'
test_expect_success 'set up a repo with a page larger than the output buffer' '
mkrepo repos/bigpage 1 &&
(
cd repos/bigpage &&
awk "BEGIN{for(i=0;i<20000;i++) print \"line \" i \" of the big file\"}" >big.txt &&
git add big.txt &&
git commit -m "add big.txt"
) &&
{
echo "virtual-root=/" &&
echo "cache-root=$PWD/cache2" &&
echo "cache-size=64" &&
echo "enable-tree-linenumbers=1" &&
echo "repo.url=bigpage" &&
echo "repo.path=$PWD/repos/bigpage/.git"
} >bigrc &&
rm -rf cache2 && mkdir cache2
'
bigpage_query() {
CGIT_CONFIG="$PWD/bigrc" QUERY_STRING="$1" cgit
}
test_expect_success 'the first request fills the slot and the second replays it' '
bigpage_query "url=bigpage/tree/big.txt" >big.first &&
test $(wc -c <big.first) -gt 65536 &&
bigpage_query "url=bigpage/tree/big.txt" >big.second &&
strip_headers <big.first >big.first.body &&
strip_headers <big.second >big.second.body &&
test_cmp big.first.body big.second.body
'
test_expect_success 'the cached body matches one generated with the cache off' '
sed -e "s/^cache-size=64$/cache-size=0/" bigrc >bignocache &&
CGIT_CONFIG="$PWD/bignocache" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.nocache &&
strip_headers <big.nocache >big.nocache.body &&
# The footer carries the time the page was generated, which the two runs
# cannot agree on.
sed -e "s/generated by .*//" big.nocache.body >big.nocache.timeless &&
sed -e "s/generated by .*//" big.second.body >big.cached.timeless &&
test_cmp big.nocache.timeless big.cached.timeless
'
test_expect_success 'the page ends where it should, so nothing was dropped' '
tail -c 200 big.second.body | grep "</html>"
'
test_expect_success 'an over-long cache key leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
sed -e "s|^cache-root=.*|cache-root=$PWD/cache3|" bigrc >bigkeyrc &&
long=$(awk "BEGIN{s=\"\";for(i=0;i<6000;i++)s=s \"k\"; print s}") &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/&q=$long" cgit >key.out 2>key.err &&
grep "</html>" key.out &&
ls cache3 >key.slots &&
test_line_count = 0 key.slots
'
test_expect_success 'an ordinary key still fills a slot' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
ls cache3 >key.slots &&
test_line_count = 1 key.slots
'
test_expect_success 'set up a repo missing a parent object' '
mkrepo repos/broken 2 &&
parent=$(git -C repos/broken rev-parse HEAD^) &&
tip=$(git -C repos/broken rev-parse HEAD) &&
rm "repos/broken/.git/objects/$(echo "$parent" | cut -c1-2)/$(echo "$parent" | cut -c3-)" &&
{
echo "virtual-root=/" &&
echo "cache-root=$PWD/cache4" &&
echo "cache-size=64" &&
echo "repo.url=broken" &&
echo "repo.path=$PWD/repos/broken/.git"
} >brokenrc &&
rm -rf cache4 && mkdir cache4
'
test_expect_success 'an error after output began ends the page with it and caches nothing' '
CGIT_CONFIG="$PWD/brokenrc" QUERY_STRING="url=broken/commit/&id=$tip" cgit >broken.out &&
grep "Not a commit" broken.out &&
test $(grep -c "^Status:" broken.out) = 1 &&
ls cache4 >broken.slots &&
test_line_count = 0 broken.slots
'
test_expect_success 'an error page leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" \
QUERY_STRING="url=bigpage/commit/&id=0123456789abcdef0123456789abcdef01234567" cgit >error.out &&
grep "Not a commit" error.out &&
ls cache3 >error.slots &&
test_line_count = 0 error.slots
'
test_expect_success 'a clone file leaves no slot behind' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/info/refs" cgit >clone.out &&
grep "refs/heads/master" clone.out &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/objects/info/packs" cgit >packs.out &&
grep "^Status: 200" packs.out &&
ls cache3 >clone.slots &&
test_line_count = 0 clone.slots
'
test_expect_success 'a response over cache-max-slot-size is served but not kept' '
rm -rf cache3 && mkdir cache3 &&
{
echo "cache-max-slot-size=32" &&
cat bigkeyrc
} >slotsizerc &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.unkept &&
tail -c 200 big.unkept | grep "</html>" &&
ls cache3 >unkept.slots &&
test_line_count = 0 unkept.slots &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/" cgit >/dev/null &&
ls cache3 >kept.slots &&
test_line_count = 1 kept.slots
'
test_expect_success 'an expired slot is dropped when its refill is too large' '
rm -rf cache3 && mkdir cache3 &&
CGIT_CONFIG="$PWD/bigkeyrc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >/dev/null &&
ls cache3 >filled.slots &&
test_line_count = 1 filled.slots &&
touch -t 200001010000 cache3/* &&
CGIT_CONFIG="$PWD/slotsizerc" QUERY_STRING="url=bigpage/tree/big.txt" cgit >big.refill &&
tail -c 200 big.refill | grep "</html>" &&
ls cache3 >refill.slots &&
test_line_count = 0 refill.slots
'
test_done
|