Skip to content

Commit bd7ed71

Browse files
evelikovLucas De Marchi
authored andcommitted
testsuite: convert all tests to TS_ASSERT()
Currently we have some inconsistencies across the tests: - using TS_ASSERT vs not - (non TS_ASSERT tests) leaking on error vs not In practical terms, we are not too worried about the leaks, since the test failure comes first. As such, convert all the tests to TS_ASSERT(). This means we loose the useful error messages in a few instances, which could be re-introduced at a later point alongside a TS_ASSERT_MSG() macro. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> Link: #375 Signed-off-by: Lucas De Marchi <ldemarchi@kernel.org>
1 parent 4cca148 commit bd7ed71

12 files changed

Lines changed: 65 additions & 205 deletions

testsuite/test-blacklist.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,34 @@ static int blacklist_1(void)
3333
static const char *const names[] = { "pcspkr", "pcspkr2", "floppy", "ext4" };
3434

3535
ctx = kmod_new(NULL, NULL);
36-
if (ctx == NULL)
37-
return EXIT_FAILURE;
36+
TS_ASSERT(ctx != NULL);
3837

3938
for (size_t i = 0; i < ARRAY_SIZE(names); i++) {
4039
err = kmod_module_new_from_name(ctx, names[i], &mod);
41-
if (err < 0)
42-
goto fail_lookup;
40+
TS_ASSERT(err == 0);
4341
list = kmod_list_append(list, mod);
4442
}
4543

4644
err = kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, list, &filtered);
47-
if (err < 0) {
48-
ERR("Could not filter: %s\n", strerror(-err));
49-
goto fail;
50-
}
51-
if (filtered == NULL) {
52-
ERR("All modules were filtered out!\n");
53-
goto fail;
54-
}
45+
TS_ASSERT(err == 0);
46+
TS_ASSERT(filtered != NULL);
5547

5648
kmod_list_foreach(l, filtered) {
5749
const char *modname;
5850
mod = kmod_module_get_module(l);
5951
modname = kmod_module_get_name(mod);
60-
if (streq("pcspkr", modname) || streq("floppy", modname))
61-
goto fail;
52+
TS_ASSERT(!streq("pcspkr", modname) && !streq("floppy", modname));
6253
len++;
6354
kmod_module_unref(mod);
6455
}
6556

66-
if (len != 2)
67-
goto fail;
57+
TS_ASSERT(len == 2);
6858

6959
kmod_module_unref_list(filtered);
7060
kmod_module_unref_list(list);
7161
kmod_unref(ctx);
7262

7363
return EXIT_SUCCESS;
74-
75-
fail:
76-
kmod_module_unref_list(list);
77-
fail_lookup:
78-
kmod_unref(ctx);
79-
return EXIT_FAILURE;
8064
}
8165

8266
DEFINE_TEST(blacklist_1, .description = "check if modules are correctly blacklisted",

testsuite/test-dependencies.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ static int test_dependencies(void)
2828
int fooa = 0, foob = 0, fooc = 0;
2929

3030
ctx = kmod_new(NULL, NULL);
31-
if (ctx == NULL)
32-
return EXIT_FAILURE;
31+
TS_ASSERT(ctx != NULL);
3332

3433
err = kmod_module_new_from_name(ctx, "mod-foo", &mod);
35-
if (err < 0 || mod == NULL) {
36-
kmod_unref(ctx);
37-
return EXIT_FAILURE;
38-
}
34+
TS_ASSERT(err == 0 && mod != NULL);
3935

4036
list = kmod_module_get_dependencies(mod);
4137

@@ -56,8 +52,7 @@ static int test_dependencies(void)
5652
}
5753

5854
/* fooa, foob, fooc */
59-
if (len != 3 || !fooa || !foob || !fooc)
60-
return EXIT_FAILURE;
55+
TS_ASSERT(len == 3 && fooa && foob && fooc);
6156

6257
kmod_module_unref_list(list);
6358
kmod_module_unref(mod);

testsuite/test-init.c

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ static int test_load_resources(void)
2424
int err;
2525

2626
ctx = kmod_new(NULL, &null_config);
27-
if (ctx == NULL)
28-
return EXIT_FAILURE;
27+
TS_ASSERT(ctx != NULL);
2928

3029
kmod_set_log_priority(ctx, 7);
3130

3231
err = kmod_load_resources(ctx);
33-
if (err != 0) {
34-
ERR("could not load libkmod resources: %s\n", strerror(-err));
35-
return EXIT_FAILURE;
36-
}
32+
TS_ASSERT(err == 0);
3733

3834
kmod_unref(ctx);
3935

@@ -64,8 +60,7 @@ static int test_initlib(void)
6460
const char *null_config = NULL;
6561

6662
ctx = kmod_new(NULL, &null_config);
67-
if (ctx == NULL)
68-
return EXIT_FAILURE;
63+
TS_ASSERT(ctx != NULL);
6964

7065
kmod_unref(ctx);
7166

@@ -81,20 +76,14 @@ static int test_insert(void)
8176
int err;
8277

