Skip to content

Commit 993eeea

Browse files
authored
Merge pull request systemd#21501 from medhefgo/test
test: Use TEST macro
2 parents 66a8edd + c462e63 commit 993eeea

File tree

120 files changed

+1349
-3357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1349
-3357
lines changed

src/shared/tests.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "sd-daemon.h"
77

88
#include "macro.h"
9+
#include "util.h"
910

1011
static inline bool manager_errno_skip_test(int r) {
1112
return IN_SET(abs(r),
@@ -77,16 +78,14 @@ static inline void run_test_table(void) {
7778
}
7879
}
7980

80-
#define DEFINE_TEST_MAIN \
81-
int main(int argc, char *argv[]) { \
82-
test_setup_logging(LOG_INFO); \
83-
run_test_table(); \
84-
return EXIT_SUCCESS; \
81+
#define DEFINE_CUSTOM_TEST_MAIN(log_level, intro, outro) \
82+
int main(int argc, char *argv[]) { \
83+
test_setup_logging(log_level); \
84+
save_argc_argv(argc, argv); \
85+
intro; \
86+
run_test_table(); \
87+
outro; \
88+
return EXIT_SUCCESS; \
8589
}
8690

87-
#define DEFINE_CUSTOM_TEST_MAIN(impl) \
88-
int main(int argc, char *argv[]) { \
89-
test_setup_logging(LOG_INFO); \
90-
run_test_table(); \
91-
return impl(); \
92-
}
91+
#define DEFINE_TEST_MAIN(log_level) DEFINE_CUSTOM_TEST_MAIN(log_level, , )

src/test/meson.build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ tests += [
389389
test_hashmap_ordered_c],
390390
[], [], [], '', 'timeout=180'],
391391

392-
[['src/test/test-set.c'],
393-
[libbasic]],
392+
[['src/test/test-set.c']],
394393

395394
[['src/test/test-ordered-set.c']],
396395

src/test/test-af-list.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "macro.h"
66
#include "string-util.h"
7+
#include "tests.h"
78
#include "util.h"
89

910
_unused_
@@ -13,11 +14,8 @@ static const struct af_name* lookup_af(register const char *str, register GPERF_
1314
#include "af-list.h"
1415
#include "af-to-name.h"
1516

16-
int main(int argc, const char *argv[]) {
17-
18-
unsigned i;
19-
20-
for (i = 0; i < ELEMENTSOF(af_names); i++) {
17+
TEST(af_list) {
18+
for (unsigned i = 0; i < ELEMENTSOF(af_names); i++) {
2119
if (af_names[i]) {
2220
assert_se(streq(af_to_name(i), af_names[i]));
2321
assert_se(af_from_name(af_names[i]) == (int) i);
@@ -28,6 +26,6 @@ int main(int argc, const char *argv[]) {
2826
assert_se(af_to_name(-1) == NULL);
2927
assert_se(af_from_name("huddlduddl") == -EINVAL);
3028
assert_se(af_from_name("") == -EINVAL);
31-
32-
return 0;
3329
}
30+
31+
DEFINE_TEST_MAIN(LOG_INFO);

src/test/test-alloc-util.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "random-util.h"
1010
#include "tests.h"
1111

12-
static void test_alloca(void) {
12+
TEST(alloca) {
1313
static const uint8_t zero[997] = { };
1414
char *t;
1515

@@ -22,7 +22,7 @@ static void test_alloca(void) {
2222
assert_se(!memcmp(t, zero, 997));
2323
}
2424

25-
static void test_GREEDY_REALLOC(void) {
25+
TEST(GREEDY_REALLOC) {
2626
_cleanup_free_ int *a = NULL, *b = NULL;
2727
size_t i, j;
2828

@@ -55,7 +55,7 @@ static void test_GREEDY_REALLOC(void) {
5555
assert_se(b[j] == (int) j);
5656
}
5757

58-
static void test_memdup_multiply_and_greedy_realloc(void) {
58+
TEST(memdup_multiply_and_greedy_realloc) {
5959
static const int org[] = { 1, 2, 3 };
6060
_cleanup_free_ int *dup;
6161
size_t i;
@@ -90,7 +90,7 @@ static void test_memdup_multiply_and_greedy_realloc(void) {
9090
assert_se(p[i] == 0);
9191
}
9292

93-
static void test_bool_assign(void) {
93+
TEST(bool_assign) {
9494
bool b, c, *cp = &c, d, e, f, g, h;
9595

9696
b = 123;
@@ -125,7 +125,7 @@ static void cleanup3(void *a) {
125125
assert_se(++cleanup_counter == *(int*) a);
126126
}
127127

128-
static void test_cleanup_order(void) {
128+
TEST(cleanup_order) {
129129
_cleanup_(cleanup1) int x1 = 4, x2 = 3;
130130
_cleanup_(cleanup3) int z = 2;
131131
_cleanup_(cleanup2) int y = 1;
@@ -135,7 +135,7 @@ static void test_cleanup_order(void) {
135135
log_debug("z: %p", &z);
136136
}
137137

138-
static void test_auto_erase_memory(void) {
138+
TEST(auto_erase_memory) {
139139
_cleanup_(erase_and_freep) uint8_t *p1, *p2;
140140

141141
/* print address of p2, else e.g. clang-11 will optimize it out */
@@ -165,7 +165,7 @@ static void test_auto_erase_memory(void) {
165165
assert_se(__builtin_object_size(f, 0) >= sizeof(*f) * n); \
166166
} while(false)
167167

168-
static void test_malloc_size_safe(void) {
168+
TEST(malloc_size_safe) {
169169
_cleanup_free_ uint32_t *f = NULL;
170170
size_t n = 4711;
171171

@@ -191,16 +191,4 @@ static void test_malloc_size_safe(void) {
191191
}
192192
}
193193

194-
int main(int argc, char *argv[]) {
195-
test_setup_logging(LOG_DEBUG);
196-
197-
test_alloca();
198-
test_GREEDY_REALLOC();
199-
test_memdup_multiply_and_greedy_realloc();
200-
test_bool_assign();
201-
test_cleanup_order();
202-
test_auto_erase_memory();
203-
test_malloc_size_safe();
204-
205-
return 0;
206-
}
194+
DEFINE_TEST_MAIN(LOG_DEBUG);

src/test/test-arphrd-util.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
#include "string-util.h"
77
#include "tests.h"
88

9-
int main(int argc, const char *argv[]) {
10-
test_setup_logging(LOG_INFO);
11-
9+
TEST(arphrd) {
1210
for (int i = 0; i <= ARPHRD_VOID + 1; i++) {
1311
const char *name;
1412

@@ -23,6 +21,6 @@ int main(int argc, const char *argv[]) {
2321
assert_se(arphrd_to_name(ARPHRD_VOID + 1) == NULL);
2422
assert_se(arphrd_from_name("huddlduddl") == -EINVAL);
2523
assert_se(arphrd_from_name("") == -EINVAL);
26-
27-
return 0;
2824
}
25+
26+
DEFINE_TEST_MAIN(LOG_INFO);

src/test/test-ask-password-api.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "strv.h"
55
#include "tests.h"
66

7-
static void test_ask_password(void) {
7+
TEST(ask_password) {
88
int r;
99
_cleanup_strv_free_ char **ret = NULL;
1010

@@ -18,9 +18,4 @@ static void test_ask_password(void) {
1818
}
1919
}
2020

21-
int main(int argc, char **argv) {
22-
test_setup_logging(LOG_DEBUG);
23-
24-
test_ask_password();
25-
return EXIT_SUCCESS;
26-
}
21+
DEFINE_TEST_MAIN(LOG_DEBUG);

src/test/test-blockdev-util.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void test_path_is_encrypted_one(const char *p, int expect) {
2222
assert_se(expect < 0 || ((r > 0) == (expect > 0)));
2323
}
2424

25-
static void test_path_is_encrypted(void) {
25+
TEST(path_is_encrypted) {
2626
int booted = sd_booted(); /* If this is run in build environments such as koji, /dev might be a
2727
* reguar fs. Don't assume too much if not running under systemd. */
2828

@@ -36,8 +36,4 @@ static void test_path_is_encrypted(void) {
3636
test_path_is_encrypted_one("/dev", booted > 0 ? false : -1);
3737
}
3838

39-
int main(int argc, char **argv) {
40-
test_setup_logging(LOG_INFO);
41-
42-
test_path_is_encrypted();
43-
}
39+
DEFINE_TEST_MAIN(LOG_INFO);

src/test/test-bus-util.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ static void destroy_callback(void *userdata) {
1414
(*n_called) ++;
1515
}
1616

17-
static void test_destroy_callback(void) {
17+
TEST(destroy_callback) {
1818
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1919
sd_bus_slot *slot = NULL;
2020
sd_bus_destroy_t t;
2121

2222
int r, n_called = 0;
2323

24-
log_info("/* %s */", __func__);
25-
2624
r = bus_open_system_watch_bind_with_description(&bus, "test-bus");
2725
if (r < 0) {
2826
log_error_errno(r, "Failed to connect to bus: %m");
@@ -46,10 +44,4 @@ static void test_destroy_callback(void) {
4644
assert_se(n_called == 1);
4745
}
4846

49-
int main(int argc, char **argv) {
50-
test_setup_logging(LOG_DEBUG);
51-
52-
test_destroy_callback();
53-
54-
return 0;
55-
}
47+
DEFINE_TEST_MAIN(LOG_DEBUG);

src/test/test-calendarspec.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "env-util.h"
66
#include "errno-util.h"
77
#include "string-util.h"
8+
#include "tests.h"
89

910
static void _test_one(int line, const char *input, const char *output) {
1011
CalendarSpec *c;
@@ -67,7 +68,7 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a
6768
}
6869
#define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect)
6970

70-
static void test_timestamp(void) {
71+
TEST(timestamp) {
7172
char buf[FORMAT_TIMESTAMP_MAX];
7273
_cleanup_free_ char *t = NULL;
7374
CalendarSpec *c;
@@ -88,7 +89,7 @@ static void test_timestamp(void) {
8889
assert_se(y == x);
8990
}
9091

91-
static void test_hourly_bug_4031(void) {
92+
TEST(hourly_bug_4031) {
9293
CalendarSpec *c;
9394
usec_t n, u, w;
9495
int r;
@@ -111,9 +112,7 @@ static void test_hourly_bug_4031(void) {
111112
calendar_spec_free(c);
112113
}
113114

114-
int main(int argc, char* argv[]) {
115-
CalendarSpec *c;
116-
115+
TEST(calendar_spec_one) {
117116
test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00");
118117
test_one("Sat,Thu,Mon..Wed,Sat..Sun", "Mon..Thu,Sat,Sun *-*-* 00:00:00");
119118
test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00");
@@ -180,7 +179,9 @@ int main(int argc, char* argv[]) {
180179
test_one("@0 UTC", "1970-01-01 00:00:00 UTC");
181180
test_one("*:05..05", "*-*-* *:05:00");
182181
test_one("*:05..10/6", "*-*-* *:05:00");
182+
}
183183

184+
TEST(calendar_spec_next) {
184185
test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000);
185186
test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000);
186187
test_next("2016-03-27 03:17:00", "EET", 12345, -1);
@@ -214,6 +215,10 @@ int main(int argc, char* argv[]) {
214215
/* Check that we don't start looping if mktime() moves us backwards */
215216
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000);
216217
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000);
218+
}
219+
220+
TEST(calendar_spec_from_string) {
221+
CalendarSpec *c;
217222

218223
assert_se(calendar_spec_from_string("test", &c) < 0);
219224
assert_se(calendar_spec_from_string(" utc", &c) < 0);
@@ -240,9 +245,6 @@ int main(int argc, char* argv[]) {
240245
assert_se(calendar_spec_from_string("00:00:2300", &c) < 0);
241246
assert_se(calendar_spec_from_string("00:00:18446744073709551615", &c) < 0);
242247
assert_se(calendar_spec_from_string("@88588582097858858", &c) == -ERANGE);
243-
244-
test_timestamp();
245-
test_hourly_bug_4031();
246-
247-
return 0;
248248
}
249+
250+
DEFINE_TEST_MAIN(LOG_INFO);

src/test/test-cap-list.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include "capability-util.h"
99
#include "parse-util.h"
1010
#include "string-util.h"
11+
#include "tests.h"
1112
#include "util.h"
1213

1314
/* verify the capability parser */
14-
static void test_cap_list(void) {
15+
TEST(cap_list) {
1516
assert_se(!capability_to_name(-1));
1617
assert_se(!capability_to_name(capability_list_length()));
1718

@@ -70,7 +71,7 @@ static void test_capability_set_one(uint64_t c, const char *t) {
7071
assert_se(c1 == c_masked);
7172
}
7273

73-
static void test_capability_set_from_string(void) {
74+
TEST(capability_set_from_string) {
7475
uint64_t c;
7576

7677
assert_se(capability_set_from_string(NULL, &c) == 0);
@@ -89,7 +90,7 @@ static void test_capability_set_from_string(void) {
8990
assert_se(c == (UINT64_C(1) << 4) - 1);
9091
}
9192

92-
static void test_capability_set_to_string(uint64_t invalid_cap_set) {
93+
static void test_capability_set_to_string_invalid(uint64_t invalid_cap_set) {
9394
uint64_t c;
9495

9596
test_capability_set_one(invalid_cap_set, "");
@@ -114,15 +115,13 @@ static void test_capability_set_to_string(uint64_t invalid_cap_set) {
114115
"cap_audit_control cap_mac_override cap_syslog"));
115116
}
116117

117-
int main(int argc, char *argv[]) {
118-
test_cap_list();
119-
test_capability_set_from_string();
120-
test_capability_set_to_string(0);
118+
TEST(capability_set_to_string) {
119+
test_capability_set_to_string_invalid(0);
121120

122121
/* once the kernel supports 63 caps, there are no 'invalid' numbers
123122
* for us to test with */
124123
if (cap_last_cap() < 63)
125-
test_capability_set_to_string(all_capabilities() + 1);
126-
127-
return 0;
124+
test_capability_set_to_string_invalid(all_capabilities() + 1);
128125
}
126+
127+
DEFINE_TEST_MAIN(LOG_INFO);

0 commit comments

Comments
 (0)