Skip to content

Commit

Permalink
test client BUGFIX more accurate error reply message
Browse files Browse the repository at this point in the history
  • Loading branch information
lePici committed Sep 4, 2024
1 parent 3d79b5a commit 52bded5
Show file tree
Hide file tree
Showing 3 changed files with 513 additions and 4 deletions.
84 changes: 81 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,63 @@ np2srv_err_nc(sr_error_info_err_t *err)
return NULL;
}

/**
* @brief Find the nth substring delimited by quotes.
*
* For example: abcd"ef"ghij"kl"mn -> rank 1 is "ef", rank 2 is "kl".
*
* @param[in] msg Input string with quoted substring.
* @param[in] rank Number starting from 1 specifying the nth substring.
* @return Copied nth substring without quotes.
*/
static char *
np2srv_err_reply_get_quoted_string(const char *msg, uint32_t rank)
{
char *ret;
const char *start = NULL, *end = NULL, *iter, *tmp;
uint32_t cnt = 0;

assert(msg && (rank > 0));

rank *= 2;
for (iter = msg; *iter != '\0'; ++iter) {
if (*iter == '\"') {
/* updating the start and end pointers */
tmp = end;
end = iter;
start = tmp;
if (++cnt == rank) {
/* nth substring found */
break;
}
}
}

if (!start) {
return NULL;
}

/* Skip first quote */
++start;
/* Copy substring */
ret = strndup(start, end - start);

return ret;
}

/**
* @brief Check that the @p str starts with the @p prefix.
*
* @param[in] prefix Required prefix.
* @param[in] str Input string to check.
* @return True if @p str start with @p prefix otherwise False.
*/
static ly_bool
np2srv_strstarts(const char *prefix, const char *str)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}

/**
* @brief Create NC error reply based on SR error info.
*
Expand All @@ -262,6 +319,9 @@ np2srv_err_reply_sr(const sr_error_info_t *err_info)
struct lyd_node *e;
const struct ly_ctx *ly_ctx;
size_t i;
char *str, *path;
const struct lysc_node *cn;
NC_ERR_TYPE errtype;

/* try to find a NETCONF error(s) */
for (i = 0; i < err_info->err_count; ++i) {
Expand All @@ -284,16 +344,34 @@ np2srv_err_reply_sr(const sr_error_info_t *err_info)

ly_ctx = sr_acquire_context(np2srv.sr_conn);
for (i = 0; i < err_info->err_count; ++i) {
/* generic error */
e = nc_err(ly_ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP);
nc_err_set_msg(e, err_info->err[i].message, "en");
if (np2srv_strstarts("Mandatory node", err_info->err[i].message) ||
np2srv_strstarts("Mandatory choice", err_info->err[i].message)) {
str = np2srv_err_reply_get_quoted_string(err_info->err[i].message, 1);
path = np2srv_err_reply_get_quoted_string(err_info->err[i].message, 2);
cn = lys_find_path(ly_ctx, NULL, path, 0);
if (cn && ((cn->nodetype & LYS_RPC) || (cn->nodetype & LYS_INPUT))) {
errtype = NC_ERR_TYPE_PROT;
} else {
errtype = NC_ERR_TYPE_APP;
}
e = nc_err(ly_ctx, NC_ERR_MISSING_ELEM, errtype, str);
free(str);
free(path);
} else if (err_info->err->err_code == SR_ERR_NO_MEMORY) {
e = nc_err(ly_ctx, NC_ERR_RES_DENIED, NC_ERR_TYPE_APP);
} else {
/* generic error */
e = nc_err(ly_ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP);
}

nc_err_set_msg(e, err_info->err[i].message, "en");
if (reply) {
nc_server_reply_add_err(reply, e);
} else {
reply = nc_server_reply_err(e);
}
}
/* clear for other errors */
sr_release_context(np2srv.sr_conn);

return reply;
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ set(TEST_SRC "np2_test.c" "np2_other_client.c")

# list of all the tests
set(TESTS test_rpc test_edit test_filter test_subscribe_filter test_subscribe_param test_parallel_sessions
test_candidate test_with_defaults test_nacm test_sub_ntf test_sub_ntf_advanced test_sub_ntf_filter test_error)
test_candidate test_with_defaults test_nacm test_sub_ntf test_sub_ntf_advanced test_sub_ntf_filter test_error test_other_client)

if(CMAKE_C_FLAGS MATCHES "-fsanitize=thread")
message(WARNING "Features which use SIGEV_THREAD are known to be broken under TSAN, disabling tests for YANG-push and confirmed commit")
Expand Down
Loading

0 comments on commit 52bded5

Please sign in to comment.