diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/com/isode/stroke/stringcodecs/Base64Test.java | 70 | ||||
-rw-r--r-- | test/com/isode/stroke/stringcodecs/HMACSHA1Test.java | 34 | ||||
-rw-r--r-- | test/com/isode/stroke/stringcodecs/PBKDF2Test.java | 56 | ||||
-rw-r--r-- | test/com/isode/stroke/stringcodecs/SHA1Test.java | 44 |
4 files changed, 126 insertions, 78 deletions
diff --git a/test/com/isode/stroke/stringcodecs/Base64Test.java b/test/com/isode/stroke/stringcodecs/Base64Test.java new file mode 100644 index 0000000..7365955 --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/Base64Test.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2010 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.stringcodecs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.stringcodecs.Base64; + +public class Base64Test { + + @Test + public void testEncodeDecodeAllChars() { + ByteArray input = new ByteArray(); + for (int i = 0; i < 255; ++i) { + char c = (char)i; + input.append((byte)c); + } + String result = Base64.encode(input); + + assertEquals(("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+"), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncodeDecodeOneBytePadding() { + ByteArray input = new ByteArray("ABCDE"); + + String result = Base64.encode(input); + + assertEquals(("QUJDREU="), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncodeDecodeTwoBytesPadding() { + ByteArray input = new ByteArray("ABCD"); + + String result = Base64.encode(input); + + assertEquals(("QUJDRA=="), result); + assertEquals(input, Base64.decode(result)); + } + + @Test + public void testEncode_NoData() { + String result = (Base64.encode(new ByteArray())); + assertEquals((""), result); + } + + @Test + public void testDecode_NoData() { + ByteArray result = (Base64.decode("")); + assertEquals(new ByteArray(), result); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java b/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java deleted file mode 100644 index dedcd7c..0000000 --- a/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tron?on. - * All rights reserved. - */ - -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; - -public class HMACSHA1Test { - - private ByteArray cast(int[] source) { - byte[] result = new byte[source.length]; - for (int i = 0; i < source.length; i++) { - result[i] = (byte)source[i]; - } - return new ByteArray(result); - } - - @Test - public void testGetResult() { - ByteArray result = HMACSHA1.getResult(new ByteArray("foo"), new ByteArray("foobar")); - assertEquals(cast(new int[]{0xa4, 0xee, 0xba, 0x8e, 0x63, 0x3d, 0x77, 0x88, 0x69, 0xf5, 0x68, 0xd0, 0x5a, 0x1b, 0x3d, 0xc7, 0x2b, 0xfd, 0x4, 0xdd}), result); - } -} diff --git a/test/com/isode/stroke/stringcodecs/PBKDF2Test.java b/test/com/isode/stroke/stringcodecs/PBKDF2Test.java new file mode 100644 index 0000000..e76ca5e --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/PBKDF2Test.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2010-2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ +/* + * Copyright (c) 2015 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +package com.isode.stroke.stringcodecs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Test; +import org.junit.Before; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.base.SafeByteArray; +import com.isode.stroke.stringcodecs.PBKDF2; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.crypto.JavaCryptoProvider; + +public class PBKDF2Test { + + private CryptoProvider crypto; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + } + + @Test + public void testGetResult_I1() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 1, crypto); + + assertEquals(new ByteArray("0c60c80f961f0e71f3a9b524af6012062fe037a6"), new ByteArray(Hexify.hexify(result))); + } + + @Test + public void testGetResult_I2() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 2, crypto); + + assertEquals(new ByteArray("ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"), new ByteArray(Hexify.hexify(result))); + } + + @Test + public void testGetResult_I4096() { + ByteArray result = PBKDF2.encode(new SafeByteArray("password"), new ByteArray("salt"), 4096, crypto); + + assertEquals(new ByteArray("4b007901b765489abead49d926f721d065a429c1"), new ByteArray(Hexify.hexify(result))); + } +} diff --git a/test/com/isode/stroke/stringcodecs/SHA1Test.java b/test/com/isode/stroke/stringcodecs/SHA1Test.java deleted file mode 100644 index c8c8a9b..0000000 --- a/test/com/isode/stroke/stringcodecs/SHA1Test.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. - */ -/* - * Copyright (c) 2010, Remko Tron?on. - * All rights reserved. - */ -package com.isode.stroke.stringcodecs; - -import com.isode.stroke.base.ByteArray; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; - -public class SHA1Test { - - public SHA1Test() { - } - - @Test - public void testGetHash() { - ByteArray result = new ByteArray(SHA1.getHash(new ByteArray("client/pc//Exodus 0.9.1<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<"))); - ByteArray target = new ByteArray(new byte[]{(byte) 0x42, (byte) 0x06, (byte) 0xb2, (byte) 0x3c, (byte) 0xa6, (byte) 0xb0, (byte) 0xa6, (byte) 0x43, (byte) 0xd2, (byte) 0x0d, (byte) 0x89, (byte) 0xb0, (byte) 0x4f, (byte) 0xf5, (byte) 0x8c, (byte) 0xf7, (byte) 0x8b, (byte) 0x80, (byte) 0x96, (byte) 0xed}); - assertEquals(target, result); - } - - @Test - public void testGetHash_Twice() { - ByteArray input = new ByteArray("client/pc//Exodus 0.9.1<http://jabber.org/protocol/caps<http://jabber.org/protocol/disco#info<http://jabber.org/protocol/disco#items<http://jabber.org/protocol/muc<"); - SHA1.getHash(input); - ByteArray result = SHA1.getHash(input); - - assertEquals(new ByteArray(new byte[]{(byte) 0x42, (byte) 0x06, (byte) 0xb2, (byte) 0x3c, (byte) 0xa6, (byte) 0xb0, (byte) 0xa6, (byte) 0x43, (byte) 0xd2, (byte) 0x0d, (byte) 0x89, (byte) 0xb0, (byte) 0x4f, (byte) 0xf5, (byte) 0x8c, (byte) 0xf7, (byte) 0x8b, (byte) 0x80, (byte) 0x96, (byte) 0xed}), result); - } - - @Test - public void testGetHash_NoData() { - ByteArray result = SHA1.getHash(new ByteArray()); - - assertEquals(new ByteArray(new byte[]{(byte) 0xda, (byte) 0x39, (byte) 0xa3, (byte) 0xee, (byte) 0x5e, (byte) 0x6b, (byte) 0x4b, (byte) 0x0d, (byte) 0x32, (byte) 0x55, (byte) 0xbf, (byte) 0xef, (byte) 0x95, (byte) 0x60, (byte) 0x18, (byte) 0x90, (byte) 0xaf, (byte) 0xd8, (byte) 0x07, (byte) 0x09}), result); - } -} |