summaryrefslogtreecommitdiffstats
path: root/src/shared/tests.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/shared/tests.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/shared/tests.h b/src/shared/tests.h
index 09fdfd6..f904c0d 100644
--- a/src/shared/tests.h
+++ b/src/shared/tests.h
@@ -76,6 +76,7 @@ void test_setup_logging(int level);
int write_tmpfile(char *pattern, const char *contents);
bool have_namespaces(void);
+bool userns_has_single_user(void);
/* We use the small but non-trivial limit here */
#define CAN_MEMLOCK_SIZE (512 * 1024U)
@@ -217,6 +218,39 @@ static inline int run_test_table(void) {
} \
})
+/* For funtions that return a boolean on success and a negative errno on failure. */
+#define ASSERT_OK_POSITIVE(expr) \
+ ({ \
+ typeof(expr) _result = (expr); \
+ if (_result < 0) { \
+ log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \
+ PROJECT_FILE, __LINE__, #expr); \
+ abort(); \
+ } \
+ if (_result == 0) { \
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be positive, but it is zero.", \
+ PROJECT_FILE, __LINE__, #expr); \
+ abort(); \
+ } \
+ })
+
+#define ASSERT_OK_ZERO(expr) \
+ ({ \
+ typeof(expr) _result = (expr); \
+ if (_result < 0) { \
+ log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to succeed but got the following error: %m", \
+ PROJECT_FILE, __LINE__, #expr); \
+ abort(); \
+ } \
+ if (_result != 0) { \
+ char _sexpr[DECIMAL_STR_MAX(typeof(expr))]; \
+ xsprintf(_sexpr, DECIMAL_STR_FMT(_result), _result); \
+ log_error("%s:%i: Assertion failed: expected \"%s\" to be zero, but it is %s.", \
+ PROJECT_FILE, __LINE__, #expr, _sexpr); \
+ abort(); \
+ } \
+ })
+
#define ASSERT_OK_ERRNO(expr) \
({ \
typeof(expr) _result = (expr); \
@@ -227,6 +261,16 @@ static inline int run_test_table(void) {
} \
})
+#define ASSERT_FAIL(expr) \
+ ({ \
+ typeof(expr) _result = (expr); \
+ if (_result >= 0) { \
+ log_error_errno(_result, "%s:%i: Assertion failed: expected \"%s\" to fail, but it succeeded", \
+ PROJECT_FILE, __LINE__, #expr); \
+ abort(); \
+ } \
+ })
+
#define ASSERT_ERROR(expr1, expr2) \
({ \
int _expr1 = (expr1); \
@@ -419,3 +463,30 @@ static inline int run_test_table(void) {
abort(); \
} \
})
+
+
+#define ASSERT_EQ_ID128(expr1, expr2) \
+ ({ \
+ typeof(expr1) _expr1 = (expr1); \
+ typeof(expr2) _expr2 = (expr2); \
+ if (!sd_id128_equal(_expr1, _expr2)) { \
+ log_error("%s:%i: Assertion failed: \"%s == %s\", but \"%s != %s\"", \
+ PROJECT_FILE, __LINE__, \
+ #expr1, #expr2, \
+ SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
+ abort(); \
+ } \
+ })
+
+#define ASSERT_NE_ID128(expr1, expr2) \
+ ({ \
+ typeof(expr1) _expr1 = (expr1); \
+ typeof(expr2) _expr2 = (expr2); \
+ if (sd_id128_equal(_expr1, _expr2)) { \
+ log_error("%s:%i: Assertion failed: \"%s != %s\", but \"%s == %s\"", \
+ PROJECT_FILE, __LINE__, \
+ #expr1, #expr2, \
+ SD_ID128_TO_STRING(_expr1), SD_ID128_TO_STRING(_expr2)); \
+ abort(); \
+ } \
+ })