Commit Graph
51 Commits
Author SHA1 Message Date
Sylvain Fasel 4396c77237 tests/stress_mt: ignore errors in threads for checking device counts
This fixes an issue where an error that occurred in thread #X will create a false
error message stating that thread #X+1 did not discover the correct number of devices

Closes #1447
2025-08-21 09:58:00 +02:00
Ingvar Stepanyan 4b9cf1fb04 tests: Fix Node.js shim for Node 21+ (webusb/emscripten)
Fixes #1648
Closes #1656
2025-06-09 11:02:57 +02:00
Sean McBride 9cf84577ce Avoid assignments within if statements
Fixes all clang-tidy bugprone-assignment-in-if-condition warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride a07ecfe02a Fix most warnings about implicit sign conversion
- Sometimes just change a type to match where it's coming from

- Sometimes add an explicit cast (but only if it's guaranteed correct,
  like a previous check that a signed value is positive before casting to
  unsigned)

- For libusb_bulk_transfer() add an assert to be certain that the signed
  return value is never negative.
  Update API documentation to underline this fact.
  Add similar assert in usbi_handle_transfer_completion().

- darwin: For parsing OS version, change scanf to use %u instead of %i

- xusb: Add additional range checks before casting

- xusb: Reverse some backwards count and size arguments to calloc, not
  that it really matters

Closes #1414
2024-04-04 09:01:26 +02:00
Sean McBride 66e63d68eb Correct some spelling and add codespell config file
In one case, renamed a variable from `larg` which codespell thought was
a typo for `large`.

Closes #1411
2024-01-20 12:09:35 +01:00
Tormod Volden 7c3d9ec61c tests/stress_mt: Ignore device descriptor mismatch for Windows HID
Some device descriptor fields are hard-coded by the HID backend, so they
will often not match. It is complicated to narrow this down to HID
devices, so we simply ignore these fields on Windows wholesale.

Hopefully we will fix the HID backend later, and this workaround can
then be reverted.

References #1360
References #1378

Signed-off-by: Tormod Volden <[email protected]>
2023-12-19 00:59:34 +01:00
Tormod Volden 5f9abfbe27 tests/stress_mt: Ignore some unavailable devices on Windows
Depending on the devices or drivers, open() may return
 LIBUSB_ERROR_ACCESS
 LIBUSB_ERROR_NOT_FOUND
 LIBUSB_ERROR_NOT_SUPPORTED
for devices "out of reach" to libusb.

References #1360

Signed-off-by: Tormod Volden <[email protected]>
2023-12-19 00:58:07 +01:00
Ingvar Stepanyan c2e150773e tests/set_option: Allow no devices in test_no_discovery
Instead of assuming there will always be some devices detected (when not
using LIBUSB_OPTION_NO_DEVICE_DISCOVERY), let the test pass but print a
warning to the user if there is none. This is to allow automated build
tests on restricted build environments without USB devices.

References #1374
Closes #1379
2023-12-19 00:38:11 +01:00
Ingvar Stepanyan 87d6686a54 tests/stress_mt: Stop early if any device fails
Previously the loop would continue even if error has already occured
since `goto close` alone doesn't stop early.

References #1360
2023-12-12 16:59:42 +01:00
Aurelien Jarno ebfbf195d4 tests/set_option: Avoid use-after-free in case of test failure
In case num_devices equals 0, LIBUSB_EXPECT() calls
LIBUSB_TEST_CLEAN_EXIT(), which calls libusb_exit() on test_ctx which
has just been freed a few lines above by a call to libusb_exit(). This
might cause a SEGFAULT or SIGBUS depending on the system.

Fixes that by assigning NULL to test_ctx just after calling
libusb_exit() like it is done elsewhere in the file.

Closes #1374
2023-12-11 23:26:09 +01:00
Tormod Volden 52bb0ede72 tests: Use AM_LDFLAGS for -static flag to allow LDFLAGS override
Fixes the following warning from automake:

tests/Makefile.am:3: warning: 'LDFLAGS' is a user variable, you should not override it;
tests/Makefile.am:3: use 'AM_LDFLAGS' instead

