File: test-plugin.c

package info (click to toggle)
mptcpd 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,612 kB
  • sloc: ansic: 9,472; sh: 5,154; makefile: 467; cpp: 61
file content (400 lines) | stat: -rw-r--r-- 13,085 bytes parent folder | download
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
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @file test-plugin.c
 *
 * @brief mptcpd plugin test.
 *
 * Copyright (c) 2018-2022, Intel Corporation
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>

#include <ell/ell.h>

#include <mptcpd/plugin.h>
#include <mptcpd/private/plugin.h>

#include "test-plugin.h"

#undef NDEBUG
#include <assert.h>


static bool run_plugin_load(mode_t mode, struct l_queue const *queue)
{
        static char const dir[]            = TEST_PLUGIN_DIR_SECURITY;
        static char const default_plugin[] = TEST_PLUGIN_FOUR;
        struct mptcpd_pm *const pm         = NULL;

        int const fd = open(dir, O_DIRECTORY);
        assert(fd != -1);

        struct stat st;
        int const stat_ok = fstat(fd, &st);
        assert(stat_ok == 0);

        int const mode_ok = fchmod(fd, mode);
        assert(mode_ok == 0);

        bool const loaded =
                mptcpd_plugin_load(dir, default_plugin, queue, pm);

        if (loaded) {
                static struct plugin_call_args const args = {
                        .token       = test_token_4,
                        .raddr_id    = test_raddr_id_4,
                        .laddr       = (struct sockaddr const *) &test_laddr_4,
                        .raddr       = (struct sockaddr const *) &test_raddr_4,
                        .backup      = test_backup_4,
                        .server_side = test_server_side_4,
                        .deny_join_id0 = test_deny_join_id0_4,
                        .error       = test_error_4
                };

                call_plugin_ops(&test_count_4, &args);

                mptcpd_plugin_unload(pm);
        }

        (void) fchmod(fd, st.st_mode);  // Restore original perms.

        (void) close(fd);

        return loaded;
}

/**
 * @brief Verify good plugin directory permissions.
 *
 * Confirm that owner and group read/write/execute permissions on a
 * given plugin directory are accepted.
 */
static void test_good_perms(void const *test_data)
{
        (void) test_data;

        // Owner and group read/write/execute permissions.
        mode_t const mode = S_IRWXU | S_IRWXG;

        bool const loaded = run_plugin_load(mode, NULL);

        assert(loaded);
}

/**
 * @brief Verify bad plugin directory permissions.
 *
 * Confirm that "other" write permissions on a given plugin directory
 * are rejected.
 */
static void test_bad_perms(void const *test_data)
{
        (void) test_data;

        // Owner, group, and other read/write/execute permissions.
        mode_t const mode = S_IRWXU | S_IRWXG | S_IRWXO;

        bool const loaded = run_plugin_load(mode, NULL);

        // Write permissions for "other" should be rejected.
        assert(!loaded);
}

/**
 * @brief Verify failure if no plugins exist.
 *
 * The @c mptcpd_plugin_load() function should return with a failure
 * if no plugins exist in the provided plugin directory.
 */
static void test_no_plugins(void const *test_data)
{
        (void) test_data;

        // Create an empty directory.
        char template[]       = "/tmp/testdir.XXXXXX";
        char const *const dir = mkdtemp(template);

        assert(dir != NULL);

        struct mptcpd_pm *const pm = NULL;
        bool const loaded = mptcpd_plugin_load(dir, NULL, NULL, pm);

        (void) rmdir(dir);

        assert(!loaded);
}

/**
 * @brief Verify load of specific existing plugins.
 *
 * Confirm that specific existing plugins are loaded.
 */
static void test_existing_plugins(void const *test_data)
{
        (void) test_data;

        // Owner and group read/write/execute permissions.
        mode_t const mode = S_IRWXU | S_IRWXG;

        struct l_queue *const plugins_list = l_queue_new();

        l_queue_push_tail(plugins_list, "four");

        bool const loaded = run_plugin_load(mode, plugins_list);

        l_queue_destroy(plugins_list, NULL);

        assert(loaded);
}

