aboutsummaryrefslogtreecommitdiff
path: root/testsuite/test-util.c
blob: 621446b10503908371c5b820fe0f6dc8a4a5e1f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (C) 2012-2013  ProFUSION embedded systems
 * Copyright (C) 2012  Pedro Pedruzzi
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <shared/util.h>

#include "testsuite.h"

static int alias_1(const struct test *t)
{
	static const char *input[] = {
		"test1234",
		"test[abcfoobar]2211",
		"bar[aaa][bbbb]sss",
		"kmod[p.b]lib",
		"[az]1234[AZ]",
		NULL,
	};

	char buf[PATH_MAX];
	size_t len;
	const char **alias;

	for (alias = input; *alias != NULL; alias++) {
		int ret;

		ret = alias_normalize(*alias, buf, &len);
		printf("input   %s\n", *alias);
		printf("return  %d\n", ret);

		if (ret == 0) {
			printf("len     %zu\n", len);
			printf("output  %s\n", buf);
		}

		printf("\n");
	}

	return EXIT_SUCCESS;
}
DEFINE_TEST(alias_1,
	.description = "check if alias_normalize does the right thing",
	.config = {
		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
	},
	.need_spawn = true,
	.output = {
		.out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
	});

static int test_freadline_wrapped(const struct test *t)
{
	FILE *fp = fopen("/freadline_wrapped-input.txt", "re");

	if (!fp)
		return EXIT_FAILURE;

	while (!feof(fp) && !ferror(fp)) {
		unsigned int num = 0;
		char *s = freadline_wrapped(fp, &num);
		if (!s)
			break;
		puts(s);
		free(s);
		printf("%u\n", num);
	}

	fclose(fp);
	return EXIT_SUCCESS;
}
DEFINE_TEST(test_freadline_wrapped,
	.description = "check if freadline_wrapped() does the right thing",
	.config = {
		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
	},
	.need_spawn = true,
	.output = {
		.out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt",
	});

static int test_strchr_replace(const struct test *t)
{
	_cleanup_free_ char *s = strdup("this is a test string");
	const char *res = "thiC iC a teCt Ctring";

	strchr_replace(s, 's', 'C');
	assert_return(streq(s, res), EXIT_FAILURE);

	return EXIT_SUCCESS;
}
DEFINE_TEST(test_strchr_replace,
	.description = "check implementation of strchr_replace()",
	.need_spawn = false,
	);

static int test_underscores(const struct test *t)
{
	struct teststr {
		char *val;
		const char *res;
	} teststr[] = {
		{ strdup("aa-bb-cc_"), "aa_bb_cc_" },
		{ strdup("-aa-bb-cc-"), "_aa_bb_cc_" },
		{ strdup("-aa[-bb-]cc-"), "_aa[-bb-]cc_" },
		{ strdup("-aa-[bb]-cc-"), "_aa_[bb]_cc_" },
		{ strdup("-aa-[b-b]-cc-"), "_aa_[b-b]_cc_" },
		{ strdup("-aa-b[-]b-cc"), "_aa_b[-]b_cc" },
		{ }
	}, *iter;

	for (iter = &teststr[0]; iter->val != NULL; iter++) {
		_cleanup_free_ char *val = iter->val;
		underscores(val);
		assert_return(streq(val, iter->res), EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}
DEFINE_TEST(test_underscores,
	.description = "check implementation of underscores()",
	.need_spawn = false,
	);

static int test_path_ends_with_kmod_ext(const struct test *t)
{
	struct teststr {
		const char *val;
		bool res;
	} teststr[] = {
		{ "/bla.ko", true },
#ifdef ENABLE_ZLIB
		{ "/bla.ko.gz", true },
#endif
#ifdef ENABLE_XZ
		{ "/bla.ko.xz", true },
#endif
#ifdef ENABLE_ZSTD
		{ "/bla.ko.zst", true },
#endif
		{ "/bla.ko.x", false },
		{ "/bla.ko.", false },
		{ "/bla.koz", false },
		{ "/b", false },
		{ }
	}, *iter;

	for (iter = &teststr[0]; iter->val != NULL; iter++) {
		assert_return(path_ends_with_kmod_ext(iter->val,
						      strlen(iter->val)) == iter->res,
			      EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}
DEFINE_TEST(test_path_ends_with_kmod_ext,
	.description = "check implementation of path_ends_with_kmod_ext()",
	.need_spawn = false,
	);

#define TEST_WRITE_STR_SAFE_FILE "/write-str-safe"
#define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-util2/" TEST_WRITE_STR_SAFE_FILE
static int test_write_str_safe(const struct test *t)
{
	const char *s = "test";
	int fd;

	fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
	assert_return(fd >= 0, EXIT_FAILURE);

	write_str_safe(fd, s, strlen(s));
	close(fd);

	return EXIT_SUCCESS;
}
DEFINE_TEST(test_write_str_safe,
	.description = "check implementation of write_str_safe()",
	.config = {
		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
	},
	.need_spawn = true,
	.output = {
		.files = (const struct keyval[]) {
			{ TEST_WRITE_STR_SAFE_PATH ".txt",
			  TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
			{ }
		},
	});

static int test_addu64_overflow(const struct test *t)
{
	uint64_t res;
	bool overflow;

	overflow = addu64_overflow(UINT64_MAX - 1, 1, &res);
	assert_return(!overflow, EXIT_FAILURE);
	assert_return(res == UINT64_MAX, EXIT_FAILURE);

	overflow = addu64_overflow(UINT64_MAX, 1, &res);
	assert_return(overflow, EXIT_FAILURE);

	return EXIT_SUCCESS;
}
DEFINE_TEST(test_addu64_overflow,
	.description = "check implementation of addu4_overflow()",
	.need_spawn = false,
	);


TESTSUITE_MAIN();