Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
devnied committed Jul 13, 2014
1 parent cfea22f commit a22f9f7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CommandApdu(final CommandEnum pEnum, final byte[] data, final int le) {
mIns = pEnum.getIns();
mP1 = pEnum.getP1();
mP2 = pEnum.getP2();
mLc = data.length;
mLc = data == null ? 0 : data.length;
mData = data;
mLe = le;
mLeUsed = true;
Expand All @@ -58,7 +58,7 @@ public CommandApdu(final CommandEnum pEnum, final int p1, final int p2, final in

public byte[] toBytes() {
int length = 4; // CLA, INS, P1, P2
if (mData.length != 0) {
if (mData != null && mData.length != 0) {
length += 1; // LC
length += mData.length; // DATA
}
Expand All @@ -77,7 +77,7 @@ public byte[] toBytes() {
index++;
apdu[index] = (byte) mP2;
index++;
if (mData.length != 0) {
if (mData != null && mData.length != 0) {
apdu[index] = (byte) mLc;
index++;
System.arraycopy(mData, 0, apdu, index, mData.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,32 @@ public static List<TLV> getlistTLV(final byte[] pData, final ITag pTag, final bo
return list;
}

/**
* Method used to get Tag value
*
* @param pData
* data
* @param pTag
* tag to find
* @return tag value or null
*/
public static byte[] getValue(final byte[] pData, final ITag pTag) {

byte[] ret = null;

ByteArrayInputStream stream = new ByteArrayInputStream(pData);
if (pData != null) {
ByteArrayInputStream stream = new ByteArrayInputStream(pData);

while (stream.available() > 0) {
while (stream.available() > 0) {

TLV tlv = TLVUtil.getNextTLV(stream);
if (tlv.getTag() == pTag) {
return tlv.getValueBytes();
} else if (tlv.getTag().isConstructed()) {
ret = TLVUtil.getValue(tlv.getValueBytes(), pTag);
if (ret != null) {
break;
TLV tlv = TLVUtil.getNextTLV(stream);
if (tlv.getTag() == pTag) {
return tlv.getValueBytes();
} else if (tlv.getTag().isConstructed()) {
ret = TLVUtil.getValue(tlv.getValueBytes(), pTag);
if (ret != null) {
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.devnied.emvnfccard.provider.PpseProviderVisa2Test;
import com.github.devnied.emvnfccard.provider.PpseProviderVisaTest;
import com.github.devnied.emvnfccard.provider.ProviderAidTest;
import com.github.devnied.emvnfccard.provider.ProviderSelectPaymentEnvTest;
import com.github.devnied.emvnfccard.provider.PseProviderTest;

import fr.devnied.bitlib.BytesUtils;
Expand Down Expand Up @@ -193,11 +194,37 @@ public void testAfl() throws Exception {
}

@Test
public void tesCardType() throws Exception {
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("4000000000000000")).isEqualTo(EMVCardScheme.VISA);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("5000000000000000")).isEqualTo(EMVCardScheme.MASTER_CARD1);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("6200000000000000")).isEqualTo(EMVCardScheme.UNIONPAY);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber(null)).isEqualTo(null);
public void testSelectPaymentEnvironment() throws Exception {
ProviderSelectPaymentEnvTest prov = new ProviderSelectPaymentEnvTest();
prov.setExpectedData("00A404000E325041592E5359532E444446303100");
Whitebox.invokeMethod(new EMVParser(prov, true), EMVParser.class, "selectPaymentEnvironment");
prov.setExpectedData("00A404000E315041592E5359532E444446303100");
Whitebox.invokeMethod(new EMVParser(prov, false), EMVParser.class, "selectPaymentEnvironment");
}

@Test
public void testExtractApplicationLabel() throws Exception {
ProviderSelectPaymentEnvTest prov = new ProviderSelectPaymentEnvTest();
String value = (String) Whitebox
.invokeMethod(
new EMVParser(prov, true),
EMVParser.class,
"extractApplicationLabel",
BytesUtils
.fromString("6F 3B 84 0E 32 50 41 59 2E 53 59 53 2E 44 44 46 30 31 A5 29 BF 0C 26 61 10 4F 07 A0 00 00 00 42 10 10 50 02 43 42 87 01 01 61 12 4F 07 A0 00 00 00 03 10 10 50 04 56 49 53 41 87 01 02 90 00"));
Assertions.assertThat(value).isEqualTo("CB");
value = (String) Whitebox.invokeMethod(new EMVParser(prov, true), EMVParser.class, "extractApplicationLabel",
(byte[]) null);
Assertions.assertThat(value).isEqualTo(null);
}

@Test
public void testSelectAID() throws Exception {
ProviderSelectPaymentEnvTest prov = new ProviderSelectPaymentEnvTest();
prov.setExpectedData("00A4040007A000000042101000");
Whitebox.invokeMethod(new EMVParser(prov, true), EMVParser.class, "selectAID", BytesUtils.fromString("A0000000421010"));
prov.setExpectedData("00A4040000");
Whitebox.invokeMethod(new EMVParser(prov, true), EMVParser.class, "selectAID", (byte[]) null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.devnied.emvnfccard.enums;

import org.fest.assertions.Assertions;
import org.junit.Test;

public class EMVCardSchemeTest {

@Test
public void tesCardType() throws Exception {
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("4000000000000000")).isEqualTo(EMVCardScheme.VISA);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("5000000000000000")).isEqualTo(EMVCardScheme.MASTER_CARD1);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber("6200000000000000")).isEqualTo(EMVCardScheme.UNIONPAY);
Assertions.assertThat(EMVCardScheme.getCardTypeByCardNumber(null)).isEqualTo(null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.devnied.emvnfccard.provider;

import org.fest.assertions.Assertions;

import com.github.devnied.emvnfccard.parser.IProvider;

import fr.devnied.bitlib.BytesUtils;

public class ProviderSelectPaymentEnvTest implements IProvider {

private String expectedData;

@Override
public byte[] transceive(final byte[] pCommand) {
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(pCommand)).isEqualTo(expectedData);
return BytesUtils.fromString("");
}

/**
* Setter for the field expectedData
*
* @param expectedData
* the expectedData to set
*/
public void setExpectedData(final String expectedData) {
this.expectedData = expectedData;
}

}

0 comments on commit a22f9f7

Please sign in to comment.