dbus: fix 64-bit compiler warnings
[platform/upstream/dbus.git] / test / test-dbus-launch-x11.sh
1 #!/bin/sh
2
3 # Copyright © 2016 Collabora Ltd.
4 #
5 # Permission is hereby granted, free of charge, to any person
6 # obtaining a copy of this software and associated documentation files
7 # (the "Software"), to deal in the Software without restriction,
8 # including without limitation the rights to use, copy, modify, merge,
9 # publish, distribute, sublicense, and/or sell copies of the Software,
10 # and to permit persons to whom the Software is furnished to do so,
11 # subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 set -e
26 set -x
27
28 export DBUS_DEBUG_OUTPUT=1
29 echo "# dbus-daemon binary: ${DBUS_TEST_DAEMON:=dbus-daemon}"
30 echo "# dbus-launch binary: ${DBUS_TEST_DBUS_LAUNCH:=dbus-launch}"
31 echo "# dbus-monitor binary: ${DBUS_TEST_DBUS_LAUNCH:=dbus-monitor}"
32 echo "# dbus-send binary: ${DBUS_TEST_DBUS_SEND:=dbus-send}"
33 echo "# dbus-uuidgen binary: ${DBUS_TEST_DBUS_UUIDGEN:=dbus-uuidgen}"
34
35 if test -n "$DBUS_TEST_DATA"; then
36     echo "# test data: $DBUS_TEST_DATA"
37     bus_config="--config-file=$DBUS_TEST_DATA/valid-config-files/session.conf"
38     launch_config="$bus_config"
39 elif test -n "$DBUS_TEST_DATADIR"; then
40     echo "# datadir: $DBUS_TEST_DATADIR"
41     bus_config="--config-file=$DBUS_TEST_DATADIR/dbus-1/session.conf"
42     launch_config="$bus_config"
43 else
44     echo "# using standard session bus configuration"
45     bus_config="--session"
46     # dbus-launch doesn't accept --session so add a harmless command-line
47     # argument
48     launch_config="--sh-syntax"
49 fi
50
51 if ! "${DBUS_TEST_DBUS_UUIDGEN}" --get >/dev/null; then
52     if test -n "$DBUS_TEST_UNINSTALLED"; then
53         echo "1..0 # SKIP - Unable to test dbus-launch without a machine ID"
54         exit 0
55     else
56         echo "Bail out! dbus not correctly installed: no machine ID"
57         exit 1
58     fi
59 fi
60
61 if ! workdir="$(mktemp -d)"; then
62     echo "1..0 # SKIP - mktemp -d doesn't work"
63     exit 0
64 fi
65
66 if ! xvfb-run --auto-servernum true; then
67     echo "1..0 # SKIP - xvfb-run doesn't work"
68     exit 0
69 fi
70
71 test_num=0
72 x_session_pid=
73 unset DISPLAY
74 unset XAUTHORITY
75
76 start_xvfb () {
77     rm -f "$workdir/display"
78     rm -f "$workdir/ready"
79     rm -f "$workdir/x-session-pid"
80     rm -f "$workdir/xauthority"
81     rm -f "$workdir/xvfb-done"
82
83     # Run an X session in the background for up to 5 minutes.
84     (
85         set +e
86         xvfb-run --auto-servernum \
87             sh -c 'echo "$$" > "$1/x-session-pid"; echo "$XAUTHORITY" > "$1/xauthority"; echo "$DISPLAY" > "$1/display"; touch "$1/ready"; exec sleep 300' \
88             sh "$workdir"
89         touch "$workdir/xvfb-done"
90     ) &
91
92     tries=0
93     while ! [ -e "$workdir/ready" ]; do
94         if [ $tries -ge 10 ]; then
95             echo "# Failed to start xvfb-run within $tries seconds"
96             exit 1
97         fi
98         tries=$(($tries + 1))
99         sleep 1
100     done
101
102     x_session_pid="$(cat "$workdir/x-session-pid")"
103     export DISPLAY="$(cat "$workdir/display")"
104     export XAUTHORITY="$(cat "$workdir/xauthority")"
105     kill -0 "$x_session_pid"
106 }
107
108 test_disconnection () {
109     rm -f "$workdir/disconnected"
110     (
111         set +e
112         ${DBUS_TEST_DBUS_MONITOR} > "$workdir/dbus-monitor.log" 2>&1
113         touch "$workdir/disconnected"
114     ) &
115
116     kill "$x_session_pid"
117
118     tries=0
119     while ! [ -e "$workdir/disconnected" ]; do
120         if [ $tries -ge 10 ]; then
121             echo "# dbus-monitor was not disconnected within $tries seconds" >&2
122             exit 1
123         fi
124         tries=$(($tries + 1))
125         sleep 1
126     done
127
128     tries=0
129     while ! [ -e "$workdir/xvfb-done" ]; do
130         if [ $tries -ge 10 ]; then
131             echo "# xvfb-run did not exit within $tries seconds" >&2
132             exit 1
133         fi
134         tries=$(($tries + 1))
135         sleep 1
136     done
137 }
138
139 test_exit_with_x11 () {
140     arg="$1"
141     unset DBUS_SESSION_BUS_ADDRESS
142     unset DBUS_SESSION_BUS_PID
143     unset DBUS_SESSION_BUS_WINDOWID
144
145     start_xvfb
146     eval "$($DBUS_TEST_DBUS_LAUNCH --sh-syntax "$arg" "$launch_config" </dev/null)"
147
148     test -n "$DBUS_SESSION_BUS_ADDRESS"
149     env | grep '^DBUS_SESSION_BUS_ADDRESS='
150
151     test -n "$DBUS_SESSION_BUS_PID"
152     test "x$(env | grep '^DBUS_SESSION_BUS_PID=')" = "x"
153     kill -0 "$DBUS_SESSION_BUS_PID"
154
155     test -n "$DBUS_SESSION_BUS_WINDOWID"
156     test "x$(env | grep '^DBUS_SESSION_BUS_WINDOWID=')" = "x"
157
158     ${DBUS_TEST_DBUS_SEND} --session --dest=org.freedesktop.DBus \
159         --type=method_call --print-reply / org.freedesktop.DBus.ListNames >&2
160
161     test_disconnection
162
163     test_num=$(($test_num + 1))
164     echo "ok ${test_num} - dbus-launch $arg"
165 }
166
167 test_autolaunch () {
168     unset DBUS_SESSION_BUS_ADDRESS
169     unset DBUS_SESSION_BUS_PID
170     unset DBUS_SESSION_BUS_WINDOWID
171     unset XDG_RUNTIME_DIR
172     fake_uuid="ffffffffffffffffffffffffffffffff"
173
174     start_xvfb
175     eval "$($DBUS_TEST_DBUS_LAUNCH --sh-syntax --autolaunch=${fake_uuid} "$launch_config" </dev/null)"
176
177     test -n "$DBUS_SESSION_BUS_ADDRESS"
178     env | grep '^DBUS_SESSION_BUS_ADDRESS='
179
180     test -n "$DBUS_SESSION_BUS_PID"
181     test "x$(env | grep '^DBUS_SESSION_BUS_PID=')" = "x"
182     kill -0 "$DBUS_SESSION_BUS_PID"
183
184     test -n "$DBUS_SESSION_BUS_WINDOWID"
185     test "x$(env | grep '^DBUS_SESSION_BUS_WINDOWID=')" = "x"
186
187     # It is idempotent
188     old_address="$DBUS_SESSION_BUS_ADDRESS"
189     old_pid="$DBUS_SESSION_BUS_PID"
190     old_window="$DBUS_SESSION_BUS_WINDOWID"
191     eval "$($DBUS_TEST_DBUS_LAUNCH --sh-syntax --autolaunch=${fake_uuid} "$launch_config" </dev/null)"
192     test "$DBUS_SESSION_BUS_ADDRESS" = "$old_address"
193     test "$DBUS_SESSION_BUS_PID" = "$old_pid"
194     test "$DBUS_SESSION_BUS_WINDOWID" = "$old_window"
195
196     ${DBUS_TEST_DBUS_SEND} --session --dest=org.freedesktop.DBus \
197         --type=method_call --print-reply / org.freedesktop.DBus.ListNames >&2
198
199     test_disconnection
200
201     test_num=$(($test_num + 1))
202     echo "ok ${test_num} - dbus-launch --autolaunch"
203 }
204
205 test_xdg_runtime_dir () {
206     unset DBUS_SESSION_BUS_ADDRESS
207     unset DBUS_SESSION_BUS_PID
208     unset DBUS_SESSION_BUS_WINDOWID
209     export XDG_RUNTIME_DIR="$workdir"
210     fake_uuid="ffffffffffffffffffffffffffffffff"
211
212     if echo "$workdir" | grep '[^-0-9A-Za-z_/.]'; then
213         test_num=$(($test_num + 1))
214         echo "ok ${test_num} # SKIP - $workdir would need escaping"
215         return
216     fi
217
218     ${DBUS_TEST_DAEMON} "$bus_config" --address="unix:path=$XDG_RUNTIME_DIR/bus" --print-pid=9 --fork 9>"$workdir/bus-pid"
219     bus_pid="$(cat "$workdir/bus-pid")"
220     kill -0 "$bus_pid"
221
222     start_xvfb
223     eval "$($DBUS_TEST_DBUS_LAUNCH --sh-syntax --autolaunch=${fake_uuid} "$launch_config" </dev/null)"
224
225     env | grep '^DBUS_SESSION_BUS_ADDRESS='
226     test "$DBUS_SESSION_BUS_ADDRESS" = "unix:path=$XDG_RUNTIME_DIR/bus"
227
228     # When dbus-launch picks up the address from XDG_RUNTIME_DIR/bus, it
229     # doesn't know the pid
230     test -z "$DBUS_SESSION_BUS_PID"
231
232     # It still knows which window it stashed the information in
233     test -n "$DBUS_SESSION_BUS_WINDOWID"
234     test "x$(env | grep '^DBUS_SESSION_BUS_WINDOWID=')" = "x"
235
236     ${DBUS_TEST_DBUS_SEND} --session --dest=org.freedesktop.DBus \
237         --type=method_call --print-reply / org.freedesktop.DBus.ListNames >&2
238
239     kill "$x_session_pid"
240
241     tries=0
242     while ! [ -e "$workdir/xvfb-done" ]; do
243         if [ $tries -ge 10 ]; then
244             echo "# xvfb-run did not exit within $tries seconds" >&2
245             exit 1
246         fi
247         tries=$(($tries + 1))
248         sleep 1
249     done
250
251     # dbus-launch has gone but the dbus-daemon is still alive
252     ${DBUS_TEST_DBUS_SEND} --session --dest=org.freedesktop.DBus \
253         --type=method_call --print-reply / org.freedesktop.DBus.ListNames >&2
254
255     kill "$bus_pid"
256
257     test_num=$(($test_num + 1))
258     echo "ok ${test_num} - dbus-launch --autolaunch with XDG_RUNTIME_DIR"
259 }
260
261 echo "1..4"
262 test_exit_with_x11 --exit-with-session
263 test_exit_with_x11 --exit-with-x11
264 test_autolaunch
265 test_xdg_runtime_dir
266
267 # vim:set sw=4 sts=4 et: