Skip to content

Commit 3977703

Browse files
Pancakemkartben
authored andcommitted
tests: posix: common: separate posix c_lib_ext to standalone test
posix.common contains testsuites that can be separated into smaller groups of tests. This change moves fnmatch, getopt and getentropy into a singular testsuite at tests/posix/c_lib_ext app directory. Signed-off-by: Marvin Ouma <[email protected]>
1 parent 18fd49e commit 3977703

File tree

12 files changed

+88
-161
lines changed

12 files changed

+88
-161
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Copyright (c) 2024 Google LLC
2-
#
31
# SPDX-License-Identifier: Apache-2.0
42

53
cmake_minimum_required(VERSION 3.20.0)
64
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7-
project(getentropy)
5+
project(posix_c_lib_ext)
86

97
FILE(GLOB app_sources src/*.c)
8+
109
target_sources(app PRIVATE ${app_sources})
10+
11+
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/lib/posix/options/getopt)
12+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
CONFIG_ENTROPY_GENERATOR=y
2-
CONFIG_POSIX_C_LIB_EXT=y
31
CONFIG_ZTEST=y
2+
3+
CONFIG_POSIX_C_LIB_EXT=y
4+
CONFIG_GETOPT_LONG=y
5+
CONFIG_ENTROPY_GENERATOR=y

tests/posix/common/src/fnmatch.c renamed to tests/posix/c_lib_ext/src/fnmatch.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <fnmatch.h>
8-
7+
#include <zephyr/posix/fnmatch.h>
98
#include <zephyr/ztest.h>
109

1110
/*
1211
* Adapted from
1312
* https://git.musl-libc.org/cgit/libc-testsuite/tree/fnmatch.c
1413
*/
15-
ZTEST(fnmatch, test_fnmatch)
14+
ZTEST(posix_c_lib_ext, test_fnmatch)
1615
{
1716
/* Note: commented out lines indicate known problems to be addressed in #55186 */
1817

@@ -82,5 +81,3 @@ ZTEST(fnmatch, test_fnmatch)
8281
zassert_ok(fnmatch("a*b", "a.b", FNM_PATHNAME | FNM_PERIOD));
8382
zassert_ok(fnmatch("a[.]b", "a.b", FNM_PATHNAME | FNM_PERIOD));
8483
}
85-
86-
ZTEST_SUITE(fnmatch, NULL, NULL, NULL, NULL, NULL);

tests/posix/getentropy/src/main.c renamed to tests/posix/c_lib_ext/src/getentropy.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <zephyr/ztest.h>
8-
97
#include <zephyr/posix/unistd.h>
8+
#include <zephyr/ztest.h>
109

11-
ZTEST(getentropy_test_suite, test_getentropy_too_large)
10+
ZTEST(posix_c_lib_ext, test_getentropy_too_large)
1211
{
13-
uint8_t buf[256 + 1] = { 0 };
12+
uint8_t buf[256 + 1] = {0};
1413
int ret;
1514

1615
ret = getentropy(buf, sizeof(buf));
1716
zassert_equal(ret, -1);
1817
zassert_equal(errno, EIO);
1918
}
2019

21-
ZTEST(getentropy_test_suite, test_getentropy_null_buffer)
20+
ZTEST(posix_c_lib_ext, test_getentropy_null_buffer)
2221
{
2322
int ret;
2423

@@ -27,18 +26,18 @@ ZTEST(getentropy_test_suite, test_getentropy_null_buffer)
2726
zassert_equal(errno, EFAULT);
2827
}
2928

30-
ZTEST(getentropy_test_suite, test_getentropy_max_size)
29+
ZTEST(posix_c_lib_ext, test_getentropy_max_size)
3130
{
32-
uint8_t buf[256] = { 0 };
31+
uint8_t buf[256] = {0};
3332
int ret;
3433

3534
ret = getentropy(buf, sizeof(buf));
3635
zassert_equal(ret, 0);
3736
}
3837

39-
ZTEST(getentropy_test_suite, test_getentropy)
38+
ZTEST(posix_c_lib_ext, test_getentropy)
4039
{
41-
uint8_t zero[16] = { 0 };
40+
uint8_t zero[16] = {0};
4241
uint8_t buf1[16];
4342
uint8_t buf2[16];
4443
int ret;
@@ -53,5 +52,3 @@ ZTEST(getentropy_test_suite, test_getentropy)
5352
zassert_true(memcmp(buf2, zero, sizeof(zero)) != 0);
5453
zassert_true(memcmp(buf1, buf2, sizeof(buf1)) != 0);
5554
}
56-
57-
ZTEST_SUITE(getentropy_test_suite, NULL, NULL, NULL, NULL, NULL);

tests/posix/getopt/src/main.c renamed to tests/posix/c_lib_ext/src/getopt.c

Lines changed: 51 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,17 @@
99
*
1010
*/
1111

12-
#include <zephyr/kernel.h>
13-
#include <zephyr/ztest.h>
14-
#include <string.h>
15-
#include <zephyr/posix/unistd.h>
1612
#include <getopt.h>
13+
#include <string.h>
1714

18-
ZTEST_SUITE(getopt_test_suite, NULL, NULL, NULL, NULL, NULL);
15+
#include <zephyr/kernel.h>
16+
#include <zephyr/posix/unistd.h>
17+
#include <zephyr/ztest.h>
1918

20-
ZTEST(getopt_test_suite, test_getopt_basic)
19+
ZTEST(posix_c_lib_ext, test_getopt_basic)
2120
{
2221
static const char *const nargv[] = {
23-
"cmd_name",
24-
"-b",
25-
"-a",
26-
"-h",
27-
"-c",
28-
"-l",
29-
"-h",
30-
"-a",
31-
"-i",
32-
"-w",
22+
"cmd_name", "-b", "-a", "-h", "-c", "-l", "-h", "-a", "-i", "-w",
3323
};
3424
static const char *accepted_opt = "abchw";
3525
static const char *expected = "bahc?ha?w";
@@ -63,7 +53,7 @@ enum getopt_idx {
6353
GETOPT_IDX_OPTARG
6454
};
6555

66-
ZTEST(getopt_test_suite, test_getopt)
56+
ZTEST(posix_c_lib_ext, test_getopt)
6757
{
6858
struct getopt_state *state;
6959
static const char *test_opts = "ac:";
@@ -96,8 +86,7 @@ ZTEST(getopt_test_suite, test_getopt)
9686
zassert_equal(0, strcmp(argv[GETOPT_IDX_OPTARG], state->optarg),
9787
"unexpected optarg result");
9888
/* Non thread safe usage: */
99-
zassert_equal(0, strcmp(argv[GETOPT_IDX_OPTARG], optarg),
100-
"unexpected optarg result");
89+
zassert_equal(0, strcmp(argv[GETOPT_IDX_OPTARG], optarg), "unexpected optarg result");
10190
}
10291

10392
enum getopt_long_idx {
@@ -107,7 +96,7 @@ enum getopt_long_idx {
10796
GETOPT_LONG_IDX_OPTARG
10897
};
10998

110-
ZTEST(getopt_test_suite, test_getopt_long)
99+
ZTEST(posix_c_lib_ext, test_getopt_long)
111100
{
112101
/* Below test is based on example
113102
* https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
@@ -120,16 +109,16 @@ ZTEST(getopt_test_suite, test_getopt_long)
120109
int c;
121110
struct option long_options[] = {
122111
/* These options set a flag. */
123-
{"verbose", no_argument, &verbose_flag, 1},
124-
{"brief", no_argument, &verbose_flag, 0},
112+
{"verbose", no_argument, &verbose_flag, 1},
113+
{"brief", no_argument, &verbose_flag, 0},
125114
/* These options don’t set a flag.
126115
* We distinguish them by their indices.
127116
*/
128-
{"add", no_argument, 0, 'a'},
129-
{"create", required_argument, 0, 'c'},
130-
{"delete", required_argument, 0, 'd'},
131-
{"long", required_argument, 0, 'e'},
132-
{0, 0, 0, 0}
117+
{"add", no_argument, 0, 'a'},
118+
{"create", required_argument, 0, 'c'},
119+
{"delete", required_argument, 0, 'd'},
120+
{"long", required_argument, 0, 'e'},
121+
{0, 0, 0, 0},
133122
};
134123
static const char *accepted_opt = "ac:d:e:";
135124

@@ -170,64 +159,51 @@ ZTEST(getopt_test_suite, test_getopt_long)
170159
/* Get state of the current thread */
171160
getopt_init();
172161
argv = (char **)argv1;
173-
c = getopt_long(argc1, argv, accepted_opt,
174-
long_options, &option_index);
162+
c = getopt_long(argc1, argv, accepted_opt, long_options, &option_index);
175163
zassert_equal(verbose_flag, 1, "verbose flag expected");
176164
c = getopt_long(argc1, argv, accepted_opt, long_options, &option_index);
177165
state = getopt_state_get();
178166
zassert_equal('c', c, "unexpected option");
179-
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]),
180-
"unexpected optarg");
181-
c = getopt_long(argc1, argv, accepted_opt,
182-
long_options, &option_index);
167+
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]), "unexpected optarg");
168+
c = getopt_long(argc1, argv, accepted_opt, long_options, &option_index);
183169
zassert_equal(-1, c, "getopt_long shall return -1");
184170

