summaryrefslogtreecommitdiffstats
path: root/src/tests/sbus_tests.c
blob: d2725cf0e542f20bc1d22db91fa04e80392e7fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
   SSSD

   sbus_codegen tests.

   Authors:
        Stef Walter <stefw@redhat.com>

   Copyright (C) Red Hat, Inc 2014

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <check.h>
#include <talloc.h>
#include <tevent.h>
#include <popt.h>

#include "common.h"

#include "sbus/sssd_dbus.h"
#include "sbus/sssd_dbus_meta.h"
#include "util/util_errors.h"

/*
 * Although one would normally rely on the codegen to generate these
 * structures, we want to test this functionality *before* we test
 * the codegen in sbus_codegen_tests ... so these are hand rolled.
 */

#define PILOT_IFACE "test.Pilot"
#define PILOT_BLINK "Blink"
#define PILOT_EAT "Eat"

#define PILOT_IFACE_INTROSPECT \
        "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" \
        "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"                \
        "<node>\n"                                                                           \
        "  <interface name=\"test.Pilot\">\n"                                                \
        "    <method name=\"Blink\">\n"                                                      \
        "    </method>\n"                                                                    \
        "    <method name=\"Eat\">\n"                                                        \
        "    </method>\n"                                                                    \
        "  </interface>\n"                                                                   \
        " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"                        \
        "   <method name=\"Introspect\">\n"                                                  \
        "     <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"                           \
        "   </method>\n"                                                                     \
        " </interface>\n"                                                                    \
        " <interface name=\"org.freedesktop.DBus.Properties\">\n"                            \
        "   <method name=\"Get\">\n"                                                         \
        "     <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"                       \
        "     <arg name=\"property\" direction=\"in\" type=\"s\"/>\n"                        \
        "     <arg name=\"value\" direction=\"out\" type=\"v\"/>\n"                          \
        "   </method>\n"                                                                     \
        " </interface>\n"                                                                    \
        "</node>\n"

/* our vtable */
struct pilot_vtable {
    struct sbus_vtable vtable;
    sbus_msg_handler_fn Blink;
    sbus_msg_handler_fn Eat;
};

const struct sbus_method_meta pilot_methods[] = {
    {
        PILOT_BLINK, /* method name */
        NULL, /* in args: manually parsed */
        NULL, /* out args: manually parsed */
        offsetof(struct pilot_vtable, Blink),
        NULL
    },
    {
        PILOT_EAT, /* method name */
        NULL, /* in args: manually parsed */
        NULL, /* out args: manually parsed */
        offsetof(struct pilot_vtable, Eat),
        NULL
    },
    { NULL, }
};

const struct sbus_interface_meta pilot_meta = {
    PILOT_IFACE, /* name */
    pilot_methods,
    NULL, /* no signals */
    NULL, /* no properties */
};

static int blink_handler(struct sbus_request *req, void *data)
{
    DBusError error = DBUS_ERROR_INIT;
    const char *path;
    dbus_int32_t duration = 0;
    dbus_bool_t crashed;

    ck_assert(req->intf->vtable->meta == &pilot_meta);
    ck_assert(data != NULL);
    ck_assert(data == req->intf->instance_data);

    path = dbus_message_get_path(req->message);
    ck_assert_str_eq(req->intf->path, path);

    if (strcmp(path, "/test/fry") == 0) {
        ck_assert_str_eq(data, "Don't crash");
    } else if (strcmp(path, "/test/leela") == 0) {
        ck_assert_str_eq(data, "Crash into the billboard");
    } else {
        ck_abort();
    }

    if (!dbus_message_get_args (req->message, &error,
                                DBUS_TYPE_INT32, &duration,
                                DBUS_TYPE_INVALID)) {
        sbus_request_fail_and_finish(req, &error);
        dbus_error_free(&error);
        return EOK;
    }

    /* Pilot crashes when eyes closed too long */
    crashed = (duration > 5);

    return sbus_request_return_and_finish(req,
                                          DBUS_TYPE_BOOLEAN, &crashed,
                                          DBUS_TYPE_INVALID);
}