Also, since stress_mt_LDFLAGS is set (even in a conditional), AM_LDFLAGS
will not be applied for stress_mt unless added explicitly.

Closes #1371

Signed-off-by: Tormod Volden <[email protected]>
2023-12-10 17:29:13 +01:00
Tormod Volden 56cee16efd tests/umockdev: Avoid unknown warning option on older gcc
According to https://gcc.gnu.org/wiki/StaticAnalyzer the
-Wanalyzer-malloc-leak and -Wanalyzer-file-leak options came in GCC 10.

When building with GCC 9 there would be warnings:

CC       umockdev-umockdev.o
../../libusb-git/tests/umockdev.c:37:32: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]
   37 | #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~
../../libusb-git/tests/umockdev.c:38:32: error: unknown option after ‘#pragma GCC diagnostic’ kind [-Werror=pragmas]
   38 | #pragma GCC diagnostic ignored "-Wanalyzer-file-leak"
      |                                ^~~~~~~~~~~~~~~~~~~~~~

Closes #1369

Signed-off-by: Tormod Volden <[email protected]>
2023-12-09 09:06:41 +01:00
Ingvar Stepanyan 066a77fc0b webusb: Wasm+WebUSB backend fixes and improvements
- Added long-awaited support for multithreading. Since WebUSB still
 doesn't support accessing same device from multiple threads, this works
 by proxying I/O operations to the main thread as discussed on the
 original issue. For applications that run on the main thread nothing
 changes and they will continue to access WebUSB via Asyncify like
 before, while other threads will use blocking mechanism until an
 asynchronous response is available from the main thread.

 - Rewrote notification mechanism to use atomic waiting via sync Wasm
 instructions or `Atomics.waitAsync` depending on the thread. This
 results in simpler and faster notifications than the previous
 `postMessage`-based approach (which was used because
 `Atomics.waitAsync` wasn't yet available), as well as allows to send
 notifications across threads for multithreading scenario described
 above.

 - Fixed notification access to only wait/notify on the event we're
 interested instead of using a global lock.

 - Rewrote descriptor reading to query device for raw device &
 configuration descriptors instead of re-serializing them from
 JavaScript object representation. This incurs slight extra cost for the
 initial device opening, but fixes number of issues with information
 that is not yet exposed via WebUSB and allows to read supplementary
 descriptors such as string and BOS which were previously not supported.

 - Fixed listing only one alternate instead of all the available ones.

 - Fixed device closing & re-opening which could previously error out
 with "device busy" error.

 - Added mandatory Emscripten-specific linking flags to the generated
 pkgconfig.

 - Added device speed inference. This is not yet exposed via WebUSB, but
 we can at least make a best effort guess based on USB version and
 packet size, like some other backends already do.

 - Simplified & fixed device session ID generation which is now
 guaranteed to be truly unique, whereas previously it could clash with
 other devices of the same type.

 - Prepare code to support building for the Web without mandatory
 multithreading (which is costly on the Web) in the future. For now
 those `#ifdef`s are defunct as libusb is always compiled with
 `-pthread`, but some upcoming changes on the Emscripten side will allow
 to leverage those codepaths for smaller Wasm binaries.

 - Require explicit `--host=wasm32-unknown-emscripten` as we might want
 to add non-Emscripten WebAssembly backends in the future.

 - Various smaller fixes and improvements.

Note that this requires Emscripten 3.1.48 or newer to build for some of
the features used here, namely `co_await` support for JavaScript values
(without it code would be both more complex and slower) and some
proxying APIs. It shouldn't be a big deal in practice as most users
retrieve Emscripten via the official emsdk installer or Docker images.

Closes #1339
2023-12-08 22:17:40 +01:00
Nathan Hjelm 1ca2bc14ce darwin: add testing for IOKit version fallbacks
This commit adds a new unit test that verifies that the interface
interface and device interface versions are as expected. The unit test
relies on new testonly symbols to get details about the implementation.
The commit also adds a sanity check on the versions to ensure that
libusb_init fails if a supported version can not be found.

In addition to the new tests this commit also updates the tests to
use the static version of libusb. This provides them access to any
hidden symbol including testonly symbols (which should never be
exported in the shared library).

Signed-off-by: Nathan Hjelm <[email protected]>
2023-12-07 12:54:03 -07:00
Nathan Hjelm d82b3d27b1 ci: do not hard code test names for CI
automake has built-in support for running unit test with make check. This commit
sets the TESTS variable in tests/Makefile.am to enable this support and updates
the CI script to use make check instead of hard-coded test names.

Closes #1361

Signed-off-by: Nathan Hjelm <[email protected]>
2023-12-05 10:58:15 -07:00
Tormod Volden ae3685e038 stress_mt: Return error if device count varies
And otherwise only print the device count once,
together with the number of devices not opened.

Signed-off-by: Tormod Volden <[email protected]>
2023-12-01 10:56:00 +01:00
Ingvar Stepanyan a8d3cd8031 tests/stress_mt: Test open/transfer/close on available devices
Verify that you can open a device, do some transfer (a device descriptor
control transfer was chosen as it should succeed with any connected
device), and close a device from multiple threads in parallel as well.

Print a warning if a device is readonly.

Consistently add thread flags to the build.

Closes #1347
2023-12-01 10:55:36 +01:00
Ingvar Stepanyan fd315026f4 Add WebUSB testing via node-usb 2023-11-30 11:52:26 +01:00
Ingvar Stepanyan 143338c342 CI: Tell tests that we don't have any devices
The test_no_discovery test would otherwise expect to find some devices
when running without NO_DEVICE_DISCOVERY.
2023-11-29 18:55:48 +01:00
Ingvar Stepanyan 5fa085efb7 testlib: Don't count skipped tests as failures 2023-11-29 18:53:19 +01:00
Ingvar Stepanyan 6d2abd0990 tests: Mark callbacks as LIBUSB_CALL
Fixes #1337
2023-11-29 18:11:50 +01:00
Tormod Volden acc7b9d4de tests: Fix builds with logging disabled
set_option.c gave warnings and init_context.c would fail to build on
Windows.

Tested with configure options --disable-log and --enable-debug-log

Signed-off-by: Tormod Volden <[email protected]>
2023-11-29 18:10:16 +01:00
Tormod Volden 1313049d7d tests: Remove unused function in init_context.c
Signed-off-by: Tormod Volden <[email protected]>
2023-11-05 14:45:37 +01:00
Ludovic Rousseau a8a70f62c0 Fix build errors of tests with --enable-debug-log
The errors were:
../../tests/set_option.c:146:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
                    ~~~~~~~~  ^
../../tests/set_option.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
../../tests/set_option.c:152:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
                    ~~~~~~~~  ^
../../tests/set_option.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
2 errors generated.
../../tests/init_context.c:112:31: error: no member named 'debug' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR);
                    ~~~~~~~~  ^