8378
ctx = kmod_new(NULL, &null_config);
84-
if (ctx == NULL)
85-
return EXIT_FAILURE;
79+
TS_ASSERT(ctx != NULL);
8680

8781
err = kmod_module_new_from_path(ctx, "/mod-simple.ko", &mod);
88-
if (err != 0) {
89-
ERR("could not create module from path: %s\n", strerror(-err));
90-
return EXIT_FAILURE;
91-
}
82+
TS_ASSERT(err == 0);
9283

9384
err = kmod_module_insert_module(mod, 0, NULL);
94-
if (err != 0) {
95-
ERR("could not insert module: %s\n", strerror(-err));
96-
return EXIT_FAILURE;
97-
}
85+
TS_ASSERT(err == 0);
86+
9887
kmod_module_unref(mod);
9988
kmod_unref(ctx);
10089

@@ -116,32 +105,19 @@ static int test_remove(void)
116105
int err;
117106

118107
ctx = kmod_new(NULL, &null_config);
119-
if (ctx == NULL)
120-
return EXIT_FAILURE;
108+
TS_ASSERT(ctx != NULL);
121109

122110
err = kmod_module_new_from_name(ctx, "mod-simple", &mod_simple);
123-
if (err != 0) {
124-
ERR("could not create module from name: %s\n", strerror(-err));
125-
return EXIT_FAILURE;
126-
}
111+
TS_ASSERT(err == 0);
127112

128113
err = kmod_module_new_from_name(ctx, "bla", &mod_bla);
129-
if (err != 0) {
130-
ERR("could not create module from name: %s\n", strerror(-err));
131-
return EXIT_FAILURE;
132-
}
114+
TS_ASSERT(err == 0);
133115

134116
err = kmod_module_remove_module(mod_simple, 0);
135-
if (err != 0) {
136-
ERR("could not remove module: %s\n", strerror(-err));
137-
return EXIT_FAILURE;
138-
}
117+
TS_ASSERT(err == 0);
139118

140119
err = kmod_module_remove_module(mod_bla, 0);
141-
if (err != -ENOENT) {
142-
ERR("wrong return code for failure test: %d\n", err);
143-
return EXIT_FAILURE;
144-
}
120+
TS_ASSERT(err == -ENOENT);
145121

146122
kmod_module_unref(mod_bla);
147123
kmod_module_unref(mod_simple);

testsuite/test-initstate.c

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,16 @@ static int test_initstate_from_lookup(void)
2727
int err, r;
2828

2929
ctx = kmod_new(NULL, &null_config);
30-
if (ctx == NULL)
31-
return EXIT_FAILURE;
30+
TS_ASSERT(ctx != NULL);
3231

3332
err = kmod_module_new_from_lookup(ctx, "fake-builtin", &list);
34-
if (err < 0) {
35-
ERR("could not create module from lookup: %s\n", strerror(-err));
36-
return EXIT_FAILURE;
37-
}
38-
39-
if (!list) {
40-
ERR("could not create module from lookup: module not found: fake-builtin\n");
41-
return EXIT_FAILURE;
42-
}
33+
TS_ASSERT(err == 0);
34+
TS_ASSERT(list != NULL);
4335

4436
mod = kmod_module_get_module(list);
4537

4638
r = kmod_module_get_initstate(mod);
47-
if (r != KMOD_MODULE_BUILTIN) {
48-
ERR("module should have builtin state but is: %s\n",
49-
kmod_module_initstate_str(r));
50-
return EXIT_FAILURE;
51-
}
39+
TS_ASSERT(r == KMOD_MODULE_BUILTIN);
5240

5341
kmod_module_unref(mod);
5442
kmod_module_unref_list(list);
@@ -73,26 +61,14 @@ static int test_initstate_from_name(void)
7361
int err, r;
7462

7563
ctx = kmod_new(NULL, &null_config);
76-
if (ctx == NULL)
77-
return EXIT_FAILURE;
64+
TS_ASSERT(ctx != NULL);
7865

7966
err = kmod_module_new_from_name(ctx, "fake-builtin", &mod);
80-
if (err != 0) {
81-
ERR("could not create module from lookup: %s\n", strerror(-err));
82-
return EXIT_FAILURE;
83-
}
84-
85-
if (!mod) {
86-
ERR("could not create module from lookup: module not found: fake-builtin\n");
87-
return EXIT_FAILURE;
88-
}
67+
TS_ASSERT(err == 0);
68+
TS_ASSERT(mod != NULL);
8969

9070
r = kmod_module_get_initstate(mod);
91-
if (r != KMOD_MODULE_BUILTIN) {
92-
ERR("module should have builtin state but is: %s\n",
93-
kmod_module_initstate_str(r));
94-
return EXIT_FAILURE;
95-
}
71+
TS_ASSERT(r == KMOD_MODULE_BUILTIN);
9672

9773
kmod_module_unref(mod);
9874
kmod_unref(ctx);

testsuite/test-loaded.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ static int loaded_1(void)
2222
int err;
2323

2424
ctx = kmod_new(NULL, &null_config);
25-
if (ctx == NULL)
26-
return EXIT_FAILURE;
25+
TS_ASSERT(ctx != NULL);
2726

