From 35bbd30c2506b3d0b18397ef1443fb18c0d893d6 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Tue, 10 Nov 2020 18:35:21 -0500 Subject: Move tests to a separate project --- IRCSharp.Tests/Tokenization/Data/JoinModel.cs | 30 +++ IRCSharp.Tests/Tokenization/Data/SplitModel.cs | 15 ++ IRCSharp.Tests/Tokenization/Data/msg-join.yaml | 221 +++++++++++++++ IRCSharp.Tests/Tokenization/Data/msg-split.yaml | 343 ++++++++++++++++++++++++ IRCSharp.Tests/Tokenization/Format.cs | 105 ++++++++ IRCSharp.Tests/Tokenization/Hostmask.cs | 64 +++++ IRCSharp.Tests/Tokenization/Parser.cs | 55 ++++ IRCSharp.Tests/Tokenization/StatefulDecoder.cs | 88 ++++++ IRCSharp.Tests/Tokenization/StatefulEncoder.cs | 84 ++++++ IRCSharp.Tests/Tokenization/Tokenization.cs | 133 +++++++++ 10 files changed, 1138 insertions(+) create mode 100644 IRCSharp.Tests/Tokenization/Data/JoinModel.cs create mode 100644 IRCSharp.Tests/Tokenization/Data/SplitModel.cs create mode 100644 IRCSharp.Tests/Tokenization/Data/msg-join.yaml create mode 100644 IRCSharp.Tests/Tokenization/Data/msg-split.yaml create mode 100644 IRCSharp.Tests/Tokenization/Format.cs create mode 100644 IRCSharp.Tests/Tokenization/Hostmask.cs create mode 100644 IRCSharp.Tests/Tokenization/Parser.cs create mode 100644 IRCSharp.Tests/Tokenization/StatefulDecoder.cs create mode 100644 IRCSharp.Tests/Tokenization/StatefulEncoder.cs create mode 100644 IRCSharp.Tests/Tokenization/Tokenization.cs (limited to 'IRCSharp.Tests/Tokenization') diff --git a/IRCSharp.Tests/Tokenization/Data/JoinModel.cs b/IRCSharp.Tests/Tokenization/Data/JoinModel.cs new file mode 100644 index 0000000..e54f4cf --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Data/JoinModel.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using YamlDotNet.Serialization; + +namespace IRCTokens.Tests.Data +{ + public class JoinModel + { + public List Tests { get; set; } + + public class Test + { + [YamlMember(Alias = "desc")] public string Description { get; set; } + + public Atoms Atoms { get; set; } + + public List Matches { get; set; } + } + + public class Atoms + { + public Dictionary Tags { get; set; } + + public string Source { get; set; } + + public string Verb { get; set; } + + public List Params { get; set; } + } + } +} diff --git a/IRCSharp.Tests/Tokenization/Data/SplitModel.cs b/IRCSharp.Tests/Tokenization/Data/SplitModel.cs new file mode 100644 index 0000000..5386326 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Data/SplitModel.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace IRCTokens.Tests.Data +{ + public class SplitModel + { + public List Tests { get; set; } + + public class Test + { + public string Input { get; set; } + public JoinModel.Atoms Atoms { get; set; } + } + } +} diff --git a/IRCSharp.Tests/Tokenization/Data/msg-join.yaml b/IRCSharp.Tests/Tokenization/Data/msg-join.yaml new file mode 100644 index 0000000..d1d7429 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Data/msg-join.yaml @@ -0,0 +1,221 @@ +# IRC parser tests +# joining atoms into sendable messages + +# Written in 2015 by Daniel Oaks +# +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication along +# with this software. If not, see +# . + +# some of the tests here originate from grawity's test vectors, which is WTFPL v2 licensed +# https://github.com/grawity/code/tree/master/lib/tests +# some of the tests here originate from Mozilla's test vectors, which is public domain +# https://dxr.mozilla.org/comm-central/source/chat/protocols/irc/test/test_ircMessage.js +# some of the tests here originate from SaberUK's test vectors, which he's indicated I am free to include here +# https://github.com/SaberUK/ircparser/tree/master/test + +tests: + # the desc string holds a description of the test, if it exists + + # the atoms dict has the keys: + # * tags: tags dict + # tags with no value are an empty string + # * source: source string, without single leading colon + # * verb: verb string + # * params: params split up as a list + # if the params key does not exist, assume it is empty + # if any other keys do no exist, assume they are null + # a key that is null does not exist or is not specified with the + # given input string + + # matches is a list of messages that match + + # simple tests + - desc: Simple test with verb and params. + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "asdf" + matches: + - "foo bar baz asdf" + - "foo bar baz :asdf" + + # with no regular params + - desc: Simple test with source and no params. + atoms: + source: "src" + verb: "AWAY" + matches: + - ":src AWAY" + + - desc: Simple test with source and empty trailing param. + atoms: + source: "src" + verb: "AWAY" + params: + - "" + matches: + - ":src AWAY :" + + # with source + - desc: Simple test with source. + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "asdf" + matches: + - ":coolguy foo bar baz asdf" + - ":coolguy foo bar baz :asdf" + + # with trailing param + - desc: Simple test with trailing param. + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "asdf quux" + matches: + - "foo bar baz :asdf quux" + + - desc: Simple test with empty trailing param. + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "" + matches: + - "foo bar baz :" + + - desc: Simple test with trailing param containing colon. + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - ":asdf" + matches: + - "foo bar baz ::asdf" + + # with source and trailing param + - desc: Test with source and trailing param. + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "asdf quux" + matches: + - ":coolguy foo bar baz :asdf quux" + + - desc: Test with trailing containing beginning+end whitespace. + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - " asdf quux " + matches: + - ":coolguy foo bar baz : asdf quux " + + - desc: Test with trailing containing what looks like another trailing param. + atoms: + source: "coolguy" + verb: "PRIVMSG" + params: + - "bar" + - "lol :) " + matches: + - ":coolguy PRIVMSG bar :lol :) " + + - desc: Simple test with source and empty trailing. + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "" + matches: + - ":coolguy foo bar baz :" + + - desc: Trailing contains only spaces. + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - " " + matches: + - ":coolguy foo bar baz : " + + - desc: Param containing tab (tab is not considered SPACE for message splitting). + atoms: + source: "coolguy" + verb: "foo" + params: + - "b\tar" + - "baz" + matches: + - ":coolguy foo b\tar baz" + - ":coolguy foo b\tar :baz" + + # with tags + - desc: Tag with no value and space-filled trailing. + atoms: + tags: + "asd": "" + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - " " + matches: + - "@asd :coolguy foo bar baz : " + + - desc: Tags with escaped values. + atoms: + verb: "foo" + tags: + "a": "b\\and\nk" + "d": "gh;764" + matches: + - "@a=b\\\\and\\nk;d=gh\\:764 foo" + - "@d=gh\\:764;a=b\\\\and\\nk foo" + + - desc: Tags with escaped values and params. + atoms: + verb: "foo" + tags: + "a": "b\\and\nk" + "d": "gh;764" + params: + - "par1" + - "par2" + matches: + - "@a=b\\\\and\\nk;d=gh\\:764 foo par1 par2" + - "@a=b\\\\and\\nk;d=gh\\:764 foo par1 :par2" + - "@d=gh\\:764;a=b\\\\and\\nk foo par1 par2" + - "@d=gh\\:764;a=b\\\\and\\nk foo par1 :par2" + + - desc: Tag with long, strange values (including LF and newline). + atoms: + tags: + foo: "\\\\;\\s \r\n" + verb: "COMMAND" + matches: + - "@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND" diff --git a/IRCSharp.Tests/Tokenization/Data/msg-split.yaml b/IRCSharp.Tests/Tokenization/Data/msg-split.yaml new file mode 100644 index 0000000..fa3f4aa --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Data/msg-split.yaml @@ -0,0 +1,343 @@ +# IRC parser tests +# splitting messages into usable atoms + +# Written in 2015 by Daniel Oaks +# +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication along +# with this software. If not, see +# . + +# some of the tests here originate from grawity's test vectors, which is WTFPL v2 licensed +# https://github.com/grawity/code/tree/master/lib/tests +# some of the tests here originate from Mozilla's test vectors, which is public domain +# https://dxr.mozilla.org/comm-central/source/chat/protocols/irc/test/test_ircMessage.js +# some of the tests here originate from SaberUK's test vectors, which he's indicated I am free to include here +# https://github.com/SaberUK/ircparser/tree/master/test + +# we follow RFC1459 with regards to multiple ascii spaces splitting atoms: +# The prefix, command, and all parameters are +# separated by one (or more) ASCII space character(s) (0x20). +# because doing it as RFC2812 says (strictly as a single ascii space) isn't sane + +tests: + # input is the string coming directly from the server to parse + + # the atoms dict has the keys: + # * tags: tags dict + # tags with no value are an empty string + # * source: source string, without single leading colon + # * verb: verb string + # * params: params split up as a list + # if the params key does not exist, assume it is empty + # if any other keys do no exist, assume they are null + # a key that is null does not exist or is not specified with the + # given input string + + # simple + - input: "foo bar baz asdf" + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "asdf" + + # with source + - input: ":coolguy foo bar baz asdf" + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "asdf" + + # with trailing param + - input: "foo bar baz :asdf quux" + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "asdf quux" + + - input: "foo bar baz :" + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - "" + + - input: "foo bar baz ::asdf" + atoms: + verb: "foo" + params: + - "bar" + - "baz" + - ":asdf" + + # with source and trailing param + - input: ":coolguy foo bar baz :asdf quux" + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "asdf quux" + + - input: ":coolguy foo bar baz : asdf quux " + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - " asdf quux " + + - input: ":coolguy PRIVMSG bar :lol :) " + atoms: + source: "coolguy" + verb: "PRIVMSG" + params: + - "bar" + - "lol :) " + + - input: ":coolguy foo bar baz :" + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - "" + + - input: ":coolguy foo bar baz : " + atoms: + source: "coolguy" + verb: "foo" + params: + - "bar" + - "baz" + - " " + + # with tags + - input: "@a=b;c=32;k;rt=ql7 foo" + atoms: + verb: "foo" + tags: + "a": "b" + "c": "32" + "k": "" + "rt": "ql7" + + # with escaped tags + - input: "@a=b\\\\and\\nk;c=72\\s45;d=gh\\:764 foo" + atoms: + verb: "foo" + tags: + "a": "b\\and\nk" + "c": "72 45" + "d": "gh;764" + + # with tags and source + - input: "@c;h=;a=b :quux ab cd" + atoms: + tags: + "c": "" + "h": "" + "a": "b" + source: "quux" + verb: "ab" + params: + - "cd" + + # different forms of last param + - input: ":src JOIN #chan" + atoms: + source: "src" + verb: "JOIN" + params: + - "#chan" + + - input: ":src JOIN :#chan" + atoms: + source: "src" + verb: "JOIN" + params: + - "#chan" + + # with and without last param + - input: ":src AWAY" + atoms: + source: "src" + verb: "AWAY" + + - input: ":src AWAY " + atoms: + source: "src" + verb: "AWAY" + + # tab is not considered + - input: ":cool\tguy foo bar baz" + atoms: + source: "cool\tguy" + verb: "foo" + params: + - "bar" + - "baz" + + # with weird control codes in the source + - input: ":coolguy!ag@net\x035w\x03ork.admin PRIVMSG foo :bar baz" + atoms: + source: "coolguy!ag@net\x035w\x03ork.admin" + verb: "PRIVMSG" + params: + - "foo" + - "bar baz" + + - input: ":coolguy!~ag@n\x02et\x0305w\x0fork.admin PRIVMSG foo :bar baz" + atoms: + source: "coolguy!~ag@n\x02et\x0305w\x0fork.admin" + verb: "PRIVMSG" + params: + - "foo" + - "bar baz" + + - input: "@tag1=value1;tag2;vendor1/tag3=value2;vendor2/tag4= :irc.example.com COMMAND param1 param2 :param3 param3" + atoms: + tags: + tag1: "value1" + tag2: "" + vendor1/tag3: "value2" + vendor2/tag4: "" + source: "irc.example.com" + verb: "COMMAND" + params: + - "param1" + - "param2" + - "param3 param3" + + - input: ":irc.example.com COMMAND param1 param2 :param3 param3" + atoms: + source: "irc.example.com" + verb: "COMMAND" + params: + - "param1" + - "param2" + - "param3 param3" + + - input: "@tag1=value1;tag2;vendor1/tag3=value2;vendor2/tag4 COMMAND param1 param2 :param3 param3" + atoms: + tags: + tag1: "value1" + tag2: "" + vendor1/tag3: "value2" + vendor2/tag4: "" + verb: "COMMAND" + params: + - "param1" + - "param2" + - "param3 param3" + + - input: "COMMAND" + atoms: + verb: "COMMAND" + + # yaml encoding + slashes is fun + - input: "@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND" + atoms: + tags: + foo: "\\\\;\\s \r\n" + verb: "COMMAND" + + # broken messages from unreal + - input: ":gravel.mozilla.org 432 #momo :Erroneous Nickname: Illegal characters" + atoms: + source: "gravel.mozilla.org" + verb: "432" + params: + - "#momo" + - "Erroneous Nickname: Illegal characters" + + - input: ":gravel.mozilla.org MODE #tckk +n " + atoms: + source: "gravel.mozilla.org" + verb: "MODE" + params: + - "#tckk" + - "+n" + + - input: ":services.esper.net MODE #foo-bar +o foobar " + atoms: + source: "services.esper.net" + verb: "MODE" + params: + - "#foo-bar" + - "+o" + - "foobar" + + # tag values should be parsed char-at-a-time to prevent wayward replacements. + - input: "@tag1=value\\\\ntest COMMAND" + atoms: + tags: + tag1: "value\\ntest" + verb: "COMMAND" + + # If a tag value has a slash followed by a character which doesn't need + # to be escaped, the slash should be dropped. + - input: "@tag1=value\\1 COMMAND" + atoms: + tags: + tag1: "value1" + verb: "COMMAND" + + # A slash at the end of a tag value should be dropped + - input: "@tag1=value1\\ COMMAND" + atoms: + tags: + tag1: "value1" + verb: "COMMAND" + + # Duplicate tags: Parsers SHOULD disregard all but the final occurence + - input: "@tag1=1;tag2=3;tag3=4;tag1=5 COMMAND" + atoms: + tags: + tag1: "5" + tag2: "3" + tag3: "4" + verb: "COMMAND" + + # vendored tags can have the same name as a non-vendored tag + - input: "@tag1=1;tag2=3;tag3=4;tag1=5;vendor/tag2=8 COMMAND" + atoms: + tags: + tag1: "5" + tag2: "3" + tag3: "4" + vendor/tag2: "8" + verb: "COMMAND" + + # Some parsers handle /MODE in a special way, make sure they do it right + - input: ":SomeOp MODE #channel :+i" + atoms: + source: "SomeOp" + verb: "MODE" + params: + - "#channel" + - "+i" + + - input: ":SomeOp MODE #channel +oo SomeUser :AnotherUser" + atoms: + source: "SomeOp" + verb: "MODE" + params: + - "#channel" + - "+oo" + - "SomeUser" + - "AnotherUser" diff --git a/IRCSharp.Tests/Tokenization/Format.cs b/IRCSharp.Tests/Tokenization/Format.cs new file mode 100644 index 0000000..7224f97 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Format.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IRCTokens.Tests +{ + [TestClass] + public class Format + { + [TestMethod] + public void Tags() + { + var line = new Line("PRIVMSG", "#channel", "hello") + { + Tags = new Dictionary {{"id", "\\" + " " + ";" + "\r\n"}} + }.Format(); + + Assert.AreEqual("@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello", line); + } + + [TestMethod] + public void MissingTag() + { + var line = new Line("PRIVMSG", "#channel", "hello").Format(); + + Assert.AreEqual("PRIVMSG #channel hello", line); + } + + [TestMethod] + public void NullTag() + { + var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary {{"a", null}}} + .Format(); + + Assert.AreEqual("@a PRIVMSG #channel hello", line); + } + + [TestMethod] + public void EmptyTag() + { + var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary {{"a", ""}}} + .Format(); + + Assert.AreEqual("@a PRIVMSG #channel hello", line); + } + + [TestMethod] + public void Source() + { + var line = new Line("PRIVMSG", "#channel", "hello") {Source = "nick!user@host"}.Format(); + + Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line); + } + + [TestMethod] + public void CommandLowercase() + { + var line = new Line {Command = "privmsg"}.Format(); + Assert.AreEqual("privmsg", line); + } + + [TestMethod] + public void CommandUppercase() + { + var line = new Line {Command = "PRIVMSG"}.Format(); + Assert.AreEqual("PRIVMSG", line); + } + + [TestMethod] + public void TrailingSpace() + { + var line = new Line("PRIVMSG", "#channel", "hello world").Format(); + + Assert.AreEqual("PRIVMSG #channel :hello world", line); + } + + [TestMethod] + public void TrailingNoSpace() + { + var line = new Line("PRIVMSG", "#channel", "helloworld").Format(); + + Assert.AreEqual("PRIVMSG #channel helloworld", line); + } + + [TestMethod] + public void TrailingDoubleColon() + { + var line = new Line("PRIVMSG", "#channel", ":helloworld").Format(); + + Assert.AreEqual("PRIVMSG #channel ::helloworld", line); + } + + [TestMethod] + public void InvalidNonLastSpace() + { + Assert.ThrowsException(() => { new Line("USER", "user", "0 *", "real name").Format(); }); + } + + [TestMethod] + public void InvalidNonLastColon() + { + Assert.ThrowsException(() => { new Line("PRIVMSG", ":#channel", "hello").Format(); }); + } + } +} diff --git a/IRCSharp.Tests/Tokenization/Hostmask.cs b/IRCSharp.Tests/Tokenization/Hostmask.cs new file mode 100644 index 0000000..17c5ad7 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Hostmask.cs @@ -0,0 +1,64 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IRCTokens.Tests +{ + [TestClass] + public class Hostmask + { + [TestMethod] + public void FullHostmask() + { + var hostmask = new IRCTokens.Hostmask("nick!user@host"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.AreEqual("user", hostmask.UserName); + Assert.AreEqual("host", hostmask.HostName); + } + + [TestMethod] + public void NoHostName() + { + var hostmask = new IRCTokens.Hostmask("nick!user"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.AreEqual("user", hostmask.UserName); + Assert.IsNull(hostmask.HostName); + } + + [TestMethod] + public void NoUserName() + { + var hostmask = new IRCTokens.Hostmask("nick@host"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.IsNull(hostmask.UserName); + Assert.AreEqual("host", hostmask.HostName); + } + + [TestMethod] + public void OnlyNickName() + { + var hostmask = new IRCTokens.Hostmask("nick"); + Assert.AreEqual("nick", hostmask.NickName); + Assert.IsNull(hostmask.UserName); + Assert.IsNull(hostmask.HostName); + } + + [TestMethod] + public void HostmaskFromLine() + { + var line = new Line(":nick!user@host PRIVMSG #channel hello"); + var hostmask = new IRCTokens.Hostmask("nick!user@host"); + Assert.AreEqual(hostmask.ToString(), line.Hostmask.ToString()); + Assert.AreEqual("nick", line.Hostmask.NickName); + Assert.AreEqual("user", line.Hostmask.UserName); + Assert.AreEqual("host", line.Hostmask.HostName); + } + + [TestMethod] + public void EmptyHostmaskFromLine() + { + var line = new Line("PRIVMSG #channel hello"); + Assert.IsNull(line.Hostmask.HostName); + Assert.IsNull(line.Hostmask.UserName); + Assert.IsNull(line.Hostmask.NickName); + } + } +} diff --git a/IRCSharp.Tests/Tokenization/Parser.cs b/IRCSharp.Tests/Tokenization/Parser.cs new file mode 100644 index 0000000..40ff803 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Parser.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using IRCTokens.Tests.Data; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace IRCTokens.Tests +{ + [TestClass] + public class Parser + { + private static T LoadYaml(string path) + { + var deserializer = new DeserializerBuilder() + .WithNamingConvention(CamelCaseNamingConvention.Instance) + .Build(); + + return deserializer.Deserialize(File.ReadAllText(path)); + } + + [TestMethod] + public void Split() + { + foreach (var test in LoadYaml("Tokenization/Data/msg-split.yaml").Tests) + { + var tokens = new Line(test.Input); + var atoms = test.Atoms; + + Assert.AreEqual(atoms.Verb.ToUpper(CultureInfo.InvariantCulture), tokens.Command, + $"command failed on: '{test.Input}'"); + Assert.AreEqual(atoms.Source, tokens.Source, $"source failed on: '{test.Input}'"); + CollectionAssert.AreEqual(atoms.Tags, tokens.Tags, $"tags failed on: '{test.Input}'"); + CollectionAssert.AreEqual(atoms.Params ?? new List(), tokens.Params, + $"params failed on: '{test.Input}'"); + } + } + + [TestMethod] + public void Join() + { + foreach (var test in LoadYaml("Tokenization/Data/msg-join.yaml").Tests) + { + var atoms = test.Atoms; + var line = new Line + { + Command = atoms.Verb, Params = atoms.Params, Source = atoms.Source, Tags = atoms.Tags + }.Format(); + + Assert.IsTrue(test.Matches.Contains(line), test.Description); + } + } + } +} diff --git a/IRCSharp.Tests/Tokenization/StatefulDecoder.cs b/IRCSharp.Tests/Tokenization/StatefulDecoder.cs new file mode 100644 index 0000000..4da7690 --- /dev/null +++ b/IRCSharp.Tests/Tokenization/StatefulDecoder.cs @@ -0,0 +1,88 @@ +using System.Collections.Generic; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IRCTokens.Tests +{ + [TestClass] + public class StatefulDecoder + { + private IRCTokens.StatefulDecoder _decoder; + + [TestInitialize] + public void Initialize() + { + _decoder = new IRCTokens.StatefulDecoder(); + } + + [TestMethod] + public void Partial() + { + var lines = _decoder.Push("PRIVMSG "); + Assert.AreEqual(0, lines.Count); + + lines = _decoder.Push("#channel hello\r\n"); + Assert.AreEqual(1, lines.Count); + + var line = new Line("PRIVMSG #channel hello"); + CollectionAssert.AreEqual(new List {line}, lines); + } + + [TestMethod] + public void Multiple() + { + var lines = _decoder.Push("PRIVMSG #channel1 hello\r\nPRIVMSG #channel2 hello\r\n"); + Assert.AreEqual(2, lines.Count); + + var line1 = new Line("PRIVMSG #channel1 hello"); + var line2 = new Line("PRIVMSG #channel2 hello"); + Assert.AreEqual(line1, lines[0]); + Assert.AreEqual(line2, lines[1]); + } + + [TestMethod] + public void EncodingIso8859() + { + var iso8859 = Encoding.GetEncoding("iso-8859-1"); + _decoder = new IRCTokens.StatefulDecoder {Encoding = iso8859}; + var bytes = iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n"); + var lines = _decoder.Push(bytes, bytes.Length); + var line = new Line("PRIVMSG #channel :hello Ç"); + Assert.IsTrue(line.Equals(lines[0])); + } + + [TestMethod] + public void EncodingFallback() + { + var latin1 = Encoding.GetEncoding("iso-8859-1"); + _decoder = new IRCTokens.StatefulDecoder {Encoding = null, Fallback = latin1}; + var bytes = latin1.GetBytes("PRIVMSG #channel hélló\r\n"); + var lines = _decoder.Push(bytes, bytes.Length); + Assert.AreEqual(1, lines.Count); + Assert.IsTrue(new Line("PRIVMSG #channel hélló").Equals(lines[0])); + } + + [TestMethod] + public void Empty() + { + var lines = _decoder.Push(string.Empty); + Assert.AreEqual(0, lines.Count); + } + + [TestMethod] + public void BufferUnfinished() + { + _decoder.Push("PRIVMSG #channel hello"); + var lines = _decoder.Push(string.Empty); + Assert.AreEqual(0, lines.Count); + } + + [TestMethod] + public void Clear() + { + _decoder.Push("PRIVMSG "); + _decoder.Clear(); + Assert.AreEqual(string.Empty, _decoder.Pending); + } + } +} diff --git a/IRCSharp.Tests/Tokenization/StatefulEncoder.cs b/IRCSharp.Tests/Tokenization/StatefulEncoder.cs new file mode 100644 index 0000000..d1e1e3e --- /dev/null +++ b/IRCSharp.Tests/Tokenization/StatefulEncoder.cs @@ -0,0 +1,84 @@ +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IRCTokens.Tests +{ + [TestClass] + public class StatefulEncoder + { + private IRCTokens.StatefulEncoder _encoder; + + [TestInitialize] + public void Initialize() + { + _encoder = new IRCTokens.StatefulEncoder(); + } + + [TestMethod] + public void Push() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + Assert.AreEqual("PRIVMSG #channel hello\r\n", _encoder.Pending()); + } + + [TestMethod] + public void PopPartial() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + _encoder.Pop("PRIVMSG #channel hello".Length); + Assert.AreEqual("\r\n", _encoder.Pending()); + } + + [TestMethod] + public void TestPopReturned() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + _encoder.Push(line); + var lines = _encoder.Pop("PRIVMSG #channel hello\r\n".Length); + Assert.AreEqual(1, lines.Count); + Assert.AreEqual(line, lines[0]); + } + + [TestMethod] + public void PopNoneReturned() + { + var line = new Line("PRIVMSG #channel hello"); + _encoder.Push(line); + var lines = _encoder.Pop(1); + Assert.AreEqual(0, lines.Count); + } + + [TestMethod] + public void PopMultipleLines() + { + var line1 = new Line("PRIVMSG #channel1 hello"); + _encoder.Push(line1); + var line2 = new Line("PRIVMSG #channel2 hello"); + _encoder.Push(line2); + + var lines = _encoder.Pop(_encoder.Pending().Length); + Assert.AreEqual(2, lines.Count); + Assert.AreEqual(string.Empty, _encoder.Pending()); + } + + [TestMethod] + public void Clear() + { + _encoder.Push(new Line("PRIVMSG #channel hello")); + _encoder.Clear(); + Assert.AreEqual(string.Empty, _encoder.Pending()); + } + + [TestMethod] + public void EncodingIso8859() + { + var iso8859 = Encoding.GetEncoding("iso-8859-1"); + _encoder = new IRCTokens.StatefulEncoder {Encoding = iso8859}; + _encoder.Push(new Line("PRIVMSG #channel :hello Ç")); + CollectionAssert.AreEqual(iso8859.GetBytes("PRIVMSG #channel :hello Ç\r\n"), _encoder.PendingBytes); + } + } +} diff --git a/IRCSharp.Tests/Tokenization/Tokenization.cs b/IRCSharp.Tests/Tokenization/Tokenization.cs new file mode 100644 index 0000000..c4c5c5a --- /dev/null +++ b/IRCSharp.Tests/Tokenization/Tokenization.cs @@ -0,0 +1,133 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace IRCTokens.Tests +{ + [TestClass] + public class Tokenization + { + [TestMethod] + public void TagsMissing() + { + var line = new Line("PRIVMSG #channel"); + Assert.IsNull(line.Tags); + } + + [TestMethod] + public void TagsMissingValue() + { + var line = new Line("@id= PRIVMSG #channel"); + Assert.AreEqual(string.Empty, line.Tags["id"]); + } + + [TestMethod] + public void TagsMissingEqual() + { + var line = new Line("@id PRIVMSG #channel"); + Assert.AreEqual(string.Empty, line.Tags["id"]); + } + + [TestMethod] + 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 TagsOverlap() + { + var line = new Line(@"@id=1\\\s\\s PRIVMSG #channel"); + Assert.AreEqual("1\\ \\s", line.Tags["id"]); + } + + [TestMethod] + public void TagsLoneEndSlash() + { + var line = new Line("@id=1\\ PRIVMSG #channel"); + Assert.AreEqual("1", line.Tags["id"]); + } + + [TestMethod] + public void SourceWithoutTags() + { + var line = new Line(":nick!user@host PRIVMSG #channel"); + Assert.AreEqual("nick!user@host", line.Source); + } + + [TestMethod] + public void SourceWithTags() + { + var line = new Line("@id=123 :nick!user@host PRIVMSG #channel"); + Assert.AreEqual("nick!user@host", line.Source); + } + + [TestMethod] + public void SourceMissingWithoutTags() + { + var line = new Line("PRIVMSG #channel"); + Assert.IsNull(line.Source); + } + + [TestMethod] + public void SourceMissingWithTags() + { + var line = new Line("@id=123 PRIVMSG #channel"); + Assert.IsNull(line.Source); + } + + [TestMethod] + public void Command() + { + var line = new Line("privmsg #channel"); + Assert.AreEqual("PRIVMSG", line.Command); + } + + [TestMethod] + public void ParamsTrailing() + { + var line = new Line("PRIVMSG #channel :hello world"); + CollectionAssert.AreEqual(new List {"#channel", "hello world"}, line.Params); + } + + [TestMethod] + public void ParamsOnlyTrailing() + { + var line = new Line("PRIVMSG :hello world"); + CollectionAssert.AreEqual(new List {"hello world"}, line.Params); + } + + [TestMethod] + public void ParamsMissing() + { + var line = new Line("PRIVMSG"); + Assert.AreEqual("PRIVMSG", line.Command); + CollectionAssert.AreEqual(new List(), line.Params); + } + + [TestMethod] + public void AllTokens() + { + var line = new Line("@id=123 :nick!user@host PRIVMSG #channel :hello world"); + CollectionAssert.AreEqual(new Dictionary {{"id", "123"}}, line.Tags); + Assert.AreEqual("nick!user@host", line.Source); + 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