185171
/* Test scenario 2 */
186172
argv = (char **)argv2;
187173
getopt_init();
188-
c = getopt_long(argc2, argv, accepted_opt,
189-
long_options, &option_index);
174+
c = getopt_long(argc2, argv, accepted_opt, long_options, &option_index);
190175
zassert_equal(verbose_flag, 0, "verbose flag expected");
191-
c = getopt_long(argc2, argv, accepted_opt,
192-
long_options, &option_index);
176+
c = getopt_long(argc2, argv, accepted_opt, long_options, &option_index);
193177
zassert_equal('d', c, "unexpected option");
194178
state = getopt_state_get();
195-
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]),
196-
"unexpected optarg");
197-
c = getopt_long(argc2, argv, accepted_opt,
198-
long_options, &option_index);
179+
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]), "unexpected optarg");
180+
c = getopt_long(argc2, argv, accepted_opt, long_options, &option_index);
199181
zassert_equal(-1, c, "getopt_long shall return -1");
200182

201183
/* Test scenario 3 */
202184
argv = (char **)argv3;
203185
getopt_init();
204-
c = getopt_long(argc3, argv, accepted_opt,
205-
long_options, &option_index);
186+
c = getopt_long(argc3, argv, accepted_opt, long_options, &option_index);
206187
zassert_equal(verbose_flag, 0, "verbose flag expected");
207-
c = getopt_long(argc3, argv, accepted_opt,
208-
long_options, &option_index);
188+
c = getopt_long(argc3, argv, accepted_opt, long_options, &option_index);
209189
zassert_equal('a', c, "unexpected option");
210-
c = getopt_long(argc3, argv, accepted_opt,
211-
long_options, &option_index);
190+
c = getopt_long(argc3, argv, accepted_opt, long_options, &option_index);
212191
zassert_equal(-1, c, "getopt_long shall return -1");
213192