2827
err = kmod_module_new_from_loaded(ctx, &list);
29-
if (err < 0) {
30-
fprintf(stderr, "%s\n", strerror(-err));
31-
kmod_unref(ctx);
32-
return EXIT_FAILURE;
33-
}
28+
TS_ASSERT(err == 0);
3429

3530
printf("Module Size Used by\n");
3631

testsuite/test-modprobe.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,7 @@ DEFINE_TEST(modprobe_module_from_abspath,
393393

394394
static int modprobe_module_from_relpath(void)
395395
{
396-
if (chdir("/home/foo") != 0) {
397-
perror("failed to change into /home/foo");
398-
return EXIT_FAILURE;
399-
}
396+
TS_ASSERT(chdir("/home/foo") == 0);
400397

401398
return EXEC_TOOL(modprobe, "./mod-simple.ko");
402399
}

testsuite/test-multi-softdep.c

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ static int check_dependencies(const char *const *modnames,
3636
const struct kmod_list *itr;
3737
bool visited[MAX_SOFTDEP_N] = {};
3838
int mod_index;
39-
bool all_loaded = true;
4039

4140
kmod_list_foreach(itr, mod_list) {
4241
struct kmod_module *softdep_mod = kmod_module_get_module(itr);
@@ -56,15 +55,9 @@ static int check_dependencies(const char *const *modnames,
5655
for (mod_index = 0; mod_index < MAX_SOFTDEP_N; mod_index++) {
5756
if (!modnames[mod_index])
5857
break;
59-
if (!visited[mod_index]) {
60-
ERR("softdep %s not loaded\n", modnames[mod_index]);
61-
all_loaded = false;
62-
}
58+
TS_ASSERT(visited[mod_index]);
6359
}
64-
if (all_loaded)
65-
return 0;
66-
else
67-
return -1;
60+
return 0;
6861
}
6962

7063
static int multi_softdep(void)
@@ -78,46 +71,33 @@ static int multi_softdep(void)
7871
int err;
7972

8073
ctx = kmod_new(NULL, NULL);
81-
if (ctx == NULL)
82-
return EXIT_FAILURE;
74+
TS_ASSERT(ctx != NULL);
8375

8476
for (mod_index = 0; mod_index < ARRAY_SIZE(test_modules); mod_index++) {
8577
modname = test_modules[mod_index].modname;
8678
printf("module %s:\n", modname);
8779
err = kmod_module_new_from_name(ctx, modname, &mod);
88-
if (err < 0)
89-
goto fail;
80+
TS_ASSERT(err == 0);
81+
9082
pre = NULL;
9183
post = NULL;
9284
err = kmod_module_get_softdeps(mod, &pre, &post);
93-
if (err < 0) {
94-
ERR("could not get softdeps of '%s': %s\n", modname,
95-
strerror(-err));
96-
goto fail;
97-
}
85+
TS_ASSERT(err == 0);
9886

9987
printf("pre: ");
10088
err = check_dependencies(test_modules[mod_index].pre, pre);
101-
if (err < 0)
102-
goto fail;
89+
TS_ASSERT(err == 0);
90+
10391
printf("post: ");
10492
err = check_dependencies(test_modules[mod_index].post, post);
105-
if (err < 0)
106-
goto fail;
93+
TS_ASSERT(err == 0);
10794

10895
kmod_module_unref_list(pre);
10996
kmod_module_unref_list(post);
11097
kmod_module_unref(mod);
11198
}
11299
kmod_unref(ctx);
113100
return EXIT_SUCCESS;
114-
115-
fail:
116-
kmod_module_unref_list(pre);
117-
kmod_module_unref_list(post);
118-
kmod_module_unref(mod);
119-
kmod_unref(ctx);
120-
return EXIT_FAILURE;
121101
}
122102

123103
/*

testsuite/test-new-module.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ static int from_name(void)
3131
int err;
3232

3333
ctx = kmod_new(NULL, &null_config);
34-
if (ctx == NULL)
35-
return EXIT_FAILURE;
34+
TS_ASSERT(ctx != NULL);
3635

3736
for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) {
3837
err = kmod_module_new_from_name(ctx, modnames[i], &mod);
39-
if (err < 0)
40-
return EXIT_FAILURE;
38+
TS_ASSERT(err == 0);
4139

4240
printf("modname: %s\n", kmod_module_get_name(mod));
4341
kmod_module_unref(mod);
@@ -65,15 +63,13 @@ static int from_alias(void)
6563
int err;
6664

6765
ctx = kmod_new(NULL, NULL);
68-
if (ctx == NULL)
69-
return EXIT_FAILURE;
66+
TS_ASSERT(ctx != NULL);
7067

7168
for (size_t i = 0; i < ARRAY_SIZE(modnames); i++) {
7269
struct kmod_list *l, *list = NULL;
7370

7471
err = kmod_module_new_from_lookup(ctx, modnames[i], &list);
75-
if (err < 0)
76-
return EXIT_FAILURE;
72+
TS_ASSERT(err == 0);
7773

7874
kmod_list_foreach(l, list) {
7975
struct kmod_module *m;

0 commit comments

Comments
 (0)