FILE
harness-selftest.c
File preview with syntax highlighting.
linux-6.19.14/tools/testing/selftests/kselftest_harness/harness-selftest.c
1// SPDX-License-Identifier: GPL-2.0 2 3#include <stdio.h> 4 5#include <sys/resource.h> 6#include <sys/prctl.h> 7 8/* Avoid any inconsistencies */ 9#define TH_LOG_STREAM stdout 10 11#include "kselftest_harness.h" 12 13static void test_helper(struct __test_metadata *_metadata) 14{ 15 ASSERT_EQ(0, 0); 16} 17 18TEST(standalone_pass) { 19 TH_LOG("before"); 20 ASSERT_EQ(0, 0); 21 EXPECT_EQ(0, 0); 22 test_helper(_metadata); 23 TH_LOG("after"); 24} 25 26TEST(standalone_fail) { 27 TH_LOG("before"); 28 EXPECT_EQ(0, 0); 29 EXPECT_EQ(0, 1); 30 ASSERT_EQ(0, 1); 31 TH_LOG("after"); 32} 33 34TEST_SIGNAL(signal_pass, SIGUSR1) { 35 TH_LOG("before"); 36 ASSERT_EQ(0, 0); 37 TH_LOG("after"); 38 kill(getpid(), SIGUSR1); 39} 40 41TEST_SIGNAL(signal_fail, SIGUSR1) { 42 TH_LOG("before"); 43 ASSERT_EQ(0, 1); 44 TH_LOG("after"); 45 kill(getpid(), SIGUSR1); 46} 47 48FIXTURE(fixture) { 49 pid_t testpid; 50}; 51 52FIXTURE_SETUP(fixture) { 53 TH_LOG("setup"); 54 self->testpid = getpid(); 55} 56 57FIXTURE_TEARDOWN(fixture) { 58 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 59} 60 61TEST_F(fixture, pass) { 62 TH_LOG("before"); 63 ASSERT_EQ(0, 0); 64 test_helper(_metadata); 65 standalone_pass(_metadata); 66 TH_LOG("after"); 67} 68 69TEST_F(fixture, fail) { 70 TH_LOG("before"); 71 ASSERT_EQ(0, 1); 72 fixture_pass(_metadata, self, variant); 73 TH_LOG("after"); 74} 75 76TEST_F_TIMEOUT(fixture, timeout, 1) { 77 TH_LOG("before"); 78 sleep(2); 79 TH_LOG("after"); 80} 81 82FIXTURE(fixture_parent) { 83 pid_t testpid; 84}; 85 86FIXTURE_SETUP(fixture_parent) { 87 TH_LOG("setup"); 88 self->testpid = getpid(); 89} 90 91FIXTURE_TEARDOWN_PARENT(fixture_parent) { 92 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 93} 94 95TEST_F(fixture_parent, pass) { 96 TH_LOG("before"); 97 ASSERT_EQ(0, 0); 98 TH_LOG("after"); 99} 100 101FIXTURE(fixture_setup_failure) { 102 pid_t testpid; 103}; 104 105FIXTURE_SETUP(fixture_setup_failure) { 106 TH_LOG("setup"); 107 self->testpid = getpid(); 108 ASSERT_EQ(0, 1); 109} 110 111FIXTURE_TEARDOWN(fixture_setup_failure) { 112 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 113} 114 115TEST_F(fixture_setup_failure, pass) { 116 TH_LOG("before"); 117 ASSERT_EQ(0, 0); 118 TH_LOG("after"); 119} 120 121int main(int argc, char **argv) 122{ 123 /* 124 * The harness uses abort() to signal assertion failures, which triggers coredumps. 125 * This may be useful to debug real failures but not for this selftest, disable them. 126 */ 127 struct rlimit rlimit = { 128 .rlim_cur = 0, 129 .rlim_max = 0, 130 }; 131 132 prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); 133 setrlimit(RLIMIT_CORE, &rlimit); 134 135 return test_harness_run(argc, argv); 136}
~/
Read-only file preview.