summaryrefslogtreecommitdiffstats
path: root/appc-common/src/main
diff options
context:
space:
mode:
authorJakub Dudycz <jakub.dudycz@nokia.com>2018-03-15 12:37:36 +0100
committerTakamune Cho <tc012c@att.com>2018-03-15 17:13:52 +0000
commitec04cd3aee774103a5af4b0b18a9c772ef5f1754 (patch)
tree436b074c7c56934e82602bb98fe3be7a9167ecbb /appc-common/src/main
parent8c466ef8e3546249a314e12b40010ee1d4094b25 (diff)
HexHelper unit tests
Improved code coverage. Change-Id: Ic3503ae79199767f3162c861f13ff1ecc889e1ec Issue-ID: APPC-742 Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-common/src/main')
-rw-r--r--appc-common/src/main/java/org/onap/appc/encryption/EncryptionException.java12
-rw-r--r--appc-common/src/main/java/org/onap/appc/encryption/HexHelper.java117
2 files changed, 67 insertions, 62 deletions
diff --git a/appc-common/src/main/java/org/onap/appc/encryption/EncryptionException.java b/appc-common/src/main/java/org/onap/appc/encryption/EncryptionException.java
index 519933598..20bcdd2ac 100644
--- a/appc-common/src/main/java/org/onap/appc/encryption/EncryptionException.java
+++ b/appc-common/src/main/java/org/onap/appc/encryption/EncryptionException.java
@@ -24,15 +24,9 @@
package org.onap.appc.encryption;
-public class EncryptionException extends RuntimeException {
+public class EncryptionException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 8259594446628138378L;
-
- public EncryptionException(String message, Throwable cause) {
- super(message, cause);
+ public EncryptionException(String message) {
+ super(message);
}
-
}
diff --git a/appc-common/src/main/java/org/onap/appc/encryption/HexHelper.java b/appc-common/src/main/java/org/onap/appc/encryption/HexHelper.java
index 250a17efb..ffea31c1b 100644
--- a/appc-common/src/main/java/org/onap/appc/encryption/HexHelper.java
+++ b/appc-common/src/main/java/org/onap/appc/encryption/HexHelper.java
@@ -50,34 +50,34 @@ public final class HexHelper {
})
public static final String CM_VERSION = "@(#) [version] [crtime]";
- private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
- 'E', 'F' };
+ private static final char[] HEX_TABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
+ 'E', 'F'};
/**
* The logger for this class.
*/
private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
- private static Map<Character, Integer> TextToHex;
+ private static Map<Character, Integer> textToHex;
static {
- TextToHex = new HashMap<>();
- TextToHex.put(Character.valueOf('0'), Integer.valueOf(0));
- TextToHex.put(Character.valueOf('1'), Integer.valueOf(1));
- TextToHex.put(Character.valueOf('2'), Integer.valueOf(2));
- TextToHex.put(Character.valueOf('3'), Integer.valueOf(3));
- TextToHex.put(Character.valueOf('4'), Integer.valueOf(4));
- TextToHex.put(Character.valueOf('5'), Integer.valueOf(5));
- TextToHex.put(Character.valueOf('6'), Integer.valueOf(6));
- TextToHex.put(Character.valueOf('7'), Integer.valueOf(7));
- TextToHex.put(Character.valueOf('8'), Integer.valueOf(8));
- TextToHex.put(Character.valueOf('9'), Integer.valueOf(9));
- TextToHex.put(Character.valueOf('A'), Integer.valueOf(10));
- TextToHex.put(Character.valueOf('B'), Integer.valueOf(11));
- TextToHex.put(Character.valueOf('C'), Integer.valueOf(12));
- TextToHex.put(Character.valueOf('D'), Integer.valueOf(13));
- TextToHex.put(Character.valueOf('E'), Integer.valueOf(14));
- TextToHex.put(Character.valueOf('F'), Integer.valueOf(15));
+ textToHex = new HashMap<>();
+ textToHex.put('0', 0);
+ textToHex.put('1', 1);
+ textToHex.put('2', 2);
+ textToHex.put('3', 3);
+ textToHex.put('4', 4);
+ textToHex.put('5', 5);
+ textToHex.put('6', 6);
+ textToHex.put('7', 7);
+ textToHex.put('8', 8);
+ textToHex.put('9', 9);
+ textToHex.put('A', 10);
+ textToHex.put('B', 11);
+ textToHex.put('C', 12);
+ textToHex.put('D', 13);
+ textToHex.put('E', 14);
+ textToHex.put('F', 15);
}
/**
@@ -88,65 +88,76 @@ public final class HexHelper {
}
/**
- * Converts an array of bytes to the equivalent string representation using hexadecimal notation and returning that
- * representation in a StringBuffer.
- *
- * @param bytes
- * The bytes to be converted to a hexadecimal string
+ * Converts an array of bytes to the equivalent string representation using hexadecimal notation
+ *
+ * @param bytes The bytes to be converted to a hexadecimal string
* @return The string representation
*/
- public static StringBuffer convertBytesToHexSB(byte[] bytes) {
- StringBuffer sb = new StringBuffer(bytes.length * 2);
- int byteLen = bytes.length;
- for (int index = 0; index < byteLen; index++) {
+ public static String convertBytesToHex(byte[] bytes) throws EncryptionException{
+
+ if (bytes == null)
+ throw new EncryptionException("Given byte array is null");
+
+ StringBuilder builder = new StringBuilder(bytes.length * 2);
+ for (byte aByte : bytes) {
char tempChar;
// Get the first 4 bits (high) Do bitwise logical AND to get rid of
// low nibble. Shift results to right by 4 and get char
// representation
- tempChar = HEX_TABLE[((bytes[index] & 0xf0) >>> 4)];
- sb.append(tempChar);
+ tempChar = HEX_TABLE[(aByte & 0xf0) >>> 4];
+ builder.append(tempChar);
// Get the last 4 bits (low) Do bitwise logical AND to get rid of
// high nibble. Get char representation
- tempChar = HEX_TABLE[(bytes[index] & 0x0f)];
- sb.append(tempChar);
+ tempChar = HEX_TABLE[aByte & 0x0f];
+ builder.append(tempChar);
}
- return sb;
+ return builder.toString();
}
/**
* Converts a hexadecimal string representation of a binary value to an array of bytes
- *
- * @param hexValue
- * The hex representation string to be converted
+ *
+ * @param hexValue The hex representation string to be converted
* @return The array of bytes that contains the binary value
*/
@SuppressWarnings("nls")
- public static byte[] convertHexToBytes(String hexValue) {
- byte[] bytes = null;
+ public static byte[] convertHexToBytes(String hexValue) throws EncryptionException {
+
+ if (hexValue ==null)
+ throw new EncryptionException("Given hex value is null");
+
+ byte[] bytes;
byte high;
byte low;
char hexChar;
- StringBuffer buffer = new StringBuffer(hexValue.toUpperCase());
- if (buffer.length() % 2 != 0) {
- LOG.warn("Invalid HEX value length. "
- + "The length of the value has to be a multiple of 2. Prepending '0' value.");
- buffer.insert(0, '0');
+ StringBuilder builder = new StringBuilder(hexValue.toUpperCase());
+ if (builder.length() % 2 != 0) {
+ LOG.warn("Invalid HEX value length. The length of the value has to be a multiple of 2."
+ + " Prepending '0' value.");
+ builder.insert(0, '0');
}
- int hexLength = buffer.length();
+ int hexLength = builder.length();
int byteLength = hexLength / 2;
bytes = new byte[byteLength];
- for (int index = 0; index < hexLength; index += 2) {
- hexChar = buffer.charAt(index);
- high = (TextToHex.get(Character.valueOf(hexChar))).byteValue();
- high = (byte) (high << 4);
- hexChar = buffer.charAt(index + 1);
- low = (TextToHex.get(Character.valueOf(hexChar))).byteValue();
- high = (byte) (high | low);
- bytes[index / 2] = high;
+ try {
+ for (int index = 0; index < hexLength; index += 2) {
+ hexChar = builder.charAt(index);
+ high = textToHex.get(hexChar).byteValue();
+ high = (byte) (high << 4);
+ hexChar = builder.charAt(index + 1);
+ low = textToHex.get(hexChar).byteValue();
+ high = (byte) (high | low);
+ bytes[index / 2] = high;
+ }
+ }
+ catch (NullPointerException e){
+ LOG.error("Given string contains not hexadecimal values", e);
+ throw new EncryptionException("Given string contains not hexadecimal values");
}
+
return bytes;
}
}