package javax.usb.tck; import javax.usb.*; import junit.framework.TestCase; import junit.framework.Assert; import javax.usb.util.*; import java.util.*; /** * Copyright (c) 2004, International Business Machines Corporation. * All Rights Reserved. * * This software is provided and licensed under the terms and conditions * of the Common Public License: * http://oss.software.ibm.com/developerworks/opensource/license-cpl.html */ /* * Change Activity: See below. * * FLAG REASON RELEASE DATE WHO DESCRIPTION * ---- -------- -------- ------ ------- ------------------------------------ * 0000 nnnnnnn yymmdd Initial Development * $P1 tck.rel1 040804 raulortz Support for UsbDisconnectedException * $P2 tck.rel1 041222 raulortz Redesign TCK to create base and optional * tests. Separate setConfig, setInterface * and isochronous transfers as optionals. */ /** * IRP Test * * This test verifies the creation of IRP's and Control IRP's from the * UsbDevice, UsbPipe, UsbDefaultIrp and UsbDefaultControlIrp classes. *@author Vinitha Modepalle */ @SuppressWarnings("all") public class IrpTest extends TestCase { public void testUsbIrpwithPipeCreate() { try { //get Pipe UsbPipe inPipe = usbPipecreate(); Assert.assertTrue("input pipe is not active!!!", inPipe.isActive()); Assert.assertFalse("input pipe isOpen() should == false): ", inPipe.isOpen()); inPipe.open(); Assert.assertTrue("input pipe isOpen() should == true): ", inPipe.isOpen()); UsbIrp usbIrp = inPipe.createUsbIrp(); Assert.assertTrue( "Create UsbInPipe Irp Failed! Got a null instance", usbIrp != null ); byte[] data = new byte[0]; int offset = 0; int length = 0; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; performIrpTests(usbIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbIrp); inPipe.close(); FindProgrammableDevice.getInstance().getProgrammableDevice().getUsbConfiguration((byte)1).getUsbInterface((byte)0).release(); } catch ( UsbClaimException uce ) { fail("UsbClaimException: " + uce); } catch ( UsbNotActiveException unae ) { fail("UsbNotActiveException: " + unae); } catch ( UsbException ue ) { fail("UsbException: " + ue); } catch ( UsbDisconnectedException uDE ) // @P1C { // @P1A fail ("A connected device should't throw the UsbDisconnectedException!"); // @P1A } // @P1A catch ( Exception e ) { fail("Exception: " + e); } } public void testUsbControlIrpwithPipeCreate() { byte[] data = new byte[0]; int offset = 0; int length = 0; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; byte bmRequestType =UsbConst.REQUESTTYPE_DIRECTION_OUT |UsbConst.REQUESTTYPE_TYPE_STANDARD |UsbConst.REQUESTTYPE_RECIPIENT_DEVICE; byte bRequest = UsbConst.REQUEST_GET_DESCRIPTOR; short wValue = UsbConst.DESCRIPTOR_TYPE_DEVICE << 8; short wIndex = 5; try { UsbPipe inPipe = usbPipecreate(); Assert.assertTrue("input pipe is not active!!!", inPipe.isActive()); Assert.assertFalse("input pipe isOpen() should == false: ", inPipe.isOpen()); inPipe.open(); Assert.assertTrue("input pipe isOpen() should == true: ", inPipe.isOpen()); UsbControlIrp usbControlIrp = inPipe.createUsbControlIrp(bmRequestType, bRequest, wValue, wIndex); Assert.assertTrue( "Create UsbInPipe Control Irp Failed! Got a null instance", usbControlIrp != null ); verifyControlIrpValues(usbControlIrp, bmRequestType, bRequest, wValue, wIndex); performIrpTests(usbControlIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbControlIrp); inPipe.close(); FindProgrammableDevice.getInstance().getProgrammableDevice().getUsbConfiguration((byte)1).getUsbInterface((byte)0).release(); } catch ( UsbClaimException uce ) { fail("UsbClaimException: " + uce); } catch ( UsbNotActiveException unae ) { fail("UsbNotActiveException: " + unae); } catch ( UsbException ue ) { fail("UsbException: " + ue); } catch ( UsbDisconnectedException uDE ) // @P1C { // @P1A fail ("A connected device should't throw the UsbDisconnectedException!"); // @P1A } // @P1A catch ( Exception e ) { fail("Exception: " + e); } } public void testUsbControlIrpwithDeviceCreate() { byte[] data = new byte[0]; int offset = 0; int length = 0; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; byte bmRequestType =UsbConst.REQUESTTYPE_DIRECTION_OUT |UsbConst.REQUESTTYPE_TYPE_STANDARD |UsbConst.REQUESTTYPE_RECIPIENT_DEVICE; byte bRequest = UsbConst.REQUEST_GET_DESCRIPTOR; short wValue = UsbConst.DESCRIPTOR_TYPE_DEVICE << 8; short wIndex = 5; UsbDevice usbDevice = FindProgrammableDevice.getInstance().getProgrammableDevice(); Assert.assertTrue( "Find Programmable board Failed! Got a null instance", usbDevice != null ); UsbControlIrp usbControlIrp = usbDevice.createUsbControlIrp(bmRequestType,bRequest, wValue, wIndex); Assert.assertTrue( "Create UsbDevice Control Irp Failed! Got a null instance", usbControlIrp != null ); verifyControlIrpValues(usbControlIrp, bmRequestType, bRequest, wValue, wIndex); performIrpTests(usbControlIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbControlIrp); } public void testUsbIrpwithNoParmsConstructor() { /** This method is to create a UsbIrp using the * constructor DefaultUsbIrp. */ byte[] data = new byte[0]; int offset = 0; int length = 0; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; UsbIrp usbIrp = new DefaultUsbIrp(); Assert.assertTrue( "Create Usb Irp Failed! Got a null instance", usbIrp != null ); performIrpTests(usbIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbIrp); } public void testUsbIrpwithDataParmConstructor() { /** This method is to create a UsbIrp with Data using the * constructor DefaultUsbIrp(data) */ byte[] data = {9,8,7,6,5,4,3,2,1}; int offset = 0; int length = 9; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; UsbIrp usbIrp = new DefaultUsbIrp(data); Assert.assertTrue( "Create Usb Irp with Data Failed! Got a null instance", usbIrp != null ); performIrpTests(usbIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbIrp); } public void testUsbIrpwithAllParmsConstructor() { /** This method is to create a UsbIrp with variables using the * constructor DefaultUsbIrp(data, offset, length, shortPacket) */ byte[] data = {9,8,7,6,5,4,3,2,1}; int offset = 2; int length = 4; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; UsbIrp usbIrp = new DefaultUsbIrp(data, offset, length, shortPacket); Assert.assertTrue( "Create Usb Irp with all variables Failed! Got a null instance", usbIrp != null ); performIrpTests(usbIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbIrp); } public void testUsbControlIrpwithBasicParmsContructor() { /**This method is to create a usbControlIrp using the * constructor DefaultUsbControlIrp(bmRequestType, bRequest,wValue,wIndex) */ byte[] data = new byte[0]; int offset = 0; int length = 0; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; byte bmRequestType =UsbConst.REQUESTTYPE_DIRECTION_OUT |UsbConst.REQUESTTYPE_TYPE_STANDARD |UsbConst.REQUESTTYPE_RECIPIENT_DEVICE; byte bRequest = UsbConst.REQUEST_GET_DESCRIPTOR; short wValue = UsbConst.DESCRIPTOR_TYPE_DEVICE << 8; short wIndex = 5; UsbControlIrp usbControlIrp = new DefaultUsbControlIrp(bmRequestType, bRequest,wValue,wIndex); Assert.assertTrue( "Create Usb Control Irp Failed! Got a null instance", usbControlIrp != null ); verifyControlIrpValues(usbControlIrp, bmRequestType, bRequest, wValue, wIndex); performIrpTests(usbControlIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbControlIrp); } public void testUsbControlIrpwithAllParmsConstructor() { /**This method is to create a usbControlIrp with Data using the * constructor DefaultUsbControlIrp(data, offset, length, *shortPacket, bmRequestType, bRequest, wValue, wIndex) */ byte[] data = {9,8,7,6,5,4,3,2,1}; int offset = 3; int length = 2; boolean shortPacket = true; int actualLength = 0; boolean bIsUsbException = false; UsbException usbException = null; boolean bIsComplete = false; byte bmRequestType =UsbConst.REQUESTTYPE_DIRECTION_OUT |UsbConst.REQUESTTYPE_TYPE_STANDARD |UsbConst.REQUESTTYPE_RECIPIENT_DEVICE; byte bRequest = UsbConst.REQUEST_GET_DESCRIPTOR; short wValue = UsbConst.DESCRIPTOR_TYPE_DEVICE << 8; short wIndex = 5; UsbControlIrp usbControlIrp = new DefaultUsbControlIrp(data, offset, length, shortPacket, bmRequestType, bRequest, wValue, wIndex); Assert.assertTrue( "Create Usb Control Irp with Data Failed! Got a null instance", usbControlIrp != null ); verifyControlIrpValues(usbControlIrp, bmRequestType, bRequest, wValue, wIndex); performIrpTests(usbControlIrp,data, offset, length, shortPacket,actualLength, bIsUsbException, usbException, bIsComplete); testWaitUntilComplete(usbControlIrp); } public void performIrpTests(UsbIrp usbIrp, byte []data, int offset, int length, boolean shortPacket, int actualLength, boolean bIsUsbException, UsbException usbException, boolean bIsComplete) { /* * Verify IRP values against values supplied in parameters */ //verify that IRP was created with expected values verifyUsbIrpValues(usbIrp, data, offset, length, shortPacket, actualLength, bIsUsbException, usbException, bIsComplete); /* * Verify expected exceptions thrown when setting bad values */ //sample IRP values to be set and verified byte[] samplebyteArrayData = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int sampleOffset = 2; int sampleLength = 6; verifyExceptionsThrownForInvalidSetValues(usbIrp, samplebyteArrayData, sampleOffset, sampleLength); /* * Set various IRP values and verify that they were set properly */ //IRP values to be set and verified byte[] byteArrayData = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; offset = 2; length = 6; shortPacket = false; actualLength = 6; bIsUsbException = true; usbException = new UsbShortPacketException(); bIsComplete = true; //set and verify correct setting of IRP values setAndVerifyIrpParameters(usbIrp, byteArrayData, offset, length, shortPacket, actualLength, bIsUsbException, usbException, bIsComplete); } /** * This method creates a UsbDevice, * UsbInterface, UsbEndpoints * and UsbPipe . */ private UsbPipe usbPipecreate() { List endpoints = null; UsbDevice usbDevice = FindProgrammableDevice.getInstance().getProgrammableDevice(); Assert.assertTrue( "Find Programmable board Failed! Got a null instance", usbDevice != null ); if ( usbDevice.isConfigured() ) { UsbConfiguration usbConfiguration = usbDevice.getUsbConfiguration((byte)1); UsbInterface iface = usbConfiguration.getUsbInterface((byte)0); try { // @P2D4 iface.claim(); assertTrue("isClaimed() should be true): ", iface.isClaimed()); endpoints = iface.getUsbEndpoints(); assertFalse("no endpoints found!", 0 == endpoints.size()); List totalep = endpoints; UsbEndpoint inEndpoint, outEndpoint; inEndpoint = findEndpoint(totalep, UsbConst.REQUESTTYPE_DIRECTION_IN, UsbConst.ENDPOINT_TYPE_ISOCHRONOUS); outEndpoint = findEndpoint(totalep,UsbConst.REQUESTTYPE_DIRECTION_OUT, UsbConst.ENDPOINT_TYPE_ISOCHRONOUS); assertFalse("could not find input endpoint", null == inEndpoint); assertFalse("could not find output endpoint", null == outEndpoint); UsbPipe iPipe = inEndpoint.getUsbPipe(); return iPipe; } catch ( UsbClaimException uce ) { fail("UsbClaimException: " + uce); } catch ( UsbNotActiveException unae ) { fail("UsbNotActiveException: " + unae); } catch ( UsbException ue ) { fail("UsbException: " + ue); } catch ( UsbDisconnectedException uDE ) // @P1A { // @P1A fail ("A connected device should't throw the UsbDisconnectedException!"); // @P1A } // @P1A catch ( Exception e ) { fail("Exception: " + e); } } else fail("device is not configured!"); return null; } /** This method is called by UsbPipeCreate method * to find an In Endpoint. */ private UsbEndpoint findEndpoint( List totalEndpoints, int inDirection, int inType) { boolean found =false; for ( int j = 0; j < totalEndpoints.size(); j++ ) { UsbEndpoint ep = (UsbEndpoint)totalEndpoints.get(j); int direction = ep.getDirection(); int type = ep.getType(); if ( ((byte)(inDirection) == (byte)direction) && (inType == type) ) { return ep; } } fail("End point NOT FOUND!!!"); return null; } private void testWaitUntilComplete(UsbIrp usbIrp) { /* * verify waitUntilComplete timeout */ ThreadTest tt = new ThreadTest(usbIrp); do tt.run(); while ( tt.donef ==false ); } /** *This method is to conduct verifications on a UsbControlIrp values * @param usbControlIrp * @param bmreqtype * @param breq * @param wval * @param wind */ private void verifyControlIrpValues(UsbControlIrp usbControlIrp,byte bmreqtype, byte breq, short wval, short wind) { Assert.assertEquals("bmRequestType does not match the value specified when UsbControlIrp was created.",bmreqtype,usbControlIrp.bmRequestType()); Assert.assertEquals("bRequest does not match the value specified when UsbControlIrp was created.",breq,usbControlIrp.bRequest()); Assert.assertEquals("wValue does not match the value specified when UsbControlIrp was created.",wval,usbControlIrp.wValue()); Assert.assertEquals("wIndex does not match the value specified when UsbControlIrp was created.",wind,usbControlIrp.wIndex()); } /** * The below procedure is to use the various set methods * on a ControlIrp/Irp and verify if the variables retain their * values after they are set. */ private void setAndVerifyIrpParameters(UsbIrp usbIrp, byte[] allData, int oset, int lgth, boolean spacket, int actualLength, boolean bException, UsbException usbException, boolean bComplete) { //default IRP values int defaultOffset = 0; int defaultLength = allData.length; boolean defaultShortPacket = true; int defaultActualLength = 0; boolean defaultIsUsbException = false; UsbException defaultUsbException = null; boolean defaultIsComplete = false; //set data byte array only; verify values usbIrp.setData(allData); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //set data byte array plus offset and length; verify values usbIrp.setData(allData, oset, lgth); verifyUsbIrpValues(usbIrp, allData, oset, lgth, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //set data back to whole byte array only; verify values usbIrp.setData(allData); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //set data back to whole byte array only and set offset and length separately; verify values usbIrp.setData(allData); usbIrp.setOffset(oset); usbIrp.setLength(lgth); verifyUsbIrpValues(usbIrp, allData, oset, lgth, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //set accept short packet; verify value spacket = true; usbIrp.setData(allData); usbIrp.setAcceptShortPacket(spacket); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, spacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); spacket = false; usbIrp.setAcceptShortPacket(spacket); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, spacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); spacket = defaultShortPacket; //set ShortPacket back to default for the rest of the tests usbIrp.setAcceptShortPacket(spacket); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //call setComplete; verify value bComplete = true; usbIrp.setData(allData); usbIrp.setComplete(bComplete); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, spacket, defaultActualLength, defaultIsUsbException, defaultUsbException, bComplete); bComplete = false; usbIrp.setData(allData); usbIrp.setComplete(bComplete); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, bComplete); //call complete; verify value usbIrp.setData(allData); bComplete = true; usbIrp.complete(); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, bComplete); usbIrp.setData(allData); usbIrp.setComplete(defaultIsComplete); //set IsComplete back to default verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //call setActualLength usbIrp.setData(allData); int expectedActualLength = defaultActualLength; usbIrp.setActualLength(expectedActualLength); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, expectedActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); expectedActualLength = actualLength; usbIrp.setActualLength(expectedActualLength); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, expectedActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); usbIrp.setActualLength(defaultActualLength); //set actual Length back to default verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); //call setUsbException; verify getUsbException and isUsbException usbIrp.setData(allData); UsbException expectedUsbException = defaultUsbException; boolean expectedIsUsbException = defaultIsUsbException; usbIrp.setUsbException(expectedUsbException); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, expectedIsUsbException, expectedUsbException, defaultIsComplete); expectedUsbException = usbException; expectedIsUsbException = bException; usbIrp.setUsbException(expectedUsbException); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, expectedIsUsbException, expectedUsbException, defaultIsComplete); usbIrp.setUsbException(defaultUsbException); verifyUsbIrpValues(usbIrp, allData, defaultOffset, defaultLength, defaultShortPacket, defaultActualLength, defaultIsUsbException, defaultUsbException, defaultIsComplete); } /** * Method verifies proper exceptions are thrown when setting invalid data in IRP */ private void verifyExceptionsThrownForInvalidSetValues(UsbIrp usbIrp, byte[] allData, int oset, int lgth) { int negLength = -1; int negOffset = -1; byte[] nullByteArray = null; boolean expectedExceptionReceived = false; //null byte array in setData, offset, length try { usbIrp.setData(nullByteArray, oset, lgth); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (1) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //neg offset in setData, offset, length try { usbIrp.setData(allData, negOffset, lgth); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (1) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //neg length in setData, offset, length try { usbIrp.setData(allData, oset, negLength); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (2) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //null byte array in setData try { usbIrp.setData(nullByteArray); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (3) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //neg Length in setLength try { usbIrp.setData(allData); usbIrp.setLength(negLength); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (4) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //neg Offset in setOffset try { usbIrp.setData(allData); usbIrp.setOffset(negOffset); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (5) not received.", expectedExceptionReceived); expectedExceptionReceived = false; //neg ActualLength in setActualLength try { usbIrp.setData(allData); usbIrp.setActualLength(negLength); expectedExceptionReceived = false; } catch ( java.lang.IllegalArgumentException e ) { expectedExceptionReceived = true; } catch ( Exception e ) { expectedExceptionReceived = false; } Assert.assertTrue("Expected illegal argument exception (5) not received.", expectedExceptionReceived); expectedExceptionReceived = false; } /* * This method is to compare the data * @param usbControlIrp or usbIrp * @ param dat2. */ private boolean comparebytearray(UsbIrp usbIrp, byte[] dat2) { byte[] dat1 ; dat1 = usbIrp.getData() ; if ( dat1.length != dat2.length ) { return false; } else { for ( int i = 0;i 6000 ) { fail("Timeout value exceeded"); } else { done = true; } } private boolean done = false; private long st, et, acttime; private UsbIrp usbIrp; } } }