../../tests/init_context.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
../../tests/init_context.c:140:31: error: no member named 'log_handler' in 'struct libusb_context'
  LIBUSB_EXPECT(==, test_ctx->log_handler, test_log_cb);
                    ~~~~~~~~  ^
../../tests/init_context.c:74:40: note: expanded from macro 'LIBUSB_EXPECT'
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
                                       ^~~
2 errors generated.

Signed-off-by: Ludovic Rousseau <[email protected]>
2023-05-09 22:48:01 +02:00
Nathan Hjelm 84ac0b0f60 Add support for setting the log callback with libusb_set_option/libusb_init_context
This commit effectively deprecates libusb_set_log_cb by adding support for
setting the callback in either libusb_set_option or libusb_init_context. Since
the libusb_set_log_cb is already in use we can not easily deprecate it without
first incrementing the major version. We may do that in the future.

Closes #1265

Signed-off-by: Nathan Hjelm <[email protected]>
2023-04-12 13:02:35 -06:00
Tormod Volden 223cf90b27 tests/stress_mt: Also run with device enumeration
Signed-off-by: Tormod Volden <[email protected]>
2023-03-12 14:52:23 +01:00
Tormod Volden ab61296036 tests/stress_mt: Add Windows threads support
Build the test on all platforms.

Closes #1128

