Add unit test for DeviceDescriptor and catch uninitialized usage of it.

This commit is contained in:
Klaus Reimer 2013-07-13 19:50:28 +02:00
parent 6f11a8b365
commit c0efcdf706
4 changed files with 263 additions and 18 deletions

View File

@ -9,13 +9,14 @@ void setDeviceDescriptor(JNIEnv* env,
struct libusb_device_descriptor* descriptor, jobject object)
{
SET_DATA(env, descriptor, sizeof(struct libusb_device_descriptor),
object, "data");
object, "deviceDescriptorData");
}
struct libusb_device_descriptor* unwrapDeviceDescriptor(JNIEnv* env,
jobject descriptor)
{
UNWRAP_DATA(env, descriptor, struct libusb_device_descriptor*, "data");
UNWRAP_DATA(env, descriptor, struct libusb_device_descriptor*,
"deviceDescriptorData");
}
/**
@ -26,7 +27,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bLength)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bLength;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
}
/**
@ -37,7 +41,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDescriptorType)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bDescriptorType;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
}
/**
@ -48,7 +55,10 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, bcdUSB)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bcdUSB;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bcdUSB;
}
/**
@ -59,7 +69,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceClass)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bDeviceClass;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceClass;
}
/**
@ -70,7 +83,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceSubClass)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bDeviceSubClass;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceSubClass;
}
/**
@ -81,7 +97,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceProtocol)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bDeviceProtocol;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceProtocol;
}
/**
@ -92,7 +111,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bMaxPacketSize0)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bMaxPacketSize0;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bMaxPacketSize0;
}
/**
@ -103,7 +125,10 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, idVendor)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->idVendor;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->idVendor;
}
/**
@ -114,7 +139,10 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, idProduct)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->idProduct;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->idProduct;
}
/**
@ -125,7 +153,10 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, bcdDevice)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bcdDevice;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bcdDevice;
}
@ -137,7 +168,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iManufacturer)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->iManufacturer;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iManufacturer;
}
/**
@ -148,7 +182,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iProduct)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->iProduct;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iProduct;
}
/**
@ -157,7 +194,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iProduct)
JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iSerialNumber)
(JNIEnv *env, jobject this)
{
return unwrapDeviceDescriptor(env, this)->iSerialNumber;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iSerialNumber;
}
/**
@ -168,5 +208,8 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bNumConfigurations)
JNIEnv *env, jobject this
)
{
return unwrapDeviceDescriptor(env, this)->bNumConfigurations;
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumConfigurations;
}

View File

@ -71,6 +71,11 @@
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, \
"Ljava/nio/ByteBuffer;"); \
jobject buffer = (*ENV)->GetObjectField(ENV, OBJECT, field); \
if (!buffer) \
{ \
illegalState(ENV, FIELD" is not initialized"); \
return NULL; \
} \
return (TYPE) (*ENV)->GetDirectBufferAddress(ENV, buffer); \
}

View File

@ -31,7 +31,7 @@ import de.ailis.usb4java.utils.DescriptorUtils;
public final class DeviceDescriptor implements UsbDeviceDescriptor
{
/** The native data of the descriptor structure. */
private ByteBuffer data;
private ByteBuffer deviceDescriptorData;
/**
* Constructs a new device descriptor which can be passed to the
@ -49,7 +49,7 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
*/
public ByteBuffer getData()
{
return this.data;
return this.deviceDescriptorData;
}
@Override

View File

@ -0,0 +1,197 @@
/*
* Copyright (C) 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
package de.ailis.usb4java.libusb;
import static de.ailis.usb4java.test.UsbAssume.assumeUsbTestsEnabled;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Tests the {@link DeviceDescriptor} class.
*
* @author Klaus Reimer (k@ailis.de)
*/
public class DeviceDescriptorTest
{
/** The test subject. */
private DeviceDescriptor descriptor;
/**
* Setup test.
*/
@Before
public void setUp()
{
assumeUsbTestsEnabled();
LibUsb.init(null);
this.descriptor = new DeviceDescriptor();
}
/**
* Tear down test.
*/
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
{
assumeUsbTestsEnabled();
this.descriptor.bLength();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDescriptorType()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorType()
{
assumeUsbTestsEnabled();
this.descriptor.bDescriptorType();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bcdUSB()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedBcdUSB()
{
assumeUsbTestsEnabled();
this.descriptor.bcdUSB();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceClass()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceClass()
{
assumeUsbTestsEnabled();
this.descriptor.bDeviceClass();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceSubClass()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceSubClass()
{
assumeUsbTestsEnabled();
this.descriptor.bDeviceSubClass();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceProtocol()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceProtocol()
{
assumeUsbTestsEnabled();
this.descriptor.bDeviceProtocol();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bMaxPacketSize0()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedMaxPacketSize0()
{
assumeUsbTestsEnabled();
this.descriptor.bMaxPacketSize0();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#idVendor()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedVendor()
{
assumeUsbTestsEnabled();
this.descriptor.idVendor();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#idProduct()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedProduct()
{
assumeUsbTestsEnabled();
this.descriptor.idProduct();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bcdDevice()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedBcdDevice()
{
assumeUsbTestsEnabled();
this.descriptor.bcdDevice();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iManufacturer()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedManufacturer()
{
assumeUsbTestsEnabled();
this.descriptor.iManufacturer();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iProduct()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedIProduct()
{
assumeUsbTestsEnabled();
this.descriptor.iProduct();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iSerialNumber()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedSerialNumber()
{
assumeUsbTestsEnabled();
this.descriptor.iSerialNumber();
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bNumConfigurations()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedNumConfigurations()
{
assumeUsbTestsEnabled();
this.descriptor.bNumConfigurations();
}
}