forked from CESNET/netopeer2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_candidate.c
542 lines (403 loc) · 13.3 KB
/
test_candidate.c
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* @file test_candidate.c
* @author Tadeas Vintrlik <[email protected]>
* @brief tests around candidate configuration and corresponding rpcs
*
* @copyright
* Copyright (c) 2019 - 2021 Deutsche Telekom AG.
* Copyright (c) 2017 - 2021 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 <setjmp.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cmocka.h>
#include <libyang/libyang.h>
#include <nc_client.h>
#include <sysrepo/netconf_acm.h>
#include "np_test.h"
#include "np_test_config.h"
static int
local_setup(void **state)
{
struct np_test *st = *state;
char test_name[256];
const char *modules[] = {NP_TEST_MODULE_DIR "/edit1.yang", NP_TEST_MODULE_DIR "/edit2.yang"};
int rc;
/* get test name */
np_glob_setup_test_name(test_name);
/* setup environment */
rc = np_glob_setup_env(test_name);
assert_int_equal(rc, 0);
/* setup netopeer2 server */
rc = np_glob_setup_np2(state, test_name, modules, sizeof modules / sizeof *modules);
assert_int_equal(rc, 0);
st = *state;
/* candidate session */
assert_int_equal(sr_session_start(st->conn, SR_DS_CANDIDATE, &st->sr_sess2), SR_ERR_OK);
/* setup NACM */
rc = setup_nacm(state);
assert_int_equal(rc, 0);
return 0;
}
static int
local_teardown(void **state)
{
struct np_test *st = *state;
const char *modules[] = {"edit1", "edit2"};
if (!st) {
return 0;
}
/* close the session */
assert_int_equal(sr_session_stop(st->sr_sess2), SR_ERR_OK);
/* close netopeer2 server */
return np_glob_teardown(state, modules, sizeof modules / sizeof *modules);
}
static int
setup_candidate(void **state)
{
struct np_test *st = *state;
const char *data;
data = "<first xmlns=\"ed1\">TestFirst</first>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static int
empty_candidate(void **state)
{
struct np_test *st = *state;
const char *data;
data = "<first xmlns=\"ed1\" xmlns:xc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xc:operation=\"remove\"/>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static int
empty_running(void **state)
{
struct np_test *st = *state;
const char *data;
data =
"<first xmlns=\"ed1\""
"xmlns:xc=\"urn:ietf:params:xml:ns:netconf:base:1.0\""
"xc:operation=\"remove\"/>";
SR_EDIT(st, data);
FREE_TEST_VARS(st);
return 0;
}
static void
test_edit_basic(void **state)
{
struct np_test *st = *state;
char *data, *expected;
/* Get a simple config into candidate */
data = "<first xmlns=\"ed1\">TestFirst</first>";
SEND_EDIT_RPC_DS(st, NC_DATASTORE_CANDIDATE, data);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
/* Check if it was merged */
GET_DS_CONFIG(st, NC_DATASTORE_CANDIDATE);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <first xmlns=\"ed1\">TestFirst</first>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Remove it from the configuration */
data = "<first xmlns=\"ed1\" xmlns:xc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xc:operation=\"remove\"/>";
SEND_EDIT_RPC_DS(st, NC_DATASTORE_CANDIDATE, data);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
}
static void
test_commit(void **state)
{
struct np_test *st = *state;
char *data, *expected;
/* Get some data into candidate */
data = "<first xmlns=\"ed1\">TestFirst</first>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
/* Check if running is empty */
GET_DS_CONFIG(st, NC_DATASTORE_RUNNING);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data/>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Send commit rpc */
st->rpc = nc_rpc_commit(0, 0, NULL, NULL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
/* Check if running is now same as candidate */
GET_DS_CONFIG(st, NC_DATASTORE_CANDIDATE);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <first xmlns=\"ed1\">TestFirst</first>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
}
static void
test_discard_changes(void **state)
{
struct np_test *st = *state;
char *expected;
/* Check if Running is empty */
GET_DS_CONFIG(st, NC_DATASTORE_RUNNING);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data/>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Check if Candidate has config */
GET_DS_CONFIG(st, NC_DATASTORE_CANDIDATE);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <first xmlns=\"ed1\">TestFirst</first>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Send discard-changes rpc */
st->rpc = nc_rpc_discard();
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
/* Check if Candidate is now empty too */
GET_DS_CONFIG(st, NC_DATASTORE_CANDIDATE);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data/>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
}
static void
test_validate_valid(void **state)
{
struct np_test *st = *state;
/* Send validate rpc */
st->rpc = nc_rpc_validate(NC_DATASTORE_CANDIDATE, NULL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
}
static void
test_validate_invalid(void **state)
{
struct np_test *st = *state;
const char *data;
data =
"<top xmlns=\"ed2\">"
" <name>TestSecond</name>"
" <num>ClearlyNotANumericValue</num>"
"</top>";
/* Send validate rpc */
st->rpc = nc_rpc_validate(NC_DATASTORE_CONFIG, data, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
}
static int
setup_discard_changes_advanced(void **state)
{
struct np_test *st = *state;
const char *data;
/* Merge config into running */
data =
"<top xmlns=\"ed2\">"
" <name>TestSecond</name>"
"</top>";
SR_EDIT(st, data);
FREE_TEST_VARS(st);
/* Merge config into candidate */
data =
"<top xmlns=\"ed2\">"
" <name>TestSecond</name>"
" <num>12</num>"
"</top>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static int
setup_commit_locked_running(void **state)
{
struct np_test *st = *state;
/* lock from another session */
st->rpc = nc_rpc_lock(NC_DATASTORE_RUNNING);
st->msgtype = nc_send_rpc(st->nc_sess2, st->rpc, 1000, &st->msgid);
assert_int_equal(st->msgtype, NC_MSG_RPC);
/* receive reply, should succeed */
ASSERT_OK_REPLY_SESS2(st);
FREE_TEST_VARS(st);
return 0;
}
static int
teardown_commit_locked_running(void **state)
{
struct np_test *st = *state;
/* lock from another session */
st->rpc = nc_rpc_unlock(NC_DATASTORE_RUNNING);
st->msgtype = nc_send_rpc(st->nc_sess2, st->rpc, 1000, &st->msgid);
assert_int_equal(st->msgtype, NC_MSG_RPC);
/* receive reply, should succeed */
ASSERT_OK_REPLY_SESS2(st);
return 0;
}
static void
test_commit_locked_running(void **state)
{
struct np_test *st = *state;
/* Send commit rpc */
st->rpc = nc_rpc_commit(0, 0, NULL, NULL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
/* Receive a reply should have error-tag in-use */
st->msgtype = nc_recv_reply(st->nc_sess, st->rpc, st->msgid, 2000, &st->envp, &st->op);
assert_int_equal(st->msgtype, NC_MSG_REPLY);
assert_null(st->op);
assert_string_equal(lyd_get_value(lyd_child(lyd_child(st->envp))->next), "in-use");
FREE_TEST_VARS(st);
}
static int
setup_lock_modified_candidate(void **state)
{
struct np_test *st = *state;
const char *data;
/* Modify candidate in any way */
data = "<first xmlns=\"ed1\">TestFirst</first>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static int
teardown_lock_modified_candidate(void **state)
{
struct np_test *st = *state;
const char *data;
/* Remove anything */
data = "<first xmlns=\"ed1\" xmlns:xc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xc:operation=\"remove\"></first>";
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static void
test_lock_modified_candidate(void **state)
{
struct np_test *st = *state;
/* lock */
st->rpc = nc_rpc_lock(NC_DATASTORE_CANDIDATE);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 1000, &st->msgid);
assert_int_equal(st->msgtype, NC_MSG_RPC);
ASSERT_RPC_ERROR(st);
FREE_TEST_VARS(st);
}
static int
teardown_discard_changes_advanced(void **state)
{
struct np_test *st = *state;
const char *data;
/* Remove config from running */
data = "<top xmlns=\"ed2\" xmlns:xc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" xc:operation=\"remove\"></top>";
SR_EDIT(st, data);
FREE_TEST_VARS(st);
/* Remove config from candidate */
SR_EDIT_SESSION(st, st->sr_sess2, data);
FREE_TEST_VARS(st);
return 0;
}
static void
test_discard_changes_advanced(void **state)
{
struct np_test *st = *state;
const char *expected;
/* Check if running has correct data */
GET_DS_CONFIG(st, NC_DATASTORE_RUNNING);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <top xmlns=\"ed2\">\n"
" <name>TestSecond</name>\n"
" </top>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Check if candidate has correct dada */
GET_DS_CONFIG(st, NC_DATASTORE_CANDIDATE);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <top xmlns=\"ed2\">\n"
" <name>TestSecond</name>\n"
" <num>12</num>\n"
" </top>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
/* Send discard-changes rpc */
st->rpc = nc_rpc_discard();
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
/* Check if candidate is now same as running */
GET_DS_CONFIG(st, NC_DATASTORE_RUNNING);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
" <top xmlns=\"ed2\">\n"
" <name>TestSecond</name>\n"
" </top>\n"
" </data>\n"
"</get-config>\n";
assert_string_equal(st->str, expected);
FREE_TEST_VARS(st);
}
int
main(int argc, char **argv)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_edit_basic),
cmocka_unit_test_setup_teardown(test_commit, setup_candidate, empty_running),
cmocka_unit_test_setup(test_discard_changes, setup_candidate),
cmocka_unit_test_setup_teardown(test_validate_valid, setup_candidate, empty_candidate),
cmocka_unit_test(test_validate_invalid),
cmocka_unit_test_setup_teardown(test_commit_locked_running, setup_commit_locked_running,
teardown_commit_locked_running),
cmocka_unit_test_setup_teardown(test_lock_modified_candidate, setup_lock_modified_candidate,
teardown_lock_modified_candidate),
cmocka_unit_test_setup_teardown(test_discard_changes_advanced, setup_discard_changes_advanced,
teardown_discard_changes_advanced),
};
if (np_is_nacm_recovery()) {
puts("Running as NACM_RECOVERY_USER. Tests will not run correctly as this user bypases NACM. Skipping.");
return 0;
}
nc_verbosity(NC_VERB_WARNING);
parse_arg(argc, argv);
return cmocka_run_group_tests(tests, local_setup, local_teardown);
}