diff options
author | Ruchi Kandoi <kandoiruchi@google.com> | 2018-10-19 10:23:49 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-10-19 10:23:49 -0700 |
commit | c29f4758b527cecf6e0153a832c2e36d9d2a95bb (patch) | |
tree | 901ae9c0bbd6439a4d2c1f97e58d8da23192518f | |
parent | 538bd51c79c5c94fc4a072d344856ebce574f79f (diff) | |
parent | 32ade37df9c50809f5a6b267949d9ee212cecf84 (diff) | |
download | cts-pie-temp.tar.gz |
Merge changes from topic "used_channels_leakage"pie-temp
am: 32ade37df9
Change-Id: Id69c20e0d653832dffa80ef4cf397f7a2b068635
-rw-r--r-- | tests/tests/secure_element/omapi/src/android/omapi/cts/OmapiTest.java | 158 |
1 files changed, 82 insertions, 76 deletions
diff --git a/tests/tests/secure_element/omapi/src/android/omapi/cts/OmapiTest.java b/tests/tests/secure_element/omapi/src/android/omapi/cts/OmapiTest.java index 991111b051f..65474cfe99d 100644 --- a/tests/tests/secure_element/omapi/src/android/omapi/cts/OmapiTest.java +++ b/tests/tests/secure_element/omapi/src/android/omapi/cts/OmapiTest.java @@ -255,18 +255,21 @@ public class OmapiTest { Reader[] readers = seService.getReaders(); for (Reader reader : readers) { - Session session = reader.openSession(); - assertNotNull("Could not open session", session); - Channel channel = session.openBasicChannel(null, (byte)0x00); + Session session = null; + Channel channel = null; + try { + session = reader.openSession(); + assertNotNull("Could not open session", session); + channel = session.openBasicChannel(null, (byte) 0x00); + } finally { + if (channel != null) channel.close(); + if (session != null) session.close(); + } if (reader.getName().startsWith(UICC_READER_PREFIX)) { assertNull("Basic channel on UICC can be opened", channel); } else { assertNotNull("Basic Channel cannot be opened", channel); } - if (channel != null) { - channel.close(); - } - session.close(); } } catch (Exception e) { fail("Unexpected Exception " + e); @@ -281,18 +284,21 @@ public class OmapiTest { Reader[] readers = seService.getReaders(); for (Reader reader : readers) { - Session session = reader.openSession(); - assertNotNull("Could not open session", session); - Channel channel = session.openBasicChannel(SELECTABLE_AID, (byte)0x00); + Session session = null; + Channel channel = null; + try { + session = reader.openSession(); + assertNotNull("Could not open session", session); + channel = session.openBasicChannel(SELECTABLE_AID, (byte) 0x00); + } finally { + if (channel != null) channel.close(); + if (session != null) session.close(); + } if (reader.getName().startsWith(UICC_READER_PREFIX)) { assertNull("Basic channel on UICC can be opened", channel); } else { assertNotNull("Basic Channel cannot be opened", channel); } - if (channel != null) { - channel.close(); - } - session.close(); } } catch (Exception e) { fail("Unexpected Exception " + e); @@ -358,86 +364,86 @@ public class OmapiTest { for (Reader reader : readers) { testNonSelectableAid(reader, NON_SELECTABLE_AID); } - } catch (TimeoutException e) { + } catch (Exception e) { fail("unexpected exception " + e); } } - private void testNonSelectableAid(Reader reader, byte[] aid) { - boolean exception = false; + private void testNonSelectableAid(Reader reader, byte[] aid) throws IOException { Session session = null; + Channel channel = null; try { assertTrue(reader.isSecureElementPresent()); session = reader.openSession(); assertNotNull("null session", session); - Channel channel = session.openLogicalChannel(aid, (byte)0x00); + channel = session.openLogicalChannel(aid, (byte) 0x00); + fail("Exception expected for this test"); } catch (NoSuchElementException e) { - exception = true; - if (session != null) { - session.close(); - } - } catch (Exception e) { - fail("unexpected exception " + e); + // Catch the expected exception here. + } finally { + if (channel != null) channel.close(); + if (session != null) session.close(); } - assertTrue(exception); } /** Tests if Security Exception in Transmit */ @Test public void testSecurityExceptionInTransmit() { - boolean exception = false; - Session session; - Channel channel; try { waitForConnection(); Reader[] readers = seService.getReaders(); for (Reader reader : readers) { - assertTrue(reader.isSecureElementPresent()); - session = reader.openSession(); - assertNotNull("null session", session); - channel = session.openLogicalChannel(SELECTABLE_AID, (byte)0x00); - assertNotNull("Null Channel", channel); - byte[] selectResponse = channel.getSelectResponse(); - assertNotNull("Null Select Response", selectResponse); - assertGreaterOrEqual(selectResponse.length, 2); - assertEquals(selectResponse[selectResponse.length - 1] & 0xFF, 0x00); - assertEquals(selectResponse[selectResponse.length - 2] & 0xFF, 0x90); - for (byte[] cmd : ILLEGAL_COMMANDS_TRANSMIT) { - try { - exception = false; - byte[] response = channel.transmit(cmd); - } catch (SecurityException e) { - exception = true; + Session session = null; + Channel channel = null; + try { + assertTrue(reader.isSecureElementPresent()); + session = reader.openSession(); + assertNotNull("null session", session); + channel = session.openLogicalChannel(SELECTABLE_AID, (byte) 0x00); + assertNotNull("Null Channel", channel); + byte[] selectResponse = channel.getSelectResponse(); + assertNotNull("Null Select Response", selectResponse); + assertGreaterOrEqual(selectResponse.length, 2); + assertThat(selectResponse[selectResponse.length - 1] & 0xFF, is(0x00)); + assertThat(selectResponse[selectResponse.length - 2] & 0xFF, is(0x90)); + for (byte[] cmd : ILLEGAL_COMMANDS_TRANSMIT) { + try { + byte[] response = channel.transmit(cmd); + fail("Exception expected for this test"); + } catch (SecurityException e) { + // Catch the expected exception here. + } } - assertTrue(exception); + } finally { + if (channel != null) channel.close(); + if (session != null) session.close(); } - channel.close(); - session.close(); } } catch (Exception e) { fail("unexpected exception " + e); } } - private byte[] internalTransmitApdu(Reader reader, byte[] apdu) { + private byte[] internalTransmitApdu(Reader reader, byte[] apdu) throws IOException { + byte[] transmitResponse = null; + Session session = null; + Channel channel = null; try { assertTrue(reader.isSecureElementPresent()); - Session session = reader.openSession(); + session = reader.openSession(); assertNotNull("null session", session); - Channel channel = session.openLogicalChannel(SELECTABLE_AID, (byte)0x00); + channel = session.openLogicalChannel(SELECTABLE_AID, (byte) 0x00); assertNotNull("Null Channel", channel); byte[] selectResponse = channel.getSelectResponse(); assertNotNull("Null Select Response", selectResponse); assertGreaterOrEqual(selectResponse.length, 2); - byte[] transmitResponse = channel.transmit(apdu); - channel.close(); - session.close(); - return transmitResponse; - } catch (Exception e) { - fail("unexpected exception " + e); + transmitResponse = channel.transmit(apdu); + } finally { + if (channel != null) channel.close(); + if (session != null) session.close(); } - return null; + return transmitResponse; } /** @@ -455,17 +461,17 @@ public class OmapiTest { for (Reader reader : readers) { for (byte[] apdu : NO_DATA_APDU) { byte[] response = internalTransmitApdu(reader, apdu); - assertEquals(response.length, 2); - assertEquals(response[response.length - 1] & 0xFF, 0x00); - assertEquals(response[response.length - 2] & 0xFF, 0x90); + assertThat(response.length, is(2)); + assertThat(response[response.length - 1] & 0xFF, is(0x00)); + assertThat(response[response.length - 2] & 0xFF, is(0x90)); } for (byte[] apdu : DATA_APDU) { byte[] response = internalTransmitApdu(reader, apdu); /* 256 byte data and 2 bytes of status word */ - assertEquals(response.length, 258); - assertEquals(response[response.length - 1] & 0xFF, 0x00); - assertEquals(response[response.length - 2] & 0xFF, 0x90); + assertThat(response.length, is(258)); + assertThat(response[response.length - 1] & 0xFF, is(0x00)); + assertThat(response[response.length - 2] & 0xFF, is(0x90)); } } } catch (Exception e) { @@ -493,8 +499,8 @@ public class OmapiTest { apdu[2] = (byte)(i+1); byte[] response = internalTransmitApdu(reader, apdu); byte[] SW = SW_62xx[i]; - assertEquals(response[response.length - 1], SW[1]); - assertEquals(response[response.length - 2], SW[0]); + assertThat(response[response.length - 1], is(SW[1])); + assertThat(response[response.length - 2], is(SW[0])); } } @@ -504,8 +510,8 @@ public class OmapiTest { byte[] response = internalTransmitApdu(reader, apdu); byte[] SW = SW_62xx[i]; assertGreaterOrEqual(response.length, 3); - assertEquals(response[response.length - 1], SW[1]); - assertEquals(response[response.length - 2], SW[0]); + assertThat(response[response.length - 1], is(SW[1])); + assertThat(response[response.length - 2], is(SW[0])); } for (byte i = 0x00; i < SW_62xx.length; i++) { @@ -518,8 +524,8 @@ public class OmapiTest { responseSubstring[0] = apdu[0]; assertTrue(Arrays.equals(responseSubstring, apdu)); byte[] SW = SW_62xx[i]; - assertEquals(response[response.length - 1], SW[1]); - assertEquals(response[response.length - 2], SW[0]); + assertThat(response[response.length - 1], is(SW[1])); + assertThat(response[response.length - 2], is(SW[0])); } } } catch (Exception e) { @@ -540,10 +546,10 @@ public class OmapiTest { byte[] b = { 0x00, 0x00, apdu[2], apdu[3] }; ByteBuffer wrapped = ByteBuffer.wrap(b); int expectedLength = wrapped.getInt(); - assertEquals(response.length, expectedLength + 2); - assertEquals(response[response.length - 1] & 0xFF, 0x00); - assertEquals(response[response.length - 2] & 0xFF, 0x90); - assertEquals(response[response.length - 3] & 0xFF, 0xFF); + assertThat(response.length, is(expectedLength + 2)); + assertThat(response[response.length - 1] & 0xFF, is(0x00)); + assertThat(response[response.length - 2] & 0xFF, is(0x90)); + assertThat(response[response.length - 3] & 0xFF, is(0xFF)); } } } catch (Exception e) { @@ -561,9 +567,9 @@ public class OmapiTest { for (Reader reader : readers) { byte[] response = internalTransmitApdu(reader, CHECK_SELECT_P2_APDU); assertGreaterOrEqual(response.length, 3); - assertEquals(response[response.length - 1] & 0xFF, 0x00); - assertEquals(response[response.length - 2] & 0xFF, 0x90); - assertEquals(response[response.length - 3] & 0xFF, 0x00); + assertThat(response[response.length - 1] & 0xFF, is(0x00)); + assertThat(response[response.length - 2] & 0xFF, is(0x90)); + assertThat(response[response.length - 3] & 0xFF, is(0x00)); } } catch (Exception e) { fail("unexpected exception " + e); |