Delete test code

This commit is contained in:
2016-07-14 08:46:18 +01:00
parent e1f6ffc410
commit 070037110c
10 changed files with 0 additions and 1618 deletions

View File

@@ -1,626 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.ntb.usb.Device;
import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Usb_Config_Descriptor;
import ch.ntb.usb.Usb_Device_Descriptor;
import ch.ntb.usb.Utils;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode;
@SuppressWarnings("deprecation")
public class DeviceTest {
private static final String testdevicePropertiesFile = "testdevice.properties";
private static final String deviceInfoKey = "testdeviceInfo";
private static AbstractDeviceInfo devinfo;
private static byte[] testData;
private static byte[] readData;
private static Device dev;
private static Logger log = Logger.getLogger(DeviceTest.class.getName());
@BeforeClass
public static void setUp() throws Exception {
// load the device info class with the key
// from 'testdevice.properties'
InputStream propInputStream = new FileInputStream(
testdevicePropertiesFile);
Properties devInfoProp = new Properties();
devInfoProp.load(propInputStream);
String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey);
if (devInfoClazzName == null) {
throw new Exception("property " + deviceInfoKey
+ " not found in file " + testdevicePropertiesFile);
}
Class<?> devInfoClazz = Class.forName(devInfoClazzName);
devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance();
// devinfo = new CY7C68013A();
// setup test data
testData = new byte[devinfo.getMaxDataSize()];
readData = new byte[testData.length];
// initialise the device
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct(),
devinfo.getBusName(), devinfo.getFilename());
assertNotNull(dev);
// print the devices
LibusbJava.usb_init();
// LibusbJava.usb_set_debug(255);
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
Utils.logBus(bus, ps);
log.info(baos.toString());
}
@Test
public void getDescriptors() throws Exception {
dev.updateDescriptors();
Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
assertNotNull(devDescriptor);
assertEquals(devinfo.getIdProduct(), devDescriptor.getIdProduct());
assertEquals(devinfo.getIdVendor(), devDescriptor.getIdVendor());
Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
assertNotNull(confDescriptors);
assertTrue(confDescriptors[0].getInterface().length > 0);
}
@Test
public void initalReset() throws Exception {
doOpen();
// this may change the bus and file name of the device
dev.reset();
timeout();
}
@Test(expected = USBException.class)
public void testClose() throws Exception {
try {
// this calls must not throw an exception
doOpen();
doClose();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
}
@Test(expected = USBException.class)
public void testReset1() throws Exception {
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
}
@Test(expected = USBException.class)
public void testReset2() throws Exception {
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device can't be closed
doClose();
}
@Test
public void open() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct());
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithBusName() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), oldBusName, null);
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithFilename() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), null, oldFilename);
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithBusAndFilename() throws Exception {
// get device by busname and filename
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), oldBusName, oldFilename);
doClose();
assertEquals(dev, dev2);
}
@Test
public void bulkWriteRead() throws Exception {
checkBulkEndpoints();
devinfo.setMode(TransferMode.Bulk);
doOpenWriteReadClose();
}
private void checkBulkEndpoints() {
if (devinfo.getInEPBulk() == -1 && devinfo.getOutEPBulk() == -1) {
throw new UnsupportedOperationException(
"no bulk endpoints defined in test device definition");
}
}
@Test
public void interruptWriteRead() throws Exception {
checkInterruptEndpoints();
devinfo.setMode(TransferMode.Interrupt);
doOpenWriteReadClose();
}
private void checkInterruptEndpoints() {
if (devinfo.getInEPInt() == -1 && devinfo.getOutEPInt() == -1) {
throw new UnsupportedOperationException(
"no interrupt endpoints defined in test device definition");
}
}
@Test
public void bulkWriteReadMultiple() throws Exception {
final int NumberOfIterations = 100;
devinfo.setMode(TransferMode.Bulk);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
}
doClose();
}
@Test
public void multipleOpenCloseWithBulkWrite() throws Exception {
devinfo.setMode(TransferMode.Bulk);
for (int i = 0; i < 5; i++) {
doOpen();
doClose();
}
doOpenWriteReadClose();
for (int i = 0; i < 10; i++) {
doOpen();
doWriteRead();
doClose();
}
doOpenWriteReadClose();
for (int i = 0; i < 5; i++) {
doOpen();
doClose();
}
}
@Test
public void bulkAndInterrupt() throws Exception {
doOpen();
// BULK
devinfo.setMode(TransferMode.Bulk);
doWriteRead();
// INTERRUPT
devinfo.setMode(TransferMode.Interrupt);
doWriteRead();
doClose();
}
@Test
public void bulkAndInterruptMultiple() throws Exception {
for (int i = 0; i < 20; i++) {
devinfo.setMode(TransferMode.Bulk);
doOpenWriteReadClose();
devinfo.setMode(TransferMode.Interrupt);
doOpenWriteReadClose();
}
}
@Test
public void controlMsg() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
devinfo.getAltinterface());
// GET STATUS (device)
byte[] data = getTestData(2);
int length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_STATUS, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x01, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET STATUS (interface)
data = getTestData(2);
length = dev.controlMsg(
USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD
| USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_GET_STATUS,
0, 0, data, data.length, devinfo.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x00, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET STATUS (endpoint)
data = getTestData(2);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_ENDPOINT,
USB.REQ_GET_STATUS, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x00, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET CONFIGURATION
data = getTestData(1);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_CONFIGURATION, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) devinfo.getConfiguration(), data[0]);
// // GET INTERFACE
// data = byte[1];
// length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
// | USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_INTERFACE,
// USB.REQ_GET_INTERFACE, 0, devinfo.getInterface(), data,
// data.length,
// devinfo
// .getTimeout(), false);
// logData(data, length);
// GET DESCRIPTOR (device descriptor)
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, 1 << 8, 0, data, data.length,
devinfo.getTimeout(), false);
validateDeviceDescriptor(data, length);
// GET DESCRIPTOR (string descriptor (1))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 1, 0, data, data.length,
devinfo.getTimeout(), false);
String s = getString(data, length);
assertEquals(s, devinfo.getManufacturer());
// GET DESCRIPTOR (string descriptor (2))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 2, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, devinfo.getProduct());
// GET DESCRIPTOR (string descriptor (3))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 3, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, devinfo.getSerialVersion());
// close the device
dev.close();
} catch (Exception e) {
closeOnException();
throw e;
} catch (AssertionError e) {
closeOnException();
throw e;
}
}
private void closeOnException() {
try {
dev.close();
} catch (USBException e1) {
// ignore exceptions
}
}
private void validateDeviceDescriptor(byte[] data, int length) {
// length read
assertEquals(18, length);
// descriptor length
assertEquals((byte) 18, data[0]);
// descriptor type
assertEquals((byte) 1, data[1]);
// USB specification number LSB
assertEquals((byte) 0, data[2]);
// USB specification number MSB
assertEquals((byte) 0x02, data[3]);
// device class (vendor specific)
assertEquals((byte) 0xff, data[4]);
// device subclass (vendor specific)
assertEquals((byte) 0xff, data[5]);
// device protocol (vendor specific)
assertEquals((byte) 0xff, data[6]);
// maximum packet size for endpoint zero
assertEquals((byte) 64, data[7]);
// Vendor ID (NTB) LSB
assertEquals((byte) 0x35, data[8]);
// Vendor ID (NTB) MSB
assertEquals((byte) 0x82, data[9]);
// Product ID (JUnit test board) LSB
assertEquals((byte) 0x22, data[10]);
// Product ID (JUnit test board) MSB
assertEquals((byte) 0x02, data[11]);
// Device release number LSB
assertEquals((byte) 0x00, data[12]);
// Device release number MSB
assertEquals((byte) 0x10, data[13]);
// Index of manufacturer string descriptor
assertEquals((byte) 0x01, data[14]);
// Index of product string descriptor
assertEquals((byte) 0x02, data[15]);
// Index of serial number string descriptor
assertEquals((byte) 0x03, data[16]);
// Number of possible configurations
assertEquals((byte) 0x01, data[17]);
}
private byte[] getTestData(int length) {
byte[] b = new byte[length];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) (Math.random() * 256);
}
return b;
}
@SuppressWarnings("unused")
private void logData(byte[] data, int length) {
if (length > 0) {
log.info("length: " + length);
String logData = "";
for (int i = 0; i < length; i++) {
logData += "0x" + Integer.toHexString(data[i] & 0xff) + "\t";
}
log.info(logData);
}
}
private String getString(byte[] data, int length)
throws UnsupportedEncodingException {
// data length
assertTrue(length > 2);
// string length
assertTrue(data[0] > 2);
// string descriptor ident
assertEquals((byte) 3, data[1]);
// create string from data
return new String(data, 2, length - 2, "UTF-16LE");
}
@Test
public void invalidConfig() throws Exception {
try {
dev.open(devinfo.getConfiguration() + 5, devinfo.getInterface(),
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
log.severe("could not set config "
+ (devinfo.getConfiguration() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidInterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface() + 5,
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
log.severe("could not claim interface "
+ (devinfo.getInterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidAltinterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
devinfo.getAltinterface() + 5);
fail("USBException expected");
} catch (USBException e) {
log.severe("could not set alt interface "
+ (devinfo.getAltinterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void testGetIdProduct() {
Assert.assertEquals(devinfo.getIdProduct(), dev.getIdProduct());
}
@Test
public void testGetIdVendor() {
Assert.assertEquals(devinfo.getIdVendor(), dev.getIdVendor());
}
@Test
public void testGetAltinterface() {
Assert.assertEquals(devinfo.getAltinterface(), dev.getAltinterface());
}
@Test
public void testGetConfiguration() {
Assert.assertEquals(devinfo.getConfiguration(), dev.getConfiguration());
}
@Test
public void testGetInterface() {
Assert.assertEquals(devinfo.getInterface(), dev.getInterface());
}
@Test
public void testGetMaxPacketSize() throws USBException {
doOpen();
Assert.assertEquals(devinfo.getMaxDataSize(), dev.getMaxPacketSize());
doClose();
}
@AfterClass
public static void tearDown() throws Exception {
if (dev != null && dev.isOpen()) {
dev.close();
}
}
private void doOpen() throws USBException {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo
.getAltinterface());
}
private void doClose() throws USBException {
dev.close();
}
private void doOpenWriteReadClose() throws Exception {
doOpen();
doWriteRead();
doClose();
}
private void doWriteRead() throws Exception {
initTestData();
try {
if (devinfo.getMode().equals(TransferMode.Bulk)) {
if (devinfo.getOutEPBulk() != -1) {
dev.writeBulk(devinfo.getOutEPBulk(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPBulk() != -1) {
dev.readBulk(devinfo.getInEPBulk(), readData,
readData.length, devinfo.getTimeout(), false);
}
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
if (devinfo.getOutEPInt() != -1) {
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPInt() != -1) {
dev.readInterrupt(devinfo.getInEPInt(), readData,
readData.length, devinfo.getTimeout(), false);
}
}
if (devinfo.doCompareData()) {
compare(testData, readData);
}
} catch (AssertionError e) {
closeOnException();
throw e;
} catch (Exception e) {
closeOnException();
throw e;
}
}
private static void compare(byte[] d1, byte[] d2) {
int minLength = Math.min(d1.length, d2.length);
for (int i = 0; i < minLength; i++) {
assertEquals(d1[i], d2[i]);
}
}
private static void timeout() {
try {
Thread.sleep(devinfo.getSleepTimeout());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void initTestData() {
for (int i = 0; i < testData.length; i++) {
testData[i] = (byte) (Math.random() * 0xff);
readData[i] = 0;
}
}
}

View File

@@ -1,334 +0,0 @@
package ch.ntb.usb.test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.junit.Test;
import ch.ntb.usb.LibusbJava1;
import ch.ntb.usb.Usb_Device;
import ch.ntb.usb.exceptions.LibusbError;
public class LibusbJava1Test {
/**
* This method does not need to be tested. This test only exists to document
* the fact that this function has not been forgotten.
*/
@Test
public void testLibusb_set_debug() {
}
@Test
public void testLibusb_init() {
try {
long handle = LibusbJava1.libusb_init();
LibusbJava1.libusb_exit(handle);
} catch (LibusbError e) {
fail("Initialization failed, Code: " + e.getErrorString());
}
}
/**
* This method can not be tested (yet). This test only exists to document
* the fact that this function has not been forgotten.
*/
@Test
public void testLibusb_exit() {
}
@Test
public void testLibusb_get_device_list() throws LibusbError {
long handle = LibusbJava1.libusb_init();
Usb_Device devices = LibusbJava1.libusb_get_device_list(handle);
assertNotNull("Got devices", devices);
System.out.println(devices.toString());
assertNull("Bus is null", devices.getBus());
LibusbJava1.libusb_exit(handle);
}
@Test
public void testLibusb_get_bus_number() throws LibusbError {
long handle = LibusbJava1.libusb_init();
Usb_Device devices = LibusbJava1.libusb_get_device_list(handle);
assertNotNull("Got devices", devices);
System.out.println(devices.getBus());
LibusbJava1.libusb_exit(handle);
}
@Test
public void testLibusb_get_max_iso_packet_size() {
fail("Not yet implemented");
}
@Test
public void testLibusb_ref_device() {
fail("Not yet implemented");
}
@Test
public void testLibusb_unref_device() {
fail("Not yet implemented");
}
@Test
public void testLibusb_open() {
fail("Not yet implemented");
}
@Test
public void testLibusb_open_device_with_vid_pid() {
fail("Not yet implemented");
}
@Test
public void testLibusb_close() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_device() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_configuration() {
fail("Not yet implemented");
}
@Test
public void testLibusb_set_configuration() {
fail("Not yet implemented");
}
@Test
public void testLibusb_claim_interface() {
fail("Not yet implemented");
}
@Test
public void testLibusb_release_interface() {
fail("Not yet implemented");
}
@Test
public void testLibusb_set_interface_alt_setting() {
fail("Not yet implemented");
}
@Test
public void testLibusb_clear_halt() {
fail("Not yet implemented");
}
@Test
public void testLibusb_reset_device() {
fail("Not yet implemented");
}
@Test
public void testLibusb_kernel_driver_active() {
fail("Not yet implemented");
}
@Test
public void testLibusb_detach_kernel_driver() {
fail("Not yet implemented");
}
@Test
public void testLibusb_attach_kernel_driver() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_active_config_descriptor() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_string_descriptor_ascii() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_descriptor() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_string_descriptor() {
fail("Not yet implemented");
}
@Test
public void testLibusb_alloc_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_free_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_submit_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_cancel_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_control_transfer_get_data() {
fail("Not yet implemented");
}
@Test
public void testLibusb_control_transfer_get_setup() {
fail("Not yet implemented");
}
@Test
public void testLibusb_fill_control_setup() {
fail("Not yet implemented");
}
@Test
public void testLibusb_fill_control_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_fill_bulk_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_fill_interrupt_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_fill_iso_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_set_iso_packet_lengths() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_iso_packet_buffer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_iso_packet_buffer_simple() {
fail("Not yet implemented");
}
@Test
public void testLibusb_try_lock_events() {
fail("Not yet implemented");
}
@Test
public void testLibusb_lock_events() {
fail("Not yet implemented");
}
@Test
public void testLibusb_unlock_events() {
fail("Not yet implemented");
}
@Test
public void testLibusb_event_handling_ok() {
fail("Not yet implemented");
}
@Test
public void testLibusb_event_handler_active() {
fail("Not yet implemented");
}
@Test
public void testLibusb_lock_event_waiters() {
fail("Not yet implemented");
}
@Test
public void testLibusb_unlock_event_waiters() {
fail("Not yet implemented");
}
@Test
public void testLibusb_wait_for_event() {
fail("Not yet implemented");
}
@Test
public void testLibusb_handle_events_timeout() {
fail("Not yet implemented");
}
@Test
public void testLibusb_handle_events() {
fail("Not yet implemented");
}
@Test
public void testLibusb_handle_events_locked() {
fail("Not yet implemented");
}
@Test
public void testLibusb_pollfds_handle_timeouts() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_next_timeout() {
fail("Not yet implemented");
}
@Test
public void testLibusb_set_pollfd_notifiers() {
fail("Not yet implemented");
}
@Test
public void testLibusb_get_pollfds() {
fail("Not yet implemented");
}
@Test
public void testLibusb_control_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_bulk_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_interrupt_transfer() {
fail("Not yet implemented");
}
@Test
public void testLibusb_strerror() {
fail("Not yet implemented");
}
@Test
public void testIsHandleValid() {
fail("Not yet implemented");
}
}

View File

@@ -1,199 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2008 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.ntb.usb.Device;
import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.USB;
import ch.ntb.usb.USBException;
import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Utils;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode;
@SuppressWarnings("deprecation")
public class MemoryLeakTest {
private static final String testdevicePropertiesFile = "testdevice.properties";
private static final String deviceInfoKey = "testdeviceInfo";
private static AbstractDeviceInfo devinfo;
private static byte[] testData;
private static byte[] readData;
private static Device dev;
private static Logger log = Logger
.getLogger(MemoryLeakTest.class.getName());
@BeforeClass
public static void setUp() throws Exception {
// load the device info class with the key
// from 'testdevice.properties'
InputStream propInputStream = new FileInputStream(
testdevicePropertiesFile);
Properties devInfoProp = new Properties();
devInfoProp.load(propInputStream);
String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey);
if (devInfoClazzName == null) {
throw new Exception("property " + deviceInfoKey
+ " not found in file " + testdevicePropertiesFile);
}
Class<?> devInfoClazz = Class.forName(devInfoClazzName);
devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance();
// setup test data
testData = new byte[devinfo.getMaxDataSize()];
readData = new byte[testData.length];
// initialise the device
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
assertNotNull(dev);
// print the devices
LibusbJava.usb_init();
// LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
Usb_Bus bus = LibusbJava.usb_get_busses();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
Utils.logBus(bus, ps);
log.info(baos.toString());
}
@Test
public void initalReset() throws Exception {
doOpen();
dev.reset();
timeout();
}
@Test
public void bulkWriteReadMultiple() throws Exception {
final int NumberOfIterations = 3000;
devinfo.setMode(TransferMode.Bulk);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
if (i % 1000 == 0) {
System.out.print(".");
}
}
doClose();
}
@Test
public void interruptWriteReadMultiple() throws Exception {
final int NumberOfIterations = 3000;
devinfo.setMode(TransferMode.Interrupt);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
if (i % 1000 == 0) {
System.out.print(".");
}
}
doClose();
}
private void closeOnException() {
try {
dev.close();
} catch (USBException e1) {
// ignore exceptions
}
}
@AfterClass
public static void tearDown() throws Exception {
if (dev != null && dev.isOpen()) {
dev.close();
}
}
private void doOpen() throws USBException {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo
.getAltinterface());
}
private void doClose() throws USBException {
dev.close();
}
private void doWriteRead() throws Exception {
initTestData();
try {
if (devinfo.getMode().equals(TransferMode.Bulk)) {
if (devinfo.getOutEPBulk() != -1) {
dev.writeBulk(devinfo.getOutEPBulk(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPBulk() != -1) {
dev.readBulk(devinfo.getInEPBulk(), readData,
readData.length, devinfo.getTimeout(), false);
}
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
if (devinfo.getOutEPInt() != -1) {
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
testData.length, devinfo.getTimeout(), false);
}
if (devinfo.getInEPInt() != -1) {
dev.readInterrupt(devinfo.getInEPInt(), readData,
readData.length, devinfo.getTimeout(), false);
}
}
if (devinfo.doCompareData()) {
compare(testData, readData);
}
} catch (AssertionError e) {
closeOnException();
throw e;
} catch (Exception e) {
closeOnException();
throw e;
}
}
private static void compare(byte[] d1, byte[] d2) {
final int minLength = Math.min(d1.length, d2.length);
for (int i = 0; i < minLength; i++) {
assertEquals(d1[i], d2[i]);
}
}
private static void timeout() {
try {
Thread.sleep(devinfo.getSleepTimeout());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void initTestData() {
for (int i = 0; i < testData.length; i++) {
testData[i] = (byte) (Math.random() * 0xff);
readData[i] = 0;
}
}
}

View File

@@ -1,239 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2007 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test;
import ch.ntb.usb.LibusbJava;
import ch.ntb.usb.Usb_Bus;
import ch.ntb.usb.Usb_Config_Descriptor;
import ch.ntb.usb.Usb_Device;
import ch.ntb.usb.Usb_Endpoint_Descriptor;
import ch.ntb.usb.Usb_Interface;
import ch.ntb.usb.Usb_Interface_Descriptor;
/**
* This class replicates the code from testlibusb.c supplied in the
* libusb-0.1.12 release.
*/
@SuppressWarnings("deprecation")
public class TestLibUsbJava {
static boolean verbose;
/**
* prints out endpoint info
*
* @param endpoint
* The end point.
*/
private static void printEndpoint(Usb_Endpoint_Descriptor endpoint) {
System.out.print(String.format(" bEndpointAddress: %02xh\n",
endpoint.getBEndpointAddress()));
System.out.print(String.format(" bmAttributes: %02xh\n",
endpoint.getBmAttributes()));
System.out.print(String.format(" wMaxPacketSize: %d\n", endpoint
.getWMaxPacketSize()));
System.out.print(String.format(" bInterval: %d\n", endpoint
.getBInterval()));
System.out.print(String.format(" bRefresh: %d\n", endpoint
.getBRefresh()));
System.out.print(String.format(" bSynchAddress: %d\n", endpoint
.getBSynchAddress()));
}
/**
* prints out the interface descriptor
*
* @param interfaceDescript
* The interface descriptor.
*/
private static void printAltsetting(
Usb_Interface_Descriptor interfaceDescript) {
System.out.print(String.format(" bInterfaceNumber: %d\n",
interfaceDescript.getBInterfaceNumber()));
System.out.print(String.format(" bAlternateSetting: %d\n",
interfaceDescript.getBAlternateSetting()));
System.out.print(String.format(" bNumEndpoints: %d\n",
interfaceDescript.getBNumEndpoints()));
System.out.print(String.format(" bInterfaceClass: %d\n",
interfaceDescript.getBInterfaceClass()));
System.out.print(String.format(" bInterfaceSubClass: %d\n",
interfaceDescript.getBInterfaceSubClass()));
System.out.print(String.format(" bInterfaceProtocol: %d\n",
interfaceDescript.getBInterfaceProtocol()));
System.out.print(String.format(" iInterface: %d\n",
interfaceDescript.getIInterface()));
for (int i = 0; i < interfaceDescript.getBNumEndpoints(); i++) {
printEndpoint(interfaceDescript.getEndpoint()[i]);
}
}
/**
* prints out interface
*
* @param usbInterface
* The interface.
*/
private static void printInterface(Usb_Interface usbInterface) {
for (int i = 0; i < usbInterface.getNumAltsetting(); i++) {
printAltsetting(usbInterface.getAltsetting()[i]);
}
}
/**
* prints out configuration
*
* @param config
* The configuration.
*/
private static void printConfiguration(Usb_Config_Descriptor config) {
System.out.print(String.format(" wTotalLength: %d\n", config
.getWTotalLength()));
System.out.print(String.format(" bNumInterfaces: %d\n", config
.getBNumInterfaces()));
System.out.print(String.format(" bConfigurationValue: %d\n", config
.getBConfigurationValue()));
System.out.print(String.format(" iConfiguration: %d\n", config
.getIConfiguration()));
System.out.print(String.format(" bmAttributes: %02xh\n",
config.getBmAttributes()));
System.out.print(String.format(" MaxPower: %d\n", config
.getMaxPower()));
for (int i = 0; i < config.getBNumInterfaces(); i++) {
printInterface(config.getInterface()[i]);
}
}
private static int printDevice(Usb_Device dev, int level) {
long udev;
String mfr;
String product;
String sn;
String spaces;
String descript;
spaces = " ";
udev = LibusbJava.usb_open(dev);
if (udev != 0) {
if (dev.getDescriptor().getIManufacturer() != 0) {
mfr = LibusbJava.usb_get_string_simple(udev, dev
.getDescriptor().getIManufacturer());
if (mfr != null) {
descript = String.format("%s - ", mfr);
} else {
descript = String.format("%04X - ", dev.getDescriptor()
.getIdVendor());
}
} else {
descript = String.format("%04X - ", dev.getDescriptor()
.getIdVendor());
}
if (dev.getDescriptor().getIProduct() != 0) {
product = LibusbJava.usb_get_string_simple(udev, dev
.getDescriptor().getIProduct());
if (product != null) {
descript = descript + String.format("%s", product);
} else {
descript = descript
+ String.format("%04X", dev.getDescriptor()
.getIdProduct());
}
} else {
descript = descript
+ String.format("%04X", dev.getDescriptor()
.getIdProduct());
}
} else {
descript = String.format("%04X - %04X", dev.getDescriptor()
.getIdVendor(), dev.getDescriptor().getIdProduct());
}
System.out.print(String.format("%sDev #%d: %s\n", spaces.substring(0,
level * 2), dev.getDevnum(), descript));
if ((udev != 0) && verbose) {
if (dev.getDescriptor().getISerialNumber() != 0) {
sn = LibusbJava.usb_get_string_simple(udev, dev.getDescriptor()
.getISerialNumber());
if (sn != null) {
System.out.print(String.format("%s - Serial Number: %s\n",
spaces.substring(0, level * 2), sn));
}
}
}
if (udev != 0) {
LibusbJava.usb_close(udev);
}
if (verbose) {
if (dev.getConfig().length == 0) {
System.out.print(" Couldn't retrieve descriptors\n");
return 0;
}
for (int i = 0; i < dev.getDescriptor().getBNumConfigurations(); i++) {
printConfiguration(dev.getConfig()[i]);
}
} else {
Usb_Device childDev = null;
for (int i = 0; i < dev.getNumChildren(); i++) {
if (i == 0) {
childDev = dev.getChildren();
} else {
childDev = childDev.getNext();
}
printDevice(childDev, level + 1);
}
}
return 0;
} // end of printDevice method
/**
* The main method.
*
* @param args
* The command line arguments.
*/
public static void main(String args[]) throws Exception {
if ((args.length > 0) && (args[0].equals("-v"))) {
verbose = true;
} else {
verbose = false;
}
// used for debugging. 0 = no debugging, 255 = with debugging
LibusbJava.usb_init();
LibusbJava.usb_set_debug(255);
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
for (Usb_Bus bus = LibusbJava.usb_get_busses(); bus != null; bus = bus
.getNext()) {
if ((bus.getRootDev() != null) && !verbose) {
printDevice(bus.getRootDev(), 0);
} else {
for (Usb_Device dev = bus.getDevices(); dev != null; dev = dev
.getNext()) {
printDevice(dev, 0);
}
}
}
} // end main
} // end of TestLibUsbJava class

View File

@@ -1,34 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test.devices;
import ch.ntb.usb.USB;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
public class AT90USB1287 extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x8235);
setIdProduct((short) 0x0200);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(-1);
setOutEPBulk(0x01);
setInEPBulk(0x82);
setOutEPInt(0x01);
setInEPInt(0x82);
setSleepTimeout(2000);
setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
setManufacturer("inf.ntb.ch");
setProduct("JUnit Test Board");
setSerialVersion("00.10.00");
}
}

View File

@@ -1,31 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test.devices;
import ch.ntb.usb.USB;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
public class CY7C68013A extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x8235);
setIdProduct((short) 0x0222);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(-1);
setOutEPInt(0x02);
setInEPInt(0x86);
setOutEPBulk(0x04);
setInEPBulk(0x88);
setSleepTimeout(2000);
setMaxDataSize(USB.HIGHSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
}
}

View File

@@ -1,32 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test.devices;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
public class MX500 extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x046d);
setIdProduct((short) 0xc025);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(0);
setOutEPInt(-1);
setInEPInt(0x81);
setOutEPBulk(-1);
setInEPBulk(-1);
setSleepTimeout(5000);
setMaxDataSize(5);
setMode(TransferMode.Interrupt);
// we only read data -> don't compare
setDoCompareData(false);
}
}

View File

@@ -1,34 +0,0 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.usb.test.devices;
import ch.ntb.usb.testApp.AbstractDeviceInfo;
public class MousePlus extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x046d);
setIdProduct((short) 0xc016);
setBusName("bus-0");
setFilename("\\\\.\\libusb0-0001--0x046d-0xc016");
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(0);
setOutEPInt(-1);
setInEPInt(0x81);
setOutEPBulk(-1);
setInEPBulk(-1);
setSleepTimeout(5000);
setMaxDataSize(4);
setMode(TransferMode.Interrupt);
// we only read data -> don't compare
setDoCompareData(false);
}
}

