]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
D-Bus: Add test for file descriptor passing
authorJürg Billeter <j@bitron.ch>
Mon, 18 Oct 2010 10:00:39 +0000 (12:00 +0200)
committerJürg Billeter <j@bitron.ch>
Sat, 23 Oct 2010 16:11:47 +0000 (18:11 +0200)
tests/Makefile.am
tests/dbus/filedescriptor.test [new file with mode: 0644]

index 26436cc614ac6edbedd0fd44ac702275703a27d3..e4bbbe10f0f8bd45d56a8a03ec6151169fd3fcef 100644 (file)
@@ -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 (file)
index 0000000..50a246d
--- /dev/null
@@ -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 ();
+}