You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCParserTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import com.dmdirc.harness.parser.TestParser;
  24. import com.dmdirc.parser.common.CallbackNotFoundException;
  25. import com.dmdirc.parser.common.MyInfo;
  26. import com.dmdirc.parser.interfaces.callbacks.AuthNoticeListener;
  27. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  28. import com.dmdirc.parser.interfaces.callbacks.ChannelKickListener;
  29. import com.dmdirc.parser.interfaces.callbacks.ConnectErrorListener;
  30. import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
  31. import com.dmdirc.parser.interfaces.callbacks.NumericListener;
  32. import com.dmdirc.parser.interfaces.callbacks.ServerErrorListener;
  33. import com.dmdirc.parser.interfaces.callbacks.ServerReadyListener;
  34. import java.net.URI;
  35. import java.net.URISyntaxException;
  36. import java.util.Arrays;
  37. import javax.net.ssl.TrustManager;
  38. import org.junit.Ignore;
  39. import org.junit.Test;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertFalse;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.mockito.Mockito.anyObject;
  44. import static org.mockito.Mockito.anyString;
  45. import static org.mockito.Mockito.eq;
  46. import static org.mockito.Mockito.mock;
  47. import static org.mockito.Mockito.never;
  48. import static org.mockito.Mockito.same;
  49. import static org.mockito.Mockito.verify;
  50. @Ignore
  51. public class IRCParserTest {
  52. private interface TestCallback extends CallbackInterface { }
  53. @Test(expected=CallbackNotFoundException.class)
  54. public void testIssue42() {
  55. // Invalid callback names are silently ignored instead of raising exceptions
  56. final IRCParser myParser = new IRCParser();
  57. }
  58. @Test
  59. public void testIssue1674() {
  60. // parser nick change error with dual 001
  61. final ErrorInfoListener error = mock(ErrorInfoListener.class);
  62. final TestParser myParser = new TestParser();
  63. myParser.injectConnectionStrings();
  64. myParser.nick = "nick2";
  65. myParser.injectConnectionStrings();
  66. myParser.injectLine(":nick2!ident@host NICK :nick");
  67. verify(error, never()).onErrorInfo(same(myParser), anyObject(), anyObject());
  68. }
  69. @Test
  70. public void testTokeniser() {
  71. final String line1 = "a b c d e";
  72. final String line2 = "a b c :d e";
  73. final String line3 = ":a b:c :d e";
  74. final String line4 = null;
  75. final String[] res1 = IRCParser.tokeniseLine(line1);
  76. final String[] res2 = IRCParser.tokeniseLine(line2);
  77. final String[] res3 = IRCParser.tokeniseLine(line3);
  78. final String[] res4 = IRCParser.tokeniseLine(line4);
  79. assertTrue(Arrays.equals(res1, new String[]{"a", "b", "c", "d", "e"}));
  80. assertTrue(Arrays.equals(res2, new String[]{"a", "b", "c", "d e"}));
  81. assertTrue(Arrays.equals(res3, new String[]{":a", "b:c", "d e"}));
  82. assertTrue(Arrays.equals(res4, new String[]{""}));
  83. }
  84. @Test
  85. public void testSendConnectionStrings1() throws URISyntaxException {
  86. final MyInfo myInfo = new MyInfo();
  87. myInfo.setNickname("Nickname");
  88. myInfo.setRealname("Real name");
  89. myInfo.setUsername("Username");
  90. final TestParser parser = new TestParser(myInfo, new URI("irc://irc.testing.dmdirc:6667/"));
  91. parser.sendConnectionStrings();
  92. assertEquals(3, parser.sentLines.size());
  93. assertTrue("Should send CAP LS line",
  94. Arrays.equals(parser.getLine(0), new String[]{"CAP", "LS"}));
  95. assertTrue("Should send nickname line",
  96. Arrays.equals(parser.getLine(1), new String[]{"NICK", "Nickname"}));
  97. final String[] userParts = parser.getLine(2);
  98. assertEquals("First token should be USER", "USER", userParts[0]);
  99. assertEquals("USER should contain username", myInfo.getUsername().toLowerCase(),
  100. userParts[1].toLowerCase());
  101. assertEquals("USER should contain server name", "irc.testing.dmdirc", userParts[3]);
  102. assertEquals("USER should contain real name", "Real name", userParts[4]);
  103. }
  104. @Test
  105. public void testSendConnectionStrings2() throws URISyntaxException {
  106. final MyInfo myInfo = new MyInfo();
  107. myInfo.setNickname("Nickname");
  108. myInfo.setRealname("Real name");
  109. myInfo.setUsername("Username");
  110. final TestParser parser = new TestParser(myInfo, new URI("irc://password@irc.testing.dmdirc:6667/"));
  111. parser.sendConnectionStrings();
  112. assertEquals(4, parser.sentLines.size());
  113. assertTrue("Should send password line",
  114. Arrays.equals(parser.getLine(1), new String[]{"PASS", "password"}));
  115. }
  116. @Test
  117. public void testPingPong() {
  118. final TestParser parser = new TestParser();
  119. parser.injectLine("PING :flubadee7291");
  120. assertTrue("Should reply to PINGs with PONGs",
  121. Arrays.equals(parser.getLine(0), new String[]{"PONG", "flubadee7291"}));
  122. }
  123. @Test
  124. public void testError() throws CallbackNotFoundException {
  125. final ServerErrorListener test = mock(ServerErrorListener.class);
  126. final TestParser parser = new TestParser();
  127. parser.injectLine("ERROR :You smell of cheese");
  128. verify(test).onServerError(same(parser), anyObject(), eq("You smell of cheese"));
  129. }
  130. @Test
  131. public void testAuthNotice() throws CallbackNotFoundException {
  132. final AuthNoticeListener test = mock(AuthNoticeListener.class);
  133. final TestParser parser = new TestParser();
  134. parser.sendConnectionStrings();
  135. parser.injectLine("NOTICE AUTH :Random auth notice?");
  136. verify(test).onNoticeAuth(same(parser), anyObject(), eq("Random auth notice?"));
  137. }
  138. @Test
  139. public void testAuthNoticeTwenty() throws CallbackNotFoundException {
  140. final AuthNoticeListener test = mock(AuthNoticeListener.class);
  141. final TestParser parser = new TestParser();
  142. parser.sendConnectionStrings();
  143. parser.injectLine(":us.ircnet.org 020 * :Stupid notice");
  144. verify(test).onNoticeAuth(same(parser), anyObject(), eq("Stupid notice"));
  145. }
  146. @Test
  147. public void testPre001NickChange() throws CallbackNotFoundException {
  148. final AuthNoticeListener test = mock(AuthNoticeListener.class);
  149. final TestParser parser = new TestParser();
  150. parser.sendConnectionStrings();
  151. parser.injectLine(":chris!@ NICK :user2");
  152. verify(test, never()).onNoticeAuth(anyObject(), anyObject(), anyString());
  153. }
  154. @Test
  155. public void testNumeric() throws CallbackNotFoundException {
  156. final NumericListener test = mock(NumericListener.class);
  157. final TestParser parser = new TestParser();
  158. parser.injectLine(":server 001 nick :Hi there, nick");
  159. verify(test).onNumeric(same(parser), anyObject(), eq(1),
  160. eq(new String[]{":server", "001", "nick", "Hi there, nick"}));
  161. }
  162. @Test
  163. public void testServerReady() throws CallbackNotFoundException {
  164. final ServerReadyListener test = mock(ServerReadyListener.class);
  165. final TestParser parser = new TestParser();
  166. final String[] strings = {
  167. "NOTICE AUTH :Blah, blah",
  168. ":server 020 * :Blah! Blah!",
  169. ":server 001 nick :Welcome to the Testing IRC Network, nick",
  170. ":server 002 nick :Your host is server.net, running version foo",
  171. "NOTICE AUTH :I'm a retarded server",
  172. ":server 003 nick :This server was created Sun Jan 6 2008 at 17:34:54 CET",
  173. ":server 004 nick server.net foo dioswkgxRXInP biklmnopstvrDcCNuMT bklov",
  174. ":server 005 nick WHOX WALLCHOPS WALLVOICES USERIP :are supported by this server",
  175. ":server 005 nick MAXNICKLEN=15 TOPICLEN=250 AWAYLEN=160 :are supported " +
  176. "by this server",
  177. ":server 375 nick :zomg, motd!",
  178. };
  179. for (String string : strings) {
  180. verify(test, never()).onServerReady(anyObject(), anyObject());
  181. parser.injectLine(string);
  182. }
  183. verify(test).onServerReady(same(parser), anyObject());
  184. }
  185. @Test
  186. public void test005Parsing() {
  187. final TestParser parser = new TestParser();
  188. final String[] strings = {
  189. ":server 001 nick :Welcome to the Testing IRC Network, nick",
  190. ":server 002 nick :Your host is server.net, running version foo",
  191. ":server 003 nick :This server was created Sun Jan 6 2008 at 17:34:54 CET",
  192. ":server 004 nick server.net foo dioswkgxRXInP biklmnopstvrDcCNuMT bklov",
  193. ":server 005 nick WHOX WALLCHOPS WALLVOICES NETWORK=moo :are supported by" +
  194. " this server",
  195. ":server 005 nick MAXNICKLEN=15 MAXLIST=b:10,e:22,I:45 :are supported by" +
  196. " this server",
  197. ":server 375 nick :zomg, motd!",
  198. };
  199. for (String string : strings) {
  200. parser.injectLine(string);
  201. }
  202. assertEquals(10, parser.getMaxListModes('b'));
  203. assertEquals(22, parser.getMaxListModes('e'));
  204. assertEquals(45, parser.getMaxListModes('I'));
  205. assertEquals("getMaxListModes should return 0 for unknowns;", 0,
  206. parser.getMaxListModes('z'));
  207. assertEquals("moo", parser.getNetworkName());
  208. assertEquals("server", parser.getServerName());
  209. }
  210. @Test
  211. public void testBindIP() {
  212. final TestParser parser = new TestParser();
  213. parser.setBindIP("abc.def.ghi.123");
  214. assertEquals("abc.def.ghi.123", parser.getBindIP());
  215. }
  216. @Test
  217. public void testAutoListMode() {
  218. final TestParser parser = new TestParser();
  219. parser.setAutoListMode(false);
  220. assertFalse(parser.getAutoListMode());
  221. parser.setAutoListMode(true);
  222. assertTrue(parser.getAutoListMode());
  223. }
  224. @Test
  225. public void testRemoveAfterCallback() {
  226. final TestParser parser = new TestParser();
  227. parser.setRemoveAfterCallback(false);
  228. assertFalse(parser.getRemoveAfterCallback());
  229. parser.setRemoveAfterCallback(true);
  230. assertTrue(parser.getRemoveAfterCallback());
  231. }
  232. @Test
  233. public void testAddLastLine() {
  234. final TestParser parser = new TestParser();
  235. parser.setAddLastLine(false);
  236. assertFalse(parser.getAddLastLine());
  237. parser.setAddLastLine(true);
  238. assertTrue(parser.getAddLastLine());
  239. }
  240. @Test
  241. public void testDisconnectOnFatal() {
  242. final TestParser parser = new TestParser();
  243. parser.setDisconnectOnFatal(false);
  244. assertFalse(parser.getDisconnectOnFatal());
  245. parser.setDisconnectOnFatal(true);
  246. assertTrue(parser.getDisconnectOnFatal());
  247. }
  248. @Test
  249. public void testTrustManager() {
  250. final TestParser parser = new TestParser();
  251. assertTrue(Arrays.equals(parser.getDefaultTrustManager(), parser.getTrustManager()));
  252. parser.setTrustManagers(new TrustManager[0]);
  253. assertTrue(Arrays.equals(new TrustManager[0], parser.getTrustManager()));
  254. }
  255. @Test
  256. public void testGetParam() {
  257. assertEquals("abc def", TestParser.getParam("foo :abc def"));
  258. assertEquals("bar :abc def", TestParser.getParam("foo :bar :abc def"));
  259. assertEquals("abc def", TestParser.getParam("abc def"));
  260. }
  261. @Test
  262. public void testKick() throws CallbackNotFoundException {
  263. final TestParser parser = new TestParser();
  264. final ChannelKickListener ick = mock(ChannelKickListener.class);
  265. parser.injectConnectionStrings();
  266. parser.injectLine(":nick JOIN #D");
  267. parser.injectLine(":bar!me@moo KICK #D nick :Bye!");
  268. verify(ick).onChannelKick(same(parser), anyObject(), anyObject(), anyObject(), anyObject(),
  269. anyString(), anyString());
  270. }
  271. @Test
  272. public void testIllegalPort2() throws URISyntaxException {
  273. final TestParser tp = new TestParser(new MyInfo(), new URI("irc://127.0.0.1:65570/"));
  274. final ConnectErrorListener tiei = mock(ConnectErrorListener.class);
  275. tp.runSuper();
  276. verify(tiei).onConnectError(same(tp), anyObject(), anyObject());
  277. }
  278. }