chiark / gitweb /
bus-policy: actually test messages against the newly added test.conf
[elogind.git] / src / bus-proxyd / test-bus-policy.c
index ed17bfe..1c1d1ef 100644 (file)
@@ -36,7 +36,6 @@
 #include "bus-internal.h"
 #include "bus-message.h"
 #include "bus-util.h"
-#include "bus-internal.h"
 #include "build.h"
 #include "strv.h"
 #include "def.h"
 
 #include <bus-proxyd/bus-policy.h>
 
-static int make_name_request(sd_bus *bus,
-                             const char *name,
-                             sd_bus_message **ret) {
-
-        int r;
-        sd_bus_message *m = NULL;
-
-        r = sd_bus_message_new_method_call(bus, &m, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName");
-        if (r < 0)
-                return r;
+static int test_policy_load(Policy *p, const char *name)
+{
+        _cleanup_free_ char *path = NULL;
+        int r = 0;
 
-        r = sd_bus_message_append_basic(m, 's', name);
-        if (r < 0)
-                return r;
+        path = strjoin(TEST_DIR, "/bus-policy/", name, NULL);
+        assert_se(path);
 
-        m->sealed = 1;
-        sd_bus_message_rewind(m, true);
+        if (access(path, R_OK) == 0)
+                policy_load(p, STRV_MAKE(path));
+        else
+                r = -ENOENT;
 
-        *ret = m;
-        return 0;
+        return r;
 }
 
 int main(int argc, char *argv[]) {
 
         Policy p = {};
-        sd_bus_message *m;
         struct ucred ucred = {};
-        _cleanup_bus_close_unref_ sd_bus *bus = NULL;;
-
-        assert_se(sd_bus_default_system(&bus) >= 0);
-
-        /* Fake pid for policy checks */
-        ucred.pid = 1;
 
         /* Ownership tests */
-        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/ownerships.conf")) == 0);
+        assert_se(test_policy_load(&p, "ownerships.conf") == 0);
 
-        assert_se(make_name_request(bus, "org.test.test1", &m) == 0);
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == true);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == true);
-        assert_se(sd_bus_message_unref(m) == 0);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test1") == true);
 
-        assert_se(make_name_request(bus, "org.test.test2", &m) == 0);
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == true);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test2") == true);
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == false);
-        assert_se(sd_bus_message_unref(m) == 0);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test2") == false);
 
-        assert_se(make_name_request(bus, "org.test.test3", &m) == 0);
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == false);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == false);
-        assert_se(sd_bus_message_unref(m) == 0);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test3") == false);
 
-        assert_se(make_name_request(bus, "org.test.test4", &m) == 0);
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == false);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test4") == false);
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == true);
-        assert_se(sd_bus_message_unref(m) == 0);
+        assert_se(policy_check_own(&p, &ucred, "org.test.test4") == true);
 
         policy_free(&p);
 
-        /* Signal test */
-        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/signals.conf")) == 0);
+        /* Signaltest */
+        assert_se(test_policy_load(&p, "signals.conf") == 0);
 
-        assert_se(sd_bus_message_new_signal(bus, &m, "/an/object/path", "bli.bla.blubb", "Name") == 0);
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == true);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == true);
 
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == false);
-        assert_se(sd_bus_message_unref(m) == 0);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_SIGNAL, "bli.bla.blubb", NULL, "/an/object/path", NULL) == false);
 
         policy_free(&p);
 
         /* Method calls */
-        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/methods.conf")) == 0);
+        assert_se(test_policy_load(&p, "methods.conf") == 0);
+        policy_dump(&p);
 
         ucred.uid = 0;
-        assert_se(sd_bus_message_new_method_call(bus, &m, "org.foo.bar", "/an/object/path", "bli.bla.blubb", "Member") == 0);
-        assert_se(policy_check(&p, m, &ucred) == false);
 
-        assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == 0);
-        assert_se(policy_check(&p, m, &ucred) == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "bli.bla.blubb", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == true);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == true);
 
-        bus->is_kernel = 1;
-        assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "org.test.int1", "Member") == 0);
-        assert_se(policy_check(&p, m, &ucred) == true);
-
-        assert_se(sd_bus_message_new_method_call(bus, &m, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == 0);
-        assert_se(policy_check(&p, m, &ucred) == true);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test3", "/an/object/path", "org.test.int3", "Member111") == true);
 
         policy_free(&p);
 
         /* User and groups */
-        assert_se(policy_load(&p, STRV_MAKE("test/bus-policy/hello.conf")) == 0);
-        assert_se(sd_bus_message_new_method_call(bus, &m, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "Hello") == 0);
+        assert_se(test_policy_load(&p, "hello.conf") == 0);
         policy_dump(&p);
 
         ucred.uid = 0;
-        assert_se(policy_check(&p, m, &ucred) == true);
+        assert_se(policy_check_hello(&p, &ucred) == true);
 
         ucred.uid = 1;
-        assert_se(policy_check(&p, m, &ucred) == false);
+        assert_se(policy_check_hello(&p, &ucred) == false);
 
         ucred.uid = 0;
         ucred.gid = 1;
-        assert_se(policy_check(&p, m, &ucred) == false);
+        assert_se(policy_check_hello(&p, &ucred) == false);
 
         policy_free(&p);
 
+        /* dbus1 test file: ownership */
+
+        assert_se(test_policy_load(&p, "check-own-rules.conf") >= 0);
+        policy_dump(&p);
+
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop") == false);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystem") == false);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems") == true);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems.foo") == true);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems.foo.bar") == true);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2") == false);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2.foo") == false);
+        assert_se(policy_check_own(&p, &ucred, "org.freedesktop.ManySystems2.foo.bar") == false);
+
+        policy_free(&p);
+
+        /* dbus1 test file: many rules */
+
+        assert_se(test_policy_load(&p, "many-rules.conf") >= 0);
+        policy_dump(&p);
+        policy_free(&p);
+
+        /* dbus1 test file: generic test */
+
+        assert_se(test_policy_load(&p, "test.conf") >= 0);
+        policy_dump(&p);
+
+        ucred.uid = 0;
+        assert_se(policy_check_own(&p, &ucred, "org.foo.FooService") == true);
+        assert_se(policy_check_own(&p, &ucred, "org.foo.FooService2") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
+
+        ucred.uid = 100;
+        assert_se(policy_check_own(&p, &ucred, "org.foo.FooService") == false);
+        assert_se(policy_check_own(&p, &ucred, "org.foo.FooService2") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.test.int2", "Member") == false);
+        assert_se(policy_check_send(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.test.test1", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == true);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService", "/an/object/path", "org.foo.FooBroadcastInterface2", "Member") == false);
+        assert_se(policy_check_recv(&p, &ucred, SD_BUS_MESSAGE_METHOD_CALL, "org.foo.FooService2", "/an/object/path", "org.foo.FooBroadcastInterface", "Member") == false);
+
+        policy_free(&p);
 
         return EXIT_SUCCESS;
 }