From 71dd672abe52d7c3a9c61373888a32984f0792a7 Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Mon, 13 Feb 2023 11:04:11 -0700 Subject: tests: Add unit test for libusb_set_option The behavior of libusb_set_option was not matching the documentation when the log level was set in the environment. This has been fixed but a regression test is needed to ensure the behavior does not deviate. This commit adds a set of unit tests to cover some of the functionality of libusb_set_option. Closes #1245 Signed-off-by: Nathan Hjelm [Xiaofan: Add Windows setenv/unsetenv wrappers] Signed-off-by: Xiaofan Chen [Tormod: Fix test_no_discovery() build on Linux, add msvc build] Signed-off-by: Tormod Volden --- libusb/version_nano.h | 2 +- msvc/libusb.sln | 34 ++++++++ msvc/set_option.vcxproj | 35 +++++++++ tests/Makefile.am | 7 +- tests/set_option.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 msvc/set_option.vcxproj create mode 100644 tests/set_option.c diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 12e1f7b..46cf934 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11785 +#define LIBUSB_NANO 11786 diff --git a/msvc/libusb.sln b/msvc/libusb.sln index f117efb..b76246c 100644 --- a/msvc/libusb.sln +++ b/msvc/libusb.sln @@ -26,6 +26,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "testlibusb", "testlibusb.vc EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xusb", "xusb.vcxproj", "{3F3138D0-7AB7-4268-9BF3-1A3EA5503A11}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "set_option", "set_option.vcxproj", "{35BD5D4B-5102-4A08-81C0-AAF3285FCB01}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM = Debug|ARM @@ -430,6 +432,38 @@ Global {3F3138D0-7AB7-4268-9BF3-1A3EA5503A11}.Release-MT|Win32.Build.0 = Release|Win32 {3F3138D0-7AB7-4268-9BF3-1A3EA5503A11}.Release-MT|x64.ActiveCfg = Release|x64 {3F3138D0-7AB7-4268-9BF3-1A3EA5503A11}.Release-MT|x64.Build.0 = Release|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|ARM.ActiveCfg = Debug|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|ARM.Build.0 = Debug|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|ARM64.Build.0 = Debug|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|Win32.ActiveCfg = Debug|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|Win32.Build.0 = Debug|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|x64.ActiveCfg = Debug|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug|x64.Build.0 = Debug|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|ARM.ActiveCfg = Debug|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|ARM.Build.0 = Debug|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|ARM64.ActiveCfg = Debug|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|ARM64.Build.0 = Debug|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|Win32.ActiveCfg = Debug|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|Win32.Build.0 = Debug|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|x64.ActiveCfg = Debug|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Debug-MT|x64.Build.0 = Debug|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|ARM.ActiveCfg = Release|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|ARM.Build.0 = Release|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|ARM64.ActiveCfg = Release|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|ARM64.Build.0 = Release|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|Win32.ActiveCfg = Release|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|Win32.Build.0 = Release|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|x64.ActiveCfg = Release|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release|x64.Build.0 = Release|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|ARM.ActiveCfg = Release|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|ARM.Build.0 = Release|ARM + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|ARM64.ActiveCfg = Release|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|ARM64.Build.0 = Release|ARM64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|Win32.ActiveCfg = Release|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|Win32.Build.0 = Release|Win32 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|x64.ActiveCfg = Release|x64 + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01}.Release-MT|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/msvc/set_option.vcxproj b/msvc/set_option.vcxproj new file mode 100644 index 0000000..ecb86bb --- /dev/null +++ b/msvc/set_option.vcxproj @@ -0,0 +1,35 @@ + + + + + {35BD5D4B-5102-4A08-81C0-AAF3285FCB01} + + + + + + + + + + + + + + + + + + + + + + + {349ee8f9-7d25-4909-aaf5-ff3fade72187} + false + + + + + + \ No newline at end of file diff --git a/tests/Makefile.am b/tests/Makefile.am index d47d4ed..18da1eb 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -2,9 +2,12 @@ AM_CPPFLAGS = -I$(top_srcdir)/libusb LDADD = ../libusb/libusb-1.0.la LIBS = -stress_SOURCES = stress.c libusb_testlib.h testlib.c +stress_SOURCES = stress.c testlib.c -noinst_PROGRAMS = stress +set_option_SOURCES = set_option.c testlib.c + +noinst_HEADERS = libusb_testlib.h +noinst_PROGRAMS = stress set_option if PLATFORM_POSIX stress_mt_SOURCES = stress_mt.c diff --git a/tests/set_option.c b/tests/set_option.c new file mode 100644 index 0000000..c04e4ea --- /dev/null +++ b/tests/set_option.c @@ -0,0 +1,200 @@ +/* -*- Mode: C; indent-tabs-mode:nil -*- */ +/* + * Unit tests for libusb_set_option + * Copyright © 2023 Nathan Hjelm + * Copyright © 2023 Google, LLC. All rights reserved. + * + * This library 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 library 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include +#include +#include +#include "libusbi.h" +#include "libusb_testlib.h" + +#if defined(_WIN32) && !defined(__CYGWIN__) +#include + +static int unsetenv(const char *env) { + return _putenv_s(env, ""); +} + +static int setenv(const char *env, const char *value, int overwrite) { + if (getenv(env) && !overwrite) + return 0; + return _putenv_s(env, value); +} +#endif + +#define LIBUSB_TEST_CLEAN_EXIT(code) \ + do { \ + if (test_ctx != NULL) { \ + libusb_exit(test_ctx); \ + } \ + unsetenv("LIBUSB_DEBUG"); \ + return (code); \ + } while (0) + +/** + * Fail the test if the expression does not evaluate to LIBUSB_SUCCESS. + */ +#define LIBUSB_TEST_RETURN_ON_ERROR(expr) \ + do { \ + int _result = (expr); \ + if (LIBUSB_SUCCESS != _result) { \ + libusb_testlib_logf("Not success (%s) at %s:%d", #expr, \ + __FILE__, __LINE__); \ + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE); \ + } \ + } while (0) + +/** + * Use relational operatator to compare two values and fail the test if the + * comparison is false. Intended to compare integer or pointer types. + * + * Example: LIBUSB_EXPECT(==, 0, 1) -> fail, LIBUSB_EXPECT(==, 0, 0) -> ok. + */ +#define LIBUSB_EXPECT(operator, lhs, rhs) \ + do { \ + int64_t _lhs = (lhs), _rhs = (rhs); \ + if (!(_lhs operator _rhs)) { \ + libusb_testlib_logf("Expected %s (%" PRId64 ") " #operator \ + " %s (%" PRId64 ") at %s:%d", #lhs, \ + (int64_t)(intptr_t)_lhs, #rhs, \ + (int64_t)(intptr_t)_rhs, __FILE__, \ + __LINE__); \ + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE); \ + } \ + } while (0) + + +static libusb_testlib_result test_set_log_level_basic(void) { +#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) + libusb_context *test_ctx = NULL; + + /* unset LIBUSB_DEBUG if it is set */ + unsetenv("LIBUSB_DEBUG"); + + /* test basic functionality */ + LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL, + /*num_options=*/0)); + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx, + LIBUSB_OPTION_LOG_LEVEL, + LIBUSB_LOG_LEVEL_ERROR)); + LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR); + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx, + LIBUSB_OPTION_LOG_LEVEL, + LIBUSB_LOG_LEVEL_NONE)); + LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_NONE); + + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS); +#else + return TEST_STATUS_SKIP; +#endif +} + +static libusb_testlib_result test_set_log_level_default(void) { +#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) + libusb_context *test_ctx = NULL; + + /* set the default debug level */ + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, + LIBUSB_LOG_LEVEL_ERROR)); + + LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL, + /*num_options=*/0)); + /* check that debug level came from the default */ + LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR); + + /* try to override the old log level. since this was set from the default it + * should be possible to change it */ + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx, + LIBUSB_OPTION_LOG_LEVEL, + LIBUSB_LOG_LEVEL_NONE)); + LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_NONE); + + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS); +#else + return TEST_STATUS_SKIP; +#endif +} + +static libusb_testlib_result test_set_log_level_env(void) { +#if defined(ENABLE_LOGGING) + libusb_context *test_ctx = NULL; + + /* check that libusb_set_option does not change the log level when it was set + * from the environment. */ + setenv("LIBUSB_DEBUG", "4", /*overwrite=*/0); + LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL, + /*num_options=*/0)); + LIBUSB_EXPECT(==, test_ctx->debug, 4); + + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx, + LIBUSB_OPTION_LOG_LEVEL, + LIBUSB_LOG_LEVEL_ERROR)); + /* environment variable should always override LIBUSB_OPTION_LOG_LEVEL if set */ + LIBUSB_EXPECT(==, test_ctx->debug, 4); + + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS); +#else + return TEST_STATUS_SKIP; +#endif +} + + +static libusb_testlib_result test_no_discovery(void) +{ +#if defined(__linux__) + libusb_context *test_ctx; + LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL, + /*num_options=*/0)); + libusb_device **device_list = NULL; + ssize_t num_devices = libusb_get_device_list(test_ctx, &device_list); + libusb_free_device_list(device_list, /*unref_devices=*/1); + libusb_exit(test_ctx); + + LIBUSB_EXPECT(>, num_devices, 0); + + LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY)); + LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL, + /*num_options=*/0)); + device_list = NULL; + num_devices = libusb_get_device_list(test_ctx, &device_list); + libusb_free_device_list(device_list, /*unref_devices=*/1); + + LIBUSB_EXPECT(==, num_devices, 0); + LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS); +#else + return TEST_STATUS_SKIP; +#endif +} + +static const libusb_testlib_test tests[] = { + { "test_set_log_level_basic", &test_set_log_level_basic }, + { "test_set_log_level_env", &test_set_log_level_env }, + { "test_no_discovery", &test_no_discovery }, + /* since default options can't be unset, run this one last */ + { "test_set_log_level_default", &test_set_log_level_default }, + LIBUSB_NULL_TEST +}; + +int main(int argc, char *argv[]) +{ + return libusb_testlib_run_tests(argc, argv, tests); +} -- cgit v1.2.1