File: test_unix_socket.c

package info (click to toggle)
libnetconf2 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,612 kB
  • sloc: ansic: 37,068; xml: 437; sh: 49; makefile: 20
file content (489 lines) | stat: -rw-r--r-- 13,542 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
 * @file test_unix_socket.c
 * @author Roman Janota <janota@cesnet.cz>
 * @author Michal Vasko <mvasko@cesnet.cz>
 * @brief libnetconf2 UNIX socket test
 *
 * @copyright
 * Copyright (c) 2022 - 2025 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#define _GNU_SOURCE

#include <errno.h>
#include <grp.h>
#include <pthread.h>
#include <pwd.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <cmocka.h>

#include "ln2_test.h"
#include "nc_client.h"

struct test_unix_socket_data {
    const char *username;
    const char *socket_path;
    int expect_fail;
};

static int
setup_glob_f(void **state)
{
    int ret;
    struct ln2_test_ctx *test_ctx;

    ret = ln2_glob_test_setup(&test_ctx);
    assert_int_equal(ret, 0);

    *state = test_ctx;

    test_ctx->test_data = calloc(1, sizeof(struct test_unix_socket_data));
    assert_non_null(test_ctx->test_data);
    test_ctx->free_test_data = ln2_glob_test_free_test_data;

    /* set the base directory for UNIX sockets */
    ret = nc_server_set_unix_socket_dir("/tmp");
    assert_int_equal(ret, 0);

    /* set two hidden paths for UNIX sockets */
    ret = nc_server_set_unix_socket_path("unix", "/tmp/nc2_test_unix_sock");
    assert_int_equal(ret, 0);
    ret = nc_server_set_unix_socket_path("unix2", "/tmp/nc2_test_unix_sock2");
    assert_int_equal(ret, 0);

    return 0;
}

static int
setup_local_f(void **state)
{
    int ret;
    struct ln2_test_ctx *test_ctx = *state;
    struct lyd_node *config = NULL;

    /* set verbosity */
    nc_verbosity(NC_VERB_VERBOSE);
    ly_log_level(LY_LLERR);

    /* create the UNIX endpoint, the hidden path will be used */
    ret = nc_server_config_add_unix_socket(test_ctx->ctx,
            "unix", NULL, NULL, NULL, NULL, &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    lyd_free_all(config);

    return 0;
}

/* TEST */
static void *
test_unix_client_thread(void *arg)
{
    int ret = 0;
    struct nc_session *session = NULL;
    struct ln2_test_ctx *test_ctx = arg;
    struct test_unix_socket_data *data = test_ctx->test_data;

    ret = nc_client_set_schema_searchpath(MODULES_DIR);
    assert_int_equal(ret, 0);

    if (data->username) {
        ret = nc_client_unix_set_username(data->username);
        assert_int_equal(ret, 0);
    }

    pthread_barrier_wait(&test_ctx->barrier);
    session = nc_connect_unix(data->socket_path, NULL);
    if (data->expect_fail) {
        assert_null(session);
    } else {
        assert_non_null(session);
    }

    nc_session_free(session, NULL);
    return NULL;
}

static void
test_connect(void **state)
{
    int ret, i;
    pthread_t tids[2];
    struct ln2_test_ctx *test_ctx = *state;
    struct test_unix_socket_data *data = test_ctx->test_data;

    assert_non_null(state);

    data->socket_path = "/tmp/nc2_test_unix_sock";
    ret = pthread_create(&tids[0], NULL, test_unix_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tids[i], NULL);
    }
}

/* TEST */
static void
test_invalid_user(void **state)
{
    int ret, i;
    pthread_t tids[2];
    struct ln2_test_ctx *test_ctx = *state;
    struct test_unix_socket_data *data = test_ctx->test_data;

    assert_non_null(state);

    /* set invalid username, the server will reject it */
    data->socket_path = "/tmp/nc2_test_unix_sock";
    data->expect_fail = 1;
    data->username = "INVALID";

    ret = pthread_create(&tids[0], NULL, test_unix_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread_fail, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tids[i], NULL);
    }
    data->expect_fail = 0;
    data->username = NULL;
}

