public class TelnetParser
{
public event EventHandler<TelnetCommandEventArgs> Command;
protected void OnCommand(TelnetCommandEventArgs e)
{
if (Command != null)
Command(this, e);
}
public byte[] Parse(byte[] bytes)
{
byte subOptionCode = 0;
List<byte> subnegotiation = null;
List<byte> nonCommandOutput = new List<byte>();
List<byte> output = nonCommandOutput;
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] == (byte)TelnetCommand.IAC)
{
i++;
var command = (TelnetCommand)bytes[i];
switch (command)
{
case TelnetCommand.IAC:
output.Add((byte)TelnetCommand.IAC);
break;
case TelnetCommand.WILL:
case TelnetCommand.WONT:
case TelnetCommand.DO:
case TelnetCommand.DONT:
i++;
byte optionCode = bytes[i];
OnCommand(new TelnetCommandEventArgs(command, optionCode));
break;
case TelnetCommand.SB:
i++;
subnegotiation = new List<byte>();
output = subnegotiation;
subOptionCode = bytes[i];
break;
case TelnetCommand.SE:
OnCommand(new TelnetCommandEventArgs(command, subOptionCode, subnegotiation.ToArray()));
output = nonCommandOutput;
break;
default:
OnCommand(new TelnetCommandEventArgs(command));
break;
}
}
else
{
output.Add(bytes[i]);
}
}
return nonCommandOutput.ToArray();
}
}
public enum TelnetCommand : byte
{
SE = 240,
NOP = 241,
DM = 242,
Break = 243,
IP = 244,
AO = 245,
AYT = 246,
EC = 247,
EL = 248,
GA = 249,
SB = 250,
WILL = 251,
WONT = 252,
DO = 253,
DONT = 254,
IAC = 255
}
public class TelnetCommandEventArgs : EventArgs
{
public Byte[] Data { get; private set; }
public byte OptionCode { get; private set; }
public TelnetCommand TelnetCommand { get; private set; }
public TelnetCommandEventArgs(TelnetCommand command, byte subOptionCode, byte[] data)
{
TelnetCommand = command;
OptionCode = subOptionCode;
Data = data;
}
public TelnetCommandEventArgs(TelnetCommand command, byte optionCode)
: this(command, optionCode, new byte[0])
{
}
public TelnetCommandEventArgs(TelnetCommand command)
: this(command, 0)
{
}
}
[TestFixture]
public class TelnetParserTests
{
[Test]
public void When_No_Commands_In_Data_Returns_Input_Data()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[] { 1, 2, 3, 4, 5 };
byte[] returnBytes = parser.Parse(bytes);
Assert.AreElementsEqual(bytes, returnBytes, "Output bytes were wrong");
}
[Test]
public void When_Two_Command_Parameters_Output_Byte_Array_Has_One_Byte_Equal_To_The_Command_Byte_Value()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.IAC };
byte[] expected = new byte[] { (byte)TelnetCommand.IAC };
byte[] returnBytes = parser.Parse(bytes);
Assert.AreElementsEqual(expected, returnBytes, "Output bytes are wrong");
}
[Test]
public void When_Two_Command_Parameters_In_Input_With_Non_Command_Characters_Command_Parameters_Are_Replaced_With_A_Single_In_Output()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[] { 1, 2, (byte)TelnetCommand.IAC, (byte)TelnetCommand.IAC, 3, 4 };
byte[] expected = new byte[] { 1, 2, (byte)TelnetCommand.IAC, 3, 4 };
byte[] returnBytes = parser.Parse(bytes);
Assert.AreElementsEqual(expected, returnBytes);
}
[Test]
public void When_Telnet_Command_Found_Fires_Event()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.NOP };
TelnetCommand foundCommand = (TelnetCommand)0;
parser.Command +=
(object sender, TelnetCommandEventArgs eventArgs) =>
{
foundCommand = eventArgs.TelnetCommand;
};
parser.Parse(bytes);
Assert.AreEqual(TelnetCommand.NOP, foundCommand, "Incorrect command set");
}
[Test]
public void When_Option_Command_Found_EventArgs_Contains_Option_Code()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[]
{
(byte)TelnetCommand.IAC, (byte)TelnetCommand.WILL, 1,
(byte)TelnetCommand.IAC, (byte)TelnetCommand.WONT, 2,
(byte)TelnetCommand.IAC, (byte)TelnetCommand.DO, 3,
(byte)TelnetCommand.IAC, (byte)TelnetCommand.DONT, 4
};
byte[] expectedOptionCodes = new byte[] { 1, 2, 3, 4 };
List<byte> optionCodes = new List<byte>();
parser.Command += (sender, e) => { optionCodes.Add(e.OptionCode); };
parser.Parse(bytes);
Assert.AreElementsEqual(expectedOptionCodes, optionCodes);
}
[Test]
public void When_Option_Is_Start_Sub_Negotiation_EventArgs_Has_Gathered_Data_Until_End_Sub_Negotiation()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[]
{
(byte)TelnetCommand.IAC, (byte)TelnetCommand.SB, 1, 2, 3, 4, (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE
};
byte expectedOptionCode = 1;
byte[] expectedData = new byte[] { 2, 3, 4 };
byte actualOptionCode = 1;
byte[] actualData = null;
parser.Command += (sender, e) => { actualData = e.Data; actualOptionCode = e.OptionCode; };
parser.Parse(bytes);
Assert.AreEqual(expectedOptionCode, actualOptionCode);
Assert.AreElementsEqual(expectedData, actualData, "Sub negotiation data was not parsed correctly");
}
[Test]
public void When_Subnegotiation_Ends_Non_Command_Data_Is_Still_Appended_To_Output()
{
TelnetParser parser = new TelnetParser();
byte[] bytes = new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.SB, 1, 2, 3, 4, (byte)TelnetCommand.IAC, (byte)TelnetCommand.SE, 5, 6, 7, 8 };
byte[] expectedBytes = new byte[] { 5, 6, 7, 8 };
byte[] returnedBytes = parser.Parse(bytes);
Assert.AreElementsEqual(expectedBytes, returnedBytes);
}
}
Refactorings
No refactoring yet !
Ants
February 4, 2010, February 04, 2010 09:15, permalink
I recommend breaking out lines 22-51 into a private method. If you read Fowler's refactoring book, he recommends that functions be kept as short as possible, and have them be doing only one thing.
Additionally, my quick scan of the Telnet protocol gave me the impression that subnegotiate commands would always be in the form of:
IAC SB subOptionCode [data]* IAC SE
So that would mean that you can also break out into another method the handler for the SB command, to just be a loop that correctly escapes IAC's and looks for the IAC SE end marker. This will let you get rid of the confusing code that set the output variable to either the subnegotiation byte list, or the non-command byte list.
So I've was just using TDD (as I'm still learning this process) to write a Telnet command parser that parses out the non command bytes, and the individual commands from a byte array. It fires an event each time a command is parsed and returns a byte array of the data without the telnet commands in it. I've only done tiny tiny tiny bits of refactoring, so I'm wondering, how much further would any experienced TDD'ers take the refactoring from here...