From: Jürg Billeter Date: Mon, 18 Oct 2010 10:00:39 +0000 (+0200) Subject: D-Bus: Add test for file descriptor passing X-Git-Tag: 0.11.1~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56ef7198d010aa7c908b8e5e41c1677a7447db72;p=thirdparty%2Fvala.git D-Bus: Add test for file descriptor passing --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 26436cc61..e4bbbe10f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -96,6 +96,7 @@ TESTS = \ dbus/async.test \ dbus/async-errors.test \ dbus/signals.test \ + dbus/filedescriptor.test \ dbus/bug596862.vala \ dbus/bug602003.test \ $(NULL) diff --git a/tests/dbus/filedescriptor.test b/tests/dbus/filedescriptor.test new file mode 100644 index 000000000..50a246dc6 --- /dev/null +++ b/tests/dbus/filedescriptor.test @@ -0,0 +1,84 @@ +Packages: gio-2.0 gio-unix-2.0 posix +D-Bus + +Program: client + +[DBus (name = "org.example.Test")] +interface Test : Object { + public abstract UnixInputStream test_in (UnixInputStream i, out UnixInputStream j) throws IOError; +} + +void main () { + // client + Test test = Bus.get_proxy_sync (BusType.SESSION, "org.example.Test", "/org/example/test"); + + uint8[] buffer = new uint8[1]; + + int[] pipe1 = new int[2]; + assert (Posix.pipe (pipe1) == 0); + buffer[0] = 42; + assert (Posix.write (pipe1[1], buffer, 1) == 1); + Posix.close (pipe1[1]); + + UnixInputStream j, k; + k = test.test_in (new UnixInputStream (pipe1[0], true), out j); + + assert (j.read (buffer, 1) == 1); + assert (buffer[0] == 23); + + assert (k.read (buffer, 1) == 1); + assert (buffer[0] == 11); +} + +Program: server + +[DBus (name = "org.example.Test")] +class Test : Object { + public UnixInputStream test_in (UnixInputStream i, out UnixInputStream j) throws IOError { + uint8[] buffer = new uint8[1]; + + assert (i.read (buffer, 1) == 1); + assert (buffer[0] == 42); + + int[] pipe1 = new int[2]; + assert (Posix.pipe (pipe1) == 0); + buffer[0] = 23; + assert (Posix.write (pipe1[1], buffer, 1) == 1); + Posix.close (pipe1[1]); + + int[] pipe2 = new int[2]; + assert (Posix.pipe (pipe2) == 0); + buffer[0] = 11; + assert (Posix.write (pipe2[1], buffer, 1) == 1); + Posix.close (pipe2[1]); + + j = new UnixInputStream (pipe1[0], true); + return new UnixInputStream (pipe2[0], true); + } +} + +MainLoop main_loop; + +void client_exit (Pid pid, int status) { + // client finished, terminate server + assert (status == 0); + main_loop.quit (); +} + +void main () { + var conn = Bus.get_sync (BusType.SESSION); + conn.register_object ("/org/example/test", new Test ()); + + // try to register service in session bus + var request_result = conn.call_sync ("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName", + new Variant ("(su)", "org.example.Test", 0x4), null, 0, -1); + assert ((uint) request_result.get_child_value (0) == 1); + + // server ready, spawn client + Pid client_pid; + Process.spawn_async (null, { "test", "/dbus/filedescriptor/client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid); + ChildWatch.add (client_pid, client_exit); + + main_loop = new MainLoop (); + main_loop.run (); +}