1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.nfc_extras.tests;
18
19import android.content.Context;
20import android.nfc.NfcAdapter;
21import android.test.InstrumentationTestCase;
22import android.util.Log;
23
24import com.android.nfc_extras.NfcAdapterExtras;
25import com.android.nfc_extras.NfcAdapterExtras.CardEmulationRoute;
26import com.android.nfc_extras.NfcExecutionEnvironment;
27
28import java.io.IOException;
29import java.util.Arrays;
30
31public class BasicNfcEeTest extends InstrumentationTestCase {
32    private Context mContext;
33    private NfcAdapterExtras mAdapterExtras;
34    private NfcExecutionEnvironment mEe;
35
36    public static final byte[] SELECT_CARD_MANAGER_COMMAND = new byte[] {
37        (byte)0x00, (byte)0xA4, (byte)0x04, (byte)0x00,  // command
38        (byte)0x08,  // data length
39        (byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00,
40        (byte)0x00,  // card manager AID
41        (byte)0x00  // trailer
42    };
43
44    public static final byte[] SELECT_CARD_MANAGER_RESPONSE = new byte[] {
45        (byte)0x90, (byte)0x00,
46    };
47
48    @Override
49    protected void setUp() throws Exception {
50        super.setUp();
51        mContext = getInstrumentation().getTargetContext();
52        mAdapterExtras = NfcAdapterExtras.get(NfcAdapter.getDefaultAdapter(mContext));
53        mEe = mAdapterExtras.getEmbeddedExecutionEnvironment();
54    }
55
56    public void testSendCardManagerApdu() throws IOException {
57        mEe.open();
58
59        try {
60            byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND);
61            assertTrue(out.length >= SELECT_CARD_MANAGER_RESPONSE.length);
62            byte[] trailing = Arrays.copyOfRange(out,
63                    out.length - SELECT_CARD_MANAGER_RESPONSE.length,
64                    out.length);
65            assertByteArrayEquals(SELECT_CARD_MANAGER_RESPONSE, trailing);
66
67        } finally {
68            mEe.close();
69        }
70
71    }
72
73    public void testSendCardManagerApduMultiple() throws IOException {
74        for (int i=0; i<10; i++) {
75            try {
76            mEe.open();
77
78            try {
79                byte[] out = mEe.transceive(SELECT_CARD_MANAGER_COMMAND);
80                byte[] trailing = Arrays.copyOfRange(out,
81                        out.length - SELECT_CARD_MANAGER_RESPONSE.length,
82                        out.length);
83
84            } finally {
85                try {Thread.sleep(1000);} catch (InterruptedException e) {}
86                mEe.close();
87            }
88            } catch (IOException e) {}
89        }
90
91        testSendCardManagerApdu();
92
93    }
94
95    public void testEnableEe() {
96        mAdapterExtras.setCardEmulationRoute(
97                new CardEmulationRoute(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, mEe));
98        CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute();
99        assertEquals(CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, newRoute.route);
100        assertEquals(mEe, newRoute.nfcEe);
101    }
102
103    public void testDisableEe() {
104        mAdapterExtras.setCardEmulationRoute(
105                new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null));
106        CardEmulationRoute newRoute = mAdapterExtras.getCardEmulationRoute();
107        assertEquals(CardEmulationRoute.ROUTE_OFF, newRoute.route);
108        assertNull(newRoute.nfcEe);
109    }
110
111    private static void assertByteArrayEquals(byte[] b1, byte[] b2) {
112        assertEquals(b1.length, b2.length);
113        for (int i = 0; i < b1.length; i++) {
114            assertEquals(b1[i], b2[i]);
115        }
116    }
117}
118