Merge branch 'nw/maint-cvsexportcommit'
[git] / t / lib-httpd.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
4 #
5
6 if test -z "$GIT_TEST_HTTPD"
7 then
8         say "skipping test, network testing disabled by default"
9         say "(define GIT_TEST_HTTPD to enable)"
10         test_done
11 fi
12
13 HTTPD_PARA=""
14
15 case $(uname) in
16         Darwin)
17                 DEFAULT_HTTPD_PATH='/usr/sbin/httpd'
18                 DEFAULT_HTTPD_MODULE_PATH='/usr/libexec/apache2'
19                 HTTPD_PARA="$HTTPD_PARA -DDarwin"
20         ;;
21         *)
22                 DEFAULT_HTTPD_PATH='/usr/sbin/apache2'
23                 DEFAULT_HTTPD_MODULE_PATH='/usr/lib/apache2/modules'
24         ;;
25 esac
26
27 LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
28 LIB_HTTPD_PORT=${LIB_HTTPD_PORT-'8111'}
29
30 TEST_PATH="$TEST_DIRECTORY"/lib-httpd
31 HTTPD_ROOT_PATH="$PWD"/httpd
32 HTTPD_DOCUMENT_ROOT_PATH=$HTTPD_ROOT_PATH/www
33
34 if ! test -x "$LIB_HTTPD_PATH"
35 then
36         say "skipping test, no web server found at '$LIB_HTTPD_PATH'"
37         test_done
38 fi
39
40 HTTPD_VERSION=`$LIB_HTTPD_PATH -v | \
41         sed -n 's/^Server version: Apache\/\([0-9]*\)\..*$/\1/p; q'`
42
43 if test -n "$HTTPD_VERSION"
44 then
45         if test -z "$LIB_HTTPD_MODULE_PATH"
46         then
47                 if ! test $HTTPD_VERSION -ge 2
48                 then
49                         say "skipping test, at least Apache version 2 is required"
50                         test_done
51                 fi
52
53                 LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
54         fi
55 else
56         error "Could not identify web server at '$LIB_HTTPD_PATH'"
57 fi
58
59 prepare_httpd() {
60         mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
61
62         ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
63
64         if test -n "$LIB_HTTPD_SSL"
65         then
66                 HTTPD_URL=https://127.0.0.1:$LIB_HTTPD_PORT
67
68                 RANDFILE_PATH="$HTTPD_ROOT_PATH"/.rnd openssl req \
69                         -config "$TEST_PATH/ssl.cnf" \
70                         -new -x509 -nodes \
71                         -out "$HTTPD_ROOT_PATH/httpd.pem" \
72                         -keyout "$HTTPD_ROOT_PATH/httpd.pem"
73                 GIT_SSL_NO_VERIFY=t
74                 export GIT_SSL_NO_VERIFY
75                 HTTPD_PARA="$HTTPD_PARA -DSSL"
76         else
77                 HTTPD_URL=http://127.0.0.1:$LIB_HTTPD_PORT
78         fi
79
80         if test -n "$LIB_HTTPD_DAV" -o -n "$LIB_HTTPD_SVN"
81         then
82                 HTTPD_PARA="$HTTPD_PARA -DDAV"
83
84                 if test -n "$LIB_HTTPD_SVN"
85                 then
86                         HTTPD_PARA="$HTTPD_PARA -DSVN"
87                         rawsvnrepo="$HTTPD_ROOT_PATH/svnrepo"
88                         svnrepo="http://127.0.0.1:$LIB_HTTPD_PORT/svn"
89                 fi
90         fi
91 }
92
93 start_httpd() {
94         prepare_httpd >&3 2>&4
95
96         trap 'code=$?; stop_httpd; (exit $code); die' EXIT
97
98         "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
99                 -f "$TEST_PATH/apache.conf" $HTTPD_PARA \
100                 -c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
101                 >&3 2>&4
102         if test $? -ne 0
103         then
104                 say "skipping test, web server setup failed"
105                 trap 'die' EXIT
106                 test_done
107         fi
108 }
109
110 stop_httpd() {
111         trap 'die' EXIT
112
113         "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
114                 -f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
115 }