Unit tests for these new APIs: tracefs_options_get_supported(); tracefs_option_is_supported(); tracefs_options_get_enabled(); tracefs_option_is_enabled(); tracefs_options_set(); tracefs_options_clear(); tracefs_option_string(); Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- utest/tracefs-utest.c | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c index 0520f49..1ade5dd 100644 --- a/utest/tracefs-utest.c +++ b/utest/tracefs-utest.c @@ -452,6 +452,129 @@ static void test_tracing_onoff(void) close(fd); } +static bool check_option(long long id, bool exist, int enabled) +{ + const char *name = tracefs_option_string(id); + char file[PATH_MAX]; + char *path = NULL; + bool ret = false; + bool supported; + struct stat st; + char buf[10]; + int fd; + int r; + int rstat; + + CU_TEST(name != NULL); + supported = tracefs_option_is_supported(test_instance, id); + CU_TEST(supported == exist); + if (supported != exist) + goto out; + snprintf(file, PATH_MAX, "options/%s", name); + path = tracefs_instance_get_file(test_instance, file); + CU_TEST(path != NULL); + rstat = stat(path, &st); + if (exist) { + CU_TEST(rstat == 0); + if (rstat != 0) + goto out; + } else { + CU_TEST(stat(path, &st) == -1); + if (rstat != -1) + goto out; + } + + fd = open(path, O_RDONLY); + if (exist) { + CU_TEST(fd >= 0); + if (fd < 0) + goto out; + } else { + CU_TEST(fd < 0); + if (fd >= 0) + goto out; + } + + if (exist && enabled >= 0) { + int val = enabled ? '1' : '0'; + + r = read(fd, buf, 10); + CU_TEST(r == 2); + CU_TEST(buf[0] == val); + if (buf[0] != val) + goto out; + } + + ret = true; +out: + tracefs_put_tracing_file(path); + if (fd >= 0) + close(fd); + return ret; +} + +static void test_tracing_options(void) +{ + unsigned long long options_enabled = 0; + unsigned long long options_all = 0; + unsigned long long i = 1; + char file[PATH_MAX]; + const char *name; + + options_all = tracefs_options_get_supported(test_instance); + options_enabled = tracefs_options_get_enabled(test_instance); + CU_TEST(options_all > 0); + + /* Invalid parameters test */ + CU_TEST(!tracefs_option_is_supported(test_instance, TRACEFS_OPTION_INVALID << 1)); + CU_TEST(!tracefs_option_is_enabled(test_instance, TRACEFS_OPTION_INVALID << 1)); + CU_TEST(tracefs_options_set(test_instance, TRACEFS_OPTION_INVALID << 1) == -1); + CU_TEST(tracefs_options_clear(test_instance, TRACEFS_OPTION_INVALID << 1) == -1); + name = tracefs_option_string(TRACEFS_OPTION_INVALID << 1); + CU_TEST(!strcmp(name, "unknown")); + + /* Test all valid options */ + do { + name = tracefs_option_string(i); + CU_TEST(name != NULL); + CU_TEST(strcmp(name, "unknown")); + snprintf(file, PATH_MAX, "options/%s", name); + + if (options_all & i) { + options_all &= ~i; + CU_TEST(check_option(i, true, -1)); + CU_TEST(tracefs_option_is_supported(test_instance, i)); + } else { + CU_TEST(check_option(i, false, -1)); + CU_TEST(!tracefs_option_is_supported(test_instance, i)); + } + + if (options_enabled & i) { + options_enabled &= ~i; + CU_TEST(check_option(i, true, 1)); + CU_TEST(tracefs_option_is_supported(test_instance, i)); + CU_TEST(tracefs_option_is_enabled(test_instance, i)); + CU_TEST(tracefs_options_clear(test_instance, i) == 0); + CU_TEST(check_option(i, true, 0)); + CU_TEST(tracefs_options_set(test_instance, i) == 0); + CU_TEST(check_option(i, true, 1)); + } else if (options_all & i) { + CU_TEST(check_option(i, true, 0)); + CU_TEST(tracefs_option_is_supported(test_instance, i)); + CU_TEST(!tracefs_option_is_enabled(test_instance, i)); + CU_TEST(tracefs_options_set(test_instance, i) == 0); + CU_TEST(check_option(i, true, 1)); + CU_TEST(tracefs_options_clear(test_instance, i) == 0); + CU_TEST(check_option(i, true, 0)); + } + i <<= 1; + } while (i < TRACEFS_OPTION_INVALID); + + CU_TEST(options_all == 0); + CU_TEST(options_enabled == 0); + +} + static void exclude_string(char **strings, char *name) { int i; @@ -761,5 +884,7 @@ void test_tracefs_lib(void) test_get_clock); CU_add_test(suite, "tracing on / off", test_tracing_onoff); + CU_add_test(suite, "tracing options", + test_tracing_options); } -- 2.29.2