changeset 874:e795a6fc02c3

json: correctly compare expected and actual test values The buffer filled by the json packing API are *not* nul-terminated. Therefore, we cannot use strcmp to compare its contents with the expected values. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 17 Apr 2024 08:20:12 -0400
parents 67a53cc79d23
children d82835b66d5a
files tests/test_json_pack.c
diffstat 1 files changed, 4 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_json_pack.c	Wed Apr 17 08:25:52 2024 -0400
+++ b/tests/test_json_pack.c	Wed Apr 17 08:20:12 2024 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2017-2020,2024 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -31,11 +31,12 @@
 static void cmp_buffers(const char *exp, struct buffer *_got)
 {
 	const char *got = buffer_data(_got);
+	const size_t glen = buffer_size(_got);
 
 	fprintf(stderr, "  expected: %s\n", exp);
-	fprintf(stderr, "  got:      %s\n", got);
+	fprintf(stderr, "  got:      %*.*s\n", (int) glen, (int) glen, got);
 
-	if (strcmp(got, exp))
+	if ((strlen(exp) != glen) || strncmp(got, exp, glen))
 		fail("json packing failed: content mismatch");
 }