Refactoring

This commit is contained in:
Benito Palacios Sánchez 2014-12-10 23:43:53 +01:00
parent 421cadfc7c
commit dfdf5ebdb1
2 changed files with 49 additions and 46 deletions

View File

@ -129,49 +129,29 @@ namespace UnitTests
this.serverStream.Write(packetBin, 0, packetBin.Length); this.serverStream.Write(packetBin, 0, packetBin.Length);
} }
private void Read()
{
Thread.Sleep(50);
byte[] buffer = new byte[10 * 1024];
this.serverStream.Read(buffer, 0, buffer.Length);
}
[Test] [Test]
public void AskHaltedReason() public void CommandUnknownReplyPacket()
{
this.SendPacket("S", "02");
StopSignal reason = this.client.AskHaltedReason();
this.Read();
Assert.IsTrue(reason.HasFlag(StopSignal.HostBreak));
}
[Test]
public void CommandError()
{ {
// Send as many times as the maximum counter of Presentation layer
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
this.SendPacket("@", ""); this.SendPacket("@", "");
StopSignal reason = this.client.AskHaltedReason(); StopSignal reason = this.client.AskHaltedReason();
this.Read();
Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(this.client.IsConnected);
Assert.AreEqual(StopSignal.Unknown, reason); Assert.AreEqual(StopSignal.Unknown, reason);
} }
[Test] [Test]
public void CommandInvalidReply() public void CommandUnexepectedReply()
{ {
this.SendPacket("OK", ""); this.SendPacket("OK", "");
StopSignal reason = this.client.AskHaltedReason(); StopSignal reason = this.client.AskHaltedReason();
this.Read();
Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(this.client.IsConnected);
Assert.AreEqual(StopSignal.Unknown, reason); Assert.AreEqual(StopSignal.Unknown, reason);
} }
[Test] [Test]
public void ConnectionClosedRaiseHandle() public void CommandConnectionLostRaiseHandle()
{ {
this.serverClient.Close(); this.serverClient.Close();
this.client.LostConnection += new LostConnectionEventHandle(LostConnection); this.client.LostConnection += new LostConnectionEventHandle(LostConnection);
@ -185,7 +165,7 @@ namespace UnitTests
} }
[Test] [Test]
public void ConnectionClosedDoesNotRaiseHandle() public void CommandConnectionLostDoesNotRaiseHandle()
{ {
this.serverClient.Close(); this.serverClient.Close();
Assert.DoesNotThrow(() => this.client.AskHaltedReason()); Assert.DoesNotThrow(() => this.client.AskHaltedReason());
@ -196,42 +176,54 @@ namespace UnitTests
{ {
this.SendPacket("S", "02"); this.SendPacket("S", "02");
bool stopped = this.client.StopExecution(); bool stopped = this.client.StopExecution();
this.Read();
Assert.IsTrue(stopped); Assert.IsTrue(stopped);
} }
[Test] [Test]
public void InterruptError() public void InterruptInvalidSignal()
{ {
this.SendPacket("S", "03");
bool stopped = this.client.StopExecution();
Assert.IsFalse(stopped);
}
[Test]
public void InterruptUnknownReplyPacket()
{
// Send as many times as the maximum counter of Presentation layer
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
this.SendPacket("@", ""); this.SendPacket("@", "");
bool stopped = this.client.StopExecution();
this.Read();
bool stopped = this.client.StopExecution();
Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(this.client.IsConnected);
Assert.IsFalse(stopped); Assert.IsFalse(stopped);
} }
[Test] [Test]
public void InterruptInvalidReply() public void InterruptUnexepectedReply()
{ {
this.SendPacket("OK", ""); this.SendPacket("OK", "");
bool stopped = this.client.StopExecution(); bool stopped = this.client.StopExecution();
this.Read();
Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(this.client.IsConnected);
Assert.IsFalse(stopped); Assert.IsFalse(stopped);
} }
[Test] [Test]
public void InterruptConnectionLost() public void InterruptConnectionLostRaiseHandle()
{ {
this.serverClient.Close(); this.serverClient.Close();
this.client.LostConnection += new LostConnectionEventHandle(LostConnection); this.client.LostConnection += new LostConnectionEventHandle(LostConnection);
bool stopped = this.client.StopExecution(); bool stopped = this.client.StopExecution();
Assert.IsFalse(stopped); Assert.IsFalse(stopped);
} }
[Test]
public void AskHaltedReason()
{
this.SendPacket("S", "02");
StopSignal reason = this.client.AskHaltedReason();
Assert.IsTrue(reason.HasFlag(StopSignal.HostBreak));
}
} }
} }

View File

@ -112,18 +112,20 @@ namespace NitroDebugger.RSP
private ReplyPacket SafeInterruption() private ReplyPacket SafeInterruption()
{ {
ReplyPacket response = null; ReplyPacket response = null;
bool error = false;
try { try {
response = this.presentation.SendInterrupt(); response = this.presentation.SendInterrupt();
} catch (SocketException) { } catch (SocketException) {
OnLostConnection(EventArgs.Empty); error = true;
} catch (ProtocolViolationException) { } catch (ProtocolViolationException) {
this.Disconnect(); error = true;
OnLostConnection(EventArgs.Empty);
} }
if (response != null && !(response is StopSignalReply)) { if (response != null && !(response is StopSignalReply))
this.Disconnect(); error = true;
OnLostConnection(EventArgs.Empty);
if (error) {
NetworkError();
response = null; response = null;
} }
@ -133,19 +135,22 @@ namespace NitroDebugger.RSP
private ReplyPacket SafeSending(CommandPacket command, params Type[] validReplyTypes) private ReplyPacket SafeSending(CommandPacket command, params Type[] validReplyTypes)
{ {
ReplyPacket response = null; ReplyPacket response = null;
bool error = false;
try { try {
this.presentation.SendCommand(command); this.presentation.SendCommand(command);
response = this.presentation.ReceiveReply(); response = this.presentation.ReceiveReply();
} catch (SocketException) { } catch (SocketException) {
OnLostConnection(EventArgs.Empty); error = true;
} catch (ProtocolViolationException) { } catch (ProtocolViolationException) {
this.Disconnect(); error = true;
OnLostConnection(EventArgs.Empty);
} }
if (response != null && !ValidateType(response, validReplyTypes)) { if (response != null && !ValidateType(response, validReplyTypes))
this.Disconnect(); error = true;
OnLostConnection(EventArgs.Empty);
if (error) {
NetworkError();
response = null; response = null;
} }
@ -156,6 +161,12 @@ namespace NitroDebugger.RSP
{ {
return validReplyTypes.Contains(reply.GetType()); return validReplyTypes.Contains(reply.GetType());
} }
private void NetworkError()
{
this.Disconnect();
OnLostConnection(EventArgs.Empty);
}
} }
} }