static int eat_handler(struct sbus_request *req, void *data)
{
    dbus_int32_t integer;
    dbus_bool_t boolean;
    const char **array;
    int count;

    if (!sbus_request_parse_or_finish (req,
                                       DBUS_TYPE_INT32, &integer,
                                       DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
                                       DBUS_TYPE_BOOLEAN, &boolean,
                                       DBUS_TYPE_INVALID)) {
        return EOK; /* handled */
    }

    ck_assert_int_eq(integer, 5);
    ck_assert(boolean == TRUE);
    ck_assert_int_eq(count, 3);
    ck_assert_str_eq(array[0], "one");
    ck_assert_str_eq(array[1], "two");
    ck_assert_str_eq(array[2], "three");

    return sbus_request_return_and_finish(req, DBUS_TYPE_INVALID);
}

struct pilot_vtable pilot_impl = {
    { &pilot_meta, 0 },
    .Blink = blink_handler,
    .Eat = eat_handler,
};

static int pilot_test_server_init(struct sbus_connection *server, void *unused)
{
    int ret;

    ret = sbus_conn_add_interface(server,
                                  sbus_new_interface(server, "/test/leela",
                                                     &pilot_impl.vtable,
                                                     "Crash into the billboard"));
    ck_assert_int_eq(ret, EOK);


    ret = sbus_conn_add_interface(server,
                                  sbus_new_interface(server, "/test/fry",
                                                     &pilot_impl.vtable,
                                                     "Don't crash"));
    ck_assert_int_eq(ret, EOK);

    return EOK;
}

START_TEST(test_raw_handler)
{
    TALLOC_CTX *ctx;
    DBusConnection *client;
    DBusError error = DBUS_ERROR_INIT;
    DBusMessage *reply;
    dbus_bool_t crashed;
    dbus_int32_t duration;

    ctx = talloc_new(NULL);
    client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);

    /* Leela crashes with a duration higher than 5 */
    duration = 10;
    reply = test_dbus_call_sync(client,
                                "/test/leela",
                                PILOT_IFACE,
                                PILOT_BLINK,
                                &error,
                                DBUS_TYPE_INT32, &duration,
                                DBUS_TYPE_INVALID);
    ck_assert(reply != NULL);
    ck_assert(!dbus_error_is_set(&error));
    ck_assert(dbus_message_get_args(reply, NULL,
                                    DBUS_TYPE_BOOLEAN, &crashed,
                                    DBUS_TYPE_INVALID));
    dbus_message_unref (reply);
    ck_assert(crashed == true);

    /* Fry daesn't crash with a duration lower than 5 */
    duration = 1;
    reply = test_dbus_call_sync(client,
                                "/test/fry",
                                PILOT_IFACE,
                                PILOT_BLINK,
                                &error,
                                DBUS_TYPE_INT32, &duration,
                                DBUS_TYPE_INVALID);
    ck_assert(reply != NULL);
    ck_assert(!dbus_error_is_set(&error));
    ck_assert(dbus_message_get_args(reply, NULL,
                                    DBUS_TYPE_BOOLEAN, &crashed,
                                    DBUS_TYPE_INVALID));
    dbus_message_unref (reply);
    ck_assert(crashed == FALSE);

    talloc_free(ctx);
}
END_TEST

START_TEST(test_request_parse_ok)
{
    const char *args[] = { "one", "two", "three" };
    const char **array;
    TALLOC_CTX *ctx;
    DBusConnection *client;
    DBusError error = DBUS_ERROR_INIT;
    DBusMessage *reply;
    dbus_bool_t boolean;
    dbus_int32_t integer;
    int count;

    ctx = talloc_new(NULL);
    client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);

    boolean = TRUE;
    integer = 5;
    count = 3;
    array = args;
    reply = test_dbus_call_sync(client,
                                "/test/leela",
                                PILOT_IFACE,
                                PILOT_EAT,
                                &error,
                                DBUS_TYPE_INT32, &integer,
                                DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, count,
                                DBUS_TYPE_BOOLEAN, &boolean,
                                DBUS_TYPE_INVALID);
    ck_assert(reply != NULL);
    ck_assert(!dbus_error_is_set(&error));
    ck_assert(dbus_message_get_args(reply, NULL,
                                    DBUS_TYPE_INVALID));
    dbus_message_unref (reply);

    talloc_free(ctx);
}
END_TEST

