]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: use assertion macroses
authorAnton Tiurin <noxiouz@meta.com>
Thu, 4 Sep 2025 17:28:52 +0000 (10:28 -0700)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 5 Sep 2025 13:14:49 +0000 (15:14 +0200)
An assertion macros helps to debug failing tests as it reports
expression, arguments and errno (if applicable)

src/test/test-cryptolib.c
src/test/test-io-util.c

index 1a09297a3fe265d4ba2ecc7654e7a44d64dfc8df..d86236bafe028538f7a5d41928505c19cc8de130 100644 (file)
@@ -7,27 +7,19 @@
 TEST(string_hashsum) {
         _cleanup_free_ char *out1 = NULL, *out2 = NULL, *out3 = NULL, *out4 = NULL;
 
-        assert_se(string_hashsum("asdf", 4,
-                                 "SHA224",
-                                 &out1) == 0);
+        ASSERT_OK(string_hashsum("asdf", 4, "SHA224", &out1));
         /* echo -n 'asdf' | sha224sum - */
         ASSERT_STREQ(out1, "7872a74bcbf298a1e77d507cd95d4f8d96131cbbd4cdfc571e776c8a");
 
-        assert_se(string_hashsum("asdf", 4,
-                                 "SHA256",
-                                 &out2) == 0);
+        ASSERT_OK(string_hashsum("asdf", 4, "SHA256", &out2));
         /* echo -n 'asdf' | sha256sum - */
         ASSERT_STREQ(out2, "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b");
 
-        assert_se(string_hashsum("", 0,
-                                 "SHA224",
-                                 &out3) == 0);
+        ASSERT_OK(string_hashsum("", 0, "SHA224", &out3));
         /* echo -n '' | sha224sum - */
         ASSERT_STREQ(out3, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f");
 
-        assert_se(string_hashsum("", 0,
-                                 "SHA256",
-                                 &out4) == 0);
+        ASSERT_OK(string_hashsum("", 0, "SHA256", &out4));
         /* echo -n '' | sha256sum - */
         ASSERT_STREQ(out4, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
 }
index 45da6b83f130529652e3c2c1281aa04ab1abe0b3..dbf3495ab91b2ffd5ed7e4b78c5b59313ecfb160 100644 (file)
 static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
         char check[n];
 
-        assert_se(lseek(fd, 0, SEEK_SET) == 0);
-        assert_se(ftruncate(fd, 0) >= 0);
-        assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
+        ASSERT_OK_EQ_ERRNO(lseek(fd, 0, SEEK_SET), 0);
+        ASSERT_OK_ERRNO(ftruncate(fd, 0));
+        ASSERT_OK_EQ(sparse_write(fd, buffer, n, 4), (ssize_t) n);
 
-        assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
-        assert_se(ftruncate(fd, n) >= 0);
+        ASSERT_OK_EQ_ERRNO(lseek(fd, 0, SEEK_CUR), (off_t) n);
+        ASSERT_OK_ERRNO(ftruncate(fd, n));
 
-        assert_se(lseek(fd, 0, SEEK_SET) == 0);
-        assert_se(read(fd, check, n) == (ssize_t) n);
+        ASSERT_OK_EQ_ERRNO(lseek(fd, 0, SEEK_SET), 0);
+        ASSERT_OK_EQ_ERRNO(read(fd, check, n), (ssize_t) n);
 
-        assert_se(memcmp(buffer, check, n) == 0);
+        ASSERT_EQ(memcmp(buffer, check, n), 0);
 }
 
 TEST(sparse_write) {
@@ -33,8 +33,7 @@ TEST(sparse_write) {
         _cleanup_close_ int fd = -EBADF;
         char fn[] = "/tmp/sparseXXXXXX";
 
-        fd = mkostemp(fn, O_CLOEXEC);
-        assert_se(fd >= 0);
+        ASSERT_OK_ERRNO(fd = mkostemp(fn, O_CLOEXEC));
         (void) unlink(fn);
 
         test_sparse_write_one(fd, test_a, sizeof(test_a));