FileDocCategorySizeDatePackage
TestGemplus1.javaAPI DocphoneME MR2 API (J2ME)9993Wed May 02 18:00:38 BST 2007com.sun.cardreader

TestGemplus1

public class TestGemplus1 extends com.sun.midp.i3test.TestCase
This test case tests basic PlatformCardReader functionality, primarily configuration properties.

Fields Summary
Constructors Summary
Methods Summary
public voidassertNotEmpty(java.lang.String message, byte[] arr)


        boolean is_null = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != (byte)0) {
                is_null = false;
            }
        }
        assertFalse(message, is_null);
        
    
public voidrunTests()
Run tests.

        try {
            declare("testInit");
            testInit();

            declare("testLocks");
            testLocks();
            
            declare("testReset");
            testReset();

            declare("testXfer");
            testXfer();
        }
        catch (Throwable t) {
            fail("" + t);
        }
    
private voidtestInit()
Test class creation and initialization.

                       
        boolean stub_flag = false;
        CardDevice cd = 
            (CardDevice)Class
                .forName("com.sun.cardreader.PlatformCardDevice")
                .newInstance();
        assertNotNull(cd); /* Creation successful */
        
        try {
            cd.init();
        } 
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }
        
        if (!stub_flag) {
                /* Init successful (no exceptions) */
            assertTrue(true); 
            cd.close();
        } else {
            assertTrue(true);
        }
    
private voidtestLocks()
Test locking and unlocking features.

                       
        /**
         * Thread class.
         */
        class TestThread extends Thread {
            private int delay;
            private CardDevice device;
            private String name;
            
            TestThread(String name, CardDevice cd, int delay) {
                super();
                this.name = name;
                this.delay = delay;
                device = cd;
            }
            
            public void run() {
                long start = System.currentTimeMillis();
                long cur;
                boolean error = false;

                try {
                    device.lock();
                    do {
                        cur = System.currentTimeMillis();
                    } while (cur < start + delay*1000);
                    device.unlock();
                } catch (IOException e) {
                    error = true;
                }
                    /* Lock/unlock pair works right */
                assertFalse("Thread " + name + " throws IOException", error);
            }
        }
        boolean stub_flag = false;
        
        CardDevice cd = 
            (CardDevice)Class
                .forName("com.sun.cardreader.PlatformCardDevice")
                .newInstance();
        
        try {
            cd.init();
        }
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }
        if (!stub_flag) {
            TestThread t1 = new TestThread("First task", cd, 5);
            TestThread t2 = new TestThread("Second task", cd, 3);
            TestThread t3 = new TestThread("Third task", cd, 3);
            boolean except = false;
            
            try {
                cd.reset();
            }
            catch (IOException e) {
                except = true;
            }
                /* RESET/XFER commands should not allowed without locking */
            assertTrue("Reset is allowed without locking", except);
            
            t1.start();
            t2.start();
            t3.start();
            
            long start = System.currentTimeMillis();
            long cur;
            
            do {
                cur = System.currentTimeMillis();
            } while (cur < start + 1000);
            
            cd.lock();
            
            except = false;
            try {
                cd.reset();
            } 
            catch (IOException e) {
                except = true;
            }
            cd.unlock();
                /* RESET command should work with locking */
            assertFalse("Reset with lock fails", except);
            
            cd.close();
            
            try {
                t1.join();
                t2.join();
                t3.join();
            } catch (InterruptedException e) {}
            
        } else {
            assertTrue(true);
        }
    
private voidtestReset()
Test resetting device.

                       
        boolean stub_flag = false;
        CardDevice cd = 
            (CardDevice)Class
                .forName("com.sun.cardreader.PlatformCardDevice")
                .newInstance();
        
        try {
            cd.init();
        } 
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }
        
        if (!stub_flag) {
            boolean is_null = true;
            
            cd.lock();
            cd.reset();
            byte[] atr = cd.getATR();
            for (int i = 0; i < atr.length; i++) {
                if (atr[i] != 0)
                    is_null = false;
            }
                /* OK, non-empty ATR received */
            assertFalse("Bad ATR received", is_null); 
            
            cd.close();
        } else {
            assertTrue(true);
        }
        
    
private voidtestXfer()
Test Xfer data.

                       
        boolean stub_flag = false;
        CardDevice cd = 
            (CardDevice)Class
                .forName("com.sun.cardreader.PlatformCardDevice")
                .newInstance();
        
        try {
            cd.init();
        } 
        catch (CardDeviceException e) {
            if (e.getMessage().equals("stub")) {
                stub_flag = true;
            } else {
                throw e;
            }
        }
        
        if (!stub_flag) {
            cd.lock();
            cd.reset();
    
            boolean is_null = true;
            byte[] request = {(byte)0x00, (byte)0xA4, (byte)0x00, (byte)0x00};
            
            byte[] response = new byte[2];
            
                /* Try SELECT FILE (master file) */
            int received = cd.cmdXfer(request, response); 
            
            for (int i = 0; i < received; i++) {
                if (response[i] != 0)
                    is_null = false;
            }
                /* OK, non-empty status received */
            assertFalse("null response received", (received >= 2 && is_null)); 
                /* OK, master file exists */
            assertTrue("bad response received", 
                (response[received - 2] == (byte)0x61  ||
                 response[received - 2] == (byte)0x90)); 

            if (response[received - 2] == (byte)0x61) {
                int resp_len = response[1];
                response = new byte[resp_len + 2];
                request = new byte[] {
                    (byte)0x00, (byte)0xC0, (byte)0x00, 
                    (byte)0x00, (byte)resp_len
                };
                cd.cmdXfer(request, response);
                
                    /* Master file is received successfuly */
                assertTrue("bad response received", 
                    (response[response.length-2] == (byte)0x90 &&
                    response[response.length-1] == (byte)0x00)); 
            }
                    
            cd.close();
        } else {
            assertTrue(true);
        }