LCOV - code coverage report
Current view: top level - test - test-varlink.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 119 123 96.7 %
Date: 2019-08-22 15:41:25 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <fcntl.h>
       4             : #include <poll.h>
       5             : #include <pthread.h>
       6             : 
       7             : #include "sd-event.h"
       8             : 
       9             : #include "fd-util.h"
      10             : #include "json.h"
      11             : #include "rm-rf.h"
      12             : #include "strv.h"
      13             : #include "tmpfile-util.h"
      14             : #include "user-util.h"
      15             : #include "varlink.h"
      16             : 
      17             : /* Let's pick some high value, that is higher than the largest listen() backlog, but leaves enough room below
      18             :    the typical RLIMIT_NOFILE value of 1024 so that we can process both sides of each socket in our
      19             :    process. Or in other words: "OVERLOAD_CONNECTIONS * 2 + x < 1024" should hold, for some small x that
      20             :    should cover any auxiliary fds, the listener server fds, stdin/stdout/stderr and whatever else. */
      21             : #define OVERLOAD_CONNECTIONS 333
      22             : 
      23             : static int n_done = 0;
      24             : static int block_write_fd = -1;
      25             : 
      26           2 : static int method_something(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
      27           2 :         _cleanup_(json_variant_unrefp) JsonVariant *ret = NULL;
      28             :         JsonVariant *a, *b;
      29             :         intmax_t x, y;
      30             :         int r;
      31             : 
      32           2 :         a = json_variant_by_key(parameters, "a");
      33           2 :         if (!a)
      34           0 :                 return varlink_error(link, "io.test.BadParameters", NULL);
      35             : 
      36           2 :         x = json_variant_integer(a);
      37             : 
      38           2 :         b = json_variant_by_key(parameters, "b");
      39           2 :         if (!b)
      40           0 :                 return varlink_error(link, "io.test.BadParameters", NULL);
      41             : 
      42           2 :         y = json_variant_integer(b);
      43             : 
      44           2 :         r = json_build(&ret, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("sum", JSON_BUILD_INTEGER(x + y))));
      45           2 :         if (r < 0)
      46           0 :                 return r;
      47             : 
      48           2 :         return varlink_reply(link, ret);
      49             : }
      50             : 
      51           1 : static int method_done(Varlink *link, JsonVariant *parameters, VarlinkMethodFlags flags, void *userdata) {
      52             : 
      53           1 :         if (++n_done == 2)
      54           1 :                 sd_event_exit(varlink_get_event(link), EXIT_FAILURE);
      55             : 
      56           1 :         return 0;
      57             : }
      58             : 
      59           1 : static int reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) {
      60             :         JsonVariant *sum;
      61             : 
      62           1 :         sum = json_variant_by_key(parameters, "sum");
      63             : 
      64           1 :         assert_se(json_variant_integer(sum) == 7+22);
      65             : 
      66           1 :         if (++n_done == 2)
      67           0 :                 sd_event_exit(varlink_get_event(link), EXIT_FAILURE);
      68             : 
      69           1 :         return 0;
      70             : }
      71             : 
      72         131 : static int on_connect(VarlinkServer *s, Varlink *link, void *userdata) {
      73         131 :         uid_t uid = UID_INVALID;
      74             : 
      75         131 :         assert(s);
      76         131 :         assert(link);
      77             : 
      78         131 :         assert_se(varlink_get_peer_uid(link, &uid) >= 0);
      79         131 :         assert_se(getuid() == uid);
      80             : 
      81         131 :         return 0;
      82             : }
      83             : 
      84           1 : static int overload_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) {
      85             : 
      86             :         /* This method call reply should always be called with a disconnection, since the method call should
      87             :          * be talking to an overloaded server */
      88             : 
      89           1 :         log_debug("Over reply triggered with error: %s", strna(error_id));
      90           1 :         assert_se(streq(error_id, VARLINK_ERROR_DISCONNECTED));
      91           1 :         sd_event_exit(varlink_get_event(link), 0);
      92             : 
      93           1 :         return 0;
      94             : }
      95             : 
      96           1 : static void flood_test(const char *address) {
      97           1 :         _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL;
      98           1 :         _cleanup_(sd_event_unrefp) sd_event *e = NULL;
      99           1 :         _cleanup_free_ Varlink **connections = NULL;
     100             :         size_t k;
     101           1 :         char x = 'x';
     102             : 
     103           1 :         log_debug("Flooding server...");
     104             : 
     105             :         /* Block the main event loop while we flood */
     106           1 :         assert_se(write(block_write_fd, &x, sizeof(x)) == sizeof(x));
     107             : 
     108           1 :         assert_se(sd_event_default(&e) >= 0);
     109             : 
     110             :         /* Flood the server with connections */
     111           1 :         assert_se(connections = new0(Varlink*, OVERLOAD_CONNECTIONS));
     112         334 :         for (k = 0; k < OVERLOAD_CONNECTIONS; k++) {
     113         333 :                 _cleanup_free_ char *t = NULL;
     114         333 :                 log_debug("connection %zu", k);
     115         333 :                 assert_se(varlink_connect_address(connections + k, address) >= 0);
     116             : 
     117         333 :                 assert_se(asprintf(&t, "flood-%zu", k) >= 0);
     118         333 :                 assert_se(varlink_set_description(connections[k], t) >= 0);
     119         333 :                 assert_se(varlink_attach_event(connections[k], e, k) >= 0);
     120         333 :                 assert_se(varlink_sendb(connections[k], "io.test.Rubbish", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("id", JSON_BUILD_INTEGER(k)))) >= 0);
     121             :         }
     122             : 
     123             :         /* Then, create one more, which should fail */
     124           1 :         log_debug("Creating overload connection...");
     125           1 :         assert_se(varlink_connect_address(&c, address) >= 0);
     126           1 :         assert_se(varlink_set_description(c, "overload-client") >= 0);
     127           1 :         assert_se(varlink_attach_event(c, e, k) >= 0);
     128           1 :         assert_se(varlink_bind_reply(c, overload_reply) >= 0);
     129           1 :         assert_se(varlink_invokeb(c, "io.test.Overload", JSON_BUILD_OBJECT(JSON_BUILD_PAIR("foo", JSON_BUILD_STRING("bar")))) >= 0);
     130             : 
     131             :         /* Unblock it */
     132           1 :         log_debug("Unblocking server...");
     133           1 :         block_write_fd = safe_close(block_write_fd);
     134             : 
     135             :         /* This loop will terminate as soon as the overload reply callback is called */
     136           1 :         assert_se(sd_event_loop(e) >= 0);
     137             : 
     138             :         /* And close all connections again */
     139         334 :         for (k = 0; k < OVERLOAD_CONNECTIONS; k++)
     140         333 :                 connections[k] = varlink_unref(connections[k]);
     141           1 : }
     142             : 
     143           1 : static void *thread(void *arg) {
     144           1 :         _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL;
     145           2 :         _cleanup_(json_variant_unrefp) JsonVariant *i = NULL;
     146           1 :         JsonVariant *o = NULL;
     147             :         const char *e;
     148             : 
     149           1 :         assert_se(json_build(&i, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(88)),
     150             :                                                    JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(99)))) >= 0);
     151             : 
     152           1 :         assert_se(varlink_connect_address(&c, arg) >= 0);
     153           1 :         assert_se(varlink_set_description(c, "thread-client") >= 0);
     154             : 
     155           1 :         assert_se(varlink_call(c, "io.test.DoSomething", i, &o, &e, NULL) >= 0);
     156           1 :         assert_se(json_variant_integer(json_variant_by_key(o, "sum")) == 88 + 99);
     157           1 :         assert_se(!e);
     158             : 
     159           1 :         assert_se(varlink_callb(c, "io.test.IDontExist", &o, &e, NULL, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("x", JSON_BUILD_REAL(5.5)))) >= 0);
     160           1 :         assert_se(streq_ptr(json_variant_string(json_variant_by_key(o, "method")), "io.test.IDontExist"));
     161           1 :         assert_se(streq(e, VARLINK_ERROR_METHOD_NOT_FOUND));
     162             : 
     163           1 :         flood_test(arg);
     164             : 
     165           1 :         assert_se(varlink_send(c, "io.test.Done", NULL) >= 0);
     166             : 
     167           1 :         return NULL;
     168             : }
     169             : 
     170           1 : static int block_fd_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
     171             :         char c;
     172             : 
     173           1 :         assert_se(fd_nonblock(fd, false) >= 0);
     174             : 
     175           1 :         assert_se(read(fd, &c, sizeof(c)) == sizeof(c));
     176             :         /* When a character is written to this pipe we'll block until the pipe is closed. */
     177             : 
     178           1 :         assert_se(read(fd, &c, sizeof(c)) == 0);
     179             : 
     180           1 :         assert_se(fd_nonblock(fd, true) >= 0);
     181             : 
     182           1 :         assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
     183             : 
     184           1 :         return 0;
     185             : }
     186             : 
     187           1 : int main(int argc, char *argv[]) {
     188           1 :         _cleanup_(sd_event_source_unrefp) sd_event_source *block_event = NULL;
     189           1 :         _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL;
     190           1 :         _cleanup_(varlink_flush_close_unrefp) Varlink *c = NULL;
     191           1 :         _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
     192           1 :         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
     193           1 :         _cleanup_(sd_event_unrefp) sd_event *e = NULL;
     194           2 :         _cleanup_(close_pairp) int block_fds[2] = { -1, -1 };
     195             :         pthread_t t;
     196             :         const char *sp;
     197             : 
     198           1 :         log_set_max_level(LOG_DEBUG);
     199           1 :         log_open();
     200             : 
     201           1 :         assert_se(mkdtemp_malloc("/tmp/varlink-test-XXXXXX", &tmpdir) >= 0);
     202           5 :         sp = strjoina(tmpdir, "/socket");
     203             : 
     204           1 :         assert_se(sd_event_default(&e) >= 0);
     205             : 
     206           1 :         assert_se(pipe2(block_fds, O_NONBLOCK|O_CLOEXEC) >= 0);
     207           1 :         assert_se(sd_event_add_io(e, &block_event, block_fds[0], EPOLLIN, block_fd_handler, NULL) >= 0);
     208           1 :         assert_se(sd_event_source_set_priority(block_event, SD_EVENT_PRIORITY_IMPORTANT) >= 0);
     209           1 :         block_write_fd = TAKE_FD(block_fds[1]);
     210             : 
     211           1 :         assert_se(varlink_server_new(&s, VARLINK_SERVER_ACCOUNT_UID) >= 0);
     212           1 :         assert_se(varlink_server_set_description(s, "our-server") >= 0);
     213             : 
     214           1 :         assert_se(varlink_server_bind_method(s, "io.test.DoSomething", method_something) >= 0);
     215           1 :         assert_se(varlink_server_bind_method(s, "io.test.Done", method_done) >= 0);
     216           1 :         assert_se(varlink_server_bind_connect(s, on_connect) >= 0);
     217           1 :         assert_se(varlink_server_listen_address(s, sp, 0600) >= 0);
     218           1 :         assert_se(varlink_server_attach_event(s, e, 0) >= 0);
     219           1 :         assert_se(varlink_server_set_connections_max(s, OVERLOAD_CONNECTIONS) >= 0);
     220             : 
     221           1 :         assert_se(varlink_connect_address(&c, sp) >= 0);
     222           1 :         assert_se(varlink_set_description(c, "main-client") >= 0);
     223           1 :         assert_se(varlink_bind_reply(c, reply) >= 0);
     224             : 
     225           1 :         assert_se(json_build(&v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_INTEGER(7)),
     226             :                                                    JSON_BUILD_PAIR("b", JSON_BUILD_INTEGER(22)))) >= 0);
     227             : 
     228           1 :         assert_se(varlink_invoke(c, "io.test.DoSomething", v) >= 0);
     229             : 
     230           1 :         assert_se(varlink_attach_event(c, e, 0) >= 0);
     231             : 
     232           1 :         assert_se(pthread_create(&t, NULL, thread, (void*) sp) == 0);
     233             : 
     234           1 :         assert_se(sd_event_loop(e) >= 0);
     235             : 
     236           1 :         assert_se(pthread_join(t, NULL) == 0);
     237             : 
     238           1 :         return 0;
     239             : }

Generated by: LCOV version 1.14