Add unit test for ConfigDescriptor and catch uninitialized usage of it

This commit is contained in:
Klaus Reimer 2013-07-13 18:20:24 +02:00
parent 7ad0eef2bc
commit 0d2a6d3d49
2 changed files with 204 additions and 10 deletions

View File

@ -15,7 +15,8 @@ void setConfigDescriptor(JNIEnv* env,
struct libusb_config_descriptor* unwrapConfigDescriptor(JNIEnv* env,
jobject descriptor)
{
UNWRAP_POINTER(env, descriptor, struct libusb_config_descriptor*, "configDescriptorPointer");
UNWRAP_POINTER(env, descriptor, struct libusb_config_descriptor*,
"configDescriptorPointer");
}
void resetConfigDescriptor(JNIEnv* env, jobject obj)
@ -31,7 +32,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bLength)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->bLength;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
}
/**
@ -42,7 +46,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bDescriptorType)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->bDescriptorType;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
}
/**
@ -53,7 +60,10 @@ JNIEXPORT jshort JNICALL METHOD_NAME(ConfigDescriptor, wTotalLength)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->wTotalLength;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wTotalLength;
}
/**
@ -64,7 +74,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bNumInterfaces)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->bNumInterfaces;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumInterfaces;
}
/**
@ -75,7 +88,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bConfigurationValue)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->bConfigurationValue;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bConfigurationValue;
}
/**
@ -86,7 +102,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, iConfiguration)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->iConfiguration;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iConfiguration;
}
/**
@ -97,7 +116,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bmAttributes)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->bmAttributes;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
}
/**
@ -108,7 +130,10 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bMaxPower)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->MaxPower;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->MaxPower;
}
/**
@ -121,6 +146,7 @@ JNIEXPORT jobjectArray JNICALL METHOD_NAME(ConfigDescriptor, iface)
{
struct libusb_config_descriptor *descriptor = unwrapConfigDescriptor(
env, this);
if (!descriptor) return NULL;
return wrapInterfaces(env, descriptor->bNumInterfaces,
descriptor->interface);
}
@ -135,6 +161,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(ConfigDescriptor, extra)
{
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) descriptor->extra,
descriptor->extra_length);
}
@ -147,5 +174,8 @@ JNIEXPORT jint JNICALL METHOD_NAME(ConfigDescriptor, extraLength)
JNIEnv *env, jobject this
)
{
return unwrapConfigDescriptor(env, this)->extra_length;
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->extra_length;
}

View File

@ -0,0 +1,164 @@
/*
* 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 ConfigDescriptor} class.
*
* @author Klaus Reimer (k@ailis.de)
*/
public class ConfigDescriptorTest
{
/** The test subject. */
private ConfigDescriptor descriptor;
/**
* Setup test.
*/
@Before
public void setUp()
{
assumeUsbTestsEnabled();
LibUsb.init(null);
this.descriptor = new ConfigDescriptor();
}
/**
* Tear down test.
*/
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
{
assumeUsbTestsEnabled();
this.descriptor.bLength();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bDescriptorType()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorType()
{
assumeUsbTestsEnabled();
this.descriptor.bDescriptorType();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#wTotalLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedTotalLength()
{
assumeUsbTestsEnabled();
this.descriptor.wTotalLength();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bNumInterfaces()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedNumInterfaces()
{
assumeUsbTestsEnabled();
this.descriptor.bNumInterfaces();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bConfigurationValue()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedConfigurationValue()
{
assumeUsbTestsEnabled();
this.descriptor.bConfigurationValue();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#iConfiguration()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedConfiguration()
{
assumeUsbTestsEnabled();
this.descriptor.iConfiguration();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bmAttributes()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorAttributes()
{
assumeUsbTestsEnabled();
this.descriptor.bmAttributes();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bMaxPower()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorMaxPower()
{
assumeUsbTestsEnabled();
this.descriptor.bMaxPower();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#iface()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorIface()
{
assumeUsbTestsEnabled();
this.descriptor.iface();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#extra()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorExtra()
{
assumeUsbTestsEnabled();
this.descriptor.extra();
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#extraLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorExtraLength()
{
assumeUsbTestsEnabled();
this.descriptor.extraLength();
}
}