Home | History | Annotate | Download | only in tests
      1 /*
      2 * Copyright 2013 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 /*
     17 * Copyright (C) 2013 The Android Open Source Project
     18 *
     19 * Licensed under the Apache License, Version 2.0 (the "License");
     20 * you may not use this file except in compliance with the License.
     21 * You may obtain a copy of the License at
     22 *
     23 *      http://www.apache.org/licenses/LICENSE-2.0
     24 *
     25 * Unless required by applicable law or agreed to in writing, software
     26 * distributed under the License is distributed on an "AS IS" BASIS,
     27 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     28 * See the License for the specific language governing permissions and
     29 * limitations under the License.
     30 */
     31 package com.example.android.cardemulation.tests;
     32 
     33 import com.example.android.cardemulation.*;
     34 
     35 import android.test.ActivityInstrumentationTestCase2;
     36 
     37 /**
     38 * Tests for CardEmulation sample.
     39 */
     40 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> {
     41 
     42     private MainActivity mTestActivity;
     43     private CardEmulationFragment mTestFragment;
     44 
     45     public SampleTests() {
     46         super(MainActivity.class);
     47     }
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52 
     53         // Starts the activity under test using the default Intent with:
     54         // action = {@link Intent#ACTION_MAIN}
     55         // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
     56         // All other fields are null or empty.
     57         mTestActivity = getActivity();
     58         mTestFragment = (CardEmulationFragment)
     59             mTestActivity.getSupportFragmentManager().getFragments().get(1);
     60     }
     61 
     62     /**
     63     * Test if the test fixture has been set up correctly.
     64     */
     65     public void testPreconditions() {
     66         //Try to add a message to add context to your assertions. These messages will be shown if
     67         //a tests fails and make it easy to understand why a test failed
     68         assertNotNull("mTestActivity is null", mTestActivity);
     69         assertNotNull("mTestFragment is null", mTestFragment);
     70     }
     71 
     72     /**
     73      * Test converting from a hex string to binary.
     74      */
     75     public void testHexToBinary() {
     76         final byte[] testData = {(byte) 0xc0, (byte) 0xff, (byte) 0xee};
     77         final byte[] output = CardService.HexStringToByteArray("C0FFEE");
     78         for (int i = 0; i < testData.length; i++) {
     79             assertEquals(testData[i], output[i]);
     80         }
     81     }
     82 
     83     /**
     84      * Test converting from binary to a hex string
     85      */
     86     public void testBinaryToHex() {
     87         final byte[] input = {(byte) 0xc0, (byte) 0xff, (byte) 0xee};
     88         final String output = CardService.ByteArrayToHexString(input);
     89         assertEquals("C0FFEE", output);
     90     }
     91 
     92     /**
     93      * Verify that account number is returned in response to APDU.
     94      */
     95     public void testRequestAccoutNumber() {
     96         AccountStorage.SetAccount(this.getActivity(), "1234");
     97         final byte[] command = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x05,
     98                 (byte) 0xF2, (byte) 0x22, (byte) 0x22, (byte) 0x22, (byte) 0x22};
     99         final byte[] expectedResponse = {(byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x34,
    100                 (byte) 0x90, (byte) 0x00};
    101         final CardService cs = new CardService();
    102         final byte[] output = cs.processCommandApdu(command, null);
    103 
    104         assertEquals(expectedResponse.length, output.length);
    105         for (int i = 0; i < expectedResponse.length; i++) {
    106             assertEquals(expectedResponse[i], output[i]);
    107         }
    108 
    109     }
    110 
    111 
    112     /**
    113      * Verify that account number is returned in response to APDU.
    114      */
    115     public void testInvalidCommand() {
    116         AccountStorage.SetAccount(this.getActivity(), "1234");
    117         final byte[] command = {(byte) 0x01, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x05,
    118                 (byte) 0xF2, (byte) 0x22, (byte) 0x22, (byte) 0x22, (byte) 0x22};
    119         final byte[] expectedResponse = {(byte) 0x00, (byte) 0x00};
    120         final CardService cs = new CardService();
    121         final byte[] output = cs.processCommandApdu(command, null);
    122 
    123         assertEquals(expectedResponse.length, output.length);
    124         for (int i = 0; i < expectedResponse.length; i++) {
    125             assertEquals(expectedResponse[i], output[i]);
    126         }
    127     }
    128 
    129     /**
    130      * Test functionality fo ConcatArrays method.
    131      */
    132     public void testArrayConcat() {
    133         final byte[] a = {(byte) 0x01, (byte) 0x02};
    134         final byte[] b = {(byte) 0x03, (byte) 0x04};
    135         final byte[] c = {(byte) 0x05, (byte) 0x06};
    136         final byte[] expected = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05,
    137                 (byte) 0x06};
    138         final byte[] result = CardService.ConcatArrays(a, b, c);
    139 
    140         assertEquals(expected.length, result.length);
    141         for (int i = 0; i < expected.length; i++) {
    142             assertEquals(expected[i], result[i]);
    143         }
    144     }
    145 
    146     /**
    147      * Test setting and getting account number
    148      */
    149     public void testSetAccountNumber() {
    150         final String account = "3456";
    151         AccountStorage.SetAccount(getActivity(), account);
    152         this.assertEquals(account, AccountStorage.GetAccount(getActivity()));
    153     }
    154 
    155     /**
    156      * Test building SELECT APDU from AID string.
    157      */
    158     public void testBuildSelectApdu() {
    159         final String aid = "1234";
    160         final byte[] expectedResult = {(byte) 0x00, (byte) 0xA4, 04, (byte) 0x00, (byte) 0x02,
    161                 (byte) 0x12, (byte) 0x34};
    162         final byte[] result = CardService.BuildSelectApdu(aid);
    163 
    164         assertEquals(expectedResult.length, result.length);
    165         for (int i = 0; i < expectedResult.length; i++) {
    166             assertEquals(expectedResult[i], result[i]);
    167         }
    168     }
    169 }