public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/2] Add TST_ASSERT_SYSCALL*() macros
@ 2020-03-05 14:36 Martin Doucha
  2020-03-05 14:36 ` [LTP] [PATCH 2/2] Reimplement TST_SAFE_TIMERFD_*() using TST_ASSERT_SYSCALL*() Martin Doucha
  2020-03-05 14:46 ` [LTP] [PATCH 1/2] Add TST_ASSERT_SYSCALL*() macros Cyril Hrubis
  0 siblings, 2 replies; 3+ messages in thread
From: Martin Doucha @ 2020-03-05 14:36 UTC (permalink / raw)
  To: ltp

These macros take care of the standard return value checking boilerplate
in cases where the test cannot continue after error.

- TST_ASSERT_SYSCALL() calls tst_brk() if retval != 0
- TST_ASSERT_SYSCALL_FD() calls tst_brk() if retval < 0

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---

Small convenience patch that'll simplify both test development and TST_SAFE_*()
library function generation.

 include/tst_test.h | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/include/tst_test.h b/include/tst_test.h
index 8508c2e38..65a5f05b8 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -281,6 +281,53 @@ extern void *TST_RET_PTR;
 		TST_ERR = errno; \
 	} while (0)
 
+/* assert that syscall returned only 0 and nothing else */
+#define TST_ASSERT_SYSCALL(SCALL) \
+	TST_ASSERT_SYSCALL_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret != 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
+/*
+ * assert that syscall returned any non-negative value (e.g. valid file
+ * descriptor)
+ */
+#define TST_ASSERT_SYSCALL_FD(SCALL) \
+	TST_ASSERT_SYSCALL_FD_IMPL(SCALL, __FILE__, __LINE__)
+
+#define TST_ASSERT_SYSCALL_FD_IMPL(SCALL, FILENAME, LINENO) \
+	({ \
+		int _tst_ret; \
+		errno = 0; \
+		_tst_ret = SCALL; \
+		if (_tst_ret == -1) { \
+			int _tst_ttype = errno == ENOTSUP ? TCONF : TBROK; \
+			tst_brk(_tst_ttype | TERRNO, "%s:%d " # SCALL \
+				" failed", FILENAME, LINENO); \
+		} \
+		if (_tst_ret < 0) { \
+			tst_brk(TBROK | TERRNO, "%s:%d " # SCALL \
+				" returned invalid value %d", FILENAME, \
+				LINENO, _tst_ret); \
+		} \
+		_tst_ret; \
+	})
+
 /*
  * Functions to convert ERRNO to its name and SIGNAL to its name.
  */
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-05 14:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-05 14:36 [LTP] [PATCH 1/2] Add TST_ASSERT_SYSCALL*() macros Martin Doucha
2020-03-05 14:36 ` [LTP] [PATCH 2/2] Reimplement TST_SAFE_TIMERFD_*() using TST_ASSERT_SYSCALL*() Martin Doucha
2020-03-05 14:46 ` [LTP] [PATCH 1/2] Add TST_ASSERT_SYSCALL*() macros Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox