Skip to content

Commit

Permalink
tests REFACTOR use macros for all message receives
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed Jun 19, 2024
1 parent 63a87d9 commit 8b32e15
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 63 deletions.
19 changes: 16 additions & 3 deletions tests/np2_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ struct np2_test {
#define ASSERT_OK_REPLY(state) \
ASSERT_OK_REPLY_PARAM(state->nc_sess, 3000, state)

#define ASSERT_OK_REPLY_SESS(state, nc_sess) \
ASSERT_OK_REPLY_PARAM(nc_sess, 3000, state)

#define ASSERT_DATA_REPLY_PARAM(nc_sess, timeout_ms, state) \
do { \
state->msgtype = nc_recv_reply(nc_sess, state->rpc, state->msgid, timeout_ms, &state->envp, &state->op); \
Expand Down Expand Up @@ -114,8 +117,8 @@ struct np2_test {
assert_null(state->op); \
assert_string_equal(LYD_NAME(lyd_child(state->envp)), "ok");

#define ASSERT_ERROR_REPLY(state) \
state->msgtype = nc_recv_reply(state->nc_sess, state->rpc, state->msgid, 3000, &state->envp, &state->op); \
#define ASSERT_ERROR_REPLY_PARAM(state, nc_sess, err_tag) \
state->msgtype = nc_recv_reply(nc_sess, state->rpc, state->msgid, 3000, &state->envp, &state->op); \
assert_int_equal(state->msgtype, NC_MSG_REPLY); \
if (!lyd_child(state->envp) || strcmp(LYD_NAME(lyd_child(state->envp)), "rpc-error")) { \
if (lyd_child(state->envp)) { \
Expand All @@ -134,7 +137,17 @@ struct np2_test {
fail(); \
} \
assert_null(state->op); \
assert_int_equal(LY_SUCCESS, lyd_print_mem(&state->str, lyd_child(state->envp), LYD_XML, LYD_PRINT_WITHSIBLINGS));
if (err_tag) { \
assert_string_equal(lyd_get_value(lyd_child(lyd_child(st->envp))->next), err_tag); \
} else { \
assert_int_equal(LY_SUCCESS, lyd_print_mem(&state->str, lyd_child(state->envp), LYD_XML, LYD_PRINT_WITHSIBLINGS)); \
}

#define ASSERT_ERROR_REPLY(state) \
ASSERT_ERROR_REPLY_PARAM(state, state->nc_sess, NULL)

#define ASSERT_ERROR_REPLY_TAG(state, err_tag) \
ASSERT_ERROR_REPLY_PARAM(state, state->nc_sess, err_tag)

#define ASSERT_ERROR_REPLY_SESS2(state) \
state->msgtype = nc_recv_reply(state->nc_sess2, state->rpc, state->msgid, 3000, &state->envp, &state->op); \
Expand Down
10 changes: 2 additions & 8 deletions tests/test_candidate.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,7 @@ test_commit_locked(void **state)
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");
ASSERT_ERROR_REPLY_TAG(st, "in-use");
FREE_TEST_VARS(st);

/* unlock from another session */
Expand All @@ -374,10 +371,7 @@ test_commit_locked(void **state)
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 2000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);

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");
ASSERT_ERROR_REPLY_TAG(st, "in-use");
FREE_TEST_VARS(st);

st->rpc = nc_rpc_unlock(NC_DATASTORE_CANDIDATE);
Expand Down
4 changes: 1 addition & 3 deletions tests/test_confirmed_commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,7 @@ test_rollback_disconnect(void **state)
assert_int_equal(st->msgtype, NC_MSG_RPC);

/* expect OK */
st->msgtype = nc_recv_reply(ncs, st->rpc, st->msgid, 3000, &st->envp, &st->op);
assert_int_equal(st->msgtype, NC_MSG_REPLY);
assert_string_equal(LYD_NAME(lyd_child(st->envp)), "ok");
ASSERT_OK_REPLY_SESS(st, ncs);
FREE_TEST_VARS(st);

/* Expect 'start' notification with 60s timeout */
Expand Down
15 changes: 5 additions & 10 deletions tests/test_sub_ntf_advanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,10 @@ test_deletesub_fail_diff_sess(void **state)
st->rpc = nc_rpc_deletesub(st->ntf_id);
st->msgtype = nc_send_rpc(tmp, st->rpc, 1000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);

/* Receive rpc-error reply */
st->msgtype = nc_recv_reply(tmp, 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_NAME(lyd_child(st->envp)), "rpc-error");
/* Check if correct error-tag */
assert_string_equal(lyd_get_value(lyd_child(lyd_child(st->envp))->next), "invalid-value");
ASSERT_ERROR_REPLY_PARAM(st, tmp, "invalid-value");

/* Check if correct error-app-tag */
assert_string_equal(lyd_get_value(lyd_child(lyd_child(st->envp))->next->next->next),
"ietf-subscribed-notifications:no-such-subscription");
Expand Down Expand Up @@ -929,11 +926,9 @@ test_killsub_diff_sess(void **state)
free(ntf);
FREE_TEST_VARS(st);
st->rpc = nc_rpc_killsub(st->ntf_id);

/* Receive OK reply */
st->msgtype = nc_recv_reply(tmp, 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_NAME(lyd_child(st->envp)), "ok");
ASSERT_OK_REPLY_SESS(st, tmp);
FREE_TEST_VARS(st);

/* Send notification, should NOT arrive */
Expand Down
22 changes: 4 additions & 18 deletions tests/test_subscribe_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,11 @@ test_subtree_filter_no_matching_node(void **state)
st->nc_sess = nc_connect_unix(st->socket_path, (struct ly_ctx *)nc_session_get_ctx(st->nc_sess2));
assert_non_null(st->nc_sess);

/* Get a subscription to receive notifications */
/* Get a subscription to receive notifications, rpc-error since no notification can match the filter */
st->rpc = nc_rpc_subscribe(NULL, filter, NULL, NULL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 1000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);

/* Check reply */
st->msgtype = nc_recv_reply(st->nc_sess, st->rpc, st->msgid, 1000, &st->envp, &st->op);
assert_int_equal(NC_MSG_REPLY, st->msgtype);
assert_null(st->op);

/* Should be an rpc-error since no notification can match the filter */
assert_string_equal(LYD_NAME(lyd_child(st->envp)), "rpc-error");
ASSERT_ERROR_REPLY(st);
FREE_TEST_VARS(st);
}

Expand Down Expand Up @@ -308,18 +301,11 @@ test_xpath_filter_no_matching_node(void **state)
st->nc_sess = nc_connect_unix(st->socket_path, (struct ly_ctx *)nc_session_get_ctx(st->nc_sess2));
assert_non_null(st->nc_sess);

/* Get a subscription to receive notifications */
/* Get a subscription to receive notifications, rpc-error since no notification can match the filter */
st->rpc = nc_rpc_subscribe(NULL, "/notif2:devices/device[name='Main']", NULL, NULL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 1000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);

/* Check reply */
st->msgtype = nc_recv_reply(st->nc_sess, st->rpc, st->msgid, 1000, &st->envp, &st->op);
assert_int_equal(NC_MSG_REPLY, st->msgtype);
assert_null(st->op);

/* Should be an rpc-error since no notification can match the filter */
assert_string_equal(LYD_NAME(lyd_child(st->envp)), "rpc-error");
ASSERT_ERROR_REPLY(st);
FREE_TEST_VARS(st);
}

Expand Down
14 changes: 1 addition & 13 deletions tests/test_subscribe_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ reestablish_sub(void **state, const char *stream, const char *start_time, const
assert_int_equal(NC_MSG_RPC, st->msgtype);

/* Check reply */
st->msgtype = NC_MSG_NOTIF;
while (st->msgtype == NC_MSG_NOTIF) {
st->msgtype = nc_recv_reply(st->nc_sess, st->rpc, st->msgid, 1000, &st->envp, &st->op);
}
assert_int_equal(NC_MSG_REPLY, st->msgtype);
assert_null(st->op);
if (strcmp(LYD_NAME(lyd_child(st->envp)), "ok")) {
lyd_print_file(stdout, st->envp, LYD_XML, 0);
fprintf(stdout, "start time: %s\n", start_time);
fprintf(stdout, "stop time: %s\n", stop_time);
fail();
}

ASSERT_OK_REPLY(st);
FREE_TEST_VARS(st);
}

Expand Down
9 changes: 1 addition & 8 deletions tests/test_with_defaults.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,9 @@ test_all_nothing_set(void **state)
st->rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, "/defaults1:*", NC_WD_ALL, NC_PARAMTYPE_CONST);
st->msgtype = nc_send_rpc(st->nc_sess, st->rpc, 1000, &st->msgid);
assert_int_equal(NC_MSG_RPC, st->msgtype);
st->msgtype = nc_recv_reply(st->nc_sess, st->rpc, st->msgid, 2000, &st->envp, &st->op);

/* Get reply, should succeed */
assert_int_equal(st->msgtype, NC_MSG_REPLY);
assert_non_null(st->op);
assert_non_null(st->envp);
assert_string_equal(LYD_NAME(lyd_child(st->op)), "data");
assert_int_equal(LY_SUCCESS, lyd_print_mem(&st->str, st->op, LYD_XML, 0));

ASSERT_DATA_REPLY(st);
expected =
"<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
" <data>\n"
Expand All @@ -91,7 +85,6 @@ test_all_nothing_set(void **state)
" </top>\n"
" </data>\n"
"</get-config>\n";

assert_string_equal(st->str, expected);

FREE_TEST_VARS(st);
Expand Down

0 comments on commit 8b32e15

Please sign in to comment.