Signed-off-by: Tormod Volden <[email protected]>
2023-03-12 14:52:08 +01:00
Nathan Hjelm 71dd672abe 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 <[email protected]>
[Xiaofan: Add Windows setenv/unsetenv wrappers]
Signed-off-by: Xiaofan Chen <[email protected]>
[Tormod: Fix test_no_discovery() build on Linux, add msvc build]
Signed-off-by: Tormod Volden <[email protected]>
2023-03-12 12:47:08 +01:00
Tormod Volden 5b7d57e61a Fix most -Wpedantic warnings
On Linux and gcc 12.2 at least.

Signed-off-by: Tormod Volden <[email protected]>
2023-01-21 11:30:06 +01:00
Nathan Hjelm 3c33e499a0 Update tests and example to use the new libusb_init_context() function
This commit updates all test and example code to use the newer
libusb_init_context() function instead of libusb_init().

Signed-off-by: Nathan Hjelm <[email protected]>
[Tormod: Update umockdev.c as well]
Signed-off-by: Tormod Volden <[email protected]>
2023-01-20 11:04:14 +01:00
Tormod Volden 47751f0b7d tests: Add multithreaded stress test for Posix platforms
Thanks to Ilya Averyanov for initial version.

This test detects issues like #1124.

Adaptation to Windows threads is on the wishlist (#1128).

References #1124
References #1128
Closes #1189

Signed-off-by: Tormod Volden <[email protected]>
2022-12-19 21:50:51 +01:00
Benjamin Berg 6b29aeb9ca core: Install first context as implicit default (and warn about its use)
There was a behaviour change in libusb 1.0.25 which triggers issues when
the API is misused. This caused for instance gutenprint to crash, see
https://bugzilla.redhat.com/show_bug.cgi?id=2055504

This seems to affect several applications "out in the wild".

For now, work around this by installing an implicit default. But, change
the code to log an error in case this "feature" is being used.

This will allow some grace time for developers to fix their
applications, before we at a later point revert to the stricter
behaviour.

Fixes #1089
2022-03-20 10:55:17 +01:00
Benjamin Berg 50b5838183 tests: Add some umockdev based tests
Add some basic umockdev tests. The setup in this case is all in one
process, but umockdev in principle could also work between processes.

This is just a start, more tests would make sense.
2022-03-18 18:49:21 +01:00
luz paz 14d3bde10f Fix various typos in docs/comments
Found via `codespell -q 3`

Closes #1015
2021-10-31 17:44:55 +01:00
Nathan Hjelm a524555c98 core: allow libusb_set_option on the default context before libusb_init
This commit updates libusb_set_option to save the options if setting them on the default
context. This ensures the options 1) can be set before libusb_init(NULL, ...), and 2)
are honored even if the default context is destroyed and re-created.

Signed-off-by: Nathan Hjelm <[email protected]>
2021-06-07 21:28:44 -06:00
Chris Dickens f2e551a5ff build: Prepare config.h for inclusion in examples/tests
There are certain games played in the examples and tests source to
account for differences in build environments and target platforms. This
can be simplified by including config.h and using the definitions there.

To that end, move the printf function attribute definition from
libusbi.h to config.h and leverage it where it is used in the examples
and tests.

Signed-off-by: Chris Dickens <[email protected]>
2020-11-27 15:22:29 -08:00
Chris Dickens a016a082a8 tests: Refactor testlib to simplify and fix Cygwin build
Signed-off-by: Chris Dickens <[email protected]>
2020-11-07 21:11:33 -08:00
Uri Lublin ab68ed4697 stress: test_get_device_list: change printf format for ssize_t
The type of list_size is ssize_t.
First idea was to use "%zd" printf format specifier.
But that is not supported by Visual Studio 2013.

Use "%ld" + cast list_size to long.

Closes #751

Suggested-by: Chris Dickens <[email protected]>
Signed-off-by: Uri Lublin <[email protected]>
Signed-off-by: Chris Dickens <[email protected]>
2020-08-10 10:51:42 -07:00
Chris Dickens 9a1bc8cafb build: Require C11 to build and clean up autoconfig/automake files
C11 compiler support has been available for many years now. It is not
unreasonable to require this now, and doing so allows some cleanup to
the configure script. It is no longer necessary to check for compiler
support of the '-fvibility' flag because any compiler that supports C11
will support this flag as well.

