1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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...