Skip to content

Commit fdfa17f

Browse files
committed
lib/tst_kconfig: Make use of boolean expression eval
Now each string in the kconfig[] array in tst_test structure is an boolean expression which is evaluated. All expressions has to be true in order for the test to continue. This also makes the parser for the kernel config a bit more robust as it was pointed out that there may have been cases where it could be mislead by hand edited config files. + Update the docs. Signed-off-by: Cyril Hrubis <[email protected]> CC: Pengfei Xu <[email protected]> Reviewed-by: Xiao Yang <[email protected]>
1 parent 6739584 commit fdfa17f

File tree

8 files changed

+307
-155
lines changed

8 files changed

+307
-155
lines changed

doc/test-writing-guidelines.txt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,21 +1643,26 @@ on the system, disabled syscalls can be detected by checking for 'ENOSYS'
16431643
errno etc.
16441644

16451645
However in rare cases core kernel features couldn't be detected based on the
1646-
kernel userspace API and we have to resort to kernel .config parsing.
1646+
kernel userspace API and we have to resort to parse the kernel .config.
16471647

1648-
For this cases the test should set the 'NULL' terminated '.needs_kconfigs' array
1649-
of kernel config options required for the test. The config option can be
1650-
specified either as plain "CONFIG_FOO" in which case it's sufficient for the
1651-
test continue if it's set to any value (typically =y or =m). Or with a value
1652-
as "CONFIG_FOO=bar" in which case the value has to match as well. The test is
1653-
aborted with 'TCONF' if any of the required options were not set.
1648+
For this cases the test should set the 'NULL' terminated '.needs_kconfigs'
1649+
array of boolean expressions with constraints on the kconfig variables. The
1650+
boolean expression consits of variables, two binary operations '&' and '|',
1651+
negation '!' and correct sequence of parentesis '()'. Variables are expected
1652+
to be in a form of "CONFIG_FOO[=bar]".
1653+
1654+
The test will continue to run if all expressions are evaluated to 'True'.
1655+
Missing variable is mapped to 'False' as well as variable with different than
1656+
specified value, e.g. 'CONFIG_FOO=bar' will evaluate to 'False' if the value
1657+
is anything else but 'bar'. If config variable is specified as plain
1658+
'CONFIG_FOO' it's evaluated to true it's set to any value (typically =y or =m).
16541659

16551660
[source,c]
16561661
-------------------------------------------------------------------------------
16571662
#include "tst_test.h"
16581663

16591664
static const char *kconfigs[] = {
1660-
"CONFIG_X86_INTEL_UMIP",
1665+
"CONFIG_X86_INTEL_UMIP | CONFIG_X86_UMIP",
16611666
NULL
16621667
};
16631668

include/tst_kconfig.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,41 @@
66
#ifndef TST_KCONFIG_H__
77
#define TST_KCONFIG_H__
88

9-
struct tst_kconfig_res {
10-
char match;
11-
char *value;
9+
struct tst_kconfig_var {
10+
char id[64];
11+
unsigned int id_len;
12+
char choice;
13+
char *val;
1214
};
1315

1416
/**
15-
* Reads a kernel config and parses it for values defined in kconfigs array.
17+
*
18+
* Reads a kernel config, parses it and writes results into an array of
19+
* tst_kconfig_var structures.
1620
*
1721
* The path to the kernel config should be autodetected in most of the cases as
1822
* the code looks for know locations. It can be explicitely set/overrided with
1923
* the KCONFIG_PATH environment variable as well.
2024
*
21-
* The kcofings array is expected to contain strings in a format "CONFIG_FOO"
22-
* or "CONFIG_FOO=bar". The result array has to be suitably sized to fit the
23-
* results.
24-
*
25-
* @param kconfigs array of config strings to look for
26-
* @param results array to store results to
27-
* @param cnt size of the arrays
25+
* The caller has to initialize the tst_kconfig_var structure. The id has to be
26+
* filled with config variable name such as 'CONFIG_FOO', the id_len should
27+
* hold the id string length and the choice and val has to be zeroed.
2828
*
29-
* The match in the tst_kconfig_res structure is set as follows:
29+
* After a call to this function each choice be set as follows:
3030
*
3131
* 'm' - config option set to m
3232
* 'y' - config option set to y
3333
* 'v' - config option set to other value
3434
* 'n' - config option is not set
3535
* 0 - config option not found
3636
*
37-
* In the case that match is set to 'v' the value points to a newly allocated
38-
* string that holds the value.
37+
* In the case that match is set to 'v' the val pointer points to a newly
38+
* allocated string that holds the value.
39+
*
40+
* @param vars An array of caller initalized tst_kconfig_var structures.
41+
* @param vars_len Length of the vars array.
3942
*/
40-
void tst_kconfig_read(const char *const kconfigs[],
41-
struct tst_kconfig_res results[], size_t cnt);
43+
void tst_kconfig_read(struct tst_kconfig_var vars[], size_t vars_len);
4244

4345
/**
4446
* Checks if required kernel configuration options are set in the kernel

lib/newlib_tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test_timer
3131
test_exec
3232
test_exec_child
3333
test_kconfig
34+
test_kconfig01
3435
variant
3536
test_guarded_buf
3637
tst_bool_expr

lib/newlib_tests/config06

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Empty

lib/newlib_tests/test_kconfig.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ static const char *kconfigs[] = {
1414
"CONFIG_MMU",
1515
"CONFIG_EXT4_FS=m",
1616
"CONFIG_PGTABLE_LEVELS=4",
17+
"CONFIG_MMU & CONFIG_EXT4_FS=m",
18+
"CONFIG_EXT4_FS=m | CONFIG_MMU",
1719
NULL
1820
};
1921

lib/newlib_tests/test_kconfig01.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 Cyril Hrubis <[email protected]>
4+
*
5+
* Invalid boolean expression test.
6+
*/
7+
8+
#include "tst_test.h"
9+
10+
static void do_test(void)
11+
{
12+
tst_res(TPASS, "Test passed!");
13+
}
14+
15+
static const char *kconfigs[] = {
16+
"CONFIG_EXT4_FS=m | CONFIG_MMU)",
17+
NULL
18+
};
19+
20+
static struct tst_test test = {
21+
.test_all = do_test,
22+
.needs_kconfigs = kconfigs,
23+
};

0 commit comments

Comments
 (0)