From 07eab220ce29d869545e2943d9c9557d98e03d64 Mon Sep 17 00:00:00 2001 From: schlaepfer Date: Wed, 6 Dec 2006 11:20:45 +0000 Subject: [PATCH] - release resources and clear device handle - tests for invalid parameters git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@227 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c --- java/src/ch/ntb/usb/Device.java | 9 +++ java/test/ch/ntb/usb/test/DeviceTest.java | 83 ++++++++++++++++++++--- 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/java/src/ch/ntb/usb/Device.java b/java/src/ch/ntb/usb/Device.java index f31865f..96436db 100644 --- a/java/src/ch/ntb/usb/Device.java +++ b/java/src/ch/ntb/usb/Device.java @@ -241,6 +241,7 @@ public class Device { if (usbDevHandle <= 0) { throw new USBException("invalid device handle"); } + release_interface(usbDevHandle, dev_interface); if (LibusbJava.usb_reset(usbDevHandle) < 0) { usbDevHandle = 0; throw new USBException("LibusbWin.usb_reset: " @@ -511,15 +512,23 @@ public class Device { private void claim_interface(int usb_dev_handle, int configuration, int interface_, int altinterface) throws USBException { if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) { + usbDevHandle = 0; throw new USBException("LibusbWin.usb_set_configuration: " + LibusbJava.usb_strerror()); } if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) { + usbDevHandle = 0; throw new USBException("LibusbWin.usb_claim_interface: " + LibusbJava.usb_strerror()); } if (altinterface >= 0) { if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) { + try { + release_interface(usb_dev_handle, interface_); + } catch (USBException e) { + // ignore + } + usbDevHandle = 0; throw new USBException("LibusbWin.usb_set_altinterface: " + LibusbJava.usb_strerror()); } diff --git a/java/test/ch/ntb/usb/test/DeviceTest.java b/java/test/ch/ntb/usb/test/DeviceTest.java index ec0dc63..811b1db 100644 --- a/java/test/ch/ntb/usb/test/DeviceTest.java +++ b/java/test/ch/ntb/usb/test/DeviceTest.java @@ -1,6 +1,6 @@ /* * Java libusb wrapper - * Copyright (c) 2005-2006 Andreas Schläpfer + * Copyright (c) 2005-2006 Andreas Schl�pfer * * This library is covered by the LGPL, read LGPL.txt for details. */ @@ -8,6 +8,7 @@ package ch.ntb.usb.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.FileInputStream; import java.io.InputStream; @@ -88,8 +89,14 @@ public class DeviceTest { @Test(expected = USBException.class) public void testClose() throws Exception { - doOpen(); - doClose(); + 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); @@ -97,9 +104,15 @@ public class DeviceTest { @Test(expected = USBException.class) public void testReset1() throws Exception { - doOpen(); - dev.reset(); - timeout(); + 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); @@ -107,9 +120,15 @@ public class DeviceTest { @Test(expected = USBException.class) public void testReset2() throws Exception { - doOpen(); - dev.reset(); - timeout(); + 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(); } @@ -182,6 +201,48 @@ public class DeviceTest { } } + @Test + public void invalidConfig() throws Exception { + try { + dev.open(devinfo.getConfiguration() + 5, devinfo.getInterface(), + devinfo.getAltinterface()); + fail("USBException expected"); + } catch (USBException e) { + System.err.println("INFO: " + getClass() + + ": error expected: 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) { + System.err.println("INFO: " + getClass() + + ": error expected: 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) { + System.err.println("INFO: " + getClass() + + ": error expected: could not set alt interface " + + (devinfo.getAltinterface() + 5)); + } + doOpenWriteReadClose(); + } + @Test public void testGetIdProduct() { Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct()); @@ -247,8 +308,8 @@ public class DeviceTest { } else if (devinfo.getMode().equals(TransferMode.Interrupt)) { dev.writeInterrupt(devinfo.getOutEPInt(), testData, testData.length, devinfo.getTimeout(), false); - dev.readInterrupt(devinfo.getInEPInt(), readData, - readData.length, devinfo.getTimeout(), false); + dev.readInterrupt(devinfo.getInEPInt(), readData, readData.length, + devinfo.getTimeout(), false); } compare(testData, readData); }