diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
commit | 2da71a8a85486a494343f1662d64fb5ae5a2a44e (patch) | |
tree | 23992f9f2a00bac23b345e5c2cc9c1194efc25be /test | |
download | stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.zip stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.tar.bz2 |
Initial import
Diffstat (limited to 'test')
-rw-r--r-- | test/com/isode/stroke/base/ByteArrayTest.java | 73 | ||||
-rw-r--r-- | test/com/isode/stroke/compress/ZLibCompressorTest.java | 48 | ||||
-rw-r--r-- | test/com/isode/stroke/compress/ZLibDecompressorTest.java | 72 | ||||
-rw-r--r-- | test/com/isode/stroke/parser/XMLParserTest.java | 316 | ||||
-rw-r--r-- | test/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticatorTest.java | 220 | ||||
-rw-r--r-- | test/com/isode/stroke/stringcodecs/HMACSHA1Test.java | 34 | ||||
-rw-r--r-- | test/com/isode/stroke/stringcodecs/SHA1Test.java | 44 |
7 files changed, 807 insertions, 0 deletions
diff --git a/test/com/isode/stroke/base/ByteArrayTest.java b/test/com/isode/stroke/base/ByteArrayTest.java new file mode 100644 index 0000000..183ae76 --- /dev/null +++ b/test/com/isode/stroke/base/ByteArrayTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010-2011, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.base; + +import java.io.UnsupportedEncodingException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ByteArrayTest { + @Test + public void testASCIIString_fromString() { + String testling = "Wheeblahpling"; + assertEquals(testling, new ByteArray(testling).toString()); + } + + @Test + public void testASCIIString_fromBytesSimple() throws UnsupportedEncodingException { + String testling = "WheeBlahBlahPling"; + byte[] bytes = testling.getBytes("UTF-8"); + assertEquals(testling, new ByteArray(bytes).toString()); + } + + @Test + public void testASCIIString_fromBytes() throws UnsupportedEncodingException { + String target = "ABCZ"; + byte[] bytes = {65, 66, 67, 90}; + assertEquals(target, new ByteArray(bytes).toString()); + } + + @Test + public void testExtendedString_fromString() { + String testling = "Wheeblahpling\u0041\u00DF\u00F7"; + assertEquals(testling, new ByteArray(testling).toString()); + } + + @Test + public void testExtendedString_fromBytes() { + String target = "ABCZ\u0041\u00DF\u00F7"; + byte[] bytes = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f, 0xc3, 0xb7}); + assertEquals(target, new ByteArray(bytes).toString()); + } + + @Test + public void testExtendedString_fromBytesSegmented() { + String target = "ABCZ\u0041\u00DF\u00F7"; + byte[] bytes = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f}); + ByteArray testling = new ByteArray(bytes); + testling.append((byte)0xc3); + testling.append((byte)0xb7); + assertEquals(target, testling.toString()); + } + + @Test + public void testExtendedBytes_fromString() { + String string = "ABCZ\u0041\u00DF\u00F7"; + byte[] target = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f, 0xc3, 0xb7}); + assertArrayEquals(target, new ByteArray(string).getData()); + } + + private byte[] byteify(int[] ints) { + byte[] bytes = new byte[ints.length]; + for (int i = 0; i < ints.length; i++) { + int j = ints[i]; + bytes[i] = (byte)j; + } + return bytes; + } +} diff --git a/test/com/isode/stroke/compress/ZLibCompressorTest.java b/test/com/isode/stroke/compress/ZLibCompressorTest.java new file mode 100644 index 0000000..3f9593b --- /dev/null +++ b/test/com/isode/stroke/compress/ZLibCompressorTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2010 Remko Tron¨on + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ +/* + * Copyright (c) 2010-2011, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.compress; + +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.stringcodecs.Hexify; +import javax.xml.bind.annotation.adapters.HexBinaryAdapter; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Kev + */ +public class ZLibCompressorTest { + + @Test + public void testProcess() throws Exception { + ZLibCompressor testling = new ZLibCompressor(); + ByteArray result = testling.process(new ByteArray("foo")); + + assertEquals("78da4acbcf07000000ffff", Hexify.hexify(result)); + } + + @Test + public void testProcess_Twice() throws ZLibException { + ZLibCompressor testling = new ZLibCompressor(); + testling.process(new ByteArray("foo")); + ByteArray result = testling.process(new ByteArray("bar")); + + assertEquals("4a4a2c02000000ffff", Hexify.hexify(result)); + } + + public static ByteArray unhex(String string) { + HexBinaryAdapter adaptor = new HexBinaryAdapter(); + return new ByteArray(adaptor.unmarshal(string)); + } +} diff --git a/test/com/isode/stroke/compress/ZLibDecompressorTest.java b/test/com/isode/stroke/compress/ZLibDecompressorTest.java new file mode 100644 index 0000000..838a324 --- /dev/null +++ b/test/com/isode/stroke/compress/ZLibDecompressorTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2010 Remko Tron¨on + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ +/* + * Copyright (c) 2010-2011, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.compress; + +import com.isode.stroke.base.ByteArray; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Kev + */ +public class ZLibDecompressorTest { + + @Test + public void testProcess() throws ZLibException { + ZLibDecompressor testling = new ZLibDecompressor(); + ByteArray result = testling.process(ZLibCompressorTest.unhex("78da4acbcf07000000ffff")); + + assertEquals(new ByteArray("foo"), result); + } + + @Test + public void testProcess_Twice() throws ZLibException { + ZLibDecompressor testling = new ZLibDecompressor(); + testling.process(ZLibCompressorTest.unhex("78da4acbcf07000000ffff")); + ByteArray result = testling.process(ZLibCompressorTest.unhex("4a4a2c02000000ffff")); + + assertEquals(new ByteArray("bar"), result); + } + + @Test(expected = ZLibException.class) + public void testProcess_Invalid() throws ZLibException { + ZLibDecompressor testling = new ZLibDecompressor(); + testling.process(new ByteArray("invalid")); + } + + @Test + public void testProcess_Huge() throws ZLibException { + ByteArray data = new ByteArray(); + for (int i = 0; i < 2048; ++i) { + data.append((byte) i); + } + ByteArray original = new ByteArray(data); + ByteArray compressed = new ZLibCompressor().process(original); + ByteArray decompressed = new ZLibDecompressor().process(compressed); + + assertEquals(original, decompressed); + } + + @Test + public void testProcess_ChunkSize() throws ZLibException { + ByteArray data = new ByteArray(); + for (int i = 0; i < 1024; ++i) { + data.append((byte) i); + } + ByteArray original = new ByteArray(data); + ByteArray compressed = new ZLibCompressor().process(original); + ByteArray decompressed = new ZLibDecompressor().process(compressed); + + assertEquals(original, decompressed); + } +} diff --git a/test/com/isode/stroke/parser/XMLParserTest.java b/test/com/isode/stroke/parser/XMLParserTest.java new file mode 100644 index 0000000..f874eea --- /dev/null +++ b/test/com/isode/stroke/parser/XMLParserTest.java @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2010-2011, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tron¨on. + * All rights reserved. + */ +package com.isode.stroke.parser; + +import com.isode.stroke.eventloop.SimpleEventLoop; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class XMLParserTest { + + @Before + public void setUp() { + client_ = new Client(); + } + + private XMLParser parser() { + return PlatformXMLParserFactory.createXMLParser(client_, new SimpleEventLoop()); + } + + private void join(XMLParser parser) { + try { + ((PullXMLParser) parser).getParserThread().join(300); + } catch (InterruptedException ex) { + Logger.getLogger(XMLParserTest.class.getName()).log(Level.SEVERE, null, ex); + } + } + + @Test + public void testParse_characters() { + XMLParser testling = parser(); + + String data = "ABCZ\u0041\u00DF\u00F7\u0410\u0498"; + assertTrue(testling.parse("<body>" + data + "</body>")); + join(testling); + assertEquals(3, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("body", client_.events.get(0).data); + + assertEquals(Client.Type.CharacterData, client_.events.get(1).type); + assertEquals(data, client_.events.get(1).data); + + assertEquals(Client.Type.EndElement, client_.events.get(2).type); + assertEquals("body", client_.events.get(2).data); + + } + + @Test + public void testParse_NestedElements() { + XMLParser testling = parser(); + + assertTrue(testling.parse( + "<iq type=\"get\">" + + "<query xmlns='jabber:iq:version'/>" + + "</iq>")); + + join(testling); + + assertEquals(4, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("iq", client_.events.get(0).data); + assertEquals(1, client_.events.get(0).attributes.size()); + assertEquals("get", client_.events.get(0).attributes.get("type")); + assertEquals("", client_.events.get(0).ns); + + assertEquals(Client.Type.StartElement, client_.events.get(1).type); + assertEquals("query", client_.events.get(1).data); + assertEquals(0, client_.events.get(1).attributes.size()); + assertEquals("jabber:iq:version", client_.events.get(1).ns); + + assertEquals(Client.Type.EndElement, client_.events.get(2).type); + assertEquals("query", client_.events.get(2).data); + assertEquals("jabber:iq:version", client_.events.get(2).ns); + + assertEquals(Client.Type.EndElement, client_.events.get(3).type); + assertEquals("iq", client_.events.get(3).data); + assertEquals("", client_.events.get(3).ns); + } + + @Test + public void testParse_ElementInNamespacedElement() { + XMLParser testling = parser(); + + assertTrue(testling.parse( + "<query xmlns='jabber:iq:version'>" + + "<name>Swift</name>" + + "</query>")); + join(testling); + assertEquals(5, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("query", client_.events.get(0).data); + assertEquals(0, client_.events.get(0).attributes.size()); + assertEquals("jabber:iq:version", client_.events.get(0).ns); + + assertEquals(Client.Type.StartElement, client_.events.get(1).type); + assertEquals("name", client_.events.get(1).data); + assertEquals("jabber:iq:version", client_.events.get(1).ns); + + assertEquals(Client.Type.CharacterData, client_.events.get(2).type); + assertEquals("Swift", client_.events.get(2).data); + + assertEquals(Client.Type.EndElement, client_.events.get(3).type); + assertEquals("name", client_.events.get(3).data); + assertEquals("jabber:iq:version", client_.events.get(3).ns); + + assertEquals(Client.Type.EndElement, client_.events.get(4).type); + assertEquals("query", client_.events.get(4).data); + assertEquals("jabber:iq:version", client_.events.get(4).ns); + } + + @Test + public void testParse_CharacterData() { + XMLParser testling = parser(); + + assertTrue(testling.parse("<html>bla<i>bli</i>blo</html>")); + join(testling); + assertEquals(7, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("html", client_.events.get(0).data); + + assertEquals(Client.Type.CharacterData, client_.events.get(1).type); + assertEquals("bla", client_.events.get(1).data); + + assertEquals(Client.Type.StartElement, client_.events.get(2).type); + assertEquals("i", client_.events.get(2).data); + + assertEquals(Client.Type.CharacterData, client_.events.get(3).type); + assertEquals("bli", client_.events.get(3).data); + + assertEquals(Client.Type.EndElement, client_.events.get(4).type); + assertEquals("i", client_.events.get(4).data); + + assertEquals(Client.Type.CharacterData, client_.events.get(5).type); + assertEquals("blo", client_.events.get(5).data); + + assertEquals(Client.Type.EndElement, client_.events.get(6).type); + assertEquals("html", client_.events.get(6).data); + } + + @Test + public void testParse_NamespacePrefix() { + XMLParser testling = parser(); + + assertTrue(testling.parse("<p:x xmlns:p='bla'><p:y/></p:x>")); + join(testling); + assertEquals(4, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("x", client_.events.get(0).data); + assertEquals("bla", client_.events.get(0).ns); + + assertEquals(Client.Type.StartElement, client_.events.get(1).type); + assertEquals("y", client_.events.get(1).data); + assertEquals("bla", client_.events.get(1).ns); + + assertEquals(Client.Type.EndElement, client_.events.get(2).type); + assertEquals("y", client_.events.get(2).data); + assertEquals("bla", client_.events.get(2).ns); + + assertEquals(Client.Type.EndElement, client_.events.get(3).type); + assertEquals("x", client_.events.get(3).data); + assertEquals("bla", client_.events.get(3).ns); + } + + @Test + public void testParse_UnhandledXML() { + XMLParser testling = parser(); + + assertTrue(testling.parse("<iq><!-- Testing --></iq>")); + join(testling); + assertEquals(2, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("iq", client_.events.get(0).data); + + assertEquals(Client.Type.EndElement, client_.events.get(1).type); + assertEquals("iq", client_.events.get(1).data); + } + + //@Test /*TODO: uncomment if we ever get a sane incremental parser */ + public void testParse_InvalidXML() { + XMLParser testling = parser(); + + assertTrue(!testling.parse("<iq><bla></iq>")); + } + + //@Test /*TODO: uncomment if we ever get a sane incremental parser */ + public void testParse_InErrorState() { + XMLParser testling = parser(); + + assertTrue(!testling.parse("<iq><bla></iq>")); + assertTrue(!testling.parse("<iq/>")); + } + + @Test + public void testParse_Incremental() { + XMLParser testling = parser(); + + assertTrue(testling.parse("<iq")); + assertTrue(testling.parse("></iq>")); + join(testling); + assertEquals(2, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("iq", client_.events.get(0).data); + + assertEquals(Client.Type.EndElement, client_.events.get(1).type); + assertEquals("iq", client_.events.get(1).data); + } + + @Test + public void testParse_IncrementalWithCloses() { + XMLParser testling = parser(); + + assertTrue(testling.parse("<iq")); + assertTrue(testling.parse("><></iq>")); + join(testling); + assertEquals(3, client_.events.size()); + + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("iq", client_.events.get(0).data); + + assertEquals(Client.Type.CharacterData, client_.events.get(1).type); + assertEquals("<>", client_.events.get(1).data); + + assertEquals(Client.Type.EndElement, client_.events.get(2).type); + assertEquals("iq", client_.events.get(2).data); + } + + @Test + public void testParse_WhitespaceInAttribute() { + XMLParser testling = parser(); + + assertTrue(testling.parse( + "<query xmlns='http://www.xmpp.org/extensions/xep-0084.html#ns-data '>")); + assertTrue(testling.parse( + "<presence/>")); + join(testling); + assertEquals(3, client_.events.size()); + assertEquals(Client.Type.StartElement, client_.events.get(0).type); + assertEquals("query", client_.events.get(0).data); + assertEquals(Client.Type.StartElement, client_.events.get(1).type); + assertEquals("presence", client_.events.get(1).data); + assertEquals(Client.Type.EndElement, client_.events.get(2).type); + assertEquals("presence", client_.events.get(2).data); + } + + private static class Client implements XMLParserClient { + + public enum Type { + + StartElement, EndElement, CharacterData + }; + + private class Event { + + Event( + Type type, + String data, + String ns, + AttributeMap attributes) { + this.type = type; + this.data = data; + this.ns = ns; + this.attributes = attributes; + } + + Event(Type type, String data, String ns) { + this.type = type; + this.data = data; + this.ns = ns; + } + + Event(Type type, String data) { + this(type, data, ""); + } + Type type; + String data; + String ns; + AttributeMap attributes; + }; + + Client() { + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + events.add(new Event(Type.StartElement, element, ns, attributes)); + } + + public void handleEndElement(String element, String ns) { + events.add(new Event(Type.EndElement, element, ns)); + } + + public void handleCharacterData(String data) { + events.add(new Event(Type.CharacterData, data)); + } + List<Event> events = new ArrayList<Event>(); + }; + private Client client_; +} diff --git a/test/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticatorTest.java b/test/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticatorTest.java new file mode 100644 index 0000000..44a179e --- /dev/null +++ b/test/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticatorTest.java @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2011, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tron?on. + * All rights reserved. + */ +package com.isode.stroke.sasl; + +import com.isode.stroke.base.ByteArray; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Kev + */ +public class SCRAMSHA1ClientAuthenticatorTest { + + @Test + public void testGetInitialResponse() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH"); + testling.setCredentials("user", "pass", ""); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("n,,n=user,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetInitialResponse_UsernameHasSpecialChars() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH"); + testling.setCredentials(",us=,er=", "pass", ""); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("n,,n==2Cus=3D=2Cer=3D,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetInitialResponse_WithAuthorizationID() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH"); + testling.setCredentials("user", "pass", "auth"); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("n,a=auth,n=user,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetInitialResponse_WithAuthorizationIDWithSpecialChars() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH"); + testling.setCredentials("user", "pass", "a=u,th"); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("n,a=a=3Du=2Cth,n=user,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetInitialResponse_WithoutChannelBindingWithTLSChannelBindingData() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH", false); + testling.setTLSChannelBindingData(new ByteArray("xyza")); + testling.setCredentials("user", "pass", ""); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("y,,n=user,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetInitialResponse_WithChannelBindingWithTLSChannelBindingData() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefghABCDEFGH", true); + testling.setTLSChannelBindingData(new ByteArray("xyza")); + testling.setCredentials("user", "pass", ""); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("p=tls-unique,,n=user,r=abcdefghABCDEFGH"), response); + } + + @Test + public void testGetFinalResponse() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + assertTrue(testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096"))); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("c=biws,r=abcdefghABCDEFGH,p=CZbjGDpIteIJwQNBgO0P8pKkMGY="), response); + } + + @Test + public void testGetFinalResponse_WithoutChannelBindingWithTLSChannelBindingData() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh", false); + testling.setCredentials("user", "pass", ""); + testling.setTLSChannelBindingData(new ByteArray("xyza")); + testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("c=eSws,r=abcdefghABCDEFGH,p=JNpsiFEcxZvNZ1+FFBBqrYvYxMk="), response); + } + + @Test + public void testGetFinalResponse_WithChannelBindingWithTLSChannelBindingData() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh", true); + testling.setCredentials("user", "pass", ""); + testling.setTLSChannelBindingData(new ByteArray("xyza")); + testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + + ByteArray response = testling.getResponse(); + + assertEquals(new ByteArray("c=cD10bHMtdW5pcXVlLCx4eXph,r=abcdefghABCDEFGH,p=i6Rghite81P1ype8XxaVAa5l7v0="), response); + } + + @Test + public void testSetFinalChallenge() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + + boolean result = testling.setChallenge(new ByteArray("v=Dd+Q20knZs9jeeK0pi1Mx1Se+yo=")); + + assertTrue(result); + } + + @Test + public void testSetChallenge() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + + assertTrue(result); + } + + @Test + public void testSetChallenge_InvalidClientNonce() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefgiABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + + assertTrue(!result); + } + + @Test + public void testSetChallenge_OnlyClientNonce() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefgh,s=MTIzNDU2NzgK,i=4096")); + + assertTrue(!result); + } + + @Test + public void testSetChallenge_InvalidIterations() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=bla")); + + assertTrue(!result); + } + + @Test + public void testSetChallenge_MissingIterations() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK")); + + assertTrue(!result); + } + + @Test + public void testSetChallenge_ZeroIterations() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=0")); + + assertTrue(!result); + } + + @Test + public void testSetChallenge_NegativeIterations() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + + boolean result = testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=-1")); + + assertTrue(!result); + } + + @Test + public void testSetFinalChallenge_InvalidChallenge() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + boolean result = testling.setChallenge(new ByteArray("v=e26kI69ICb6zosapLLxrER/631A=")); + + assertTrue(!result); + } + + @Test + public void testGetResponseAfterFinalChallenge() { + SCRAMSHA1ClientAuthenticator testling = new SCRAMSHA1ClientAuthenticator("abcdefgh"); + testling.setCredentials("user", "pass", ""); + testling.setChallenge(new ByteArray("r=abcdefghABCDEFGH,s=MTIzNDU2NzgK,i=4096")); + testling.setChallenge(new ByteArray("v=Dd+Q20knZs9jeeK0pi1Mx1Se+yo=")); + + assertTrue(testling.getResponse() == null); + } +} diff --git a/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java b/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java new file mode 100644 index 0000000..dedcd7c --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/HMACSHA1Test.java @@ -0,0 +1,34 @@ +/* + * 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/SHA1Test.java b/test/com/isode/stroke/stringcodecs/SHA1Test.java new file mode 100644 index 0000000..c8c8a9b --- /dev/null +++ b/test/com/isode/stroke/stringcodecs/SHA1Test.java @@ -0,0 +1,44 @@ +/* + * 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); + } +} |