blob: b894dcc891bf1bca4e50835fe483ac6a4ac7bddf [file] [log] [blame]
tadeas-vintrlik36af6962021-08-23 16:18:21 +02001/**
2 * \file test_thread_messages
3 * \author Tadeas Vintrlik <xvint04@stud.fit.vutbr.cz>
4 * \brief libnetconf2 tests - thread-safety for receiving messages
5 *
6 * Copyright 2021 Deutsche Telekom AG.
7 * Copyright 2021 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#include <pthread.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/wait.h>
22#include <unistd.h>
23
24#include <libyang/libyang.h>
25
26#include <log.h>
27#include <messages_p.h>
28#include <messages_server.h>
29#include <session_client.h>
30#include <session_server.h>
31#include "tests/config.h"
32
33/* millisec */
34#define NC_ACCEPT_TIMEOUT 5000
35/* millisec */
36#define NC_PS_POLL_TIMEOUT 5000
37/* sec */
38#define CLIENT_SSH_AUTH_TIMEOUT 10
39
40#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
41
42#if _POSIX_BARRIERS >= 200112L
43pthread_barrier_t barrier;
44pthread_barrier_t barrier_msg;
45#endif
46
47typedef struct arg {
48 int in;
49 int out;
50 struct ly_ctx *ctx;
51} arg_t;
52
53struct nc_server_reply *
54rpc_clb(struct lyd_node *rpc, struct nc_session *session)
55{
56 (void)rpc; (void)session;
57 return nc_server_reply_ok();
58}
59
60static void *
61server_thread(void *arg)
62{
63 struct nc_session *sess;
64 struct nc_server_notif *notif;
65 struct lyd_node *ntf;
66 struct ly_in *in;
67 struct nc_pollsession *ps;
68 arg_t args = *(arg_t *)arg;
69 char *eventtime;
70 struct timespec ts;
71 const char *data;
72 int poll;
73
Michal Vasko93224072021-11-09 12:14:28 +010074 nc_assert(!nc_server_init());
75 nc_assert(nc_accept_inout(args.in, args.out, "test", args.ctx, &sess) == NC_MSG_HELLO);
tadeas-vintrlik36af6962021-08-23 16:18:21 +020076 nc_session_inc_notif_status(sess);
77 data =
78 "<n1 xmlns=\"n1\">\n"
79 " <first>Test</first>\n"
80 "</n1>\n";
81
82 nc_assert(ly_in_new_memory(data, &in) == LY_SUCCESS);
83 nc_assert(lyd_parse_op(args.ctx, NULL, in, LYD_XML, LYD_TYPE_NOTIF_YANG, &ntf, NULL) == LY_SUCCESS);
84 ly_in_free(in, 0);
85
86 nc_assert(clock_gettime(CLOCK_REALTIME, &ts) != -1);
87 nc_assert(ly_time_ts2str(&ts, &eventtime) == LY_SUCCESS);
88 notif = nc_server_notif_new(ntf, eventtime, NC_PARAMTYPE_FREE);
89
90 ps = nc_ps_new();
91 nc_assert(ps);
92 nc_ps_add_session(ps, sess);
Michal Vasko5e32c402022-01-12 14:05:09 +010093
94 /* get for ietf-yang-library data; delete-config in test */
tadeas-vintrlik36af6962021-08-23 16:18:21 +020095 poll = nc_ps_poll(ps, 1000, &sess);
tadeas-vintrlik36af6962021-08-23 16:18:21 +020096 nc_assert(poll == NC_PSPOLL_RPC);
Michal Vasko5e32c402022-01-12 14:05:09 +010097 poll = nc_ps_poll(ps, 1000, &sess);
98 nc_assert(poll == NC_PSPOLL_RPC);
99
100 nc_server_notif_send(sess, notif, 1000);
101
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200102 nc_ps_clear(ps, 1, NULL);
103 nc_ps_free(ps);
104
105 /* Waiting for end of test */
106 pthread_barrier_wait(&barrier);
107
108 nc_server_notif_free(notif);
109 return arg;
110}
111
112static void *
113notif_thread(void *arg)
114{
115 struct nc_session *sess = (struct nc_session *)arg;
116 struct lyd_node *envp;
117 struct lyd_node *op;
118 NC_MSG_TYPE msgtype;
119
120 /* Sync threads for receiving message to increase chance of datarace */
121 pthread_barrier_wait(&barrier_msg);
122 do {
123 msgtype = nc_recv_notif(sess, 1000, &envp, &op);
124 } while (msgtype == NC_MSG_REPLY);
125 nc_assert(msgtype == NC_MSG_NOTIF);
126 lyd_free_tree(envp);
127 lyd_free_tree(op);
128 return arg;
129}
130
131int
132main(void)
133{
134 int pipes[4];
135 struct nc_session *sess;
136 struct lyd_node *op, *envp;
Michal Vasko1d116a12022-02-03 15:23:20 +0100137 struct ly_ctx *server_ctx, *client_ctx;
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200138 struct nc_rpc *rpc;
139 uint64_t msgid;
140 NC_MSG_TYPE msgtype;
141 const char *features[] = {"startup", NULL};
142 arg_t thread_arg;
143 pthread_t t[2];
144
145 pthread_barrier_init(&barrier, NULL, 2);
146 pthread_barrier_init(&barrier_msg, NULL, 2);
147
148 /* Create a two pipes */
149 nc_assert(pipe(pipes) != -1);
150 nc_assert(pipe(pipes + 2) != -1);
151 thread_arg.in = pipes[0];
152 thread_arg.out = pipes[3];
153
Michal Vasko1d116a12022-02-03 15:23:20 +0100154 /* Create both contexts */
155 nc_assert(ly_ctx_new(TESTS_DIR "/data/modules", 0, &server_ctx) == LY_SUCCESS);
156 nc_assert(ly_ctx_load_module(server_ctx, "ietf-netconf", NULL, features));
157 nc_assert(ly_ctx_load_module(server_ctx, "notif1", NULL, NULL));
158 thread_arg.ctx = server_ctx;
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200159 nc_set_global_rpc_clb(rpc_clb);
160
Michal Vasko1d116a12022-02-03 15:23:20 +0100161 nc_assert(ly_ctx_new(TESTS_DIR "/data/modules", 0, &client_ctx) == LY_SUCCESS);
162 nc_assert(ly_ctx_load_module(client_ctx, "ietf-netconf", NULL, features));
163 nc_assert(ly_ctx_load_module(client_ctx, "notif1", NULL, NULL));
164
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200165 /* Start server thread */
166 pthread_create(&t[0], NULL, server_thread, &thread_arg);
167 nc_client_init();
168
169 /* Listen for notifications */
Michal Vasko1d116a12022-02-03 15:23:20 +0100170 sess = nc_connect_inout(pipes[2], pipes[1], client_ctx);
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200171 nc_assert(sess);
172 pthread_create(&t[1], NULL, notif_thread, sess);
173
174 /* Send rpc */
175 rpc = nc_rpc_delete(NC_DATASTORE_STARTUP, NULL, NC_PARAMTYPE_CONST);
176 nc_assert(nc_send_rpc(sess, rpc, 1000, &msgid) == NC_MSG_RPC);
177
178 /* Sync threads for receiving message to increase chance of datarace */
179 pthread_barrier_wait(&barrier_msg);
180 do {
181 msgtype = nc_recv_reply(sess, rpc, msgid, 1000, &envp, &op);
182 } while (msgtype == NC_MSG_NOTIF);
183 nc_assert(msgtype == NC_MSG_REPLY);
184 nc_rpc_free(rpc);
185 lyd_free_tree(envp);
186
187 /* Waiting of end of test */
188 pthread_barrier_wait(&barrier);
189 pthread_join(t[0], NULL);
190 pthread_join(t[1], NULL);
191
192 /* Cleanup */
193 nc_session_free(sess, NULL);
Michal Vasko1d116a12022-02-03 15:23:20 +0100194 ly_ctx_destroy(server_ctx);
195 ly_ctx_destroy(client_ctx);
tadeas-vintrlik36af6962021-08-23 16:18:21 +0200196 for (uint8_t i = 0; i < 4; i++) {
197 close(pipes[i]);
198 }
199 return 0;
200}