tests: "wc -c" portability for *BSDs
[rainbows.git] / t / byte-range-common.sh
blob2c12a8b872944767593079027c0dbfee1e6710ff
1 t_begin "byte-range setup vars" && {
2 random_blob_size=$(count_bytes < random_blob)
3 rb_1=$(( $random_blob_size - 1 ))
4 range_head=-r-365
5 range_tail=-r155-
6 range_mid=-r200-300
7 range_n1=-r0-$rb_1
8 range_n2=-r0-$(($rb_1 - 1))
9 range_1b_head=-r0-0
10 range_1b_tail=-r$rb_1-$rb_1
11 range_1b_mid=-r200-200
12 range_all=-r0-$random_blob_size
13 url=http://$listen/random_blob
16 check_content_range () {
17 grep '^< HTTP/1\.1 206 Partial Content' $err
18 grep 'Range:' $err
19 # Content-Range: bytes #{offset}-#{offset+count-1}/#{clen}
20 d='\([0-9]\+\)'
21 start= end= size=
22 eval $(< $err sed -n -e \
23 "s/^< Content-Range: bytes $d-$d\/$d"'.*$/start=\1 end=\2 size=\3/p')
24 test -n "$start"
25 test -n "$end"
26 test -n "$size"
28 # ensure we didn't screw up the sed invocation
29 expect="< Content-Range: bytes $start-$end/$size"
30 test x"$(grep -F "$expect" $err)" = x"$(grep '^< Content-Range:' $err)"
32 test $start -le $end
33 test $end -lt $size
36 t_begin "read random blob sha1s" && {
37 sha1_head=$(curl -sSff $range_head file://random_blob | rsha1)
38 sha1_tail=$(curl -sSff $range_tail file://random_blob | rsha1)
39 sha1_mid=$(curl -sSff $range_mid file://random_blob | rsha1)
40 sha1_n1=$(curl -sSff $range_n1 file://random_blob | rsha1)
41 sha1_n2=$(curl -sSff $range_n2 file://random_blob | rsha1)
42 sha1_1b_head=$(curl -sSff $range_1b_head file://random_blob | rsha1)
43 sha1_1b_tail=$(curl -sSff $range_1b_tail file://random_blob | rsha1)
44 sha1_1b_mid=$(curl -sSff $range_1b_mid file://random_blob | rsha1)
45 sha1_all=$(rsha1 < random_blob)
46 echo "$sha1_all=$sha1_n1"
49 t_begin "normal full request matches" && {
50 sha1="$(curl -v 2>$err -sSf $url | rsha1)"
51 test x"$sha1_all" = x"$sha1"
52 grep 'Content-Range:' $err && die "Content-Range unexpected"
53 grep 'HTTP/1.1 200 OK' $err || die "200 response expected"
56 t_begin "crazy offset goes over" && {
57 range_insane=-r$(($random_blob_size * 2))-$(($random_blob_size * 4))
58 curl -vsS 2>$err $range_insane $url >/dev/null
59 grep '^< HTTP/1\.[01] 416 ' $err || die "expected 416 error"
60 grep '^< Content-Range: bytes \*/'$random_blob_size $err || \
61 die "expected Content-Range: bytes */SIZE"
64 t_begin "keepalive/pipelining is supported on 416 responses" && {
65 rm -f $tmp
67 cat $fifo > $tmp &
68 printf 'GET /byte-range-common.sh HTTP/1.1\r\n'
69 printf 'Host: %s\r\n' $listen
70 printf 'Range: bytes=9999999999-9999999999\r\n\r\n'
71 printf 'GET /byte-range-common.sh HTTP/1.1\r\n'
72 printf 'Host: %s\r\n' $listen
73 printf 'Connection: close\r\n'
74 printf 'Range: bytes=0-0\r\n\r\n'
75 wait
76 ) | socat - TCP:$listen > $fifo
78 < $tmp awk '
79 /^HTTP\/1\.1 / && NR == 1 && $2 == 416 { first = $2 }
80 /^HTTP\/1\.1 / && NR != 1 && $2 == 206 { second = $2 }
81 END { exit((first == 416 && second == 206) ? 0 : 1) }
85 t_begin "full request matches with explicit ranges" && {
86 sha1="$(curl -v 2>$err $range_all -sSf $url | rsha1)"
87 check_content_range
88 test x"$sha1_all" = x"$sha1"
90 sha1="$(curl -v 2>$err $range_n1 -sSf $url | rsha1)"
91 check_content_range
92 test x"$sha1_all" = x"$sha1"
94 range_over=-r0-$(($random_blob_size * 2))
95 sha1="$(curl -v 2>$err $range_over -sSf $url | rsha1)"
96 check_content_range
97 test x"$sha1_all" = x"$sha1"
100 t_begin "no fence post errors" && {
101 sha1="$(curl -v 2>$err $range_n2 -sSf $url | rsha1)"
102 check_content_range
103 test x"$sha1_n2" = x"$sha1"
105 sha1="$(curl -v 2>$err $range_1b_head -sSf $url | rsha1)"
106 check_content_range
107 test x"$sha1_1b_head" = x"$sha1"
109 sha1="$(curl -v 2>$err $range_1b_tail -sSf $url | rsha1)"
110 check_content_range
111 test x"$sha1_1b_tail" = x"$sha1"
113 sha1="$(curl -v 2>$err $range_1b_mid -sSf $url | rsha1)"
114 check_content_range
115 test x"$sha1_1b_mid" = x"$sha1"
118 t_begin "head range matches" && {
119 sha1="$(curl -sSfv 2>$err $range_head $url | rsha1)"
120 check_content_range
121 test x"$sha1_head" = x"$sha1"
124 t_begin "tail range matches" && {
125 sha1="$(curl -sSfv 2>$err $range_tail $url | rsha1)"
126 check_content_range
127 test x"$sha1_tail" = x"$sha1"
130 t_begin "mid range matches" && {
131 sha1="$(curl -sSfv 2>$err $range_mid $url | rsha1)"
132 check_content_range
133 test x"$sha1_mid" = x"$sha1"
136 t_begin "shutdown server" && {
137 kill -QUIT $rainbows_pid
140 t_begin "check stderr" && check_stderr
142 t_done