diff --git a/src/main/c/src/ConfigDescriptor.c b/src/main/c/src/ConfigDescriptor.c index e69edc4..465dd1d 100644 --- a/src/main/c/src/ConfigDescriptor.c +++ b/src/main/c/src/ConfigDescriptor.c @@ -9,13 +9,18 @@ void setConfigDescriptor(JNIEnv* env, struct libusb_config_descriptor* descriptor, jobject object) { - SET_POINTER(env, descriptor, object, "pointer"); + SET_POINTER(env, descriptor, object, "configDescriptorPointer"); } struct libusb_config_descriptor* unwrapConfigDescriptor(JNIEnv* env, jobject descriptor) { - UNWRAP_POINTER(env, descriptor, struct libusb_config_descriptor*, "pointer"); + UNWRAP_POINTER(env, descriptor, struct libusb_config_descriptor*, "configDescriptorPointer"); +} + +void resetConfigDescriptor(JNIEnv* env, jobject obj) +{ + RESET_POINTER(env, obj, "configDescriptorPointer"); } /** diff --git a/src/main/c/src/ConfigDescriptor.h b/src/main/c/src/ConfigDescriptor.h index 4626915..b5d8bea 100644 --- a/src/main/c/src/ConfigDescriptor.h +++ b/src/main/c/src/ConfigDescriptor.h @@ -10,5 +10,6 @@ void setConfigDescriptor(JNIEnv*, struct libusb_config_descriptor*, jobject); struct libusb_config_descriptor* unwrapConfigDescriptor(JNIEnv*, jobject); +void resetConfigDescriptor(JNIEnv*, jobject); #endif diff --git a/src/main/c/src/Context.c b/src/main/c/src/Context.c index 97fb679..5f98f03 100644 --- a/src/main/c/src/Context.c +++ b/src/main/c/src/Context.c @@ -14,3 +14,8 @@ libusb_context* unwrapContext(JNIEnv* env, jobject context) { UNWRAP_POINTER(env, context, libusb_context*, "contextPointer"); } + +void resetContext(JNIEnv* env, jobject obj) +{ + RESET_POINTER(env, obj, "contextPointer"); +} diff --git a/src/main/c/src/Context.h b/src/main/c/src/Context.h index e12578b..d348a1a 100644 --- a/src/main/c/src/Context.h +++ b/src/main/c/src/Context.h @@ -10,5 +10,6 @@ void setContext(JNIEnv*, libusb_context*, jobject); libusb_context* unwrapContext(JNIEnv*, jobject); +void resetContext(JNIEnv*, jobject); #endif diff --git a/src/main/c/src/DeviceHandle.c b/src/main/c/src/DeviceHandle.c index d1de0fd..0de351b 100644 --- a/src/main/c/src/DeviceHandle.c +++ b/src/main/c/src/DeviceHandle.c @@ -7,15 +7,20 @@ void setDeviceHandle(JNIEnv* env, libusb_device_handle* deviceHandle, jobject object) { - SET_POINTER(env, deviceHandle, object, "handlePointer"); + SET_POINTER(env, deviceHandle, object, "deviceHandlePointer"); } jobject wrapDeviceHandle(JNIEnv* env, libusb_device_handle* deviceHandle) { - WRAP_POINTER(env, deviceHandle, "DeviceHandle", "handlePointer"); + WRAP_POINTER(env, deviceHandle, "DeviceHandle", "deviceHandlePointer"); } libusb_device_handle* unwrapDeviceHandle(JNIEnv* env, jobject deviceHandle) { - UNWRAP_POINTER(env, deviceHandle, libusb_device_handle*, "handlePointer"); + UNWRAP_POINTER(env, deviceHandle, libusb_device_handle*, "deviceHandlePointer"); +} + +void resetDeviceHandle(JNIEnv* env, jobject object) +{ + RESET_POINTER(env, object, "deviceHandlePointer"); } diff --git a/src/main/c/src/DeviceHandle.h b/src/main/c/src/DeviceHandle.h index 98de02f..81ff999 100644 --- a/src/main/c/src/DeviceHandle.h +++ b/src/main/c/src/DeviceHandle.h @@ -11,5 +11,6 @@ void setDeviceHandle(JNIEnv*, libusb_device_handle*, jobject); jobject wrapDeviceHandle(JNIEnv*, libusb_device_handle*); libusb_device_handle* unwrapDeviceHandle(JNIEnv*, jobject); +void resetDeviceHandle(JNIEnv*, jobject); #endif diff --git a/src/main/c/src/DeviceList.c b/src/main/c/src/DeviceList.c index ace7acd..c51e339 100644 --- a/src/main/c/src/DeviceList.c +++ b/src/main/c/src/DeviceList.c @@ -8,7 +8,7 @@ void setDeviceList(JNIEnv* env, libusb_device** list, int size, jobject object) { - SET_POINTER(env, list, object, "pointer"); + SET_POINTER(env, list, object, "deviceListPointer"); jclass cls = (*env)->GetObjectClass(env, object); jfieldID field = (*env)->GetFieldID(env, cls, "size", "I"); @@ -17,7 +17,12 @@ void setDeviceList(JNIEnv* env, libusb_device** list, int size, jobject object) libusb_device** unwrapDeviceList(JNIEnv* env, jobject list) { - UNWRAP_POINTER(env, list, libusb_device**, "pointer"); + UNWRAP_POINTER(env, list, libusb_device**, "deviceListPointer"); +} + +void resetDeviceList(JNIEnv* env, jobject obj) +{ + RESET_POINTER(env, obj, "deviceListPointer"); } /** diff --git a/src/main/c/src/DeviceList.h b/src/main/c/src/DeviceList.h index 9060588..79cf469 100644 --- a/src/main/c/src/DeviceList.h +++ b/src/main/c/src/DeviceList.h @@ -10,5 +10,6 @@ void setDeviceList(JNIEnv*, libusb_device**, int, jobject); libusb_device** unwrapDeviceList(JNIEnv*, jobject); +void resetDeviceList(JNIEnv*, jobject); #endif diff --git a/src/main/c/src/EndpointDescriptor.c b/src/main/c/src/EndpointDescriptor.c index 21f861a..b8c72dd 100644 --- a/src/main/c/src/EndpointDescriptor.c +++ b/src/main/c/src/EndpointDescriptor.c @@ -8,7 +8,7 @@ jobject wrapEndpointDescriptor(JNIEnv *env, const struct libusb_endpoint_descriptor *descriptor) { - WRAP_POINTER(env, descriptor, "EndpointDescriptor", "pointer"); + WRAP_POINTER(env, descriptor, "EndpointDescriptor", "endpointDescriptorPointer"); } jobjectArray wrapEndpointDescriptors(JNIEnv *env, int count, @@ -28,7 +28,7 @@ jobjectArray wrapEndpointDescriptors(JNIEnv *env, int count, struct libusb_endpoint_descriptor *unwrapEndpointDescriptor(JNIEnv *env, jobject obj) { - UNWRAP_POINTER(env, obj, struct libusb_endpoint_descriptor*, "pointer"); + UNWRAP_POINTER(env, obj, struct libusb_endpoint_descriptor*, "endpointDescriptorPointer"); } /** diff --git a/src/main/c/src/LibUsb.c b/src/main/c/src/LibUsb.c index f05ba96..d02c89d 100644 --- a/src/main/c/src/LibUsb.c +++ b/src/main/c/src/LibUsb.c @@ -65,7 +65,10 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, exit) JNIEnv *env, jclass class, jobject context ) { - libusb_exit(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_exit(ctx); + resetContext(env, context); } /** @@ -76,7 +79,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, setDebug) JNIEnv *env, jclass class, jobject context, jint level ) { - libusb_set_debug(unwrapContext(env, context), level); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_set_debug(ctx, level); } /** @@ -88,9 +93,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDeviceList) ) { NOT_NULL(env, deviceList, return 0); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; libusb_device **list; - ssize_t result = libusb_get_device_list(unwrapContext(env, context), - &list); + ssize_t result = libusb_get_device_list(ctx, &list); if (result >= 0) setDeviceList(env, list, result, deviceList); return result; } @@ -104,7 +110,10 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, freeDeviceList) ) { NOT_NULL(env, deviceList, return); - libusb_free_device_list(unwrapDeviceList(env, deviceList), unrefDevices); + libusb_device **list = unwrapDeviceList(env, deviceList); + if (!list) return; + libusb_free_device_list(list, unrefDevices); + resetDeviceList(env, deviceList); } /** @@ -116,7 +125,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getBusNumber) ) { NOT_NULL(env, device, return 0); - return libusb_get_bus_number(unwrapDevice(env, device)); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + return libusb_get_bus_number(dev); } /** @@ -128,8 +139,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getPortNumber) ) { NOT_NULL(env, device, return 0); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; #if defined(LIBUSBX_API_VERSION) - return libusb_get_port_number(unwrapDevice(env, device)); + return libusb_get_port_number(dev); #else return 0; #endif @@ -145,11 +158,14 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getPortPath) { NOT_NULL(env, device, return 0); NOT_NULL(env, path, return 0); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; jsize size = (*env)->GetArrayLength(env, path); unsigned char buffer[size]; #if defined(LIBUSBX_API_VERSION) - int result = libusb_get_port_path(unwrapContext(env, context), - unwrapDevice(env, device), buffer, size); + int result = libusb_get_port_path(ctx, dev, buffer, size); #else int result = 0; #endif @@ -166,8 +182,10 @@ JNIEXPORT jobject JNICALL METHOD_NAME(LibUsb, getParent) ) { NOT_NULL(env, device, return NULL); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return NULL; #if defined(LIBUSBX_API_VERSION) - return wrapDevice(env, libusb_get_parent(unwrapDevice(env, device))); + return wrapDevice(env, libusb_get_parent(dev)); #else return NULL; #endif @@ -182,7 +200,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDeviceAddress) ) { NOT_NULL(env, device, return 0); - return libusb_get_device_address(unwrapDevice(env, device)); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + return libusb_get_device_address(dev); } /** @@ -194,7 +214,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDeviceSpeed) ) { NOT_NULL(env, device, return 0); - return libusb_get_device_speed(unwrapDevice(env, device)); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + return libusb_get_device_speed(dev); } /** @@ -206,7 +228,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getMaxPacketSize) ) { NOT_NULL(env, device, return 0); - return libusb_get_max_packet_size(unwrapDevice(env, device), endpoint); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + return libusb_get_max_packet_size(dev, endpoint); } /** @@ -218,7 +242,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getMaxIsoPacketSize) ) { NOT_NULL(env, device, return 0); - return libusb_get_max_iso_packet_size(unwrapDevice(env, device), endpoint); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + return libusb_get_max_iso_packet_size(dev, endpoint); } /** @@ -230,7 +256,9 @@ JNIEXPORT jobject JNICALL METHOD_NAME(LibUsb, refDevice) ) { NOT_NULL(env, device, return NULL); - libusb_ref_device(unwrapDevice(env, device)); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return NULL; + libusb_ref_device(dev); return device; } @@ -243,7 +271,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unrefDevice) ) { NOT_NULL(env, device, return); - libusb_unref_device(unwrapDevice(env, device)); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return; + libusb_unref_device(dev); } /** @@ -256,8 +286,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, open) { NOT_NULL(env, device, return 0); NOT_NULL(env, handle, return 0); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; libusb_device_handle *deviceHandle; - int result = libusb_open(unwrapDevice(env, device), &deviceHandle); + int result = libusb_open(dev, &deviceHandle); if (!result) setDeviceHandle(env, deviceHandle, handle); return result; } @@ -271,8 +303,10 @@ JNIEXPORT jobject JNICALL METHOD_NAME(LibUsb, openDeviceWithVidPid) jint productId ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return NULL; return wrapDeviceHandle(env, libusb_open_device_with_vid_pid( - unwrapContext(env, context), vendorId, productId)); + ctx, vendorId, productId)); } /** @@ -284,7 +318,10 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, close) ) { NOT_NULL(env, handle, return); - libusb_close(unwrapDeviceHandle(env, handle)); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return; + libusb_close(dev_handle); + resetDeviceHandle(env, handle); } /** @@ -296,7 +333,9 @@ JNIEXPORT jobject JNICALL METHOD_NAME(LibUsb, getDevice) ) { NOT_NULL(env, handle, return NULL); - return wrapDevice(env, libusb_get_device(unwrapDeviceHandle(env, handle))); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return NULL; + return wrapDevice(env, libusb_get_device(dev_handle)); } /** @@ -309,9 +348,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getConfiguration) { NOT_NULL(env, handle, return 0); NOT_NULL(env, buffer, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; int config; - int result = libusb_get_configuration(unwrapDeviceHandle(env, handle), - &config); + int result = libusb_get_configuration(dev_handle, &config); if (!result) { jclass cls = (*env)->GetObjectClass(env, buffer); @@ -330,7 +370,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, setConfiguration) ) { NOT_NULL(env, handle, return 0); - return libusb_set_configuration(unwrapDeviceHandle(env, handle), config); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_set_configuration(dev_handle, config); } /** @@ -342,7 +384,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, claimInterface) ) { NOT_NULL(env, handle, return 0); - return libusb_claim_interface(unwrapDeviceHandle(env, handle), iface); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_claim_interface(dev_handle, iface); } /** @@ -354,7 +398,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, releaseInterface) ) { NOT_NULL(env, handle, return 0); - return libusb_release_interface(unwrapDeviceHandle(env, handle), iface); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_release_interface(dev_handle, iface); } /** @@ -366,8 +412,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, setInterfaceAltSetting) ) { NOT_NULL(env, handle, return 0); - return libusb_set_interface_alt_setting(unwrapDeviceHandle(env, handle), - iface, setting); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_set_interface_alt_setting(dev_handle, iface, setting); } /** @@ -379,7 +426,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, clearHalt) ) { NOT_NULL(env, handle, return 0); - return libusb_clear_halt(unwrapDeviceHandle(env, handle), endpoint); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_clear_halt(dev_handle, endpoint); } /** @@ -391,7 +440,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, resetDevice) ) { NOT_NULL(env, handle, return 0); - return libusb_reset_device(unwrapDeviceHandle(env, handle)); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_reset_device(dev_handle); } /** @@ -403,7 +454,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, kernelDriverActive) ) { NOT_NULL(env, handle, return 0); - return libusb_kernel_driver_active(unwrapDeviceHandle(env, handle), iface); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_kernel_driver_active(dev_handle, iface); } /** @@ -415,7 +468,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, detachKernelDriver) ) { NOT_NULL(env, handle, return 0); - return libusb_detach_kernel_driver(unwrapDeviceHandle(env, handle), iface); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_detach_kernel_driver(dev_handle, iface); } /** @@ -427,7 +482,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, attachKernelDriver) ) { NOT_NULL(env, handle, return 0); - return libusb_attach_kernel_driver(unwrapDeviceHandle(env, handle), iface); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; + return libusb_attach_kernel_driver(dev_handle, iface); } /** @@ -484,8 +541,11 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDeviceDescriptor) { NOT_NULL(env, device, return 0); NOT_NULL(env, descriptor, return 0); - struct libusb_device_descriptor *data = malloc(sizeof(struct libusb_device_descriptor)); - int result = libusb_get_device_descriptor(unwrapDevice(env, device), data); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; + struct libusb_device_descriptor *data = + malloc(sizeof(struct libusb_device_descriptor)); + int result = libusb_get_device_descriptor(dev, data); if (!result) setDeviceDescriptor(env, data, descriptor); return result; } @@ -501,9 +561,11 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getStringDescriptorAscii) { NOT_NULL(env, handle, return 0); NOT_NULL(env, string, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; unsigned char buffer[length + 1]; int result = libusb_get_string_descriptor_ascii( - unwrapDeviceHandle(env, handle), index, buffer, length); + dev_handle, index, buffer, length); if (result >= 0) { buffer[result] = 0; @@ -525,9 +587,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getActiveConfigDescriptor) { NOT_NULL(env, device, return 0); NOT_NULL(env, descriptor, return 0); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; struct libusb_config_descriptor *config; - int result = libusb_get_active_config_descriptor( - unwrapDevice(env, device), &config); + int result = libusb_get_active_config_descriptor(dev, &config); if (!result) setConfigDescriptor(env, config, descriptor); return result; } @@ -542,9 +605,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getConfigDescriptor) { NOT_NULL(env, device, return 0); NOT_NULL(env, descriptor, return 0); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; struct libusb_config_descriptor *config; - int result = libusb_get_config_descriptor( - unwrapDevice(env, device), index, &config); + int result = libusb_get_config_descriptor(dev, index, &config); if (!result) setConfigDescriptor(env, config, descriptor); return result; } @@ -559,9 +623,11 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getConfigDescriptorByValue) { NOT_NULL(env, device, return 0); NOT_NULL(env, descriptor, return 0); + libusb_device *dev = unwrapDevice(env, device); + if (!dev) return 0; struct libusb_config_descriptor *config; int result = libusb_get_config_descriptor_by_value( - unwrapDevice(env, device), index, &config); + dev, index, &config); if (!result) setConfigDescriptor(env, config, descriptor); return result; } @@ -575,7 +641,11 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, freeConfigDescriptor) ) { NOT_NULL(env, descriptor, return); - libusb_free_config_descriptor(unwrapConfigDescriptor(env, descriptor)); + struct libusb_config_descriptor *config = unwrapConfigDescriptor(env, + descriptor); + if (!config) return; + libusb_free_config_descriptor(config); + resetConfigDescriptor(env, descriptor); } /** @@ -590,10 +660,11 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getDescriptor) NOT_NULL(env, handle, return 0); NOT_NULL(env, data, return 0); DIRECT_BUFFER(env, data, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; unsigned char *ptr = (*env)->GetDirectBufferAddress(env, data); jlong size = (*env)->GetDirectBufferCapacity(env, data); - return libusb_get_descriptor(unwrapDeviceHandle(env, handle), - type, index, ptr, size); + return libusb_get_descriptor(dev_handle, type, index, ptr, size); } /** @@ -608,10 +679,11 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getStringDescriptor) NOT_NULL(env, handle, return 0); NOT_NULL(env, data, return 0); DIRECT_BUFFER(env, data, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; unsigned char *ptr = (*env)->GetDirectBufferAddress(env, data); jlong size = (*env)->GetDirectBufferCapacity(env, data); - return libusb_get_string_descriptor(unwrapDeviceHandle(env, handle), - index, langId, ptr, size); + return libusb_get_string_descriptor(dev_handle, index, langId, ptr, size); } /** @@ -626,10 +698,12 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, controlTransfer) NOT_NULL(env, handle, return 0); NOT_NULL(env, data, return 0); DIRECT_BUFFER(env, data, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; unsigned char *ptr = (*env)->GetDirectBufferAddress(env, data); jlong size = (*env)->GetDirectBufferCapacity(env, data); - return libusb_control_transfer(unwrapDeviceHandle(env, handle), - bmRequestType, bRequest, wValue, wIndex, ptr, size, timeout); + return libusb_control_transfer(dev_handle, bmRequestType, bRequest, + wValue, wIndex, ptr, size, timeout); } /** @@ -645,11 +719,13 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, bulkTransfer) NOT_NULL(env, data, return 0); NOT_NULL(env, transferred, return 0); DIRECT_BUFFER(env, data, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; int sent; unsigned char *ptr = (*env)->GetDirectBufferAddress(env, data); jlong size = (*env)->GetDirectBufferCapacity(env, data); - int result = libusb_bulk_transfer(unwrapDeviceHandle(env, handle), - endpoint, ptr, size, &sent, timeout); + int result = libusb_bulk_transfer(dev_handle, endpoint, ptr, size, &sent, + timeout); if (!result) { jclass cls = (*env)->GetObjectClass(env, transferred); @@ -673,11 +749,13 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, interruptTransfer) NOT_NULL(env, data, return 0); NOT_NULL(env, transferred, return 0); DIRECT_BUFFER(env, data, return 0); + libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle); + if (!dev_handle) return 0; int sent; unsigned char *ptr = (*env)->GetDirectBufferAddress(env, data); jlong size = (*env)->GetDirectBufferCapacity(env, data); - int result = libusb_interrupt_transfer(unwrapDeviceHandle(env, handle), - endpoint, ptr, size, &sent, timeout); + int result = libusb_interrupt_transfer(dev_handle, endpoint, ptr, size, + &sent, timeout); if (!result) { jclass cls = (*env)->GetObjectClass(env, transferred); @@ -696,7 +774,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, tryLockEvents) JNIEnv *env, jclass class, jobject context ) { - return libusb_try_lock_events(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + return libusb_try_lock_events(ctx); } /** @@ -707,7 +787,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, lockEvents) JNIEnv *env, jclass class, jobject context ) { - libusb_lock_events(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_lock_events(ctx); } /** @@ -718,7 +800,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unlockEvents) JNIEnv *env, jclass class, jobject context ) { - libusb_unlock_events(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_unlock_events(ctx); } /** @@ -729,7 +813,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, eventHandlingOk) JNIEnv *env, jclass class, jobject context ) { - return libusb_event_handling_ok(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + return libusb_event_handling_ok(ctx); } /** @@ -740,7 +826,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, eventHandlerActive) JNIEnv *env, jclass class, jobject context ) { - return libusb_event_handler_active(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + return libusb_event_handler_active(ctx); } /** @@ -751,7 +839,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, lockEventWaiters) JNIEnv *env, jclass class, jobject context ) { - libusb_lock_event_waiters(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_lock_event_waiters(ctx); } /** @@ -762,7 +852,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unlockEventWaiters) JNIEnv *env, jclass class, jobject context ) { - libusb_unlock_event_waiters(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_unlock_event_waiters(ctx); } /** @@ -773,10 +865,12 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, waitForEvent) JNIEnv *env, jclass class, jobject context, jlong timeout ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; - return libusb_wait_for_event(unwrapContext(env, context), &tv); + return libusb_wait_for_event(ctx, &tv); } /** @@ -788,12 +882,13 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsTimeoutCompleted) jobject completed ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; int complete; - int result = libusb_handle_events_timeout_completed( - unwrapContext(env, context), &tv, &complete); + int result = libusb_handle_events_timeout_completed(ctx, &tv, &complete); if (!result && completed) { jclass cls = (*env)->GetObjectClass(env, completed); @@ -811,10 +906,12 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsTimeout) JNIEnv *env, jclass class, jobject context, jlong timeout ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; - return libusb_handle_events_timeout(unwrapContext(env, context), &tv); + return libusb_handle_events_timeout(ctx, &tv); } /** @@ -825,7 +922,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEvents) JNIEnv *env, jclass class, jobject context ) { - return libusb_handle_events(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + return libusb_handle_events(ctx); } /** @@ -836,9 +935,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsCompleted) JNIEnv *env, jclass class, jobject context, jobject completed ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; int complete; - int result = libusb_handle_events_completed( - unwrapContext(env, context), &complete); + int result = libusb_handle_events_completed(ctx, &complete); if (!result && completed) { jclass cls = (*env)->GetObjectClass(env, completed); @@ -856,10 +956,12 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, handleEventsLocked) JNIEnv *env, jclass class, jobject context, jlong timeout ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; struct timeval tv; tv.tv_sec = timeout / 1000000; tv.tv_usec = timeout % 1000000; - return libusb_handle_events_locked(unwrapContext(env, context), &tv); + return libusb_handle_events_locked(ctx, &tv); } /** @@ -870,7 +972,9 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, pollfdsHandleTimeouts) JNIEnv *env, jclass class, jobject context ) { - return libusb_pollfds_handle_timeouts(unwrapContext(env, context)); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; + return libusb_pollfds_handle_timeouts(ctx); } /** @@ -881,9 +985,10 @@ JNIEXPORT jint JNICALL METHOD_NAME(LibUsb, getNextTimeout) JNIEnv *env, jclass class, jobject context, jobject timeout ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return 0; struct timeval tv; - int result = libusb_get_next_timeout( - unwrapContext(env, context), &tv); + int result = libusb_get_next_timeout(ctx, &tv); if (result == 1) { jclass cls = (*env)->GetObjectClass(env, timeout); @@ -935,9 +1040,11 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, setPollfdNotifiers) JNIEnv *env, jclass class, jobject context ) { + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; (*env)->GetJavaVM(env, &jvm); - libusb_set_pollfd_notifiers(unwrapContext(env, context), - triggerPollfdAdded, triggerPollfdRemoved, NULL); + libusb_set_pollfd_notifiers(ctx, triggerPollfdAdded, triggerPollfdRemoved, + NULL); } /** @@ -948,8 +1055,9 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, unsetPollfdNotifiers) JNIEnv *env, jclass class, jobject context ) { - libusb_set_pollfd_notifiers(unwrapContext(env, context), - NULL, NULL, NULL); + struct libusb_context *ctx = unwrapContext(env, context); + if (!ctx) return; + libusb_set_pollfd_notifiers(ctx, NULL, NULL, NULL); } /** @@ -971,5 +1079,8 @@ JNIEXPORT void JNICALL METHOD_NAME(LibUsb, freeTransfer) JNIEnv *env, jclass class, jobject transfer ) { - libusb_free_transfer(unwrapTransfer(env, transfer)); + struct libusb_transfer *handle = unwrapTransfer(env, transfer); + if (!handle) return; + libusb_free_transfer(handle); + resetTransfer(env, transfer); } diff --git a/src/main/c/src/Transfer.c b/src/main/c/src/Transfer.c index 41f40f8..80bdc0b 100644 --- a/src/main/c/src/Transfer.c +++ b/src/main/c/src/Transfer.c @@ -16,6 +16,11 @@ struct libusb_transfer* unwrapTransfer(JNIEnv *env, jobject obj) UNWRAP_POINTER(env, obj, struct libusb_transfer*, "pointer"); } +void resetTransfer(JNIEnv* env, jobject obj) +{ + RESET_POINTER(env, obj, "pointer"); +} + /** * void setDevHandle(DeviceHandle) */ diff --git a/src/main/c/src/Transfer.h b/src/main/c/src/Transfer.h index e7686fd..dbca82d 100644 --- a/src/main/c/src/Transfer.h +++ b/src/main/c/src/Transfer.h @@ -10,5 +10,6 @@ jobject wrapTransfer(JNIEnv*, struct libusb_transfer*); struct libusb_transfer* unwrapTransfer(JNIEnv*, jobject); +void resetTransfer(JNIEnv*, jobject); #endif diff --git a/src/main/c/src/usb4java.c b/src/main/c/src/usb4java.c index 0a219b0..c148ca7 100644 --- a/src/main/c/src/usb4java.c +++ b/src/main/c/src/usb4java.c @@ -10,3 +10,10 @@ jint illegalArgument(JNIEnv *env, char *message) jclass cls = (*env)->FindClass(env, "java/lang/IllegalArgumentException"); return (*env)->ThrowNew(env, cls, message); } + +jint illegalState(JNIEnv *env, char *message) +{ + jclass cls = (*env)->FindClass(env, "java/lang/IllegalStateException"); + return (*env)->ThrowNew(env, cls, message); +} + diff --git a/src/main/c/src/usb4java.h b/src/main/c/src/usb4java.h index b9e45b6..dba1afc 100644 --- a/src/main/c/src/usb4java.h +++ b/src/main/c/src/usb4java.h @@ -26,6 +26,13 @@ (*ENV)->SetLongField(ENV, OBJECT, field, (jptr) PTR); \ } +#define RESET_POINTER(ENV, OBJECT, FIELD) \ +{ \ + jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \ + jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \ + (*ENV)->SetLongField(ENV, OBJECT, field, 0); \ +} + #define WRAP_POINTER(ENV, PTR, CLASS_NAME, FIELD) \ { \ if (!PTR) return NULL; \ @@ -44,6 +51,8 @@ if (!OBJECT) return NULL; \ jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \ jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \ + jptr ptr = (jptr) (*ENV)->GetLongField(ENV, OBJECT, field); \ + if (!ptr) illegalState(ENV, FIELD" is not initialized"); \ return (TYPE) (jptr) (*ENV)->GetLongField(ENV, OBJECT, field); \ } @@ -94,5 +103,6 @@ if (getEnvResult == JNI_EDETACHED) (*jvm)->DetachCurrentThread(jvm); jint illegalArgument(JNIEnv *env, char *message); +jint illegalState(JNIEnv *env, char *message); #endif diff --git a/src/main/java/de/ailis/usb4java/libusb/ConfigDescriptor.java b/src/main/java/de/ailis/usb4java/libusb/ConfigDescriptor.java index a9dda09..291f69a 100644 --- a/src/main/java/de/ailis/usb4java/libusb/ConfigDescriptor.java +++ b/src/main/java/de/ailis/usb4java/libusb/ConfigDescriptor.java @@ -31,7 +31,7 @@ import de.ailis.usb4java.utils.DescriptorUtils; public final class ConfigDescriptor implements UsbConfigurationDescriptor { /** The native pointer to the descriptor structure. */ - private long pointer; + private long configDescriptorPointer; /** * Constructs a new config descriptor which can be passed to the @@ -49,7 +49,7 @@ public final class ConfigDescriptor implements UsbConfigurationDescriptor */ public long getPointer() { - return this.pointer; + return this.configDescriptorPointer; } @Override diff --git a/src/main/java/de/ailis/usb4java/libusb/DeviceHandle.java b/src/main/java/de/ailis/usb4java/libusb/DeviceHandle.java index 0ce4430..24a9b39 100644 --- a/src/main/java/de/ailis/usb4java/libusb/DeviceHandle.java +++ b/src/main/java/de/ailis/usb4java/libusb/DeviceHandle.java @@ -27,7 +27,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; public final class DeviceHandle { /** The native pointer to the device handle structure. */ - private long handlePointer; + private long deviceHandlePointer; /** * Constructs a new device handle. Must be passed to @@ -46,13 +46,13 @@ public final class DeviceHandle */ public long getPointer() { - return this.handlePointer; + return this.deviceHandlePointer; } @Override public int hashCode() { - return new HashCodeBuilder().append(this.handlePointer).toHashCode(); + return new HashCodeBuilder().append(this.deviceHandlePointer).toHashCode(); } @Override @@ -61,12 +61,12 @@ public final class DeviceHandle if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; final DeviceHandle other = (DeviceHandle) obj; - return this.handlePointer == other.handlePointer; + return this.deviceHandlePointer == other.deviceHandlePointer; } @Override public String toString() { - return String.format("libusb handle 0x%x", this.handlePointer); + return String.format("libusb handle 0x%x", this.deviceHandlePointer); } } diff --git a/src/main/java/de/ailis/usb4java/libusb/DeviceList.java b/src/main/java/de/ailis/usb4java/libusb/DeviceList.java index b0ccd5f..0c8cc00 100644 --- a/src/main/java/de/ailis/usb4java/libusb/DeviceList.java +++ b/src/main/java/de/ailis/usb4java/libusb/DeviceList.java @@ -24,7 +24,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder; public final class DeviceList implements Iterable { /** The native pointer to the devices array. */ - private long pointer; + private long deviceListPointer; /** The number of devices in the list. */ private int size; @@ -45,7 +45,7 @@ public final class DeviceList implements Iterable */ public long getPointer() { - return this.pointer; + return this.deviceListPointer; } /** @@ -76,7 +76,7 @@ public final class DeviceList implements Iterable @Override public int hashCode() { - return new HashCodeBuilder().append(this.pointer).toHashCode(); + return new HashCodeBuilder().append(this.deviceListPointer).toHashCode(); } @Override @@ -85,6 +85,6 @@ public final class DeviceList implements Iterable if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; final DeviceList other = (DeviceList) obj; - return this.pointer == other.pointer; + return this.deviceListPointer == other.deviceListPointer; } } diff --git a/src/main/java/de/ailis/usb4java/libusb/EndpointDescriptor.java b/src/main/java/de/ailis/usb4java/libusb/EndpointDescriptor.java index f445677..4fd5dfa 100644 --- a/src/main/java/de/ailis/usb4java/libusb/EndpointDescriptor.java +++ b/src/main/java/de/ailis/usb4java/libusb/EndpointDescriptor.java @@ -31,7 +31,7 @@ import de.ailis.usb4java.utils.DescriptorUtils; public final class EndpointDescriptor implements UsbEndpointDescriptor { /** The native pointer to the descriptor structure. */ - private long pointer; + private long endpointDescriptorPointer; /** * Package-private constructor to prevent manual instantiation. Endpoint @@ -49,7 +49,7 @@ public final class EndpointDescriptor implements UsbEndpointDescriptor */ public long getPointer() { - return this.pointer; + return this.endpointDescriptorPointer; } @Override diff --git a/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so b/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so index 9e9b201..589f3bb 100644 Binary files a/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so and b/src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so differ diff --git a/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java b/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java index 5dd0697..8763142 100644 --- a/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java +++ b/src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java @@ -5,10 +5,11 @@ package de.ailis.usb4java.libusb; -import static de.ailis.usb4java.UsbAssume.assumeUsbTestsEnabled; +import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.junit.Assume.assumeNotNull; import java.nio.ByteBuffer; @@ -56,7 +57,8 @@ public class LibUSBDeviceTest LibUsb.init(this.context); this.device = findTestDevice(); if (this.device == null) - throw new IllegalStateException("Need at least one USB device " + + throw new IllegalStateException("Need at least one USB device " + + "with at least one endpoint to execute this test"); } catch (Throwable e) @@ -140,16 +142,6 @@ public class LibUSBDeviceTest assertTrue(LibUsb.getBusNumber(this.device) >= 0); } - /** - * Tests the {@link LibUsb#getBusNumber(Device)} method without a device. - */ - @Test(expected = IllegalArgumentException.class) - public void testGetBusNumberWithoutDevice() - { - assumeUsbTestsEnabled(); - LibUsb.getBusNumber(null); - } - /** * Tests the {@link LibUsb#getPortNumber(Device)} method. */ @@ -161,16 +153,6 @@ public class LibUSBDeviceTest assertTrue(LibUsb.getPortNumber(this.device) >= 0); } - /** - * Tests the {@link LibUsb#getPortNumber(Device)} method without a device. - */ - @Test(expected = IllegalArgumentException.class) - public void testGetPortNumberWithoutDevice() - { - assumeUsbTestsEnabled(); - LibUsb.getPortNumber(null); - } - /** * Tests the {@link LibUsb#getPortPath(Context, Device, byte[])} method. */ @@ -221,6 +203,30 @@ public class LibUSBDeviceTest LibUsb.getPortPath(this.context, this.device, null); } + /** + * Tests {@link LibUsb#getPortPath(Context, Device, byte[])} method with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testGetPortPathWithUninitializedContext() + { + assumeUsbTestsEnabled(); + assumeNotNull(this.device); + final Context context = new Context(); + LibUsb.getPortPath(context, this.device, new byte[16]); + } + + /** + * Tests {@link LibUsb#getPortPath(Context, Device, byte[])} method with + * uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetPortPathWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getPortPath(this.context, new Device(), new byte[16]); + } + /** * Tests the {@link LibUsb#getParent(Device)} method. */ @@ -425,14 +431,27 @@ public class LibUSBDeviceTest * crash. */ @Test - public void testOpen() + public void testOpenAndClose() { assumeUsbTestsEnabled(); assumeNotNull(this.device); DeviceHandle handle = new DeviceHandle(); int result = LibUsb.open(this.device, handle); assertTrue(result == LibUsb.SUCCESS || result == LibUsb.ERROR_ACCESS); - if (result == LibUsb.SUCCESS) LibUsb.close(handle); + if (result == LibUsb.SUCCESS) + { + LibUsb.close(handle); + + try + { + LibUsb.close(handle); + fail("Double-close should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } + } } /** @@ -878,6 +897,16 @@ public class LibUSBDeviceTest finally { LibUsb.freeConfigDescriptor(desc); + + try + { + LibUsb.freeConfigDescriptor(desc); + fail("Double-free should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } } } @@ -1104,4 +1133,27 @@ public class LibUSBDeviceTest LibUsb.interruptTransfer(new DeviceHandle(), 0, ByteBuffer.allocate(0), null, 0); } + + /** + * Tests the {@link LibUsb#getDeviceList(Context, DeviceList)} and + * LibUsb#freeDeviceList(DeviceList, boolean)} methods. + */ + @Test + public void testGetAndFreeDeviceList() + { + assumeUsbTestsEnabled(); + DeviceList list = new DeviceList(); + assertTrue(LibUsb.getDeviceList(this.context, list) >= 0); + LibUsb.freeDeviceList(list, true); + + try + { + LibUsb.freeDeviceList(list, true); + fail("Double-free should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } + } } diff --git a/src/test/java/de/ailis/usb4java/libusb/LibUSBGlobalTest.java b/src/test/java/de/ailis/usb4java/libusb/LibUSBGlobalTest.java index 885de28..eb8b2d6 100644 --- a/src/test/java/de/ailis/usb4java/libusb/LibUSBGlobalTest.java +++ b/src/test/java/de/ailis/usb4java/libusb/LibUSBGlobalTest.java @@ -5,7 +5,7 @@ package de.ailis.usb4java.libusb; -import static de.ailis.usb4java.UsbAssume.assumeUsbTestsEnabled; +import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; diff --git a/src/test/java/de/ailis/usb4java/libusb/LibUSBTest.java b/src/test/java/de/ailis/usb4java/libusb/LibUSBTest.java index 223a82b..2a12b33 100644 --- a/src/test/java/de/ailis/usb4java/libusb/LibUSBTest.java +++ b/src/test/java/de/ailis/usb4java/libusb/LibUSBTest.java @@ -5,13 +5,22 @@ package de.ailis.usb4java.libusb; -import static de.ailis.usb4java.UsbAssume.assumeUsbTestsEnabled; +import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.FileDescriptor; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; import org.junit.Test; +import de.ailis.usb4java.libusb.mocks.PollfdListenerMock; + /** * Tests the {@link LibUsb} class. * @@ -166,10 +175,13 @@ public class LibUSBTest assumeUsbTestsEnabled(); assertEquals(LibUsb.SUCCESS, LibUsb.init(null)); LibUsb.exit(null); + + // Double-exit without a context should work + LibUsb.exit(null); } /** - * Tests the initialization and deinitialization of libusb with a custom + * Tests the initialization and deinitialization of libusb with a custom USB * context. */ @Test @@ -179,5 +191,752 @@ public class LibUSBTest Context context = new Context(); assertEquals(LibUsb.SUCCESS, LibUsb.init(context)); LibUsb.exit(context); + + try + { + LibUsb.exit(context); + fail("Double-exit should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } + } + + /** + * Tests {@link LibUsb#exit(Context)} method with uninitialized Context + */ + @Test(expected = IllegalStateException.class) + public void testExitWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.exit(context); + } + + /** + * Tests {@link LibUsb#setDebug(Context, int)} method with uninitialized USB + * context + */ + @Test(expected = IllegalStateException.class) + public void testSetDebugWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.setDebug(context, 0); + } + + /** + * Tests {@link LibUsb#getDeviceList(Context, DeviceList)} method with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testGetDeviceListWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.getDeviceList(context, new DeviceList()); + } + + /** + * Tests {@link LibUsb#freeDeviceList(DeviceList, boolean)} method with + * uninitialized list. + */ + @Test(expected = IllegalStateException.class) + public void testFreeDeviceListWithUninitializedList() + { + assumeUsbTestsEnabled(); + LibUsb.freeDeviceList(new DeviceList(), true); + } + + /** + * Tests {@link LibUsb#freeDeviceList(DeviceList, boolean)} method without + * list. + */ + @Test(expected = IllegalArgumentException.class) + public void testFreeDeviceListWithoutList() + { + assumeUsbTestsEnabled(); + LibUsb.freeDeviceList(null, true); + } + + /** + * Tests the {@link LibUsb#getBusNumber(Device)} method without a device. + */ + @Test(expected = IllegalArgumentException.class) + public void testGetBusNumberWithoutDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getBusNumber(null); + } + + /** + * Tests the {@link LibUsb#getBusNumber(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testGetBusNumberWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getBusNumber(new Device()); + } + + /** + * Tests the {@link LibUsb#getPortNumber(Device)} method without a device. + */ + @Test(expected = IllegalArgumentException.class) + public void testGetPortNumberWithoutDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getPortNumber(null); + } + + /** + * Tests the {@link LibUsb#getPortNumber(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testGetPortNumberWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getPortNumber(new Device()); + } + + /** + * Tests the {@link LibUsb#getParent(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testGetParentWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getParent(new Device()); + } + + /** + * Tests the {@link LibUsb#getDeviceAddress(Device)} method with + * uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetDeviceAddressWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getDeviceAddress(new Device()); + } + + /** + * Tests the {@link LibUsb#getDeviceSpeed(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testGetDeviceDeviceSpeedWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getDeviceSpeed(new Device()); + } + + /** + * Tests the {@link LibUsb#getMaxPacketSize(Device, int)} method with + * uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testMaxPacketSizeWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getMaxPacketSize(new Device(), 0); + } + + /** + * Tests the {@link LibUsb#getMaxIsoPacketSize(Device, int)} method with + * uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testMaxIsoPacketSizeWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getMaxIsoPacketSize(new Device(), 0); + } + + /** + * Tests the {@link LibUsb#refDevice(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testRefDeviceWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.refDevice(new Device()); + } + + /** + * Tests the {@link LibUsb#unrefDevice(Device)} method with uninitialized + * device. + */ + @Test(expected = IllegalStateException.class) + public void testUnrefDeviceWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.unrefDevice(new Device()); + } + + /** + * Tests the {@link LibUsb#open(Device, DeviceHandle)} method with + * uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testOpenWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.open(new Device(), new DeviceHandle()); + } + + /** + * Tests the {@link LibUsb#close(DeviceHandle)} method with uninitialized + * device handle. + */ + @Test(expected = IllegalStateException.class) + public void testCloseWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.close(new DeviceHandle()); + } + + /** + * Tests the {@link LibUsb#getDevice(DeviceHandle)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testGetDeviceWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.getDevice(new DeviceHandle()); + } + + /** + * Tests the {@link LibUsb#getConfiguration(DeviceHandle, IntBuffer)} method + * with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testGetConfigurationWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.getConfiguration(new DeviceHandle(), IntBuffer.allocate(1)); + } + + /** + * Tests the {@link LibUsb#setConfiguration(DeviceHandle, int)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testSetConfigurationWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.setConfiguration(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#claimInterface(DeviceHandle, int)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testClaimInterfaceWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.claimInterface(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#releaseInterface(DeviceHandle, int)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testReleaseInterfaceWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.releaseInterface(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#setInterfaceAltSetting(DeviceHandle, int, int)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testSetInterfaceAltSettingWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.setInterfaceAltSetting(new DeviceHandle(), 0, 0); + } + + /** + * Tests the {@link LibUsb#clearHalt(DeviceHandle, int)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testClearHaltWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.clearHalt(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#resetDevice(DeviceHandle)} method with + * uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testResetDeviceWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.resetDevice(new DeviceHandle()); + } + + /** + * Tests the {@link LibUsb#kernelDriverActive(DeviceHandle, int)} method + * with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testKernelDriverActiveWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.kernelDriverActive(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#detachKernelDriver(DeviceHandle, int)} method + * with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testDetachKernelDriverWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.detachKernelDriver(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#attachKernelDriver(DeviceHandle, int)} method + * with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testAttachKernelDriverWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.attachKernelDriver(new DeviceHandle(), 0); + } + + /** + * Tests the {@link LibUsb#getDeviceDescriptor(Device, DeviceDescriptor)} + * method with uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetDeviceDescriptorWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getDeviceDescriptor(new Device(), new DeviceDescriptor()); + } + + /** + * Tests the + * {@link LibUsb#getStringDescriptorAscii(DeviceHandle, int, StringBuffer, int)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testGetStringDescriptorAsciiWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.getStringDescriptorAscii(new DeviceHandle(), 0, + new StringBuffer(), 0); + } + + /** + * Tests the + * {@link LibUsb#getActiveConfigDescriptor(Device, ConfigDescriptor)} method + * with uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetActiveConfigDescriptorWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getActiveConfigDescriptor(new Device(), new ConfigDescriptor()); + } + + /** + * Tests the + * {@link LibUsb#getConfigDescriptor(Device, int, ConfigDescriptor)} method + * with uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetConfigDescriptorWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getConfigDescriptor(new Device(), 0, new ConfigDescriptor()); + } + + /** + * Tests the + * {@link LibUsb#getConfigDescriptorByValue(Device, int, ConfigDescriptor)} + * method with uninitialized device. + */ + @Test(expected = IllegalStateException.class) + public void testGetConfigDescriptorByValueWithUninitializedDevice() + { + assumeUsbTestsEnabled(); + LibUsb.getConfigDescriptorByValue(new Device(), 0, + new ConfigDescriptor()); + } + + /** + * Tests the {@link LibUsb#freeConfigDescriptor(ConfigDescriptor)} method + * with uninitialized descriptor. + */ + @Test(expected = IllegalStateException.class) + public void testFreeConfigDescriptorWithUninitializedDescriptor() + { + assumeUsbTestsEnabled(); + LibUsb.freeConfigDescriptor(new ConfigDescriptor()); + } + + /** + * Tests the + * {@link LibUsb#getDescriptor(DeviceHandle, int, int, ByteBuffer)} method + * with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testGetDescriptorWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.getDescriptor(new DeviceHandle(), 0, 0, + ByteBuffer.allocateDirect(1)); + } + + /** + * Tests the + * {@link LibUsb#getStringDescriptor(DeviceHandle, int, int, ByteBuffer)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testGetStringDescriptorWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.getStringDescriptor(new DeviceHandle(), 0, 0, + ByteBuffer.allocateDirect(1)); + } + + /** + * Tests the + * {@link LibUsb#controlTransfer(DeviceHandle, int, int, int, int, ByteBuffer, int)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testControlTransferWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.controlTransfer(new DeviceHandle(), 0, 0, 0, 0, + ByteBuffer.allocateDirect(1), 0); + } + + /** + * Tests the + * {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testBulkTransferWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.bulkTransfer(new DeviceHandle(), 0, + ByteBuffer.allocateDirect(1), IntBuffer.allocate(1), 0); + } + + /** + * Tests the + * {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)} + * method with uninitialized device handle. + */ + @Test(expected = IllegalStateException.class) + public void testInterruptTransferWithUninitializedHandle() + { + assumeUsbTestsEnabled(); + LibUsb.interruptTransfer(new DeviceHandle(), 0, + ByteBuffer.allocateDirect(1), IntBuffer.allocate(1), 0); + } + + /** + * Tests the {@link LibUsb#freeTransfer(Transfer)} method with uninitialized + * device handle. + */ + @Test(expected = IllegalStateException.class) + public void testFreeTransferWithUninitializedTransfer() + { + assumeUsbTestsEnabled(); + LibUsb.freeTransfer(new Transfer()); + } + + /** + * Tests {@link LibUsb#openDeviceWithVidPid(Context, int, int)} with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testOpenDeviceWithVidPid() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.openDeviceWithVidPid(context, 0, 0); + } + + /** + * Tests {@link LibUsb#tryLockEvents(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testTryLockEventsWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.tryLockEvents(context); + } + + /** + * Tests {@link LibUsb#lockEvents(Context)} with uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testLockEventsWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.lockEvents(context); + } + + /** + * Tests {@link LibUsb#unlockEvents(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testUnlockEventsWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.unlockEvents(context); + } + + /** + * Tests {@link LibUsb#eventHandlingOk(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testEventHandlingOkWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.eventHandlingOk(context); + } + + /** + * Tests {@link LibUsb#eventHandlerActive(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testEventHandlerActiveWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.eventHandlerActive(context); + } + + /** + * Tests {@link LibUsb#lockEventWaiters(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testLockEventWaitersWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.lockEventWaiters(context); + } + + /** + * Tests {@link LibUsb#unlockEventWaiters(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testUnlockEventWaitersWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.unlockEventWaiters(context); + } + + /** + * Tests {@link LibUsb#waitForEvent(Context, long)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testWaitForEventWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.waitForEvent(context, 53); + } + + /** + * Tests + * {@link LibUsb#handleEventsTimeoutCompleted(Context, long, IntBuffer)} + * with uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testHandleEventsTimeoutCompletedWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.handleEventsTimeoutCompleted(context, 53, IntBuffer.allocate(1)); + } + + /** + * Tests {@link LibUsb#handleEventsTimeout(Context, long)} with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testHandleEventsTimeoutWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.handleEventsTimeout(context, 53); + } + + /** + * Tests {@link LibUsb#handleEvents(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testHandleEventsWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.handleEvents(context); + } + + /** + * Tests {@link LibUsb#handleEventsCompleted(Context, IntBuffer)} with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testHandleEventsCompletedWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.handleEventsCompleted(context, IntBuffer.allocate(1)); + } + + /** + * Tests {@link LibUsb#handleEventsLocked(Context, long)} with uninitialized + * USB context. + */ + @Test(expected = IllegalStateException.class) + public void testHandleEventsLockedWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.handleEventsLocked(context, 53); + } + + /** + * Tests {@link LibUsb#pollfdsHandleTimeouts(Context)} with uninitialized + * USB context. + */ + @Test(expected = IllegalStateException.class) + public void testPollfdsHandleTimeoutsWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.pollfdsHandleTimeouts(context); + } + + /** + * Tests {@link LibUsb#getNextTimeout(Context, IntBuffer)} with + * uninitialized USB context. + */ + @Test(expected = IllegalStateException.class) + public void testGetNextTimeoutWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.getNextTimeout(context, IntBuffer.allocate(1)); + } + + /** + * Tests {@link LibUsb#setPollfdNotifiers(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testSetPollfdNotifiersWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.setPollfdNotifiers(context); + } + + /** + * Tests {@link LibUsb#unsetPollfdNotifiers(Context)} with uninitialized USB + * context. + */ + @Test(expected = IllegalStateException.class) + public void testUnsetPollfdNotifiersWithUninitializedContext() + { + assumeUsbTestsEnabled(); + final Context context = new Context(); + LibUsb.unsetPollfdNotifiers(context); + } + + /** + * Tests the + * {@link LibUsb#setPollfdNotifiers(Context, PollfdListener, Object)} + * method. + */ + @Test + public void testPollFdNotifiers() + { + assumeUsbTestsEnabled(); + PollfdListenerMock listener = new PollfdListenerMock(); + Context context = new Context(); + LibUsb.init(context); + LibUsb.setPollfdNotifiers(context, listener, "test"); + + FileDescriptor fd = new FileDescriptor(); + LibUsb.triggerPollfdAdded(fd, 53); + assertEquals(53, listener.addedEvents); + assertSame(fd, listener.addedFd); + assertSame("test", listener.addedUserData); + assertNull(listener.removedFd); + assertNull(listener.removedUserData); + + listener.reset(); + + fd = new FileDescriptor(); + LibUsb.triggerPollfdRemoved(fd); + assertEquals(0, listener.addedEvents); + assertNull(listener.addedFd); + assertNull(listener.addedUserData); + assertSame(fd, listener.removedFd); + assertSame("test", listener.removedUserData); + + LibUsb.setPollfdNotifiers(context, null, null); + listener.reset(); + + fd = new FileDescriptor(); + LibUsb.triggerPollfdAdded(fd, 53); + assertEquals(0, listener.addedEvents); + assertNull(listener.addedFd); + assertNull(listener.addedUserData); + assertNull(listener.removedFd); + assertNull(listener.removedUserData); + + listener.reset(); + + fd = new FileDescriptor(); + LibUsb.triggerPollfdRemoved(fd); + assertEquals(0, listener.addedEvents); + assertNull(listener.addedFd); + assertNull(listener.addedUserData); + assertNull(listener.removedFd); + assertNull(listener.removedUserData); } } diff --git a/src/test/java/de/ailis/usb4java/libusb/TransferTest.java b/src/test/java/de/ailis/usb4java/libusb/TransferTest.java index 9dd4474..5bf6bde 100644 --- a/src/test/java/de/ailis/usb4java/libusb/TransferTest.java +++ b/src/test/java/de/ailis/usb4java/libusb/TransferTest.java @@ -5,12 +5,13 @@ package de.ailis.usb4java.libusb; -import static de.ailis.usb4java.UsbAssume.assumeUsbTestsEnabled; +import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import java.lang.reflect.Field; @@ -64,6 +65,16 @@ public class TransferTest Transfer transfer = LibUsb.allocTransfer(0); assertNotNull(transfer); LibUsb.freeTransfer(transfer); + + try + { + LibUsb.freeTransfer(transfer); + fail("Double-free should throw IllegalStateException"); + } + catch (IllegalStateException e) + { + // Expected behavior + } } /** @@ -78,7 +89,8 @@ public class TransferTest { try { - Field field = DeviceHandle.class.getDeclaredField("handlePointer"); + Field field = + DeviceHandle.class.getDeclaredField("deviceHandlePointer"); field.setAccessible(true); field.set(handle, pointer); } diff --git a/src/test/java/de/ailis/usb4java/libusb/mocks/PollfdListenerMock.java b/src/test/java/de/ailis/usb4java/libusb/mocks/PollfdListenerMock.java new file mode 100644 index 0000000..f4cd97c --- /dev/null +++ b/src/test/java/de/ailis/usb4java/libusb/mocks/PollfdListenerMock.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2013 Klaus Reimer + * See LICENSE.md for licensing information. + */ +package de.ailis.usb4java.libusb.mocks; + +import java.io.FileDescriptor; + +import de.ailis.usb4java.libusb.PollfdListener; + +/** + * A mocked pollfd listener implementation. + * + * @author Klaus Reimer (k@ailis.de) + */ +public class PollfdListenerMock implements PollfdListener +{ + /** The file descriptor reported by the added event. */ + public FileDescriptor addedFd; + + /** The file descriptor reported by the removed event. */ + public FileDescriptor removedFd; + + /** The events number reported by the added event. */ + public int addedEvents; + + /** The user data reported by the added event. */ + public Object addedUserData; + + /** The user data reported by the removed event. */ + public Object removedUserData; + + @Override + public void pollfdAdded(FileDescriptor fd, int events, Object userData) + { + this.addedEvents = events; + this.addedFd = fd; + this.addedUserData = userData; + } + + @Override + public void pollfdRemoved(FileDescriptor fd, Object userData) + { + this.removedFd = fd; + this.removedUserData = userData; + } + + /** + * Resets the mock object state. + */ + public void reset() + { + this.addedEvents = 0; + this.addedFd = null; + this.addedUserData = null; + this.removedFd = null; + this.removedUserData = null; + } +} diff --git a/src/test/java/de/ailis/usb4java/TCKRunner.java b/src/test/java/de/ailis/usb4java/test/TCKRunner.java similarity index 97% rename from src/test/java/de/ailis/usb4java/TCKRunner.java rename to src/test/java/de/ailis/usb4java/test/TCKRunner.java index 7c487b0..0f37223 100644 --- a/src/test/java/de/ailis/usb4java/TCKRunner.java +++ b/src/test/java/de/ailis/usb4java/test/TCKRunner.java @@ -3,7 +3,7 @@ * See LICENSE.md for licensing information. */ -package de.ailis.usb4java; +package de.ailis.usb4java.test; import org.junit.internal.runners.JUnit38ClassRunner; import org.junit.runner.Description; diff --git a/src/test/java/de/ailis/usb4java/UsbAssume.java b/src/test/java/de/ailis/usb4java/test/UsbAssume.java similarity index 98% rename from src/test/java/de/ailis/usb4java/UsbAssume.java rename to src/test/java/de/ailis/usb4java/test/UsbAssume.java index f54fc59..e53fd32 100644 --- a/src/test/java/de/ailis/usb4java/UsbAssume.java +++ b/src/test/java/de/ailis/usb4java/test/UsbAssume.java @@ -3,7 +3,7 @@ * See LICENSE.md for licensing information. */ -package de.ailis.usb4java; +package de.ailis.usb4java.test; import static org.junit.Assume.assumeTrue; diff --git a/src/test/java/javax/usb/tck/BulkIOErrorConditionsTest.java b/src/test/java/javax/usb/tck/BulkIOErrorConditionsTest.java index 51d9ecd..04d973c 100755 --- a/src/test/java/javax/usb/tck/BulkIOErrorConditionsTest.java +++ b/src/test/java/javax/usb/tck/BulkIOErrorConditionsTest.java @@ -25,7 +25,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; //import javax.usb.util.*; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/BulkIOTests.java b/src/test/java/javax/usb/tck/BulkIOTests.java index e805bdb..452b74c 100755 --- a/src/test/java/javax/usb/tck/BulkIOTests.java +++ b/src/test/java/javax/usb/tck/BulkIOTests.java @@ -15,7 +15,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.*; diff --git a/src/test/java/javax/usb/tck/BulkIOTestwithSynchronizedUsbPipe.java b/src/test/java/javax/usb/tck/BulkIOTestwithSynchronizedUsbPipe.java index bc0f4b9..0fb4d88 100755 --- a/src/test/java/javax/usb/tck/BulkIOTestwithSynchronizedUsbPipe.java +++ b/src/test/java/javax/usb/tck/BulkIOTestwithSynchronizedUsbPipe.java @@ -16,7 +16,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.*; diff --git a/src/test/java/javax/usb/tck/BulkShortPacketIOTests.java b/src/test/java/javax/usb/tck/BulkShortPacketIOTests.java index 2a7cb55..dcfad81 100755 --- a/src/test/java/javax/usb/tck/BulkShortPacketIOTests.java +++ b/src/test/java/javax/usb/tck/BulkShortPacketIOTests.java @@ -15,7 +15,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; diff --git a/src/test/java/javax/usb/tck/ConstantsTest.java b/src/test/java/javax/usb/tck/ConstantsTest.java index 174f54f..1ea2566 100755 --- a/src/test/java/javax/usb/tck/ConstantsTest.java +++ b/src/test/java/javax/usb/tck/ConstantsTest.java @@ -15,7 +15,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.Assert; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/ControlIOTest.java b/src/test/java/javax/usb/tck/ControlIOTest.java index 4104d2c..8dd2b8a 100755 --- a/src/test/java/javax/usb/tck/ControlIOTest.java +++ b/src/test/java/javax/usb/tck/ControlIOTest.java @@ -31,7 +31,7 @@ import javax.usb.event.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; /** * Control I/O Test - Synchronous and asynchronous Control Irp and Control diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditions.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditions.java index c17eca5..398d63a 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditions.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditions.java @@ -16,7 +16,7 @@ import javax.usb.event.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditionswithSynchronizedUsbDevice.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditionswithSynchronizedUsbDevice.java index 30e5ebc..92c7746 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditionswithSynchronizedUsbDevice.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestErrorConditionswithSynchronizedUsbDevice.java @@ -15,7 +15,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRP.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRP.java index e7c79d2..cd91212 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRP.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRP.java @@ -25,7 +25,7 @@ import javax.usb.event.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPList.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPList.java index 44300a6..ab25995 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPList.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPList.java @@ -25,7 +25,7 @@ import javax.usb.event.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPListwithSynchronizedUsbDevice.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPListwithSynchronizedUsbDevice.java index f4140c0..5e4f0f1 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPListwithSynchronizedUsbDevice.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPListwithSynchronizedUsbDevice.java @@ -14,7 +14,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPwithSynchronizedUsbDevice.java b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPwithSynchronizedUsbDevice.java index 3530cfb..3f9e973 100755 --- a/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPwithSynchronizedUsbDevice.java +++ b/src/test/java/javax/usb/tck/DefaultControlPipeTestIRPwithSynchronizedUsbDevice.java @@ -14,7 +14,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/FindControlIODevice.java b/src/test/java/javax/usb/tck/FindControlIODevice.java index ad8b864..7c4f248 100755 --- a/src/test/java/javax/usb/tck/FindControlIODevice.java +++ b/src/test/java/javax/usb/tck/FindControlIODevice.java @@ -27,7 +27,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; /** * FindControlIODevice diff --git a/src/test/java/javax/usb/tck/HotPlugTest.java b/src/test/java/javax/usb/tck/HotPlugTest.java index a23bb86..1e63726 100755 --- a/src/test/java/javax/usb/tck/HotPlugTest.java +++ b/src/test/java/javax/usb/tck/HotPlugTest.java @@ -29,7 +29,7 @@ import javax.usb.util.StandardRequest; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/InterruptIOErrorConditionsTest.java b/src/test/java/javax/usb/tck/InterruptIOErrorConditionsTest.java index f79bded..b077620 100755 --- a/src/test/java/javax/usb/tck/InterruptIOErrorConditionsTest.java +++ b/src/test/java/javax/usb/tck/InterruptIOErrorConditionsTest.java @@ -23,7 +23,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; //import javax.usb.util.*; import junit.framework.TestCase; diff --git a/src/test/java/javax/usb/tck/InterruptIOTests.java b/src/test/java/javax/usb/tck/InterruptIOTests.java index 5520f71..095f493 100755 --- a/src/test/java/javax/usb/tck/InterruptIOTests.java +++ b/src/test/java/javax/usb/tck/InterruptIOTests.java @@ -15,7 +15,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; diff --git a/src/test/java/javax/usb/tck/InterruptIOTestwithSynchronizedUsbPipe.java b/src/test/java/javax/usb/tck/InterruptIOTestwithSynchronizedUsbPipe.java index 7bb959f..79f9012 100755 --- a/src/test/java/javax/usb/tck/InterruptIOTestwithSynchronizedUsbPipe.java +++ b/src/test/java/javax/usb/tck/InterruptIOTestwithSynchronizedUsbPipe.java @@ -16,7 +16,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import junit.framework.*; diff --git a/src/test/java/javax/usb/tck/InterruptShortPacketIOTests.java b/src/test/java/javax/usb/tck/InterruptShortPacketIOTests.java index 405f872..60ff2d2 100755 --- a/src/test/java/javax/usb/tck/InterruptShortPacketIOTests.java +++ b/src/test/java/javax/usb/tck/InterruptShortPacketIOTests.java @@ -15,7 +15,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; diff --git a/src/test/java/javax/usb/tck/IrpTest.java b/src/test/java/javax/usb/tck/IrpTest.java index b090ca4..fca936e 100755 --- a/src/test/java/javax/usb/tck/IrpTest.java +++ b/src/test/java/javax/usb/tck/IrpTest.java @@ -7,7 +7,7 @@ import javax.usb.util.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import java.util.*; diff --git a/src/test/java/javax/usb/tck/SignatureTest.java b/src/test/java/javax/usb/tck/SignatureTest.java index b1f8f6f..552676c 100755 --- a/src/test/java/javax/usb/tck/SignatureTest.java +++ b/src/test/java/javax/usb/tck/SignatureTest.java @@ -13,7 +13,7 @@ import javax.usb.tck.sigtest.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; import java.io.*; import junit.framework.*; diff --git a/src/test/java/javax/usb/tck/UsbInterfacePolicyTest.java b/src/test/java/javax/usb/tck/UsbInterfacePolicyTest.java index c37a012..1869f0c 100755 --- a/src/test/java/javax/usb/tck/UsbInterfacePolicyTest.java +++ b/src/test/java/javax/usb/tck/UsbInterfacePolicyTest.java @@ -28,7 +28,7 @@ import javax.usb.*; import org.junit.runner.RunWith; -import de.ailis.usb4java.TCKRunner; +import de.ailis.usb4java.test.TCKRunner; /** * Usb Interface Policy Test