/**
 * @brief Verify failure if specified plugins do not exist.
 *
 * The @c mptcpd_plugin_load() function should return with a failure
 * if the specified plugins do not exist in the provided plugin directory.
 */
static void test_nonexistent_plugins(void const *test_data)
{
        (void) test_data;

        // Owner and group read/write/execute permissions.
        mode_t const mode = S_IRWXU | S_IRWXG;

        struct l_queue *const plugins_list = l_queue_new();

        static char nonexistent_plugin[] = "nonexistent_plugin";
        l_queue_push_tail(plugins_list, nonexistent_plugin);

        bool const loaded = run_plugin_load(mode, plugins_list);

        l_queue_destroy(plugins_list, NULL);

        /*
          Force plugin name-to-ops lookup to fail excercise additional
          error paths.
        */
        mptcpd_plugin_new_connection(nonexistent_plugin,
                                     0,     // token
                                     NULL,  // laddr
                                     NULL,  // raddr
                                     false, // server_side
                                     false, // deny_join_id0
                                     NULL); // pm

        assert(!loaded);
}

/**
 * @brief Verify dispatch of plugin operations.
 *
 * Confirm that specific plugins are dispatched with the expected
 * arguments.
 */
static void test_plugin_dispatch(void const *test_data)
{
        (void) test_data;

        static char const dir[] = TEST_PLUGIN_DIR_PRIORITY;
        static char const *const default_plugin = NULL;
        struct mptcpd_pm *const pm = NULL;

        bool const loaded =
                mptcpd_plugin_load(dir, default_plugin, NULL, pm);
        assert(loaded);

        // Notice that we call plugin 1 twice.
        // Plugin 1
        struct plugin_call_args const args1 = {
                .name        = TEST_PLUGIN_ONE,
                .token       = test_token_1,
                .raddr_id    = test_raddr_id_1,
                .laddr       = (struct sockaddr const *) &test_laddr_1,
                .raddr       = (struct sockaddr const *) &test_raddr_1,
                .backup      = test_backup_1,
                .server_side = test_server_side_1,
                .deny_join_id0 = test_deny_join_id0_1,
                .error       = test_error_1
        };

        call_plugin_ops(&test_count_1, &args1);

        // Plugin 1 as default (no plugin name specified)
        struct plugin_call_args const args1_default = {
                .token       = args1.token,
                .raddr_id    = args1.raddr_id,
                .laddr       = args1.laddr,
                .raddr       = args1.raddr,
                .backup      = args1.backup,
                .server_side = args1.server_side,
                .deny_join_id0 = args1.deny_join_id0,
                .error       = args1.error
        };

        call_plugin_ops(&test_count_1, &args1_default);

        // Plugin 2
        struct plugin_call_args const args2 = {
                .name        = TEST_PLUGIN_TWO,
                .token       = test_token_2,
                .raddr_id    = test_raddr_id_2,
                .laddr       = (struct sockaddr const *) &test_laddr_2,
                .raddr       = (struct sockaddr const *) &test_raddr_2,
                .backup      = test_backup_2,
                .server_side = test_server_side_2,
                .deny_join_id0 = test_deny_join_id0_2,
                .error       = test_error_2
        };

        call_plugin_ops(&test_count_2, &args2);

        /*
          Invalid MPTCP token - no plugin dispatch should occur.

          This should be the case for all operations corresponding to
          MPTCP genl API events that are triggered after the initial
          new connection (MPTCP_CONNECTION_CREATED) event,
          i.e. connection_established, connection_closed,
          new_address, address_removed, new_subflow, subflow_closed,
          and subflow_priority.

          We do not call call_plugin_ops() like above since that would
          cause the token to be associated with a plugin, which is not
          what we want in this case.
        */
        mptcpd_plugin_connection_established(
                test_bad_token,
                (struct sockaddr const *) &test_laddr_2,
                (struct sockaddr const *) &test_raddr_2,
                test_server_side_2,
                test_deny_join_id0_2,
                NULL);

        // Test assertions will be triggered during plugin unload.
        mptcpd_plugin_unload(pm);
}

/**
 * @brief Verify graceful handling of NULL plugin operations.
 *
 * Confirm that the mptcpd plugin framework will not call operations
 * that have not been specified by the user, i.e. those that are
 * @c NULL.
 */
