diff options
Diffstat (limited to 'test/com')
4 files changed, 433 insertions, 20 deletions
diff --git a/test/com/isode/stroke/compress/ZLibCompressorTest.java b/test/com/isode/stroke/compress/ZLibCompressorTest.java index 5767415..c9d1f69 100644 --- a/test/com/isode/stroke/compress/ZLibCompressorTest.java +++ b/test/com/isode/stroke/compress/ZLibCompressorTest.java @@ -9,7 +9,7 @@ */ package com.isode.stroke.compress; -import com.isode.stroke.base.ByteArray; +import com.isode.stroke.base.SafeByteArray; import com.isode.stroke.stringcodecs.Hexify; import javax.xml.bind.annotation.adapters.HexBinaryAdapter; @@ -27,7 +27,7 @@ public class ZLibCompressorTest { @Test public void testProcess() throws Exception { ZLibCompressor testling = new ZLibCompressor(); - ByteArray result = testling.process(new ByteArray("foo")); + SafeByteArray result = testling.process(new SafeByteArray("foo")); assertEquals("78da4acbcf07000000ffff", Hexify.hexify(result)); } @@ -35,14 +35,14 @@ public class ZLibCompressorTest { @Test public void testProcess_Twice() throws ZLibException { ZLibCompressor testling = new ZLibCompressor(); - testling.process(new ByteArray("foo")); - ByteArray result = testling.process(new ByteArray("bar")); + testling.process(new SafeByteArray("foo")); + SafeByteArray result = testling.process(new SafeByteArray("bar")); assertEquals("4a4a2c02000000ffff", Hexify.hexify(result)); } - public static ByteArray unhex(String string) { + public static SafeByteArray unhex(String string) { HexBinaryAdapter adaptor = new HexBinaryAdapter(); - return new ByteArray(adaptor.unmarshal(string)); + return new SafeByteArray(adaptor.unmarshal(string)); } } diff --git a/test/com/isode/stroke/compress/ZLibDecompressorTest.java b/test/com/isode/stroke/compress/ZLibDecompressorTest.java index f4f5e06..d691363 100644 --- a/test/com/isode/stroke/compress/ZLibDecompressorTest.java +++ b/test/com/isode/stroke/compress/ZLibDecompressorTest.java @@ -9,7 +9,7 @@ */ package com.isode.stroke.compress; -import com.isode.stroke.base.ByteArray; +import com.isode.stroke.base.SafeByteArray; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -24,48 +24,48 @@ public class ZLibDecompressorTest { @Test public void testProcess() throws ZLibException { ZLibDecompressor testling = new ZLibDecompressor(); - ByteArray result = testling.process(ZLibCompressorTest.unhex("78da4acbcf07000000ffff")); + SafeByteArray result = testling.process(ZLibCompressorTest.unhex("78da4acbcf07000000ffff")); - assertEquals(new ByteArray("foo"), result); + assertEquals(new SafeByteArray("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")); + SafeByteArray result = testling.process(ZLibCompressorTest.unhex("4a4a2c02000000ffff")); - assertEquals(new ByteArray("bar"), result); + assertEquals(new SafeByteArray("bar"), result); } @Test(expected = ZLibException.class) public void testProcess_Invalid() throws ZLibException { ZLibDecompressor testling = new ZLibDecompressor(); - testling.process(new ByteArray("invalid")); + testling.process(new SafeByteArray("invalid")); } @Test public void testProcess_Huge() throws ZLibException { - ByteArray data = new ByteArray(); + SafeByteArray data = new SafeByteArray(); 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); + SafeByteArray original = new SafeByteArray(data); + SafeByteArray compressed = new ZLibCompressor().process(original); + SafeByteArray decompressed = new ZLibDecompressor().process(compressed); assertEquals(original, decompressed); } @Test public void testProcess_ChunkSize() throws ZLibException { - ByteArray data = new ByteArray(); + SafeByteArray data = new SafeByteArray(); 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); + SafeByteArray original = new SafeByteArray(data); + SafeByteArray compressed = new ZLibCompressor().process(original); + SafeByteArray decompressed = new ZLibDecompressor().process(compressed); assertEquals(original, decompressed); } diff --git a/test/com/isode/stroke/streamstack/StreamStackTest.java b/test/com/isode/stroke/streamstack/StreamStackTest.java new file mode 100644 index 0000000..b868a59 --- /dev/null +++ b/test/com/isode/stroke/streamstack/StreamStackTest.java @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2010-2014 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.streamstack; + +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.SafeByteArray; +import com.isode.stroke.streamstack.StreamStack; +import com.isode.stroke.streamstack.LowLayer; +import com.isode.stroke.streamstack.XMPPLayer; +import com.isode.stroke.streamstack.StreamLayer; +import com.isode.stroke.parser.PlatformXMLParserFactory; +import com.isode.stroke.parser.payloadparsers.FullPayloadParserFactoryCollection; +import com.isode.stroke.serializer.payloadserializers.FullPayloadSerializerCollection; +import com.isode.stroke.elements.Element; +import com.isode.stroke.elements.StreamType; +import com.isode.stroke.signals.Slot1; +import java.util.Vector; + +public class StreamStackTest { + + private class MyStreamLayer extends StreamLayer { + + public MyStreamLayer(final String prepend) { + this.prepend_ = prepend; + } + + public void writeData(final SafeByteArray data) { + writeDataToChildLayer(new SafeByteArray(prepend_).append(data)); + } + + public void handleDataRead(final SafeByteArray data) { + writeDataToParentLayer(new SafeByteArray(prepend_).append(data)); + } + + private String prepend_ = ""; + }; + + /* Multiple-inheritance workarounds */ + + private StreamLayer fakeStreamLayer_ = new StreamLayer() { + public void writeData(SafeByteArray data) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void handleDataRead(SafeByteArray data) { + writeDataToParentLayer(data); + } + }; + + private class TestLowLayer implements LowLayer { + + public Vector<SafeByteArray> data_ = new Vector<SafeByteArray>(); + + public TestLowLayer() { + + } + + public void writeData(final SafeByteArray data) { + data_.add(data); + } + + public void onDataRead(SafeByteArray data) { + writeDataToParentLayer(data); + } + + public HighLayer getParentLayer() { + return fakeStreamLayer_.getParentLayer(); + } + + public void setParentLayer(HighLayer highLayer) { + fakeStreamLayer_.setParentLayer(highLayer); + } + + public void writeDataToParentLayer(SafeByteArray data) { + fakeStreamLayer_.writeDataToParentLayer(data); + } + }; + + private FullPayloadParserFactoryCollection parserFactories_ = new FullPayloadParserFactoryCollection(); + private FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); + private TestLowLayer physicalStream_; + private XMPPLayer xmppStream_; + private int elementsReceived_; + private int dataWriteReceived_; + + @Before + public void setUp() { + physicalStream_ = new TestLowLayer(); + xmppStream_ = new XMPPLayer(parserFactories_, serializers_, StreamType.ClientStreamType); + elementsReceived_ = 0; + dataWriteReceived_ = 0; + } + + public void handleElement(Element element) { + ++elementsReceived_; + } + + public void handleWriteData(final SafeByteArray SafeByteArray) { + ++dataWriteReceived_; + } + + @Test + public void testWriteData_NoIntermediateStreamStack() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + + xmppStream_.writeData("foo"); + + assertEquals(1, physicalStream_.data_.size()); + assertEquals(new SafeByteArray("foo"), physicalStream_.data_.get(0)); + } + + @Test + public void testWriteData_OneIntermediateStream() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + MyStreamLayer xStream = new MyStreamLayer("X"); + testling.addLayer(xStream); + + xmppStream_.writeData("foo"); + + assertEquals(1, physicalStream_.data_.size()); + assertEquals(new SafeByteArray("Xfoo"), physicalStream_.data_.get(0)); + } + + @Test + public void testWriteData_TwoIntermediateStreamStack() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + MyStreamLayer xStream = new MyStreamLayer("X"); + MyStreamLayer yStream = new MyStreamLayer("Y"); + testling.addLayer(xStream); + testling.addLayer(yStream); + + xmppStream_.writeData("foo"); + + assertEquals(1, physicalStream_.data_.size()); + assertEquals(new SafeByteArray("XYfoo"), physicalStream_.data_.get(0)); + } + + @Test + public void testReadData_NoIntermediateStreamStack() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + xmppStream_.onElement.connect(new Slot1<Element>() { + @Override + public void call(Element e1) { + handleElement(e1); + } + }); + physicalStream_.onDataRead(new SafeByteArray("<stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + assertEquals(1, elementsReceived_); + } + + @Test + public void testReadData_OneIntermediateStream() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + xmppStream_.onElement.connect(new Slot1<Element>() { + @Override + public void call(Element e1) { + handleElement(e1); + } + }); + MyStreamLayer xStream = new MyStreamLayer("<"); + testling.addLayer(xStream); + + physicalStream_.onDataRead(new SafeByteArray("stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + assertEquals(1, elementsReceived_); + } + + @Test + public void testReadData_TwoIntermediateStreamStack() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + xmppStream_.onElement.connect(new Slot1<Element>() { + @Override + public void call(Element e1) { + handleElement(e1); + } + }); + MyStreamLayer xStream = new MyStreamLayer("s"); + MyStreamLayer yStream = new MyStreamLayer("<"); + testling.addLayer(xStream); + testling.addLayer(yStream); + + physicalStream_.onDataRead(new SafeByteArray("tream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>")); + + assertEquals(1, elementsReceived_); + } + + @Test + public void testAddLayer_ExistingOnWriteDataSlot() { + StreamStack testling = new StreamStack(xmppStream_, physicalStream_); + xmppStream_.onWriteData.connect(new Slot1<SafeByteArray>() { + @Override + public void call(SafeByteArray b1) { + handleWriteData(b1); + } + }); + MyStreamLayer xStream = new MyStreamLayer("X"); + testling.addLayer(xStream); + + xmppStream_.writeData("foo"); + + assertEquals(1, dataWriteReceived_); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/streamstack/XMPPLayerTest.java b/test/com/isode/stroke/streamstack/XMPPLayerTest.java new file mode 100644 index 0000000..1043291 --- /dev/null +++ b/test/com/isode/stroke/streamstack/XMPPLayerTest.java @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2010-2014 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.streamstack; + +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.SafeByteArray; +import com.isode.stroke.streamstack.XMPPLayer; +import com.isode.stroke.streamstack.StreamStack; +import com.isode.stroke.streamstack.LowLayer; +import com.isode.stroke.streamstack.XMPPLayer; +import com.isode.stroke.streamstack.StreamLayer; +import com.isode.stroke.parser.PlatformXMLParserFactory; +import com.isode.stroke.parser.payloadparsers.FullPayloadParserFactoryCollection; +import com.isode.stroke.serializer.payloadserializers.FullPayloadSerializerCollection; +import com.isode.stroke.parser.PayloadParserFactoryCollection; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.elements.ProtocolHeader; +import com.isode.stroke.elements.Element; +import com.isode.stroke.elements.StreamType; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.Slot; +import java.util.Vector; + +public class XMPPLayerTest { + + private class XMPPLayerExposed extends XMPPLayer { + + public XMPPLayerExposed(PayloadParserFactoryCollection payloadParserFactories, PayloadSerializerCollection payloadSerializers, StreamType streamType) { + super(payloadParserFactories, payloadSerializers, streamType); + } + + //using XMPPLayer::handleDataRead; + //using HighLayer::setChildLayer; + }; + + /* Multiple-inheritance workarounds */ + + private StreamLayer fakeStreamLayer_ = new StreamLayer() { + public void writeData(SafeByteArray data) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void handleDataRead(SafeByteArray data) { + writeDataToParentLayer(data); + } + }; + + private class DummyLowLayer implements LowLayer { + + public String writtenData = ""; + + public DummyLowLayer() { + + } + + public void writeData(final SafeByteArray data) { + writtenData += data.toString(); + } + + public HighLayer getParentLayer() { + return fakeStreamLayer_.getParentLayer(); + } + + public void setParentLayer(HighLayer highLayer) { + fakeStreamLayer_.setParentLayer(highLayer); + } + + public void writeDataToParentLayer(SafeByteArray data) { + fakeStreamLayer_.writeDataToParentLayer(data); + } + }; + + private FullPayloadParserFactoryCollection parserFactories_ = new FullPayloadParserFactoryCollection(); + private FullPayloadSerializerCollection serializers_ = new FullPayloadSerializerCollection(); + private DummyLowLayer lowLayer_; + private XMPPLayerExposed testling_; + private PlatformXMLParserFactory xmlParserFactory_; + private int elementsReceived_; + private int errorReceived_; + + @Before + public void setUp() { + lowLayer_ = new DummyLowLayer(); + testling_ = new XMPPLayerExposed(parserFactories_, serializers_, StreamType.ClientStreamType); + testling_.setChildLayer(lowLayer_); + elementsReceived_ = 0; + errorReceived_ = 0; + } + + public void handleElement(Element element) { + ++elementsReceived_; + } + + public void handleElementAndReset(Element element) { + ++elementsReceived_; + testling_.resetParser(); + } + + public void handleError() { + ++errorReceived_; + } + + @Test + public void testParseData_Error() { + testling_.onError.connect(new Slot() { + @Override + public void call() { + handleError(); + } + }); + + testling_.handleDataRead(new SafeByteArray("<iq>")); + + assertEquals(1, errorReceived_); + } + + @Test + public void testResetParser() { + testling_.onElement.connect(new Slot1<Element>() { + @Override + public void call(Element e1) { + handleElement(e1); + } + }); + testling_.onError.connect(new Slot() { + @Override + public void call() { + handleError(); + } + }); + + testling_.handleDataRead(new SafeByteArray("<stream:stream to=\"example.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" >")); + testling_.resetParser(); + testling_.handleDataRead(new SafeByteArray("<stream:stream to=\"example.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" >")); + testling_.handleDataRead(new SafeByteArray("<presence/>")); + + assertEquals(1, elementsReceived_); + assertEquals(0, errorReceived_); + } + + @Test + public void testResetParser_FromSlot() { + testling_.onElement.connect(new Slot1<Element>() { + @Override + public void call(Element e1) { + handleElementAndReset(e1); + } + }); + testling_.handleDataRead(new SafeByteArray("<stream:stream to=\"example.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" ><presence/>")); + testling_.handleDataRead(new SafeByteArray("<stream:stream to=\"example.com\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" ><presence/>")); + + assertEquals(2, elementsReceived_); + assertEquals(0, errorReceived_); + } + + @Test + public void testWriteHeader() { + ProtocolHeader header = new ProtocolHeader(); + header.setTo("example.com"); + testling_.writeHeader(header); + + assertEquals("<?xml version=\"1.0\"?><stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"example.com\" version=\"1.0\">", lowLayer_.writtenData); + } + + @Test + public void testWriteElement() { + testling_.writeElement(new Presence()); + + assertEquals("<presence/>", lowLayer_.writtenData); + } + + @Test + public void testWriteFooter() { + testling_.writeFooter(); + + assertEquals("</stream:stream>", lowLayer_.writtenData); + } +}
\ No newline at end of file |