selftests/bpf: Move and extend ASSERT_xxx() testing macros
authorAndrii Nakryiko <andriin@fb.com>
Tue, 29 Sep 2020 04:30:44 +0000 (21:30 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 29 Sep 2020 19:21:23 +0000 (12:21 -0700)
Move existing ASSERT_xxx() macros out of btf_write selftest into test_progs.h
to use across all selftests. Also expand a set of macros for typical cases.

Now there are the following macros:
  - ASSERT_EQ() -- check for equality of two integers;
  - ASSERT_STREQ() -- check for equality of two C strings;
  - ASSERT_OK() -- check for successful (zero) return result;
  - ASSERT_ERR() -- check for unsuccessful (non-zero) return result;
  - ASSERT_NULL() -- check for NULL pointer;
  - ASSERT_OK_PTR() -- check for a valid pointer;
  - ASSERT_ERR_PTR() -- check for NULL or negative error encoded in a pointer.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200929043046.1324350-2-andriin@fb.com
tools/testing/selftests/bpf/prog_tests/btf_write.c
tools/testing/selftests/bpf/test_progs.h

index 88dce2c..314e1e7 100644 (file)
@@ -3,40 +3,6 @@
 #include <test_progs.h>
 #include <bpf/btf.h>
 
-#define ASSERT_EQ(actual, expected, name) ({                           \
-       typeof(actual) ___act = (actual);                               \
-       typeof(expected) ___exp = (expected);                           \
-       bool ___ok = ___act == ___exp;                                  \
-       CHECK(!___ok, (name),                                           \
-             "unexpected %s: actual %lld != expected %lld\n",          \
-             (name), (long long)(___act), (long long)(___exp));        \
-       ___ok;                                                          \
-})
-
-#define ASSERT_STREQ(actual, expected, name) ({                                \
-       const char *___act = actual;                                    \
-       const char *___exp = expected;                                  \
-       bool ___ok = strcmp(___act, ___exp) == 0;                       \
-       CHECK(!___ok, (name),                                           \
-             "unexpected %s: actual '%s' != expected '%s'\n",          \
-             (name), ___act, ___exp);                                  \
-       ___ok;                                                          \
-})
-
-#define ASSERT_OK(res, name) ({                                                \
-       long long ___res = (res);                                       \
-       bool ___ok = ___res == 0;                                       \
-       CHECK(!___ok, (name), "unexpected error: %lld\n", ___res);      \
-       ___ok;                                                          \
-})
-
-#define ASSERT_ERR(res, name) ({                                       \
-       long long ___res = (res);                                       \
-       bool ___ok = ___res < 0;                                        \
-       CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);    \
-       ___ok;                                                          \
-})
-
 static int duration = 0;
 
 void test_btf_write() {
index dbb820d..238f5f6 100644 (file)
@@ -130,6 +130,69 @@ extern int test__join_cgroup(const char *path);
 #define CHECK_ATTR(condition, tag, format...) \
        _CHECK(condition, tag, tattr.duration, format)
 
+#define ASSERT_EQ(actual, expected, name) ({                           \
+       static int duration = 0;                                        \
+       typeof(actual) ___act = (actual);                               \
+       typeof(expected) ___exp = (expected);                           \
+       bool ___ok = ___act == ___exp;                                  \
+       CHECK(!___ok, (name),                                           \
+             "unexpected %s: actual %lld != expected %lld\n",          \
+             (name), (long long)(___act), (long long)(___exp));        \
+       ___ok;                                                          \
+})
+
+#define ASSERT_STREQ(actual, expected, name) ({                                \
+       static int duration = 0;                                        \
+       const char *___act = actual;                                    \
+       const char *___exp = expected;                                  \
+       bool ___ok = strcmp(___act, ___exp) == 0;                       \
+       CHECK(!___ok, (name),                                           \
+             "unexpected %s: actual '%s' != expected '%s'\n",          \
+             (name), ___act, ___exp);                                  \
+       ___ok;                                                          \
+})
+
+#define ASSERT_OK(res, name) ({                                                \
+       static int duration = 0;                                        \
+       long long ___res = (res);                                       \
+       bool ___ok = ___res == 0;                                       \
+       CHECK(!___ok, (name), "unexpected error: %lld\n", ___res);      \
+       ___ok;                                                          \
+})
+
+#define ASSERT_ERR(res, name) ({                                       \
+       static int duration = 0;                                        \
+       long long ___res = (res);                                       \
+       bool ___ok = ___res < 0;                                        \
+       CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);    \
+       ___ok;                                                          \
+})
+
+#define ASSERT_NULL(ptr, name) ({                                      \
+       static int duration = 0;                                        \
+       const void *___res = (ptr);                                     \
+       bool ___ok = !___res;                                           \
+       CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);      \
+       ___ok;                                                          \
+})
+
+#define ASSERT_OK_PTR(ptr, name) ({                                    \
+       static int duration = 0;                                        \
+       const void *___res = (ptr);                                     \
+       bool ___ok = !IS_ERR_OR_NULL(___res);                           \
+       CHECK(!___ok, (name),                                           \
+             "unexpected error: %ld\n", PTR_ERR(___res));              \
+       ___ok;                                                          \
+})
+
+#define ASSERT_ERR_PTR(ptr, name) ({                                   \
+       static int duration = 0;                                        \
+       const void *___res = (ptr);                                     \
+       bool ___ok = IS_ERR(___res)                                     \
+       CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);      \
+       ___ok;                                                          \
+})
+
 static inline __u64 ptr_to_u64(const void *ptr)
 {
        return (__u64) (unsigned long) ptr;