Skip to content

Commit

Permalink
tests: fix _nltst_object_to_string() to print one line only
Browse files Browse the repository at this point in the history
  • Loading branch information
thom311 committed May 17, 2024
1 parent 529c2ab commit 401c248
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions tests/nl-test-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,45 @@ void _nltst_object_identical(const void *a, const void *b)

/*****************************************************************************/

char *_nltst_object_to_string(struct nl_object *obj)
char *_nltst_object_to_string(const struct nl_object *obj)
{
size_t L = 1024;
size_t l;
char *s;
struct nl_dump_params dp;
char canary;

if (!obj)
return strdup("(null)");

s = malloc(L);
ck_assert_ptr_nonnull(s);

nl_object_dump_buf(obj, s, L);
canary = _nltst_rand_u32();
s[L - 1u] = canary;

dp = (struct nl_dump_params){
.dp_type = NL_DUMP_LINE,
.dp_buf = s,
.dp_buflen = L - 1u,
};

nl_object_dump((struct nl_object *)obj, &dp);

l = strlen(s);
ck_assert_int_ge(l, 2);
ck_assert_int_lt(l, L);
ck_assert(canary == s[L - 1u]);
s = realloc(s, l + 1);
ck_assert_ptr_nonnull(s);

ck_assert_msg(s[l - 1u] == '\n',
"expects newline after dump. Got \"%s\"", s);
s[l - 1u] = '\0';

ck_assert_msg(!strchr(s, '\n'),
"no further newline expected. Got \"%s\"", s);

return s;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/nl-test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ void nltst_netns_leave(struct nltst_netns *nsdata);

void _nltst_object_identical(const void *a, const void *b);

char *_nltst_object_to_string(struct nl_object *obj);
char *_nltst_object_to_string(const struct nl_object *obj);

struct nl_object **_nltst_cache_get_all(struct nl_cache *cache,
size_t *out_len);
Expand Down

0 comments on commit 401c248

Please sign in to comment.