214193
/* Test scenario 4 */
215194
argv = (char **)argv4;
216195
getopt_init();
217-
c = getopt_long(argc4, argv, accepted_opt,
218-
long_options, &option_index);
196+
c = getopt_long(argc4, argv, accepted_opt, long_options, &option_index);
219197
zassert_equal(verbose_flag, 0, "verbose flag expected");
220-
c = getopt_long(argc4, argv, accepted_opt,
221-
long_options, &option_index);
198+
c = getopt_long(argc4, argv, accepted_opt, long_options, &option_index);
222199
/* Function was called with option '-l'. It is expected it will be
223200
* NOT evaluated to '--long' which has flag 'e'.
224201
*/
225202
zassert_not_equal('e', c, "unexpected option match");
226-
c = getopt_long(argc4, argv, accepted_opt,
227-
long_options, &option_index);
203+
c = getopt_long(argc4, argv, accepted_opt, long_options, &option_index);
228204
}
229205

230-
ZTEST(getopt_test_suite, test_getopt_long_only)
206+
ZTEST(posix_c_lib_ext, test_getopt_long_only)
231207
{
232208
/* Below test is based on example
233209
* https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
@@ -240,16 +216,16 @@ ZTEST(getopt_test_suite, test_getopt_long_only)
240216
int c;
241217
struct option long_options[] = {
242218
/* These options set a flag. */
243-
{"verbose", no_argument, &verbose_flag, 1},
244-
{"brief", no_argument, &verbose_flag, 0},
219+
{"verbose", no_argument, &verbose_flag, 1},
220+
{"brief", no_argument, &verbose_flag, 0},
245221
/* These options don’t set a flag.
246222
* We distinguish them by their indices.
247223
*/
248-
{"add", no_argument, 0, 'a'},
249-
{"create", required_argument, 0, 'c'},
250-
{"delete", required_argument, 0, 'd'},
251-
{"long", required_argument, 0, 'e'},
252-
{0, 0, 0, 0}
224+
{"add", no_argument, 0, 'a'},
225+
{"create", required_argument, 0, 'c'},
226+
{"delete", required_argument, 0, 'd'},
227+
{"long", required_argument, 0, 'e'},
228+
{0, 0, 0, 0},
253229
};
254230
static const char *accepted_opt = "ac:d:e:";
255231

