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 13 static void test_helper(struct __test_metadata *_metadata) 14 { 15 ASSERT_EQ(0, 0); 16 } 17 18 TEST(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 26 TEST(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 34 TEST_SIGNAL(signal_pass, SIGUSR1) { 35 TH_LOG("before"); 36 ASSERT_EQ(0, 0); 37 TH_LOG("after"); 38 kill(getpid(), SIGUSR1); 39 } 40 41 TEST_SIGNAL(signal_fail, SIGUSR1) { 42 TH_LOG("before"); 43 ASSERT_EQ(0, 1); 44 TH_LOG("after"); 45 kill(getpid(), SIGUSR1); 46 } 47 48 FIXTURE(fixture) { 49 pid_t testpid; 50 }; 51 52 FIXTURE_SETUP(fixture) { 53 TH_LOG("setup"); 54 self->testpid = getpid(); 55 } 56 57 FIXTURE_TEARDOWN(fixture) { 58 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 59 } 60 61 TEST_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 69 TEST_F(fixture, fail) { 70 TH_LOG("before"); 71 ASSERT_EQ(0, 1); 72 fixture_pass(_metadata, self, variant); 73 TH_LOG("after"); 74 } 75 76 TEST_F_TIMEOUT(fixture, timeout, 1) { 77 TH_LOG("before"); 78 sleep(2); 79 TH_LOG("after"); 80 } 81 82 FIXTURE(fixture_parent) { 83 pid_t testpid; 84 }; 85 86 FIXTURE_SETUP(fixture_parent) { 87 TH_LOG("setup"); 88 self->testpid = getpid(); 89 } 90 91 FIXTURE_TEARDOWN_PARENT(fixture_parent) { 92 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 93 } 94 95 TEST_F(fixture_parent, pass) { 96 TH_LOG("before"); 97 ASSERT_EQ(0, 0); 98 TH_LOG("after"); 99 } 100 101 FIXTURE(fixture_setup_failure) { 102 pid_t testpid; 103 }; 104 105 FIXTURE_SETUP(fixture_setup_failure) { 106 TH_LOG("setup"); 107 self->testpid = getpid(); 108 ASSERT_EQ(0, 1); 109 } 110 111 FIXTURE_TEARDOWN(fixture_setup_failure) { 112 TH_LOG("teardown same-process=%d", self->testpid == getpid()); 113 } 114 115 TEST_F(fixture_setup_failure, pass) { 116 TH_LOG("before"); 117 ASSERT_EQ(0, 0); 118 TH_LOG("after"); 119 } 120 121 int 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 } 137