From a1e1922e62419de75d2909dfae1ee7caa9ed487f Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sat, 16 May 2020 22:08:40 -0400 Subject: Add xmldocs, and handle null bytes --- IRCTokens/IRCTokens.csproj | 1 + IRCTokens/StatefulDecoder.cs | 9 ++++++++ IRCTokens/Tests/Format.cs | 24 ++++++++++---------- IRCTokens/Tests/Hostmask.cs | 12 +++++----- IRCTokens/Tests/Parser.cs | 4 ++-- IRCTokens/Tests/StatefulDecoder.cs | 16 +++++++------- IRCTokens/Tests/StatefulEncoder.cs | 14 ++++++------ IRCTokens/Tests/Tokenization.cs | 45 +++++++++++++++++++++++++------------- 8 files changed, 75 insertions(+), 50 deletions(-) (limited to 'IRCTokens') diff --git a/IRCTokens/IRCTokens.csproj b/IRCTokens/IRCTokens.csproj index ed2e0c2..cbee30e 100644 --- a/IRCTokens/IRCTokens.csproj +++ b/IRCTokens/IRCTokens.csproj @@ -13,6 +13,7 @@ https://tildegit.org/ben/ircsharp/src/branch/master/IRCTokens git irc + 1.0.1 diff --git a/IRCTokens/StatefulDecoder.cs b/IRCTokens/StatefulDecoder.cs index 82630f6..619253b 100644 --- a/IRCTokens/StatefulDecoder.cs +++ b/IRCTokens/StatefulDecoder.cs @@ -59,6 +59,15 @@ namespace IRCTokens _buffer = _buffer == null ? Array.Empty() : _buffer.Concat(data.Take(bytesReceived)).ToArray(); + // truncate message at NUL if found + if (_buffer.Contains((byte) 0)) + { + _buffer = _buffer + .Take(Array.IndexOf(_buffer, (byte) 0)) + .Concat(new []{(byte) '\n'}) + .ToArray(); + } + var listLines = _buffer.Split((byte) '\n').Select(l => l.Trim((byte) '\r')).ToList(); _buffer = listLines.LastOrDefault() ?? Array.Empty(); diff --git a/IRCTokens/Tests/Format.cs b/IRCTokens/Tests/Format.cs index 8319069..7224f97 100644 --- a/IRCTokens/Tests/Format.cs +++ b/IRCTokens/Tests/Format.cs @@ -8,7 +8,7 @@ namespace IRCTokens.Tests public class Format { [TestMethod] - public void TestTags() + public void Tags() { var line = new Line("PRIVMSG", "#channel", "hello") { @@ -19,7 +19,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestMissingTag() + public void MissingTag() { var line = new Line("PRIVMSG", "#channel", "hello").Format(); @@ -27,7 +27,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestNullTag() + public void NullTag() { var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary {{"a", null}}} .Format(); @@ -36,7 +36,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEmptyTag() + public void EmptyTag() { var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary {{"a", ""}}} .Format(); @@ -45,7 +45,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestSource() + public void Source() { var line = new Line("PRIVMSG", "#channel", "hello") {Source = "nick!user@host"}.Format(); @@ -53,21 +53,21 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestCommandLowercase() + public void CommandLowercase() { var line = new Line {Command = "privmsg"}.Format(); Assert.AreEqual("privmsg", line); } [TestMethod] - public void TestCommandUppercase() + public void CommandUppercase() { var line = new Line {Command = "PRIVMSG"}.Format(); Assert.AreEqual("PRIVMSG", line); } [TestMethod] - public void TestTrailingSpace() + public void TrailingSpace() { var line = new Line("PRIVMSG", "#channel", "hello world").Format(); @@ -75,7 +75,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestTrailingNoSpace() + public void TrailingNoSpace() { var line = new Line("PRIVMSG", "#channel", "helloworld").Format(); @@ -83,7 +83,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestTrailingDoubleColon() + public void TrailingDoubleColon() { var line = new Line("PRIVMSG", "#channel", ":helloworld").Format(); @@ -91,13 +91,13 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestInvalidNonLastSpace() + public void InvalidNonLastSpace() { Assert.ThrowsException(() => { new Line("USER", "user", "0 *", "real name").Format(); }); } [TestMethod] - public void TestInvalidNonLastColon() + public void InvalidNonLastColon() { Assert.ThrowsException(() => { new Line("PRIVMSG", ":#channel", "hello").Format(); }); } diff --git a/IRCTokens/Tests/Hostmask.cs b/IRCTokens/Tests/Hostmask.cs index 2446013..17c5ad7 100644 --- a/IRCTokens/Tests/Hostmask.cs +++ b/IRCTokens/Tests/Hostmask.cs @@ -6,7 +6,7 @@ namespace IRCTokens.Tests public class Hostmask { [TestMethod] - public void TestHostmask() + public void FullHostmask() { var hostmask = new IRCTokens.Hostmask("nick!user@host"); Assert.AreEqual("nick", hostmask.NickName); @@ -15,7 +15,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestNoHostName() + public void NoHostName() { var hostmask = new IRCTokens.Hostmask("nick!user"); Assert.AreEqual("nick", hostmask.NickName); @@ -24,7 +24,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestNoUserName() + public void NoUserName() { var hostmask = new IRCTokens.Hostmask("nick@host"); Assert.AreEqual("nick", hostmask.NickName); @@ -33,7 +33,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestOnlyNickName() + public void OnlyNickName() { var hostmask = new IRCTokens.Hostmask("nick"); Assert.AreEqual("nick", hostmask.NickName); @@ -42,7 +42,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestHostmaskFromLine() + public void HostmaskFromLine() { var line = new Line(":nick!user@host PRIVMSG #channel hello"); var hostmask = new IRCTokens.Hostmask("nick!user@host"); @@ -53,7 +53,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEmptyHostmaskFromLine() + public void EmptyHostmaskFromLine() { var line = new Line("PRIVMSG #channel hello"); Assert.IsNull(line.Hostmask.HostName); diff --git a/IRCTokens/Tests/Parser.cs b/IRCTokens/Tests/Parser.cs index bd0a92d..a560793 100644 --- a/IRCTokens/Tests/Parser.cs +++ b/IRCTokens/Tests/Parser.cs @@ -21,7 +21,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestSplit() + public void Split() { foreach (var test in LoadYaml("Tests/Data/msg-split.yaml").Tests) { @@ -38,7 +38,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestJoin() + public void Join() { foreach (var test in LoadYaml("Tests/Data/msg-join.yaml").Tests) { diff --git a/IRCTokens/Tests/StatefulDecoder.cs b/IRCTokens/Tests/StatefulDecoder.cs index d37310f..4da7690 100644 --- a/IRCTokens/Tests/StatefulDecoder.cs +++ b/IRCTokens/Tests/StatefulDecoder.cs @@ -10,13 +10,13 @@ namespace IRCTokens.Tests private IRCTokens.StatefulDecoder _decoder; [TestInitialize] - public void TestInitialize() + public void Initialize() { _decoder = new IRCTokens.StatefulDecoder(); } [TestMethod] - public void TestPartial() + public void Partial() { var lines = _decoder.Push("PRIVMSG "); Assert.AreEqual(0, lines.Count); @@ -29,7 +29,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestMultiple() + public void Multiple() { var lines = _decoder.Push("PRIVMSG #channel1 hello\r\nPRIVMSG #channel2 hello\r\n"); Assert.AreEqual(2, lines.Count); @@ -41,7 +41,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEncoding() + public void EncodingIso8859() { var iso8859 = Encoding.GetEncoding("iso-8859-1"); _decoder = new IRCTokens.StatefulDecoder {Encoding = iso8859}; @@ -52,7 +52,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEncodingFallback() + public void EncodingFallback() { var latin1 = Encoding.GetEncoding("iso-8859-1"); _decoder = new IRCTokens.StatefulDecoder {Encoding = null, Fallback = latin1}; @@ -63,14 +63,14 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEmpty() + public void Empty() { var lines = _decoder.Push(string.Empty); Assert.AreEqual(0, lines.Count); } [TestMethod] - public void TestBufferUnfinished() + public void BufferUnfinished() { _decoder.Push("PRIVMSG #channel hello"); var lines = _decoder.Push(string.Empty); @@ -78,7 +78,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestClear() + public void Clear() { _decoder.Push("PRIVMSG "); _decoder.Clear(); diff --git a/IRCTokens/Tests/StatefulEncoder.cs b/IRCTokens/Tests/StatefulEncoder.cs index 5ced4d2..d1e1e3e 100644 --- a/IRCTokens/Tests/StatefulEncoder.cs +++ b/IRCTokens/Tests/StatefulEncoder.cs @@ -9,13 +9,13 @@ namespace IRCTokens.Tests private IRCTokens.StatefulEncoder _encoder; [TestInitialize] - public void TestInitialize() + public void Initialize() { _encoder = new IRCTokens.StatefulEncoder(); } [TestMethod] - public void TestPush() + public void Push() { var line = new Line("PRIVMSG #channel hello"); _encoder.Push(line); @@ -23,7 +23,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestPopPartial() + public void PopPartial() { var line = new Line("PRIVMSG #channel hello"); _encoder.Push(line); @@ -43,7 +43,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestPopNoneReturned() + public void PopNoneReturned() { var line = new Line("PRIVMSG #channel hello"); _encoder.Push(line); @@ -52,7 +52,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestPopMultipleLines() + public void PopMultipleLines() { var line1 = new Line("PRIVMSG #channel1 hello"); _encoder.Push(line1); @@ -65,7 +65,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestClear() + public void Clear() { _encoder.Push(new Line("PRIVMSG #channel hello")); _encoder.Clear(); @@ -73,7 +73,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestEncoding() + public void EncodingIso8859() { var iso8859 = Encoding.GetEncoding("iso-8859-1"); _encoder = new IRCTokens.StatefulEncoder {Encoding = iso8859}; diff --git a/IRCTokens/Tests/Tokenization.cs b/IRCTokens/Tests/Tokenization.cs index 03959de..c4c5c5a 100644 --- a/IRCTokens/Tests/Tokenization.cs +++ b/IRCTokens/Tests/Tokenization.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.Linq; +using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IRCTokens.Tests @@ -7,98 +9,98 @@ namespace IRCTokens.Tests public class Tokenization { [TestMethod] - public void TestTagsMissing() + public void TagsMissing() { var line = new Line("PRIVMSG #channel"); Assert.IsNull(line.Tags); } [TestMethod] - public void TestTagsMissingValue() + public void TagsMissingValue() { var line = new Line("@id= PRIVMSG #channel"); Assert.AreEqual(string.Empty, line.Tags["id"]); } [TestMethod] - public void TestTagsMissingEqual() + public void TagsMissingEqual() { var line = new Line("@id PRIVMSG #channel"); Assert.AreEqual(string.Empty, line.Tags["id"]); } [TestMethod] - public void TestTagsUnescape() + public void TagsUnescape() { var line = new Line(@"@id=1\\\:\r\n\s2 PRIVMSG #channel"); Assert.AreEqual("1\\;\r\n 2", line.Tags["id"]); } [TestMethod] - public void TestTagsOverlap() + public void TagsOverlap() { var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel"); Assert.AreEqual("1\\ \\s", line.Tags["id"]); } [TestMethod] - public void TestTagsLoneEndSlash() + public void TagsLoneEndSlash() { var line = new Line("@id=1\\ PRIVMSG #channel"); Assert.AreEqual("1", line.Tags["id"]); } [TestMethod] - public void TestSourceWithoutTags() + public void SourceWithoutTags() { var line = new Line(":nick!user@host PRIVMSG #channel"); Assert.AreEqual("nick!user@host", line.Source); } [TestMethod] - public void TestSourceWithTags() + public void SourceWithTags() { var line = new Line("@id=123 :nick!user@host PRIVMSG #channel"); Assert.AreEqual("nick!user@host", line.Source); } [TestMethod] - public void TestSourceMissingWithoutTags() + public void SourceMissingWithoutTags() { var line = new Line("PRIVMSG #channel"); Assert.IsNull(line.Source); } [TestMethod] - public void TestSourceMissingWithTags() + public void SourceMissingWithTags() { var line = new Line("@id=123 PRIVMSG #channel"); Assert.IsNull(line.Source); } [TestMethod] - public void TestCommand() + public void Command() { var line = new Line("privmsg #channel"); Assert.AreEqual("PRIVMSG", line.Command); } [TestMethod] - public void TestParamsTrailing() + public void ParamsTrailing() { var line = new Line("PRIVMSG #channel :hello world"); CollectionAssert.AreEqual(new List {"#channel", "hello world"}, line.Params); } [TestMethod] - public void TestParamsOnlyTrailing() + public void ParamsOnlyTrailing() { var line = new Line("PRIVMSG :hello world"); CollectionAssert.AreEqual(new List {"hello world"}, line.Params); } [TestMethod] - public void TestParamsMissing() + public void ParamsMissing() { var line = new Line("PRIVMSG"); Assert.AreEqual("PRIVMSG", line.Command); @@ -106,7 +108,7 @@ namespace IRCTokens.Tests } [TestMethod] - public void TestAllTokens() + public void AllTokens() { var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world"); CollectionAssert.AreEqual(new Dictionary {{"id", "123"}}, line.Tags); @@ -114,5 +116,18 @@ namespace IRCTokens.Tests Assert.AreEqual("PRIVMSG", line.Command); CollectionAssert.AreEqual(new List {"#channel", "hello world"}, line.Params); } + + [TestMethod] + public void NulByte() + { + var decoder = new IRCTokens.StatefulDecoder(); + var bytes = Encoding.UTF8.GetBytes(":nick!user@host PRIVMSG #channel :hello") + .Concat(Encoding.UTF8.GetBytes("\0")) + .Concat(Encoding.UTF8.GetBytes("world")) + .ToArray(); + var line = decoder.Push(bytes, bytes.Length).First(); + + CollectionAssert.AreEqual(new List {"#channel", "hello"}, line.Params); + } } } -- cgit 1.4.1