1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "sd-bus.h"
4 
5 #include "bus-internal.h"
6 #include "log.h"
7 #include "string-util.h"
8 #include "strv.h"
9 #include "tests.h"
10 
test_one_address(sd_bus * b,const char * host,int result,const char * expected)11 static void test_one_address(sd_bus *b,
12                              const char *host,
13                              int result, const char *expected) {
14         int r;
15 
16         r = bus_set_address_system_remote(b, host);
17         log_info("\"%s\" → %d, \"%s\"", host, r, strna(r >= 0 ? b->address : NULL));
18         if (result < 0 || expected) {
19                 assert_se(r == result);
20                 if (r >= 0)
21                         assert_se(streq(b->address, expected));
22         }
23 }
24 
TEST(bus_set_address_system_remote)25 TEST(bus_set_address_system_remote) {
26         _cleanup_(sd_bus_unrefp) sd_bus *b = NULL;
27 
28         assert_se(sd_bus_new(&b) >= 0);
29         if (!strv_isempty(saved_argv + 1)) {
30                 STRV_FOREACH(a, saved_argv + 1)
31                         test_one_address(b, *a, 0, NULL);
32                 return;
33         };
34 
35         test_one_address(b, "host",
36                          0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=host,argv4=systemd-stdio-bridge");
37         test_one_address(b, "host:123",
38                          0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=123,argv4=--,argv5=host,argv6=systemd-stdio-bridge");
39         test_one_address(b, "host:123:123",
40                          -EINVAL, NULL);
41                 test_one_address(b, "host:",
42                                  -EINVAL, NULL);
43         test_one_address(b, "user@host",
44                          0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40host,argv4=systemd-stdio-bridge");
45         test_one_address(b, "user@host@host",
46                          -EINVAL, NULL);
47         test_one_address(b, "[::1]",
48                          0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=%3a%3a1,argv4=systemd-stdio-bridge");
49         test_one_address(b, "user@[::1]",
50                          0, "unixexec:path=ssh,argv1=-xT,argv2=--,argv3=user%40%3a%3a1,argv4=systemd-stdio-bridge");
51         test_one_address(b, "user@[::1]:99",
52                          0, "unixexec:path=ssh,argv1=-xT,argv2=-p,argv3=99,argv4=--,argv5=user%40%3a%3a1,argv6=systemd-stdio-bridge");
53         test_one_address(b, "user@[::1]:",
54                          -EINVAL, NULL);
55         test_one_address(b, "user@[::1:",
56                          -EINVAL, NULL);
57         test_one_address(b, "user@",
58                          -EINVAL, NULL);
59         test_one_address(b, "user@@",
60                          -EINVAL, NULL);
61 }
62 
63 DEFINE_TEST_MAIN(LOG_INFO);
64