summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Olbrich <m.olbrich@pengutronix.de>2010-05-28 16:57:59 +0200
committerMichael Olbrich <m.olbrich@pengutronix.de>2010-06-03 17:08:10 +0200
commitb382520554ea1b78523c55e9d74999853263fbde (patch)
tree5d280123b259e03be78403cdbe8b630907e1bee4
parentaa46fed658fee6897d223eb754a5869446954b54 (diff)
downloadjson-dbus-bridge-b382520554ea1b78523c55e9d74999853263fbde.tar.gz
json-dbus-bridge-b382520554ea1b78523c55e9d74999853263fbde.tar.xz
[tests] first unit tests
Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
-rw-r--r--Makefile.am3
-rw-r--r--configure.ac10
-rw-r--r--tests/Makefile.am4
-rwxr-xr-xtests/basic_test.sh23
-rwxr-xr-xtests/dbus-test-service.py93
-rw-r--r--tests/libtest.sh105
-rw-r--r--tests/lighttpd.conf.in37
-rw-r--r--tests/run_test.sh.in47
8 files changed, 316 insertions, 6 deletions
diff --git a/Makefile.am b/Makefile.am
index 3cbb368..7d0a1bb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,7 +3,8 @@ ACLOCAL_AMFLAGS=-I m4
SUBDIRS = \
src \
- demo
+ demo \
+ tests
EXTRA_DIST = \
autogen.sh \
diff --git a/configure.ac b/configure.ac
index 58dc703..3fc867a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -86,13 +86,13 @@ AC_CONFIG_FILES([
demo/dbus-simple-service/Makefile
demo/lighttpd.conf
demo/Makefile
+ tests/lighttpd.conf
+ tests/Makefile
src/Makefile
Makefile
])
-AC_CONFIG_FILES([
- demo/run.sh
-], [
- chmod +x demo/run.sh
-])
+AC_CONFIG_FILES([demo/run.sh],[chmod +x demo/run.sh])
+AC_CONFIG_FILES([tests/run_test.sh],[chmod +x tests/run_test.sh])
+
AC_OUTPUT
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..d6839c2
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,4 @@
+
+TESTS = \
+ run_test.sh
+
diff --git a/tests/basic_test.sh b/tests/basic_test.sh
new file mode 100755
index 0000000..6314615
--- /dev/null
+++ b/tests/basic_test.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+if [ "`basename $0`" = "basic_test.sh" ]; then
+ . `dirname $0`/libtest.sh
+fi
+
+JDB_TEST_SERVICE="com.pengutronix.jdb.test|/"
+JDB_TEST_IFACE="com.pengutronix.jdb.Test.Basic"
+
+jdb_test_bridge || return
+
+jdb_test_echo EchoBYTE y 30
+jdb_test_echo EchoBYTE y 127
+jdb_test_echo EchoBYTE y 1000
+
+jdb_test_echo EchoBOOLEAN b true
+jdb_test_echo EchoBOOLEAN b True true
+jdb_test_echo EchoBOOLEAN b false
+jdb_test_echo EchoBOOLEAN b FALSE false
+jdb_test_echo EchoBOOLEAN b '"foo"' null "boolean value expected."
+jdb_test_echo EchoBOOLEAN b 42 null "boolean value expected."
+jdb_test_echo EchoBOOLEAN b 0 null "boolean value expected."
+
diff --git a/tests/dbus-test-service.py b/tests/dbus-test-service.py
new file mode 100755
index 0000000..c484851
--- /dev/null
+++ b/tests/dbus-test-service.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python2.5
+#
+# 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/>.
+#
+
+import gobject
+
+import dbus
+import dbus.service
+import dbus.mainloop.glib
+
+class Service(dbus.service.Object):
+ def __init__(self, bus, name):
+ self._state = 1
+ dbus.service.Object.__init__(self, None, "/", name)
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='y', out_signature='y')
+ def EchoBYTE(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='b', out_signature='b')
+ def EchoBOOLEAN(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='n', out_signature='n')
+ def EchoINT16(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='q', out_signature='q')
+ def EchoUINT16(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='i', out_signature='i')
+ def EchoINT32(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='u', out_signature='u')
+ def EchoUINT32(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='x', out_signature='x')
+ def EchoINT64(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='t', out_signature='t')
+ def EchoUINT64(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='d', out_signature='d')
+ def EchoDOUBLE(self, val):
+ return val;
+
+ @dbus.service.method("com.pengutronix.jdb.Test.Basic",
+ in_signature='s', out_signature='s')
+ def EchoSTRING(self, val):
+ return val;
+
+
+if __name__ == '__main__':
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ session_bus = dbus.SessionBus()
+ name = dbus.service.BusName("com.pengutronix.jdb.test", session_bus)
+ object = Service(session_bus, name)
+
+ mainloop = gobject.MainLoop()
+ mainloop.run()
+
+
diff --git a/tests/libtest.sh b/tests/libtest.sh
new file mode 100644
index 0000000..118d4d9
--- /dev/null
+++ b/tests/libtest.sh
@@ -0,0 +1,105 @@
+#!/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
+ 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"` || return
+ 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"
+}
+
+
diff --git a/tests/lighttpd.conf.in b/tests/lighttpd.conf.in
new file mode 100644
index 0000000..7a52b0b
--- /dev/null
+++ b/tests/lighttpd.conf.in
@@ -0,0 +1,37 @@
+server.document-root = "@abs_srcdir@"
+
+server.port = 8080
+server.bind = "localhost"
+server.tag = "lighttpd"
+
+server.errorlog = "@abs_builddir@/error.log"
+
+server.modules = (
+ "mod_fastcgi",
+)
+
+# mimetype mapping
+mimetype.assign = (
+ ".gif" => "image/gif",
+ ".jpg" => "image/jpeg",
+ ".jpeg" => "image/jpeg",
+ ".png" => "image/png",
+ ".css" => "text/css",
+ ".html" => "text/html",
+ ".htm" => "text/html",
+ ".js" => "text/javascript",
+ ".xml" => "text/xml",
+)
+
+index-file.names = ( "index.html" )
+
+fastcgi.server = (
+ "/rpc" => ((
+ "bin-path" => "@abs_top_builddir@/src/json-dbus-bridge",
+ "socket" => "@abs_builddir@/json-dbus-bridge.socket",
+ "check-local" => "disable",
+ "mode" => "responder",
+ "max-procs" => 1,
+ )),
+)
+
diff --git a/tests/run_test.sh.in b/tests/run_test.sh.in
new file mode 100644
index 0000000..d15a212
--- /dev/null
+++ b/tests/run_test.sh.in
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+. @abs_srcdir@/libtest.sh
+
+finish() {
+ kill $test_service_pid
+ kill $lighttpd_pid
+
+ jdb_status
+ exit $?
+}
+
+trap finish SIGINT
+trap finish SIGQUIT
+
+"@abs_srcdir@/dbus-test-service.py" &
+test_service_pid=$!
+
+/usr/sbin/lighttpd -f "@abs_builddir@/lighttpd.conf" -D &
+lighttpd_pid=$!
+
+JDB_TEST_URL="http://localhost:8080/rpc"
+
+echo -n "waiting for lighttpd "
+for i in `seq 10`; do
+ sleep 1
+ echo -n "."
+ jdb_test_bridge > /dev/null 2>&1 && break
+done
+
+if [ $? -ne 0 ]; then
+ echo
+ echo
+ echo starting lighttpd failed!
+ finish
+ exit 1
+else
+ echo
+ echo
+ echo lighttpd sucessfully started. Running tests
+ echo
+fi
+
+. "@abs_srcdir@/basic_test.sh"
+
+finish
+