You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/11/30 08:09:36 UTC
directory-kerby git commit: Consolidate the relevant hex-bytes
converting utilities for more tests
Repository: directory-kerby
Updated Branches:
refs/heads/master 78f67dc07 -> ca1e8c10e
Consolidate the relevant hex-bytes converting utilities for more tests
Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/ca1e8c10
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/ca1e8c10
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/ca1e8c10
Branch: refs/heads/master
Commit: ca1e8c10e12fc3b614a771d121d16acc3b09fdbb
Parents: 78f67dc
Author: Kai Zheng <ka...@intel.com>
Authored: Mon Nov 30 15:09:12 2015 +0800
Committer: Kai Zheng <ka...@intel.com>
Committed: Mon Nov 30 15:09:12 2015 +0800
----------------------------------------------------------------------
.../java/org/apache/kerby/asn1/HexUtil.java | 113 ++++++++++++++
.../org/apache/kerby/asn1/TestAsn1Boolean.java | 4 +-
.../apache/kerby/asn1/TestAsn1Collection.java | 4 +-
.../org/apache/kerby/asn1/TestAsn1Integer.java | 4 +-
.../kerby/asn1/TestAsn1ObjectIdentifier.java | 4 +-
.../org/apache/kerby/asn1/TestAsn1UtcTime.java | 4 +-
.../apache/kerby/asn1/TestPersonnelRecord.java | 20 +--
.../test/java/org/apache/kerby/asn1/Util.java | 153 -------------------
8 files changed, 133 insertions(+), 173 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
new file mode 100644
index 0000000..a2a167f
--- /dev/null
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
@@ -0,0 +1,113 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.kerby.asn1;
+
+/**
+ * This is only for test, be careful when use in production codes.
+ */
+public class HexUtil {
+
+ static final String HEX_CHARS_STR = "0123456789ABCDEF";
+ static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray();
+
+ /**
+ * Convert bytes into friendly format as:
+ * 0x02 02 00 80
+ */
+ public static String bytesToHexFriendly(byte[] bytes) {
+ int len = bytes.length * 2;
+ len += bytes.length; // for ' ' appended for each char
+ len += 2; // for '0x' prefix
+ char[] hexChars = new char[len];
+ hexChars[0] = '0';
+ hexChars[1] = 'x';
+ for (int j = 0; j < bytes.length; j++) {
+ int v = bytes[j] & 0xFF;
+ hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4];
+ hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F];
+ hexChars[j * 3 + 4] = ' ';
+ }
+
+ return new String(hexChars);
+ }
+
+ /**
+ * Convert friendly hex string like follows into byte array
+ * 0x02 02 00 80
+ */
+ public static byte[] hex2bytesFriendly(String hexString) {
+ hexString = hexString.toUpperCase();
+ String hexStr = hexString;
+ if (hexString.startsWith("0X")) {
+ hexStr = hexString.substring(2);
+ }
+ String[] hexParts = hexStr.split(" ");
+
+ byte[] bytes = new byte[hexParts.length];
+ char[] hexPart;
+ for (int i = 0; i < hexParts.length; ++i) {
+ hexPart = hexParts[i].toCharArray();
+ if (hexPart.length != 2) {
+ throw new IllegalArgumentException("Invalid hex string to convert");
+ }
+ bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4) +
+ HEX_CHARS_STR.indexOf(hexPart[1]));
+ }
+
+ return bytes;
+ }
+
+ /**
+ * Convert bytes into format as:
+ * 02020080
+ * @param bytes The bytes
+ * @return The hex string
+ */
+ public static String bytesToHex(byte[] bytes) {
+ int len = bytes.length * 2;
+ char[] hexChars = new char[len];
+ for (int j = 0; j < bytes.length; j++) {
+ int v = bytes[j] & 0xFF;
+ hexChars[j * 2] = HEX_CHARS[v >>> 4];
+ hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
+ }
+
+ return new String(hexChars);
+ }
+
+ /**
+ * Convert hex string like follows into byte array
+ * 02020080
+ * @param hexString The hex string
+ * @return The bytes
+ */
+ public static byte[] hex2bytes(String hexString) {
+ hexString = hexString.toUpperCase();
+ int len = hexString.length() / 2;
+ byte[] bytes = new byte[len];
+ char[] hexChars = hexString.toCharArray();
+ for (int i = 0, j = 0; i < len; ++i) {
+ bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4) +
+ HEX_CHARS_STR.indexOf(hexChars[j++]));
+ }
+
+ return bytes;
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
index 177ddef..227579c 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Boolean.java
@@ -36,7 +36,7 @@ public class TestAsn1Boolean {
private void testEncodingWith(Boolean value, String expectedEncoding,
boolean isDer) {
- byte[] expected = Util.hex2bytes(expectedEncoding);
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
Asn1Boolean aValue = new Asn1Boolean(value);
if (isDer) {
aValue.useDER();
@@ -63,7 +63,7 @@ public class TestAsn1Boolean {
} else {
decoded.useBER();
}
- decoded.decode(Util.hex2bytes(content));
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
assertThat(decoded.getValue()).isEqualTo(expectedValue);
}
}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
index cfb5e29..d7210fe 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Collection.java
@@ -33,8 +33,8 @@ public class TestAsn1Collection {
static final String TEST_STR = "Jones";
static final Boolean TEST_BOOL = true;
static final byte[] EXPECTED_BYTES = new byte[] {(byte) 0x30, (byte) 0x0A,
- (byte) 0x16, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E, (byte) 0x65, (byte) 0x73,
- (byte) 0x01, (byte) 0x01, (byte) 0xFF
+ (byte) 0x16, (byte) 0x05, (byte) 0x4A, (byte) 0x6F, (byte) 0x6E,
+ (byte) 0x65, (byte) 0x73, (byte) 0x01, (byte) 0x01, (byte) 0xFF
};
@Test
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Integer.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Integer.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Integer.java
index 8eb4b4d..1e33bf3 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Integer.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1Integer.java
@@ -42,7 +42,7 @@ public class TestAsn1Integer {
}
private void testEncodingWith(int value, String expectedEncoding) {
- byte[] expected = Util.hex2bytes(expectedEncoding);
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
Asn1Integer aValue = new Asn1Integer(value);
aValue.useDER();
byte[] encodingBytes = aValue.encode();
@@ -65,7 +65,7 @@ public class TestAsn1Integer {
private void testDecodingWith(int expectedValue, String content) throws IOException {
Asn1Integer decoded = new Asn1Integer();
decoded.useDER();
- decoded.decode(Util.hex2bytes(content));
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
assertThat(decoded.getValue().intValue()).isEqualTo(expectedValue);
}
}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1ObjectIdentifier.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1ObjectIdentifier.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1ObjectIdentifier.java
index 726c23e..75b8d4e 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1ObjectIdentifier.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1ObjectIdentifier.java
@@ -43,7 +43,7 @@ public class TestAsn1ObjectIdentifier {
}
private void testEncodingWith(String oid, String expectedEncoding) {
- byte[] expected = Util.hex2bytes(expectedEncoding);
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
Asn1ObjectIdentifier aValue = new Asn1ObjectIdentifier(oid);
aValue.useDER();
byte[] encodingBytes = aValue.encode();
@@ -59,7 +59,7 @@ public class TestAsn1ObjectIdentifier {
private void testDecodingWith(String expectedValue, String content) throws IOException {
Asn1ObjectIdentifier decoded = new Asn1ObjectIdentifier();
decoded.useDER();
- decoded.decode(Util.hex2bytes(content));
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
assertThat(decoded.getValue()).isEqualTo(expectedValue);
}
}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1UtcTime.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1UtcTime.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1UtcTime.java
index ebc1509..efef9ae 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1UtcTime.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestAsn1UtcTime.java
@@ -46,7 +46,7 @@ public class TestAsn1UtcTime {
}
private void testEncodingWith(Date value, String expectedEncoding) {
- byte[] expected = Util.hex2bytes(expectedEncoding);
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
Asn1UtcTime aValue = new Asn1UtcTime(value);
aValue.useDER();
byte[] encodingBytes = aValue.encode();
@@ -65,7 +65,7 @@ public class TestAsn1UtcTime {
private void testDecodingWith(Date expectedValue, String content) throws IOException {
Asn1UtcTime decoded = new Asn1UtcTime();
decoded.useDER();
- decoded.decode(Util.hex2bytes(content));
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
assertThat(decoded.getValue()).isEqualTo(expectedValue);
}
}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestPersonnelRecord.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestPersonnelRecord.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestPersonnelRecord.java
index 21413e8..7f97293 100644
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestPersonnelRecord.java
+++ b/kerby-asn1/src/test/java/org/apache/kerby/asn1/TestPersonnelRecord.java
@@ -39,30 +39,30 @@ public class TestPersonnelRecord {
if (verbose) {
System.out.println("Name:");
- System.out.println(Util.bytesToHex(pr.getName().encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getName().encode()));
/*
System.out.println("Title:");
- System.out.println(Util.bytesToHex(pr.getFieldAs(1, Asn1VisibleString.class).encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getFieldAs(1, Asn1VisibleString.class).encode()));
System.out.println("EmployeeNumber:");
- System.out.println(Util.bytesToHex(pr.getFieldAs(2, EmployeeNumber.class).encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getFieldAs(2, EmployeeNumber.class).encode()));
*/
System.out.println("DateOfHire:");
- System.out.println(Util.bytesToHex(pr.getDateOfHire().encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getDateOfHire().encode()));
System.out.println("SpouseName:");
- System.out.println(Util.bytesToHex(pr.getNameOfSpouse().encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getNameOfSpouse().encode()));
System.out.println("Child1:");
- System.out.println(Util.bytesToHex(pr.getChildren().getElements().get(0).encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().getElements().get(0).encode()));
System.out.println("Child2:");
- System.out.println(Util.bytesToHex(pr.getChildren().getElements().get(1).encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().getElements().get(1).encode()));
System.out.println("Children:");
- System.out.println(Util.bytesToHex(pr.getChildren().encode()));
+ System.out.println(HexUtil.bytesToHexFriendly(pr.getChildren().encode()));
}
byte[] data = TestData.createSammplePersonnelEncodingData();
@@ -70,10 +70,10 @@ public class TestPersonnelRecord {
if (verbose) {
System.out.println("ExpectedData:");
- System.out.println(Util.bytesToHex(data));
+ System.out.println(HexUtil.bytesToHexFriendly(data));
System.out.println("Encoded:");
- System.out.println(Util.bytesToHex(encoded));
+ System.out.println(HexUtil.bytesToHexFriendly(encoded));
}
assertThat(encoded).isEqualTo(data);
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/ca1e8c10/kerby-asn1/src/test/java/org/apache/kerby/asn1/Util.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Util.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/Util.java
deleted file mode 100644
index 679a23b..0000000
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/Util.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- */
-package org.apache.kerby.asn1;
-
-public class Util {
-
- static final String HEX_CHARS_STR = "0123456789ABCDEF";
- static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray();
-
- /**
- * Convert bytes into format as:
- * 0x02 02 00 80
- */
- public static String bytesToHex(byte[] bytes) {
- int len = bytes.length * 2;
- len += bytes.length; // for ' ' appended for each char
- len += 2; // for '0x' prefix
- char[] hexChars = new char[len];
- hexChars[0] = '0';
- hexChars[1] = 'x';
- for (int j = 0; j < bytes.length; j++) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4];
- hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F];
- hexChars[j * 3 + 4] = ' ';
- }
-
- return new String(hexChars);
- }
-
- /**
- * Convert hex string like follows into byte array
- * 0x02 02 00 80
- */
- public static byte[] hex2bytes(String hexString) {
- if (hexString == null) {
- throw new IllegalArgumentException("Invalid hex string to convert : null");
- }
-
- char[] hexStr = hexString.toCharArray();
-
- if (hexStr.length < 4) {
- throw new IllegalArgumentException("Invalid hex string to convert : length below 4");
- }
-
- if ((hexStr[0] != '0') || ((hexStr[1] != 'x') && (hexStr[1] != 'X'))) {
- throw new IllegalArgumentException("Invalid hex string to convert : not starting with '0x'");
- }
-
- byte[] bytes = new byte[(hexStr.length - 1) / 3];
- int pos = 0;
- boolean high = false;
- boolean prefix = true;
-
- for (char c : hexStr) {
- if (prefix) {
- if ((c == 'x') || (c == 'X')) {
- prefix = false;
- }
-
- continue;
- }
-
- switch (c) {
- case ' ' :
- if (high) {
- // We have had only the high part
- throw new IllegalArgumentException("Invalid hex string to convert");
- }
-
- // A hex pair has been decoded
- pos++;
- high = false;
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if (high) {
- bytes[pos] += (byte) (c - '0');
- } else {
- bytes[pos] = (byte) ((c - '0') << 4);
- }
-
- high = !high;
- break;
-
- case 'a' :
- case 'b' :
- case 'c' :
- case 'd' :
- case 'e' :
- case 'f' :
- if (high) {
- bytes[pos] += (byte) (c - 'a' + 10);
- } else {
- bytes[pos] = (byte) ((c - 'a' + 10) << 4);
- }
-
- high = !high;
- break;
-
- case 'A' :
- case 'B' :
- case 'C' :
- case 'D' :
- case 'E' :
- case 'F' :
- if (high) {
- bytes[pos] += (byte) (c - 'A' + 10);
- } else {
- bytes[pos] = (byte) ((c - 'A' + 10) << 4);
- }
-
- high = !high;
- break;
-
- default :
- throw new IllegalArgumentException("Invalid hex string to convert");
- }
- }
-
- if (high) {
- throw new IllegalArgumentException("Invalid hex string to convert");
- }
-
- return bytes;
- }
-}