/* TEST */
static void *
proxy_client_thread(void *arg)
{
    int ret, fd;
    const char *msg;
    char *buf = NULL;
    uint32_t buf_len = 0;
    struct ln2_test_ctx *test_ctx = arg;

    ret = nc_client_set_schema_searchpath(MODULES_DIR);
    assert_int_equal(ret, 0);

    /* wait before connecting */
    pthread_barrier_wait(&test_ctx->barrier);

    /* connect the proxy */
    fd = nc_proxy_unix_connect("/tmp/nc2_test_unix_sock", NULL);
    assert_int_not_equal(fd, 0);

    /* send the hello message */
    msg = "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
            "<capabilities>"
            "<capability>urn:ietf:params:netconf:base:1.0</capability>"
            "<capability>urn:ietf:params:netconf:base:1.1</capability>"
            "</capabilities>"
            "</hello>";
    ret = nc_proxy_write_msg(fd, NC_PROT_VERSION_10, msg, strlen(msg));
    assert_int_equal(ret, strlen(msg));

    /* read the hello message */
    ret = nc_proxy_read_msg(fd, NC_PROT_VERSION_10, -1, &buf, &buf_len);
    assert_int_not_equal(ret, -1);

    /* close session */
    msg = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"first\">"
            "<close-session/>"
            "</rpc>";
    ret = nc_proxy_write_msg(fd, NC_PROT_VERSION_11, msg, strlen(msg));
    assert_int_equal(ret, strlen(msg));

    /* read OK reply */
    ret = nc_proxy_read_msg(fd, NC_PROT_VERSION_11, -1, &buf, &buf_len);
    assert_int_not_equal(ret, -1);
    assert_string_equal(buf, "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"first\"><ok/></rpc-reply>");

    /* close the proxy */
    ret = nc_proxy_unix_close(fd);
    assert_int_equal(ret, 0);

    free(buf);
    return NULL;
}

static void
test_proxy(void **state)
{
    int ret, i;
    pthread_t tids[2];

    assert_non_null(state);

    ret = pthread_create(&tids[0], NULL, proxy_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tids[i], NULL);
    }
}

/* TEST */
static void *
auth_client_thread(void *arg)
{
    int ret = 0;
    struct nc_session *session = NULL;
    struct ln2_test_ctx *test_ctx = arg;

    ret = nc_client_set_schema_searchpath(MODULES_DIR);
    assert_int_equal(ret, 0);

    pthread_barrier_wait(&test_ctx->barrier);

    /* session fails to be created with the default username */
    session = nc_connect_unix("/tmp/nc2_test_unix_sock", NULL);
    assert_null(session);

    /* set the expected username */
    nc_client_unix_set_username("auth_user");

    /* session created */
    session = nc_connect_unix("/tmp/nc2_test_unix_sock", NULL);
    assert_non_null(session);

    /* free the session */
    nc_session_free(session, NULL);

    return NULL;
}

static void *
auth_server_thread(void *arg)
{
    int ret;
    NC_MSG_TYPE msgtype;
    struct nc_session *session = NULL;
    struct ln2_test_ctx *test_ctx = arg;
    struct nc_pollsession *ps = NULL;
    struct lyd_node *config = NULL;
    struct passwd *pw;

    pw = getpwuid(getuid());
    assert_non_null(pw);

    /* create UNIX user mapping for the current user, hidden path is used */
    ret = nc_server_config_add_unix_socket(test_ctx->ctx,
            "unix", NULL, NULL, NULL, NULL, &config);
    assert_int_equal(ret, 0);
    ret = nc_server_config_add_unix_user_mapping(test_ctx->ctx, "unix", pw->pw_name, "auth_user", &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    lyd_free_all(config);

    /* wait for the client to be ready to connect */
    pthread_barrier_wait(&test_ctx->barrier);

    /* session of the process user is not accepted */
    msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
    assert_int_equal(msgtype, NC_MSG_ERROR);

    /* session with the correct username is accepted */
    msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
    assert_int_equal(msgtype, NC_MSG_HELLO);

    /* session closed */
    ps = nc_ps_new();
    assert_non_null(ps);
    ret = nc_ps_add_session(ps, session);
    assert_int_equal(ret, 0);
    do {
        ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
        assert_true(ret & NC_PSPOLL_RPC);
    } while (!(ret & NC_PSPOLL_SESSION_TERM));
    nc_ps_clear(ps, 1, NULL);
    nc_ps_free(ps);

    return NULL;
}

static void
test_auth(void **state)
{
    int ret, i;
    pthread_t tids[2];

    assert_non_null(state);

    ret = pthread_create(&tids[0], NULL, auth_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tids[1], NULL, auth_server_thread, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tids[i], NULL);
    }
}

