File: ln2_test.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 (200 lines) | stat: -rw-r--r-- 4,342 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
/**
 * @file ln2_test.c
 * @author Roman Janota <janota@cesnet.cz>
 * @brief base source for libnetconf2 testing
 *
 * @copyright
 * Copyright (c) 2024 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 <assert.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ln2_test.h"

int
ln2_glob_test_get_ports(int port_count, ...)
{
    va_list ap;
    int i, ret = 0, *port_ptr;
    const char **port_str_ptr, *env;
    char *env_name = NULL;

    va_start(ap, port_count);

    for (i = 0; i < port_count; i++) {
        port_ptr = va_arg(ap, int *);
        port_str_ptr = va_arg(ap, const char **);

        if (asprintf(&env_name, "TEST_PORT_%d", i) == -1) {
            ret = 1;
            goto cleanup;
        }

        /* try to get the env variable, which is set by CTest */
        env = getenv(env_name);
        free(env_name);
        if (!env) {
            /* the default value will be used instead */
            continue;
        }

        *port_ptr = atoi(env);
        *port_str_ptr = env;
    }

cleanup:
    va_end(ap);
    return ret;
}

void *
ln2_glob_test_server_thread(void *arg)
{
    int ret;
    NC_MSG_TYPE msgtype;
    struct nc_session *session = NULL;
    struct nc_pollsession *ps = NULL;
    struct ln2_test_ctx *test_ctx = arg;

    ps = nc_ps_new();
    assert(ps);

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

    /* accept a session and add it to the poll session structure */
    msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
    if (msgtype != NC_MSG_HELLO) {
        SETUP_FAIL_LOG;
        nc_ps_free(ps);
        return NULL;
    }

    ret = nc_ps_add_session(ps, session);
    assert(!ret);

    /* poll until the session is terminated by the client */
    do {
        ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
        assert(ret & NC_PSPOLL_RPC);
    } while (!(ret & NC_PSPOLL_SESSION_TERM));

    nc_ps_clear(ps, 1, NULL);
    nc_ps_free(ps);
    return NULL;
}

void *
ln2_glob_test_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 we expect it to fail */
    msgtype = nc_accept(NC_ACCEPT_TIMEOUT, test_ctx->ctx, &session);
    assert(msgtype == NC_MSG_ERROR);
    assert(!session);

    return NULL;
}

int
ln2_glob_test_setup(struct ln2_test_ctx **test_ctx)
{
    int ret;

    *test_ctx = calloc(1, sizeof **test_ctx);
    if (!*test_ctx) {
        SETUP_FAIL_LOG;
        ret = 1;
        goto cleanup;
    }

    /* set verbosity */
    nc_verbosity(NC_VERB_VERBOSE);

    /* initialize server */
    ret = nc_server_init();
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }

    /* initialize client */
    ret = nc_client_init();
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }

    /* init barrier */
    ret = pthread_barrier_init(&(*test_ctx)->barrier, NULL, 2);
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }

    /* create libyang context */
    ret = ly_ctx_new(MODULES_DIR, 0, &(*test_ctx)->ctx);
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }

    /* load default yang modules */
    ret = nc_server_init_ctx(&(*test_ctx)->ctx);
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }
    ret = nc_server_config_load_modules(&(*test_ctx)->ctx);
    if (ret) {
        SETUP_FAIL_LOG;
        goto cleanup;
    }

cleanup:
    return ret;
}

int
ln2_glob_test_teardown(void **state)
{
    struct ln2_test_ctx *test_ctx = *state;

    nc_client_destroy();
    nc_server_destroy();

    if (test_ctx->free_test_data) {
        test_ctx->free_test_data(test_ctx->test_data);
    }

    pthread_barrier_destroy(&test_ctx->barrier);
    ly_ctx_destroy(test_ctx->ctx);
    free(test_ctx);

    return 0;
}

void
ln2_glob_test_free_test_data(void *test_data)
{
    free(test_data);
}