@@ -289,62 +265,48 @@ ZTEST(getopt_test_suite, test_getopt_long_only)
289265
/* Test scenario 1 */
290266
argv = (char **)argv1;
291267
getopt_init();
292-
c = getopt_long_only(argc1, argv, accepted_opt,
293-
long_options, &option_index);
268+
c = getopt_long_only(argc1, argv, accepted_opt, long_options, &option_index);
294269
zassert_equal(verbose_flag, 1, "verbose flag expected");
295-
c = getopt_long_only(argc1, argv, accepted_opt,
296-
long_options, &option_index);
270+
c = getopt_long_only(argc1, argv, accepted_opt, long_options, &option_index);
297271
state = getopt_state_get();
298272
zassert_equal('c', c, "unexpected option");
299-
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]),
300-
"unexpected optarg");
301-
c = getopt_long_only(argc1, argv, accepted_opt,
302-
long_options, &option_index);
273+
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]), "unexpected optarg");
274+
c = getopt_long_only(argc1, argv, accepted_opt, long_options, &option_index);
303275
zassert_equal(-1, c, "getopt_long_only shall return -1");
304276

305277
/* Test scenario 2 */
306278
argv = (char **)argv2;
307279
getopt_init();
308280
state = getopt_state_get();
309-
c = getopt_long_only(argc2, argv, accepted_opt,
310-
long_options, &option_index);
281+
c = getopt_long_only(argc2, argv, accepted_opt, long_options, &option_index);
311282
zassert_equal(verbose_flag, 0, "verbose flag expected");
312-
c = getopt_long_only(argc2, argv, accepted_opt,
313-
long_options, &option_index);
283+
c = getopt_long_only(argc2, argv, accepted_opt, long_options, &option_index);
314284
state = getopt_state_get();
315285
zassert_equal('d', c, "unexpected option");
316-
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]),
317-
"unexpected optarg");
318-
c = getopt_long_only(argc2, argv, accepted_opt,
319-
long_options, &option_index);
286+
zassert_equal(0, strcmp(state->optarg, argv[GETOPT_LONG_IDX_OPTARG]), "unexpected optarg");
287+
c = getopt_long_only(argc2, argv, accepted_opt, long_options, &option_index);
320288
zassert_equal(-1, c, "getopt_long_only shall return -1");
321289

322290
/* Test scenario 3 */
323291
argv = (char **)argv3;
324292
getopt_init();
325-
c = getopt_long_only(argc3, argv, accepted_opt,
326-
long_options, &option_index);
293+
c = getopt_long_only(argc3, argv, accepted_opt, long_options, &option_index);
327294
zassert_equal(verbose_flag, 0, "verbose flag expected");
328-
c = getopt_long_only(argc3, argv, accepted_opt,
329-
long_options, &option_index);
295+
c = getopt_long_only(argc3, argv, accepted_opt, long_options, &option_index);
330296
zassert_equal('a', c, "unexpected option");
331-
c = getopt_long_only(argc3, argv, accepted_opt,
332-
long_options, &option_index);
297+
c = getopt_long_only(argc3, argv, accepted_opt, long_options, &option_index);
333298
zassert_equal(-1, c, "getopt_long_only shall return -1");
334299

335300
/* Test scenario 4 */
336301
argv = (char **)argv4;
337302
getopt_init();
338-
c = getopt_long_only(argc4, argv, accepted_opt,
339-
long_options, &option_index);
303+
c = getopt_long_only(argc4, argv, accepted_opt, long_options, &option_index);
340304
zassert_equal(verbose_flag, 0, "verbose flag expected");
341-
c = getopt_long_only(argc4, argv, accepted_opt,
342-
long_options, &option_index);
305+
c = getopt_long_only(argc4, argv, accepted_opt, long_options, &option_index);
343306

344307
/* Function was called with option '-l'. It is expected it will be
345308
* evaluated to '--long' which has flag 'e'.
346309
*/
347310
zassert_equal('e', c, "unexpected option");
348-
c = getopt_long_only(argc4, argv, accepted_opt,
349-
long_options, &option_index);
311+
c = getopt_long_only(argc4, argv, accepted_opt, long_options, &option_index);
350312
}

tests/posix/c_lib_ext/src/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright (c) 2024 Marvin Ouma <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
ZTEST_SUITE(posix_c_lib_ext, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)