/* TEST */
static void
test_config(void **state)
{
    int ret, i, ngroups = 0;
    struct ln2_test_ctx *test_ctx = *state;
    struct lyd_node *config = NULL;
    struct passwd *pw;
    struct group *gr = NULL;
    gid_t *groups;

    /* get the current user and one of its groups to use */
    pw = getpwuid(getuid());
    assert_non_null(pw);

    getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups);
    groups = malloc(ngroups * sizeof *groups);
    assert_non_null(groups);
    getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);

    /* keep the last group */
    for (i = 0; i < ngroups; ++i) {
        gr = getgrgid(groups[i]);
        assert_non_null(gr);
    }
    free(groups);

    /* create the UNIX socket */
    ret = nc_server_config_add_unix_socket(test_ctx->ctx,
            "unix2", NULL, "0666", pw->pw_name, gr->gr_name, &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    /* deletes the UNIX socket */
    ret = nc_server_config_del_endpt("unix2", &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    lyd_free_all(config);
}

/* TEST */
void *
test_unix_server_thread_fail(void *arg)
{
    NC_MSG_TYPE msgtype;
    struct nc_session *session = NULL;
    struct ln2_test_ctx *test_ctx = arg;

    /* wait for the client to be ready to connect */
    pthread_barrier_wait(&test_ctx->barrier);

    /* try to accept a session, but expect it to time out, as the client should fail to connect, but
     * not cause an error on the server side */
    msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
    assert_int_equal(msgtype, NC_MSG_WOULDBLOCK);
    assert_null(session);

    return NULL;
}

static void
test_cleartext_path(void **state)
{
    int ret, i;
    struct ln2_test_ctx *test_ctx = *state;
    struct lys_module *mod;
    const char *ln2_mod_features[] = {
        "unix-socket-path",
        NULL
    };
    struct lyd_node *config = NULL;
    pthread_t tid[2];
    struct test_unix_socket_data *data = test_ctx->test_data;

    /* enable the 'cleartext-unixsocket-path' feature */
    mod = ly_ctx_get_module_implemented(test_ctx->ctx, "libnetconf2-netconf-server");
    assert_non_null(mod);
    mod = ly_ctx_load_module(test_ctx->ctx, mod->name, mod->revision, ln2_mod_features);
    assert_non_null(mod);

    /* create the UNIX socket with a different cleartext path */
    ret = nc_server_config_add_unix_socket(test_ctx->ctx,
            "unix2", "nc2_test_cleartext_unix_sock", "0666", NULL, NULL, &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    /* start the client and server threads, the client should be able to connect to the cleartext path */
    data->socket_path = "/tmp/nc2_test_cleartext_unix_sock";
    ret = pthread_create(&tid[0], NULL, test_unix_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tid[1], NULL, ln2_glob_test_server_thread, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tid[i], NULL);
    }

    lyd_free_all(config);
    config = NULL;

    /* set hidden path again */
    ret = nc_server_config_add_unix_socket(test_ctx->ctx,
            "unix2", NULL, "0666", NULL, NULL, &config);
    assert_int_equal(ret, 0);

    ret = nc_server_config_setup_data(config);
    assert_int_equal(ret, 0);

    /* client should fail to connect to the old cleartext path */
    data->expect_fail = 1;
    ret = pthread_create(&tid[0], NULL, test_unix_client_thread, *state);
    assert_int_equal(ret, 0);
    ret = pthread_create(&tid[1], NULL, test_unix_server_thread_fail, *state);
    assert_int_equal(ret, 0);

    for (i = 0; i < 2; i++) {
        pthread_join(tid[i], NULL);
    }

    data->expect_fail = 0;

    lyd_free_all(config);
}

int
main(void)
{
    const struct CMUnitTest tests[] = {
        cmocka_unit_test_setup(test_connect, setup_local_f),
        cmocka_unit_test_setup(test_invalid_user, setup_local_f),
        cmocka_unit_test_setup(test_proxy, setup_local_f),
        cmocka_unit_test_setup(test_auth, setup_local_f),
        cmocka_unit_test_setup(test_config, setup_local_f),
        cmocka_unit_test_setup(test_cleartext_path, setup_local_f),
    };

    setenv("CMOCKA_TEST_ABORT", "1", 1);
    return cmocka_run_group_tests(tests, setup_glob_f, ln2_glob_test_teardown);
}