diff options
-rw-r--r-- | src/org/connectbot/util/PubkeyUtils.java | 12 | ||||
-rw-r--r-- | tests/src/org/connectbot/util/PubkeyUtilsTest.java | 62 |
2 files changed, 68 insertions, 6 deletions
diff --git a/src/org/connectbot/util/PubkeyUtils.java b/src/org/connectbot/util/PubkeyUtils.java index db884d2..e10ee20 100644 --- a/src/org/connectbot/util/PubkeyUtils.java +++ b/src/org/connectbot/util/PubkeyUtils.java @@ -338,17 +338,17 @@ public class PubkeyUtils { return sb.toString(); } - final static private char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', + private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - private static String encodeHex(byte[] bytes) { - char[] hex = new char[bytes.length * 2]; + protected static String encodeHex(byte[] bytes) { + final char[] hex = new char[bytes.length * 2]; int i = 0; for (byte b : bytes) { - hex[i++] = hexDigit[(b >> 4) & 0x0f]; - hex[i++] = hexDigit[b & 0x0f]; + hex[i++] = HEX_DIGITS[(b >> 4) & 0x0f]; + hex[i++] = HEX_DIGITS[b & 0x0f]; } - return new String(hex); + return String.valueOf(hex); } } diff --git a/tests/src/org/connectbot/util/PubkeyUtilsTest.java b/tests/src/org/connectbot/util/PubkeyUtilsTest.java new file mode 100644 index 0000000..635b3be --- /dev/null +++ b/tests/src/org/connectbot/util/PubkeyUtilsTest.java @@ -0,0 +1,62 @@ +/* + * ConnectBot: simple, powerful, open-source SSH client for Android + * Copyright 2007 Kenny Root, Jeffrey Sharkey + * + * Licensed 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.connectbot.util; + +import java.util.Arrays; + +import android.test.AndroidTestCase; + +/** + * @author Kenny Root + * + */ +public class PubkeyUtilsTest extends AndroidTestCase { + public void testEncodeHex_Null_Failure() throws Exception { + try { + PubkeyUtils.encodeHex(null); + fail("Should throw null pointer exception when argument is null"); + } catch (NullPointerException e) { + // success + } + } + public void testEncodeHex_Success() throws Exception { + byte[] input = {(byte) 0xFF, 0x00, (byte) 0xA5, 0x5A, 0x12, 0x23}; + String expected = "ff00a55a1223"; + + assertEquals("Encoded hex should match expected", + PubkeyUtils.encodeHex(input), expected); + } + + public void testSha256_Empty_Success() throws Exception { + byte[] empty_hashed = new byte[] { + (byte) 0xe3, (byte) 0xb0, (byte) 0xc4, (byte) 0x42, + (byte) 0x98, (byte) 0xfc, (byte) 0x1c, (byte) 0x14, + (byte) 0x9a, (byte) 0xfb, (byte) 0xf4, (byte) 0xc8, + (byte) 0x99, (byte) 0x6f, (byte) 0xb9, (byte) 0x24, + (byte) 0x27, (byte) 0xae, (byte) 0x41, (byte) 0xe4, + (byte) 0x64, (byte) 0x9b, (byte) 0x93, (byte) 0x4c, + (byte) 0xa4, (byte) 0x95, (byte) 0x99, (byte) 0x1b, + (byte) 0x78, (byte) 0x52, (byte) 0xb8, (byte) 0x55, + }; + + final byte[] empty = new byte[] {}; + + assertTrue("Empty string should be equal to known test vector", + Arrays.equals(empty_hashed, PubkeyUtils.sha256(empty))); + } +} |