/** * */ package anoncard.util; /** * @author christoph * */ public class Helpers { /** * Compares the response from an ResponseException (see aspects) with a * expected response. * * @param actual * @param expected * @return */ public static boolean checkResponseInException(ResponseException actual, byte[] expected) { if (actual.getResponse() == null) { return false; } return actual.getResponse().equals(makeResponse(expected)); } /** * Compares the response from an ISOExceptionException (see aspects) with a * expected response. * * @param actual * @param expected * @return */ public static boolean checkErrorCodeInException(ISOExceptionException actual, short expected) { if (actual.getErrorCode() == null) { return false; } return actual.getErrorCode().equals(shortToHexString(expected)); } /** * Makes the buffer of an response apdu to a string. * * @param buffer * @return */ public static String makeResponse(byte[] buffer) { return mixconfig.tools.dataretention.smartcard.Helpers.bytesToHexDigits(buffer); } /** * Makes the error code of an response apdu to a string. * * @param errorCode * @return */ public static String shortToHexString(short errorCode) { String hexString = ""; hexString += mixconfig.tools.dataretention.smartcard.Helpers.toHexChar((errorCode / (16 * 16 * 16))); errorCode %= 16 * 16 * 16; hexString += mixconfig.tools.dataretention.smartcard.Helpers.toHexChar((errorCode / (16 * 16))) + " "; errorCode %= 16 * 16; hexString += mixconfig.tools.dataretention.smartcard.Helpers.toHexChar((errorCode / 16)); errorCode %= 16; hexString += mixconfig.tools.dataretention.smartcard.Helpers.toHexChar(errorCode); return hexString; } /** * Creates a readable output message on case, the actual response is not as * expected. * * @param actual * @param expected * @return */ public static String alertResponseMismatch(ResponseException actual, byte[] expected) { String message = "response mismatch\nactual:\t"; message += actual.getResponse(); message += "\nexpect:\t" + makeResponse(expected); return message; } /** * Creates a readable output message on case, the actual error code is not * as expected. * * @param actual * @param expected * @return */ public static String alertResponseMismatch(ISOExceptionException actual, short expected) { return "response mismatch\nactual:\t" + actual.getErrorCode() + "\nexpect:\t" + shortToHexString(expected); } }