diff --git a/SharpXMPP.NUnit/TestUtils/MockedXmppTcpConnection.cs b/SharpXMPP.NUnit/TestUtils/MockedXmppTcpConnection.cs new file mode 100644 index 0000000..3c9f499 --- /dev/null +++ b/SharpXMPP.NUnit/TestUtils/MockedXmppTcpConnection.cs @@ -0,0 +1,14 @@ +using System.IO; +using System.Xml; +using SharpXMPP.XMPP; + +namespace SharpXMPP.NUnit.TestUtils +{ + public class MockedXmppTcpConnection : XmppTcpConnection + { + public MockedXmppTcpConnection(Stream stream) : base("", new JID(), "") + { + Reader = XmlReader.Create(stream); + } + } +} diff --git a/SharpXMPP.NUnit/XmppTcpConnectionTests.cs b/SharpXMPP.NUnit/XmppTcpConnectionTests.cs new file mode 100644 index 0000000..95184a9 --- /dev/null +++ b/SharpXMPP.NUnit/XmppTcpConnectionTests.cs @@ -0,0 +1,30 @@ +using System.IO; +using System.Text; +using NUnit.Framework; +using SharpXMPP.Errors; +using SharpXMPP.NUnit.TestUtils; + +namespace SharpXMPP.NUnit +{ + [TestFixture] + public class XmppTcpConnectionTests + { + private const string EmptyXmppStream = @" + +"; + + [Test] + public void ConnectionShouldStopAfterStreamClosed() + { + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(EmptyXmppStream))) + using (var connection = new MockedXmppTcpConnection(stream)) + { + Assert.Throws(() => connection.NextElement()); + } + } + } +} diff --git a/SharpXMPP.Shared/Errors/XmppConnectionTerminatedException.cs b/SharpXMPP.Shared/Errors/XmppConnectionTerminatedException.cs new file mode 100644 index 0000000..3433f4b --- /dev/null +++ b/SharpXMPP.Shared/Errors/XmppConnectionTerminatedException.cs @@ -0,0 +1,9 @@ +using System; + +namespace SharpXMPP.Errors +{ + public class XmppConnectionTerminatedException : Exception + { + public override string Message => "XMPP connection was terminated by server"; + } +} diff --git a/SharpXMPP.Shared/Properties/AssemblyInfo.cs b/SharpXMPP.Shared/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4b5adf3 --- /dev/null +++ b/SharpXMPP.Shared/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("SharpXMPP.NUnit")] diff --git a/SharpXMPP.Shared/SharpXMPP.Shared.csproj b/SharpXMPP.Shared/SharpXMPP.Shared.csproj index 41ba93b..77e3758 100644 --- a/SharpXMPP.Shared/SharpXMPP.Shared.csproj +++ b/SharpXMPP.Shared/SharpXMPP.Shared.csproj @@ -5,6 +5,7 @@ netstandard1.6;net451 portable SharpXMPP.Shared + SharpXMPP 1.6.0 diff --git a/SharpXMPP.Shared/XmppTcpConnection.cs b/SharpXMPP.Shared/XmppTcpConnection.cs index 1970252..cd2501c 100644 --- a/SharpXMPP.Shared/XmppTcpConnection.cs +++ b/SharpXMPP.Shared/XmppTcpConnection.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; +using SharpXMPP.Errors; using SharpXMPP.XMPP; using SharpXMPP.XMPP.Bind; using SharpXMPP.XMPP.Client; @@ -41,7 +42,7 @@ protected XmppTcpConnection(string ns, JID jid, string password) : base(ns) Password = password; } - public System.IO.Stream ConnectionStream { get; private set; } + public System.IO.Stream ConnectionStream { get; private protected set; } protected XmlReader Reader; protected XmlWriter Writer; @@ -76,7 +77,10 @@ public override XElement NextElement() } do { - Reader.Read(); + if (!Reader.Read()) + { + throw new XmppConnectionTerminatedException(); + } } while (Reader.NodeType != XmlNodeType.Element); var result = XElement.Load(Reader.ReadSubtree()); OnElement(new ElementArgs { Stanza = result, IsInput = true });