START_TEST(test_request_parse_bad_args)
{
    TALLOC_CTX *ctx;
    DBusConnection *client;
    DBusError error = DBUS_ERROR_INIT;
    DBusMessage *reply;

    ctx = talloc_new(NULL);
    client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);

    reply = test_dbus_call_sync(client,
                                "/test/leela",
                                PILOT_IFACE,
                                PILOT_EAT,
                                &error,
                                DBUS_TYPE_INVALID); /* bad agruments */
    ck_assert(reply == NULL);
    ck_assert(dbus_error_is_set(&error));
    ck_assert(dbus_error_has_name(&error, DBUS_ERROR_INVALID_ARGS));
    dbus_error_free(&error);

    talloc_free(ctx);
}
END_TEST

START_TEST(test_introspection)
{
    TALLOC_CTX *ctx;
    DBusConnection *client;
    DBusError error = DBUS_ERROR_INIT;
    DBusMessage *reply;
    char *xml;

    ctx = talloc_new(NULL);
    client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);

    reply = test_dbus_call_sync(client,
                                "/test/leela",
                                DBUS_INTROSPECT_INTERFACE,
                                DBUS_INTROSPECT_METHOD,
                                &error,
                                DBUS_TYPE_INVALID); /* bad agruments */

    ck_assert(reply != NULL);
    ck_assert(!dbus_error_is_set(&error));
    ck_assert(dbus_message_get_args(reply, NULL,
                                    DBUS_TYPE_STRING, &xml,
                                    DBUS_TYPE_INVALID));
    ck_assert_str_eq(PILOT_IFACE_INTROSPECT, xml);

    dbus_message_unref(reply);

    talloc_free(ctx);
}
END_TEST

START_TEST(test_sbus_new_error)
{
    TALLOC_CTX *ctx;
    DBusError *error;

    ctx = talloc_new(NULL);

    error = sbus_error_new(ctx, DBUS_ERROR_IO_ERROR, "Input-output error");
    ck_assert(error != NULL);
    ck_assert(dbus_error_is_set(error));
    ck_assert(dbus_error_has_name(error, DBUS_ERROR_IO_ERROR));
    talloc_free(error);

    error = sbus_error_new(ctx, DBUS_ERROR_IO_ERROR,
                           "The answer should have been %d", 42);
    ck_assert(error != NULL);
    ck_assert(dbus_error_is_set(error));
    ck_assert(dbus_error_has_name(error, DBUS_ERROR_IO_ERROR));
    talloc_free(error);

    /* NULL message must also work */
    error = sbus_error_new(ctx, DBUS_ERROR_IO_ERROR, NULL);
    ck_assert(error != NULL);
    ck_assert(dbus_error_is_set(error));
    ck_assert(dbus_error_has_name(error, DBUS_ERROR_IO_ERROR));
    talloc_free(error);

    talloc_free(ctx);
}
END_TEST

TCase *create_sbus_tests(void)
{
    TCase *tc = tcase_create("tests");

    tcase_add_test(tc, test_raw_handler);
    tcase_add_test(tc, test_request_parse_ok);
    tcase_add_test(tc, test_request_parse_bad_args);
    tcase_add_test(tc, test_introspection);
    tcase_add_test(tc, test_sbus_new_error);

    return tc;
}

Suite *create_suite(void)
{
    Suite *s = suite_create("sbus");
    suite_add_tcase(s, create_sbus_tests());
    return s;
}

int main(int argc, const char *argv[])
{
    int opt;
    poptContext pc;
    int failure_count;
    Suite *suite;
    SRunner *sr;

    struct poptOption long_options[] = {
        POPT_AUTOHELP
        POPT_TABLEEND
    };

    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    while ((opt = poptGetNextOpt(pc)) != -1) {
        switch (opt) {
        default:
            fprintf(stderr, "\nInvalid option %s: %s\n\n",
                    poptBadOption(pc, 0), poptStrerror(opt));
            poptPrintUsage(pc, stderr, 0);
            return 1;
        }
    }
    poptFreeContext(pc);

    suite = create_suite();
    sr = srunner_create(suite);
    /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    srunner_run_all(sr, CK_ENV);
    failure_count = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (failure_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}