[Concept,03/15] test: Check for null string in assert functions

Message ID 20251111124131.1198930-4-sjg@u-boot.org
State New
Headers
Series luks: Provide support for LUKSv2 |

Commit Message

Simon Glass Nov. 11, 2025, 12:41 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Update ut_asserteq_str() and ut_asserteq_strn() to check for NULL. This
allows tests to avoid doing this.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 include/test/ut.h | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
  

Patch

diff --git a/include/test/ut.h b/include/test/ut.h
index 6731b43dba9..70eaaea5e0e 100644
--- a/include/test/ut.h
+++ b/include/test/ut.h
@@ -206,7 +206,15 @@  int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
 	const char *_val1 = (expr1), *_val2 = (expr2);			\
 	int __ret = 0;							\
 									\
-	if (strcmp(_val1, _val2)) {					\
+	if (!_val1 || !_val2) {						\
+		ut_failf(uts, __FILE__, __LINE__, __func__,		\
+			 #expr1 " = " #expr2,				\
+			 "Expected \"%s\", got \"%s\"",			\
+			 _val1 ? _val1 : "(null)",			\
+			 _val2 ? _val2 : "(null)");			\
+		if (!uts->soft_fail)					\
+			return CMD_RET_FAILURE;				\
+	} else if (strcmp(_val1, _val2)) {				\
 		ut_failf(uts, __FILE__, __LINE__, __func__,		\
 			 #expr1 " = " #expr2,				\
 			 "Expected \"%s\", got \"%s\"", _val1, _val2);	\
@@ -222,16 +230,26 @@  int ut_check_console_dump(struct unit_test_state *uts, int total_bytes);
  */
 #define ut_asserteq_strn(expr1, expr2) ({				\
 	const char *_val1 = (expr1), *_val2 = (expr2);			\
-	int _len = strlen(_val1);					\
 	int __ret = 0;							\
 									\
-	if (memcmp(_val1, _val2, _len)) {				\
+	if (!_val1 || !_val2) {						\
 		ut_failf(uts, __FILE__, __LINE__, __func__,		\
 			 #expr1 " = " #expr2,				\
-			 "Expected \"%.*s\", got \"%.*s\"",		\
-			 _len, _val1, _len, _val2);			\
+			 "Expected \"%s\", got \"%s\"",			\
+			 _val1 ? _val1 : "(null)",			\
+			 _val2 ? _val2 : "(null)");			\
 		if (!uts->soft_fail)					\
 			return CMD_RET_FAILURE;				\
+	} else {							\
+		int _len = strlen(_val1);				\
+		if (memcmp(_val1, _val2, _len)) {			\
+			ut_failf(uts, __FILE__, __LINE__, __func__,	\
+				 #expr1 " = " #expr2,			\
+				 "Expected \"%.*s\", got \"%.*s\"",	\
+				 _len, _val1, _len, _val2);		\
+			if (!uts->soft_fail)				\
+				return CMD_RET_FAILURE;			\
+		}							\
 	}								\
 	__ret;								\
 })