mirror of
https://github.com/shadps4-emu/ext-libusb.git
synced 2026-01-31 00:55:21 +01:00
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 <hjelmn@google.com>
This commit is contained in:
committed by
Nathan Hjelm
parent
13a6953379
commit
1ca2bc14ce
@@ -34,8 +34,10 @@ if OS_DARWIN
|
||||
OS_SRC = $(OS_DARWIN_SRC)
|
||||
endif
|
||||
|
||||
noinst_LTLIBRARIES =
|
||||
|
||||
if OS_HAIKU
|
||||
noinst_LTLIBRARIES = libusb_haiku.la
|
||||
noinst_LTLIBRARIES += libusb_haiku.la
|
||||
libusb_haiku_la_SOURCES = $(OS_HAIKU_SRC)
|
||||
libusb_1_0_la_LIBADD = libusb_haiku.la
|
||||
endif
|
||||
@@ -50,7 +52,7 @@ endif
|
||||
endif
|
||||
|
||||
if OS_EMSCRIPTEN
|
||||
noinst_LTLIBRARIES = libusb_emscripten.la
|
||||
noinst_LTLIBRARIES += libusb_emscripten.la
|
||||
libusb_emscripten_la_SOURCES = $(OS_EMSCRIPTEN_SRC)
|
||||
libusb_1_0_la_LIBADD = libusb_emscripten.la
|
||||
endif
|
||||
|
||||
@@ -69,6 +69,11 @@ static usbi_mutex_t darwin_cached_devices_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct list_head darwin_cached_devices;
|
||||
static const char *darwin_device_class = "IOUSBDevice";
|
||||
|
||||
uint32_t libusb_testonly_fake_running_version __attribute__ ((visibility ("hidden")));
|
||||
int libusb_testonly_using_running_interface_version __attribute__ ((visibility ("hidden")));
|
||||
int libusb_testonly_using_running_device_version __attribute__ ((visibility ("hidden")));
|
||||
bool libusb_testonly_clear_running_version_cache __attribute__ ((visibility ("hidden")));
|
||||
|
||||
#define DARWIN_CACHED_DEVICE(a) (((struct darwin_device_priv *)usbi_get_device_priv((a)))->dev)
|
||||
|
||||
/* async event thread */
|
||||
@@ -147,6 +152,9 @@ static const struct darwin_iokit_interface *get_interface_interface(void) {
|
||||
},
|
||||
};
|
||||
static struct darwin_iokit_interface cached_interface = {.version = 0};
|
||||
if (libusb_testonly_clear_running_version_cache) {
|
||||
memset (&cached_interface, 0, sizeof (cached_interface));
|
||||
}
|
||||
if (0 == cached_interface.version) {
|
||||
uint32_t os_version = get_running_version();
|
||||
for (int i = 0 ; interfaces[i].version > 0 ; ++i) {
|
||||
@@ -154,6 +162,8 @@ static const struct darwin_iokit_interface *get_interface_interface(void) {
|
||||
cached_interface = interfaces[i];
|
||||
}
|
||||
}
|
||||
|
||||
libusb_testonly_using_running_interface_version = cached_interface.version;
|
||||
}
|
||||
|
||||
return &cached_interface;
|
||||
@@ -214,6 +224,9 @@ static const struct darwin_iokit_interface *get_device_interface(void) {
|
||||
},
|
||||
};
|
||||
static struct darwin_iokit_interface cached_interface = {.version = 0};
|
||||
if (libusb_testonly_clear_running_version_cache) {
|
||||
memset (&cached_interface, 0, sizeof (cached_interface));
|
||||
}
|
||||
if (0 == cached_interface.version) {
|
||||
uint32_t os_version = get_running_version();
|
||||
for (int i = 0 ; interfaces[i].version > 0 ; ++i) {
|
||||
@@ -221,6 +234,7 @@ static const struct darwin_iokit_interface *get_device_interface(void) {
|
||||
cached_interface = interfaces[i];
|
||||
}
|
||||
}
|
||||
libusb_testonly_using_running_device_version = cached_interface.version;
|
||||
}
|
||||
|
||||
return &cached_interface;
|
||||
@@ -342,6 +356,10 @@ static enum libusb_error darwin_to_libusb (IOReturn result) {
|
||||
}
|
||||
|
||||
uint32_t get_running_version(void) {
|
||||
if (libusb_testonly_fake_running_version > 0) {
|
||||
return libusb_testonly_fake_running_version;
|
||||
}
|
||||
|
||||
int ret;
|
||||
#if !defined(TARGET_OS_OSX) || TARGET_OS_OSX == 1
|
||||
char os_version_string[64] = {'\0'};;
|
||||
@@ -830,6 +848,16 @@ static int darwin_first_time_init(void) {
|
||||
list_init (&darwin_cached_devices);
|
||||
}
|
||||
|
||||
/* cache the interface versions that will be used. as a sanity check verify
|
||||
* that the interface versions are non-zero. */
|
||||
const struct darwin_iokit_interface *interface_interface = get_interface_interface();
|
||||
const struct darwin_iokit_interface *device_interface = get_device_interface();
|
||||
if (0 == interface_interface->version || 0 == device_interface->version) {
|
||||
usbi_err(NULL, "could not determine the device or interface interface to use with this version "
|
||||
"of macOS (or MacOS X), current_running_version = %" PRIu32, get_running_version());
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
}
|
||||
|
||||
if (!list_empty(&darwin_cached_devices)) {
|
||||
usbi_err(NULL, "libusb_device reference not released on last exit. will not continue");
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define LIBUSB_NANO 11841
|
||||
#define LIBUSB_NANO 11842
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
AM_CPPFLAGS = -I$(top_srcdir)/libusb
|
||||
LDADD = ../libusb/libusb-1.0.la
|
||||
LIBS =
|
||||
LDFLAGS = -static
|
||||
|
||||
stress_SOURCES = stress.c testlib.c
|
||||
stress_mt_SOURCES = stress_mt.c
|
||||
set_option_SOURCES = set_option.c testlib.c
|
||||
init_context_SOURCES = init_context.c testlib.c
|
||||
macos_SOURCES = macos.c testlib.c
|
||||
|
||||
stress_mt_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS)
|
||||
stress_mt_LDADD = $(LDADD) $(THREAD_LIBS)
|
||||
@@ -20,6 +21,9 @@ endif
|
||||
|
||||
noinst_HEADERS = libusb_testlib.h
|
||||
noinst_PROGRAMS = stress stress_mt set_option init_context
|
||||
if OS_DARWIN
|
||||
noinst_PROGRAMS += macos
|
||||
endif
|
||||
|
||||
if BUILD_UMOCKDEV_TEST
|
||||
# NOTE: We add libumockdev-preload.so so that we can run tests in-process
|
||||
|
||||
130
tests/macos.c
Normal file
130
tests/macos.c
Normal file
@@ -0,0 +1,130 @@
|
||||
/* -*- Mode: C; indent-tabs-mode:nil -*- */
|
||||
/*
|
||||
* Unit tests for libusb_set_option
|
||||
* Copyright © 2023 Nathan Hjelm <hjelmn@cs.unm.edu>
|
||||
* 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 <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "libusbi.h"
|
||||
#include "libusb_testlib.h"
|
||||
|
||||
#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)
|
||||
|
||||
|
||||
extern uint32_t libusb_testonly_fake_running_version;
|
||||
extern int libusb_testonly_using_running_interface_version;
|
||||
extern int libusb_testonly_using_running_device_version;
|
||||
extern bool libusb_testonly_clear_running_version_cache;
|
||||
|
||||
static libusb_testlib_result test_macos_version_fallback(void) {
|
||||
libusb_context *test_ctx = NULL;
|
||||
libusb_testonly_fake_running_version = 100001;
|
||||
libusb_testonly_clear_running_version_cache = true;
|
||||
|
||||
LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
|
||||
/*num_options=*/0));
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 220);
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 197);
|
||||
|
||||
libusb_exit(test_ctx);
|
||||
test_ctx = NULL;
|
||||
|
||||
libusb_testonly_fake_running_version = 100900;
|
||||
|
||||
LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
|
||||
/*num_options=*/0));
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 650);
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 650);
|
||||
|
||||
libusb_exit(test_ctx);
|
||||
test_ctx = NULL;
|
||||
|
||||
libusb_testonly_fake_running_version = 101200;
|
||||
|
||||
LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
|
||||
/*num_options=*/0));
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 800);
|
||||
LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 650);
|
||||
|
||||
libusb_exit(test_ctx);
|
||||
test_ctx = NULL;
|
||||
|
||||
// Test a version smaller than 10.0. Initialization should fail.
|
||||
libusb_testonly_fake_running_version = 99999;
|
||||
|
||||
int error = libusb_init_context(&test_ctx, /*options=*/NULL,
|
||||
/*num_options=*/0);
|
||||
LIBUSB_EXPECT(!=, error, LIBUSB_SUCCESS);
|
||||
|
||||
|
||||
LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
|
||||
}
|
||||
|
||||
static const libusb_testlib_test tests[] = {
|
||||
{ "test_macos_version_fallback", &test_macos_version_fallback },
|
||||
LIBUSB_NULL_TEST
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
return libusb_testlib_run_tests(argc, argv, tests);
|
||||
}
|
||||
Reference in New Issue
Block a user