001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.cardreader;
028:
029: import com.sun.midp.i3test.TestCase;
030: import java.io.IOException;
031:
032: /**
033: * This test case tests basic PlatformCardReader
034: * functionality, primarily configuration
035: * properties.
036: */
037: public class TestGemplus1 extends TestCase {
038:
039: /**
040: * Test class creation and initialization.
041: */
042: private void testInit() throws java.io.IOException,
043: ClassNotFoundException, InstantiationException,
044: CardDeviceException, IllegalAccessException {
045:
046: boolean stub_flag = false;
047: CardDevice cd = (CardDevice) Class.forName(
048: "com.sun.cardreader.PlatformCardDevice").newInstance();
049: assertNotNull(cd); /* Creation successful */
050:
051: try {
052: cd.init();
053: } catch (CardDeviceException e) {
054: if (e.getMessage().equals("stub")) {
055: stub_flag = true;
056: } else {
057: throw e;
058: }
059: }
060:
061: if (!stub_flag) {
062: /* Init successful (no exceptions) */
063: assertTrue(true);
064: cd.close();
065: } else {
066: assertTrue(true);
067: }
068: }
069:
070: /**
071: * Test resetting device.
072: */
073: private void testReset() throws java.io.IOException,
074: ClassNotFoundException, InstantiationException,
075: CardDeviceException, IllegalAccessException {
076:
077: boolean stub_flag = false;
078: CardDevice cd = (CardDevice) Class.forName(
079: "com.sun.cardreader.PlatformCardDevice").newInstance();
080:
081: try {
082: cd.init();
083: } catch (CardDeviceException e) {
084: if (e.getMessage().equals("stub")) {
085: stub_flag = true;
086: } else {
087: throw e;
088: }
089: }
090:
091: if (!stub_flag) {
092: boolean is_null = true;
093:
094: cd.lock();
095: cd.reset();
096: byte[] atr = cd.getATR();
097: for (int i = 0; i < atr.length; i++) {
098: if (atr[i] != 0)
099: is_null = false;
100: }
101: /* OK, non-empty ATR received */
102: assertFalse("Bad ATR received", is_null);
103:
104: cd.close();
105: } else {
106: assertTrue(true);
107: }
108:
109: }
110:
111: /**
112: * Test Xfer data.
113: */
114: private void testXfer() throws java.io.IOException,
115: ClassNotFoundException, InstantiationException,
116: CardDeviceException, IllegalAccessException {
117:
118: boolean stub_flag = false;
119: CardDevice cd = (CardDevice) Class.forName(
120: "com.sun.cardreader.PlatformCardDevice").newInstance();
121:
122: try {
123: cd.init();
124: } catch (CardDeviceException e) {
125: if (e.getMessage().equals("stub")) {
126: stub_flag = true;
127: } else {
128: throw e;
129: }
130: }
131:
132: if (!stub_flag) {
133: cd.lock();
134: cd.reset();
135:
136: boolean is_null = true;
137: byte[] request = { (byte) 0x00, (byte) 0xA4, (byte) 0x00,
138: (byte) 0x00 };
139:
140: byte[] response = new byte[2];
141:
142: /* Try SELECT FILE (master file) */
143: int received = cd.cmdXfer(request, response);
144:
145: for (int i = 0; i < received; i++) {
146: if (response[i] != 0)
147: is_null = false;
148: }
149: /* OK, non-empty status received */
150: assertFalse("null response received",
151: (received >= 2 && is_null));
152: /* OK, master file exists */
153: assertTrue(
154: "bad response received",
155: (response[received - 2] == (byte) 0x61 || response[received - 2] == (byte) 0x90));
156:
157: if (response[received - 2] == (byte) 0x61) {
158: int resp_len = response[1];
159: response = new byte[resp_len + 2];
160: request = new byte[] { (byte) 0x00, (byte) 0xC0,
161: (byte) 0x00, (byte) 0x00, (byte) resp_len };
162: cd.cmdXfer(request, response);
163:
164: /* Master file is received successfuly */
165: assertTrue(
166: "bad response received",
167: (response[response.length - 2] == (byte) 0x90 && response[response.length - 1] == (byte) 0x00));
168: }
169:
170: cd.close();
171: } else {
172: assertTrue(true);
173: }
174: }
175:
176: /**
177: * Test locking and unlocking features.
178: */
179: private void testLocks() throws java.io.IOException,
180: ClassNotFoundException, InstantiationException,
181: CardDeviceException, IllegalAccessException {
182:
183: /**
184: * Thread class.
185: */
186: class TestThread extends Thread {
187: private int delay;
188: private CardDevice device;
189: private String name;
190:
191: TestThread(String name, CardDevice cd, int delay) {
192: super ();
193: this .name = name;
194: this .delay = delay;
195: device = cd;
196: }
197:
198: public void run() {
199: long start = System.currentTimeMillis();
200: long cur;
201: boolean error = false;
202:
203: try {
204: device.lock();
205: do {
206: cur = System.currentTimeMillis();
207: } while (cur < start + delay * 1000);
208: device.unlock();
209: } catch (IOException e) {
210: error = true;
211: }
212: /* Lock/unlock pair works right */
213: assertFalse("Thread " + name + " throws IOException",
214: error);
215: }
216: }
217: boolean stub_flag = false;
218:
219: CardDevice cd = (CardDevice) Class.forName(
220: "com.sun.cardreader.PlatformCardDevice").newInstance();
221:
222: try {
223: cd.init();
224: } catch (CardDeviceException e) {
225: if (e.getMessage().equals("stub")) {
226: stub_flag = true;
227: } else {
228: throw e;
229: }
230: }
231: if (!stub_flag) {
232: TestThread t1 = new TestThread("First task", cd, 5);
233: TestThread t2 = new TestThread("Second task", cd, 3);
234: TestThread t3 = new TestThread("Third task", cd, 3);
235: boolean except = false;
236:
237: try {
238: cd.reset();
239: } catch (IOException e) {
240: except = true;
241: }
242: /* RESET/XFER commands should not allowed without locking */
243: assertTrue("Reset is allowed without locking", except);
244:
245: t1.start();
246: t2.start();
247: t3.start();
248:
249: long start = System.currentTimeMillis();
250: long cur;
251:
252: do {
253: cur = System.currentTimeMillis();
254: } while (cur < start + 1000);
255:
256: cd.lock();
257:
258: except = false;
259: try {
260: cd.reset();
261: } catch (IOException e) {
262: except = true;
263: }
264: cd.unlock();
265: /* RESET command should work with locking */
266: assertFalse("Reset with lock fails", except);
267:
268: cd.close();
269:
270: try {
271: t1.join();
272: t2.join();
273: t3.join();
274: } catch (InterruptedException e) {
275: }
276:
277: } else {
278: assertTrue(true);
279: }
280: }
281:
282: /**
283: * Run tests.
284: */
285: public void runTests() {
286: try {
287: declare("testInit");
288: testInit();
289:
290: declare("testLocks");
291: testLocks();
292:
293: declare("testReset");
294: testReset();
295:
296: declare("testXfer");
297: testXfer();
298: } catch (Throwable t) {
299: fail("" + t);
300: }
301: }
302:
303: public void assertNotEmpty(String message, byte[] arr) {
304:
305: boolean is_null = true;
306: for (int i = 0; i < arr.length; i++) {
307: if (arr[i] != (byte) 0) {
308: is_null = false;
309: }
310: }
311: assertFalse(message, is_null);
312:
313: }
314: }
|