Skip to content

Commit

Permalink
Add a check for a connection terminated by server (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 13, 2019
1 parent 8dffcde commit aaed998
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
14 changes: 14 additions & 0 deletions SharpXMPP.NUnit/TestUtils/MockedXmppTcpConnection.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
30 changes: 30 additions & 0 deletions SharpXMPP.NUnit/XmppTcpConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -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 = @"<?xml version='1.0'?>
<stream:stream from='example.com'
id='someid'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'
version='1.0'>
</stream:stream>";

[Test]
public void ConnectionShouldStopAfterStreamClosed()
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(EmptyXmppStream)))
using (var connection = new MockedXmppTcpConnection(stream))
{
Assert.Throws<XmppConnectionTerminatedException>(() => connection.NextElement());
}
}
}
}
9 changes: 9 additions & 0 deletions SharpXMPP.Shared/Errors/XmppConnectionTerminatedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace SharpXMPP.Errors
{
public class XmppConnectionTerminatedException : Exception
{
public override string Message => "XMPP connection was terminated by server";
}
}
3 changes: 3 additions & 0 deletions SharpXMPP.Shared/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("SharpXMPP.NUnit")]
1 change: 1 addition & 0 deletions SharpXMPP.Shared/SharpXMPP.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFrameworks>netstandard1.6;net451</TargetFrameworks>
<DebugType>portable</DebugType>
<AssemblyName>SharpXMPP.Shared</AssemblyName>
<RootNamespace>SharpXMPP</RootNamespace>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.6' ">1.6.0</NetStandardImplicitPackageVersion>
</PropertyGroup>

Expand Down
8 changes: 6 additions & 2 deletions SharpXMPP.Shared/XmppTcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit aaed998

Please sign in to comment.