selftests/seccomp: Allow get_metadata to XFAIL
authorKees Cook <keescook@chromium.org>
Thu, 15 Mar 2018 16:59:16 +0000 (09:59 -0700)
committerShuah Khan <shuahkh@osg.samsung.com>
Wed, 21 Mar 2018 16:42:46 +0000 (10:42 -0600)
Since seccomp_get_metadata() depends on CHECKPOINT_RESTORE, XFAIL the
test if the ptrace reports it as missing.

Signed-off-by: Kees Cook <keescook@chromium.org>
Acked-by: Tycho Andersen <tycho@tycho.ws>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
tools/testing/selftests/kselftest_harness.h
tools/testing/selftests/seccomp/seccomp_bpf.c

index e81bd28bdd8940366eacb3bda6428bf7d47b5f24..6ae3730c4ee35b1cfc2e93ed39aac6b2bf3755ad 100644 (file)
                fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \
                        __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
 
+/**
+ * XFAIL(statement, fmt, ...)
+ *
+ * @statement: statement to run after reporting XFAIL
+ * @fmt: format string
+ * @...: optional arguments
+ *
+ * This forces a "pass" after reporting a failure with an XFAIL prefix,
+ * and runs "statement", which is usually "return" or "goto skip".
+ */
+#define XFAIL(statement, fmt, ...) do { \
+       if (TH_LOG_ENABLED) { \
+               fprintf(TH_LOG_STREAM, "[  XFAIL!  ] " fmt "\n", \
+                       ##__VA_ARGS__); \
+       } \
+       /* TODO: find a way to pass xfail to test runner process. */ \
+       _metadata->passed = 1; \
+       _metadata->trigger = 0; \
+       statement; \
+} while (0)
+
 /**
  * TEST(test_name) - Defines the test function and creates the registration
  * stub
 
 /**
  * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
- * *_metadata* is included so that ASSERT_* work as a convenience
+ * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
  *
  * @fixture_name: fixture name
  *
                FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
 /**
  * FIXTURE_TEARDOWN(fixture_name)
+ * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
  *
  * @fixture_name: fixture name
  *
  * Defines a test that depends on a fixture (e.g., is part of a test case).
  * Very similar to TEST() except that *self* is the setup instance of fixture's
  * datatype exposed for use by the implementation.
+ *
+ * Warning: use of ASSERT_* here will skip TEARDOWN.
  */
 /* TODO(wad) register fixtures on dedicated test lists. */
 #define TEST_F(fixture_name, test_name) \
index 5df609950a666f8000307653a0b1e8d4368549a2..168c66d74fc5ca67f300cd7235182e4fd3d572aa 100644 (file)
@@ -2860,6 +2860,7 @@ TEST(get_metadata)
        int pipefd[2];
        char buf;
        struct seccomp_metadata md;
+       long ret;
 
        ASSERT_EQ(0, pipe(pipefd));
 
@@ -2893,16 +2894,26 @@ TEST(get_metadata)
        ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid));
        ASSERT_EQ(pid, waitpid(pid, NULL, 0));
 
+       /* Past here must not use ASSERT or child process is never killed. */
+
        md.filter_off = 0;
-       ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md));
+       errno = 0;
+       ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md);
+       EXPECT_EQ(sizeof(md), ret) {
+               if (errno == EINVAL)
+                       XFAIL(goto skip, "Kernel does not support PTRACE_SECCOMP_GET_METADATA (missing CONFIG_CHECKPOINT_RESTORE?)");
+       }
+
        EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG);
        EXPECT_EQ(md.filter_off, 0);
 
        md.filter_off = 1;
-       ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md));
+       ret = ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md);
+       EXPECT_EQ(sizeof(md), ret);
        EXPECT_EQ(md.flags, 0);
        EXPECT_EQ(md.filter_off, 1);
 
+skip:
        ASSERT_EQ(0, kill(pid, SIGKILL));
 }