View File

@@ -1,76 +0,0 @@
package ch.ntb.usb.test.exceptions;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import ch.ntb.usb.exceptions.LibusbError;
@RunWith(Parameterized.class)
public class LibusbErrorTest {
@Parameters
public static Collection<Object[]> codesToTest() {
return Arrays.asList(new Object[][] { {"ERROR_UNKNWON (0)", 0 },
{"ERROR_IO", -1 },
{"ERROR_INVALID_PARAM", -2},
{"ERROR_ACCESS", -3},
{"ERROR_NO_DEVICE", -4},
{"ERROR_NOT_FOUND", -5},
{"ERROR_BUSY", -6},
{"ERROR_TIMEOUT", -7},
{"ERROR_OVERFLOW", -8},
{"ERROR_PIPE", -9},
{"ERROR_INTERRUPTED", -10},
{"ERROR_NO_MEM", -11},
{"ERROR_NOT_SUPPORTED", -12},
{"ERROR_UNKNWON (-13)", -13},
{"ERROR_UNKNWON (-98)", -98},
{"ERROR_JAVA_REFERENCES_NOT_LOADED", -100},
{"ERROR_JAVA_WRONG_ENVIRONMENT", -101},
{"ERROR_JAVA_ILEGAL_DEVICE_HANDLE", -102},
{"ERROR_UNKNWON (-103)", -103},
{"ERROR_OTHER", -99} });
}
private String exp_desc;
private int code;
private LibusbError e;
public LibusbErrorTest(String exp_desc, int code) {
this.exp_desc = exp_desc;
this.code = code;
try {
throw new LibusbError(code);
} catch (LibusbError e) {
this.e = e;
}
}
@Test
public void testGetErrorCode() {
assertEquals("Error code is correct", e.getErrorCode(), code);
}
@Test
public void testGetStringFromCode() {
String gen_desc = LibusbError.getStringFromCode(code);
assertEquals("Correct error description for " + code, exp_desc, gen_desc);
}
@Test
public void testGetErrorString() {
assertEquals("Correct error string for " + code, e.getErrorString(), exp_desc);
}
@Test
public void testGetMessage() {
assertEquals("Correct error string for " + code, e.getMessage(), "libusb result: " + exp_desc);
}
}

View File

@@ -1,13 +0,0 @@
################################################################
# define the usb test class for test/ch.ntb.inf.libusbJava.DeviceTest here
# it must implement ch.ntb.inf.libusbJava.AbstractDeviceInfo
################################################################
# Atmel AVR AT90USB1287
testdeviceInfo=ch.ntb.inf.libusbJava.test.devices.AT90USB1287
# Cypress FX2 CY7C68013A
#testdeviceInfo=ch.ntb.inf.libusbJava.test.devices.CY7C68013A
# MX500 Mouse
#testdeviceInfo=ch.ntb.inf.libusbJava.test.devices.MX500
# Logitech Notebook Mouse Plus
#testdeviceInfo=ch.ntb.inf.libusbJava.test.devices.MousePlus