Fix up the way that compiler and linker flags are passed down to the
various makefiles. The compiler flags should be shared by all, but the
linker flags and libraries should be separated between the library and
the examples/tests. The visibility flag is only relevant for the
library and the thread flags are only relevant for sources using thread
constructs, so provide them as needed.

Rearrange configure.ac to group similar functionality and consolidate
where possible.

Based on these changes, update the Travis configuration file to include
newer versions of test platforms to ensure proper C11 compiler support.

Signed-off-by: Chris Dickens <[email protected]>
2020-03-30 12:28:11 -07:00
Chris Dickens f90d07613c Windows: Remove support for WinCE and Visual Studio older than 2013
There appears to be no need for the WinCE backend anymore, and it is
increasingly difficult to keep healthy as the rest of the library
changes.

Require at least Visual Studio 2013 to compile. This simplifies matters
as there is some semblance of C99 support there.

Signed-off-by: Chris Dickens <[email protected]>
2020-01-20 18:02:19 -08:00
Chris Dickens 539f22e2fd core: Introduce libusb_set_option() API function
This new function allows more flexibility in extending the library to
support more user-configurable options. It is intended to provide a
single API that can support a wide variety of needs and eliminates the
need for new API functions to set future options.

The function is introduced with a single option (LIBUSB_OPTION_LOG_LEVEL)
that replaces the libusb_set_debug() function. Documentation relating to
libusb_set_debug() and the uses of this function in the examples and
tests have been updated accordingly.

Signed-off-by: Chris Dickens <[email protected]>
2017-07-16 14:02:05 -07:00
[email protected] 1eff220474 Misc: Revert all references to libusb/libusb.info 2014-01-08 23:51:01 +00:00
Ludovic Rousseau c4f2c353b9 tests: set output_file to a valid value
If fdopen(3) fails then ctx->output_file is NULL and should not be used
in cleanup_test_output() called next.

Problem detected by the Coverity tool
CID 1042543 (#1 of 1): Dereference after null check (FORWARD_NULL)9.
var_deref_model: Passing "ctx" to function
"cleanup_test_output(libusbx_testlib_ctx *)", which dereferences null
"ctx->output_file".
2013-09-28 12:19:16 +02:00
Toby Gray c632101790 Android: Add examples and stress tests 2013-08-11 22:14:13 +01:00
Pete Batard 196acee784 libusbx 1.0.15-rc1
* Also fix missing header in release tarball
2013-04-02 20:21:48 +01:00
Pete Batard 5ab16a2165 Tests: Suppress Visual Studio 2012 Code Analysis warnings 2013-03-16 23:40:30 +00:00
Pete Batard 0ded9c126e Misc: Simplify includes and misc. cleanup
* fxload sample provenance
* No need for <sys/types.h> in samples as already in libusb.h
* Don't bother about sscanf_s in xusb
* Use HAVE_### and rely on config.h where possible
* Formal inclusion of <winsock.h> in libusb.h for WinCE and WDK
* Cleanup of Windows' config.h
* Avoid ENAMETOOLONG and ENOTEMPTY conflict between errno.h and winsock.h for WinCE
* Additional newlines & braces cleanup
2013-02-27 22:58:49 +00:00
Xiaofan Chen c56f890684 Tests: Fix broken OS-X and Linux builds
* _dup2 is Windows only. Use dup2 instead
* Breakage was introduced in 76eecc6ce1
2013-02-24 23:03:32 +00:00
Pete Batard 76eecc6ce1 Tests: Simplify stdout redirection and fix Windows assertion error on cleanup
* cleanup_test_output() produced an assertion error when compiled with MSVC
  due to closing ctx->output_file
* set ctx->output_file to stdout by default
2013-02-22 00:49:29 +00:00
Toby Gray 244271931a WinCE: Add support for WinCE (sources) 2013-01-23 00:40:18 +00:00