summaryrefslogtreecommitdiffstats
path: root/tests/libtest.sh
blob: 0b29435aeb579cc682e1120e31b3a3415ce69149 (plain)
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
#!/bin/bash
#
#  json dbus bridge
#
#  Copyright (c) 2009 by Michael Olbrich <m.olbrich@pengutronix.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of
#  the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

if [ -z "$libtest_tag" ]; then
	libtest_tag=1
	id=0
	error_count=0
fi

jdb_status() {
	echo
	echo $error_count tests failed.
	return $error_count
}

jdb_call() {
	local url="$JDB_TEST_URL"
	local data="$1"

	local result=`tempfile`
	local debug=`tempfile`

	if [ -z "$url" ]; then
		echo "FATAL: URL not set" 1>&2
		exit 1
	fi

	wget -O "$result" --post-data="$data" "$url" > "$debug" 2>&1
	status=$?
	if [ $status -ne 0 ]; then
		echo 1>&2
		cat "$debug" 1>&2
	else
		cat "$result"
	fi
	rm "$result" "$debug"
	return $status
}

jdb_test_bridge() {
	local data='{"service":"org.freedesktop.DBus|/","method":"org.freedesktop.DBus.Introspectable.Introspect","id":0,"params": []}'

	jdb_call "$data" > /dev/null
}

jdb_test() {
	local service="$JDB_TEST_SERVICE"
	local iface="$JDB_TEST_IFACE"
	local method="${iface:+${iface}.}$1"
	local params="$2"
	local response="${3:-null}"
	local error
	if [ -z "$4" ]; then
		error=null
	else
		error="{ \"origin\": 1, \"code\": 1, \"message\": \"$4\" }"
	fi
	id=$[id+1]

	local data="{ \"service\":\"$service\", \"method\":\"$method\", \"id\":$id, \"params\":$params }"
	local expect="{ \"id\": $id, \"error\": $error, \"result\": $response }"

	echo -n "$method($params)"
	local result
	result=`jdb_call "$data"`
	if [ $? -ne 0 ]; then
		echo " Failed:"
		echo Sent:     $data
		error_count=$[error_count+1]
		return 1
	fi
	if [ "x$result" != "x$expect" ]; then
		echo " Failed:"
		echo Sent:     $data
		echo Expected: $expect
		echo Got:      $result
		echo
		error_count=$[error_count+1]
		return 1
	else
		echo "."
	fi
}

jdb_test_echo() {
	local method="$1"
	local type="$2"
	local value="$3"
	local response="${4:-$value}"
	local error="$5"

	jdb_test "$method" "[ \"$type\", $value ]" "$response" "$error"
}