]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/test-bus-kernel-bloom.c
tty-ask-password: Split out password sending
[thirdparty/systemd.git] / src / libsystemd / sd-bus / test-bus-kernel-bloom.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "sd-bus.h"
23
24 #include "alloc-util.h"
25 #include "bus-kernel.h"
26 #include "bus-util.h"
27 #include "fd-util.h"
28 #include "log.h"
29 #include "util.h"
30
31 static int test_match(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
32 int *found = userdata;
33
34 *found = 1;
35
36 return 0;
37 }
38
39 static void test_one(
40 const char *path,
41 const char *interface,
42 const char *member,
43 bool as_list,
44 const char *arg0,
45 const char *match,
46 bool good) {
47
48 _cleanup_close_ int bus_ref = -1;
49 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL;
50 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
51 sd_bus *a, *b;
52 int r, found = 0;
53
54 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
55
56 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
57 if (bus_ref == -ENOENT)
58 exit(EXIT_TEST_SKIP);
59
60 assert_se(bus_ref >= 0);
61
62 address = strappend("kernel:path=", bus_name);
63 assert_se(address);
64
65 r = sd_bus_new(&a);
66 assert_se(r >= 0);
67
68 r = sd_bus_new(&b);
69 assert_se(r >= 0);
70
71 r = sd_bus_set_address(a, address);
72 assert_se(r >= 0);
73
74 r = sd_bus_set_address(b, address);
75 assert_se(r >= 0);
76
77 r = sd_bus_start(a);
78 assert_se(r >= 0);
79
80 r = sd_bus_start(b);
81 assert_se(r >= 0);
82
83 log_debug("match");
84 r = sd_bus_add_match(b, NULL, match, test_match, &found);
85 assert_se(r >= 0);
86
87 log_debug("signal");
88
89 if (as_list)
90 r = sd_bus_emit_signal(a, path, interface, member, "as", 1, arg0);
91 else
92 r = sd_bus_emit_signal(a, path, interface, member, "s", arg0);
93 assert_se(r >= 0);
94
95 r = sd_bus_process(b, &m);
96 assert_se(r >= 0 && good == !!found);
97
98 sd_bus_unref(a);
99 sd_bus_unref(b);
100 }
101
102 int main(int argc, char *argv[]) {
103 log_set_max_level(LOG_DEBUG);
104
105 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "", true);
106 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo'", true);
107 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo/tuut'", false);
108 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "interface='waldo.com'", true);
109 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "member='Piep'", true);
110 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "member='Pi_ep'", false);
111 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "arg0='foobar'", true);
112 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "arg0='foo_bar'", false);
113 test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0='foobar'", false);
114 test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0='foo_bar'", false);
115 test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0has='foobar'", true);
116 test_one("/foo/bar/waldo", "waldo.com", "Piep", true, "foobar", "arg0has='foo_bar'", false);
117 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo',interface='waldo.com',member='Piep',arg0='foobar'", true);
118 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo',interface='waldo.com',member='Piep',arg0='foobar2'", false);
119
120 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo'", true);
121 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar'", false);
122 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo'", false);
123 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/'", false);
124 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo/quux'", false);
125 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/bar/waldo'", true);
126 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/bar'", true);
127 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo'", true);
128 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/'", true);
129 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/quux'", false);
130 test_one("/", "waldo.com", "Piep", false, "foobar", "path_namespace='/'", true);
131
132 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/bar/waldo/'", false);
133 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path='/foo/'", false);
134 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/bar/waldo/'", false);
135 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "foobar", "path_namespace='/foo/'", true);
136
137 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "/foo/bar/waldo", "arg0path='/foo/'", true);
138 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "/foo", "arg0path='/foo'", true);
139 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "/foo", "arg0path='/foo/bar/waldo'", false);
140 test_one("/foo/bar/waldo", "waldo.com", "Piep", false, "/foo/", "arg0path='/foo/bar/waldo'", true);
141
142 return 0;
143 }