static void test_null_plugin_ops(void const *test_data)
{
        (void) test_data;

        static char const        dir[]          = TEST_PLUGIN_DIR_NOOP;
        static char const *const default_plugin = NULL;
        struct mptcpd_pm *const pm = NULL;

        bool const loaded = mptcpd_plugin_load(dir, default_plugin, NULL, pm);
        assert(loaded);

        char const name[] = "null ops";
        struct mptcpd_plugin_ops const ops = { .new_connection = NULL };

        bool registered = mptcpd_plugin_register_ops(name, &ops);
        assert(registered);

        // Unused dummy arguments.
        static mptcpd_token_t const token = 0;
        static mptcpd_aid_t const id = 0;
        static struct sockaddr const *const laddr = NULL;
        static struct sockaddr const *const raddr = NULL;
        static bool backup = false;
        static bool server_side = false;
        static bool deny_join_id0 = false;
        static uint8_t error = 0;
        static struct mptcpd_interface const *const interface = NULL;

        // No dispatch should occur in the following calls.
        mptcpd_plugin_new_connection(name, token, laddr, raddr, server_side,
                                     deny_join_id0, pm);
        mptcpd_plugin_connection_established(token, laddr, raddr, server_side,
                                             deny_join_id0, pm);
        mptcpd_plugin_connection_closed(token, pm);
        mptcpd_plugin_new_address(token, id, raddr, pm);
        mptcpd_plugin_address_removed(token, id, pm);
        mptcpd_plugin_new_subflow(token, laddr, raddr, backup, pm);
        mptcpd_plugin_subflow_closed(token, laddr, raddr, backup, error, pm);
        mptcpd_plugin_subflow_priority(token, laddr, raddr, backup, pm);
        mptcpd_plugin_listener_created(name, laddr, pm);
        mptcpd_plugin_listener_closed(name, laddr, pm);
        mptcpd_plugin_new_interface(interface, pm);
        mptcpd_plugin_update_interface(interface, pm);
        mptcpd_plugin_delete_interface(interface, pm);
        mptcpd_plugin_new_local_address(interface, laddr, pm);
        mptcpd_plugin_delete_local_address(interface, laddr, pm);

        mptcpd_plugin_unload(pm);
}

/**
 * @brief Verify graceful handling of @c NULL plugin directory.
 */
static void test_null_plugin_dir(void const *test_data)
{
        (void) test_data;

        char const *const dir                       = NULL;
        char const *const default_plugin            = NULL;
        struct l_queue const *const plugins_to_load = NULL;
        struct mptcpd_pm *const pm                  = NULL;

        bool const loaded =
                mptcpd_plugin_load(dir,
                                   default_plugin,
                                   plugins_to_load,
                                   pm);
        assert(!loaded);
}

/**
 * @brief Verify graceful handling of bad/malformed plugins.
 */
static void test_bad_plugins(void const *test_data)
{
        (void) test_data;

        char const *const dir                       = TEST_PLUGIN_DIR_BAD;
        char const *const default_plugin            = NULL;
        struct l_queue const *const plugins_to_load = NULL;
        struct mptcpd_pm *const pm                  = NULL;

        bool const loaded =
                mptcpd_plugin_load(dir,
                                   default_plugin,
                                   plugins_to_load,
                                   pm);
        assert(!loaded);
}

int main(int argc, char *argv[])
{
        l_test_init(&argc, &argv);

        l_test_add("good permissions",   test_good_perms,          NULL);
        l_test_add("bad  permissions",   test_bad_perms,           NULL);
        l_test_add("no plugins",         test_no_plugins,          NULL);
        l_test_add("existing plugin",    test_existing_plugins,    NULL);
        l_test_add("nonexistent plugin", test_nonexistent_plugins, NULL);
        l_test_add("plugin dispatch",    test_plugin_dispatch,     NULL);
        l_test_add("null plugin ops",    test_null_plugin_ops,     NULL);
        l_test_add("null plugin dir",    test_null_plugin_dir,     NULL);
        l_test_add("bad plugins",        test_bad_plugins,         NULL);

        return l_test_run();
}


/*
  Local Variables:
  c-file-style: "linux"
  End:
*/