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.

IRCReaderTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2006-2017 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.parser.interfaces.Encoder;
  24. import com.dmdirc.parser.irc.IRCReader.ReadLine;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.nio.charset.Charset;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31. import org.junit.Assert;
  32. import org.junit.Test;
  33. import static org.junit.Assert.assertArrayEquals;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertNull;
  36. import static org.junit.Assert.assertTrue;
  37. import static org.mockito.Mockito.any;
  38. import static org.mockito.Mockito.anyInt;
  39. import static org.mockito.Mockito.anyString;
  40. import static org.mockito.Mockito.eq;
  41. import static org.mockito.Mockito.isNull;
  42. import static org.mockito.Mockito.mock;
  43. import static org.mockito.Mockito.verify;
  44. import static org.mockito.Mockito.when;
  45. public class IRCReaderTest {
  46. /** Reads and verifies a single line. */
  47. @Test
  48. public void testReadLine() throws IOException {
  49. final InputStream stream = new ByteArrayInputStream("test\r\n".getBytes());
  50. final Encoder encoder = mock(Encoder.class);
  51. final IRCReader reader = new IRCReader(stream, encoder);
  52. final ReadLine line = reader.readLine();
  53. assertEquals("test", line.getLine());
  54. }
  55. /** Reads and verifies a single line with only a trailing LF. */
  56. @Test
  57. public void testReadLineWithOnlyLF() throws IOException {
  58. final InputStream stream = new ByteArrayInputStream("test\n".getBytes());
  59. final Encoder encoder = mock(Encoder.class);
  60. final IRCReader reader = new IRCReader(stream, encoder);
  61. final ReadLine line = reader.readLine();
  62. assertEquals("test", line.getLine());
  63. }
  64. /** Reads and verifies a single line with a trailing parameter. */
  65. @Test
  66. public void testReadLineWithParameter() throws IOException {
  67. final InputStream stream = new ByteArrayInputStream("te t :foo\r\n".getBytes());
  68. final Encoder encoder = mock(Encoder.class);
  69. when(encoder.encode(isNull(), isNull(),
  70. any(), anyInt(), eq(3)))
  71. .thenReturn("encoded");
  72. final IRCReader reader = new IRCReader(stream, encoder);
  73. final ReadLine line = reader.readLine();
  74. assertArrayEquals(new String[]{"te", "t", "encoded",},
  75. line.getTokens());
  76. }
  77. /** Reads and verifies a single line with a trailing parameter. */
  78. @Test
  79. public void testReadLineWithMultipleSpaces() throws IOException {
  80. final InputStream stream = new ByteArrayInputStream("foo bar baz :qux baz\r\n".getBytes());
  81. final Encoder encoder = mock(Encoder.class);
  82. when(encoder.encode(isNull(), isNull(), any(), eq(15), eq(8))).thenReturn("qux baz");
  83. final IRCReader reader = new IRCReader(stream, encoder);
  84. final ReadLine line = reader.readLine();
  85. assertArrayEquals(new String[]{"foo", "bar", "baz", "qux baz",}, line.getTokens());
  86. }
  87. /** Verifies that a source is extracted properly. */
  88. @Test
  89. public void testGetSource() throws IOException {
  90. final InputStream stream = new ByteArrayInputStream(":src x :o\r\n".getBytes());
  91. final Encoder encoder = mock(Encoder.class);
  92. new IRCReader(stream, encoder).readLine();
  93. verify(encoder).encode(eq("src"), isNull(), any(), anyInt(), anyInt());
  94. }
  95. /** Verifies that no source is passed if the source is empty. */
  96. @Test
  97. public void testGetEmptySource() throws IOException {
  98. final InputStream stream = new ByteArrayInputStream(": rc x :o\r\n".getBytes());
  99. final Encoder encoder = mock(Encoder.class);
  100. new IRCReader(stream, encoder).readLine();
  101. verify(encoder).encode(isNull(), isNull(), any(), anyInt(), anyInt());
  102. }
  103. /** Verifies that a destination is extracted properly. */
  104. @Test
  105. public void testGetDestination() throws IOException {
  106. final InputStream stream = new ByteArrayInputStream(":src x y :z\r\n".getBytes());
  107. final Encoder encoder = mock(Encoder.class);
  108. new IRCReader(stream, encoder).readLine();
  109. verify(encoder).encode(anyString(), eq("y"), any(), anyInt(), anyInt());
  110. }
  111. /** Verifies that no destination is extracted if there's no source. */
  112. @Test
  113. public void testGetDestinationNoSource() throws IOException {
  114. final InputStream stream = new ByteArrayInputStream("_src x y :z\r\n".getBytes());
  115. final Encoder encoder = mock(Encoder.class);
  116. new IRCReader(stream, encoder).readLine();
  117. verify(encoder).encode(isNull(), isNull(), any(), anyInt(), anyInt());
  118. }
  119. /** Verifies that no destination is extracted if there are too few args. */
  120. @Test
  121. public void testGetDestinationTooShort() throws IOException {
  122. final InputStream stream = new ByteArrayInputStream(":src x :abz\r\n".getBytes());
  123. final Encoder encoder = mock(Encoder.class);
  124. new IRCReader(stream, encoder).readLine();
  125. verify(encoder).encode(anyString(), isNull(), any(), anyInt(), anyInt());
  126. }
  127. /** Verifies that a numeric's destination is extracted properly. */
  128. @Test
  129. public void testGetDestinationWithNumeric() throws IOException {
  130. final InputStream stream = new ByteArrayInputStream(":src 1 x y z :x\r\n".getBytes());
  131. final Encoder encoder = mock(Encoder.class);
  132. new IRCReader(stream, encoder).readLine();
  133. verify(encoder).encode(anyString(), eq("y"), any(), anyInt(), anyInt());
  134. }
  135. /** Verifies that the close call is proxied to the stream. */
  136. @Test
  137. public void testClose() throws IOException {
  138. final InputStream stream = mock(InputStream.class);
  139. final Encoder encoder = mock(Encoder.class);
  140. new IRCReader(stream, encoder).close();
  141. verify(stream).close();
  142. }
  143. /** Verifies that the reader works with improperly coded unicode. */
  144. @Test
  145. public void testHandlesBadCoding() throws IOException {
  146. final InputStream stream = mock(InputStream.class);
  147. final Encoder encoder = mock(Encoder.class);
  148. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r', (int) 'c', (int) ' ',
  149. (int) '1', (int) ' ', 0xF6, (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
  150. (int) ':', (int) 'x', (int) '\r', (int) '\n');
  151. when(encoder.encode(anyString(), anyString(), any(), anyInt(), anyInt())).thenReturn("x");
  152. final ReadLine line = new IRCReader(stream, encoder, Charset.forName("UTF-8")).readLine();
  153. Assert.assertArrayEquals(new String[]{":src", "1", "\uFFFD", "y", "z", "x"}, line.getTokens());
  154. }
  155. /** Verify tokeniser with TSIRC Time Stamp */
  156. @Test
  157. public void testReadLineTSIRC() throws IOException {
  158. final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123@:test ing"));
  159. assertEquals(":test", line.getTokens()[0]);
  160. assertEquals("ing", line.getTokens()[1]);
  161. assertEquals("123", line.getTags().get("tsirc date"));
  162. assertNull(line.getTags().get("time"));
  163. }
  164. /** Verify tokeniser with TSIRC Time Stamp and Message Tags (tags first). */
  165. @Test
  166. public void testReadLineTSIRCandTag() throws IOException {
  167. final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@tag=value @123@:test ing"));
  168. assertEquals(":test", line.getTokens()[0]);
  169. assertEquals("ing", line.getTokens()[1]);
  170. assertEquals("123", line.getTags().get("tsirc date"));
  171. assertEquals("value", line.getTags().get("tag"));
  172. }
  173. /** Verify tokeniser with TSIRC Time Stamp and Message Tags (tags second). */
  174. @Test
  175. public void testReadLineTSIRCandTag2() throws IOException {
  176. final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123@@tag=value :test ing"));
  177. assertEquals(":test", line.getTokens()[0]);
  178. assertEquals("ing", line.getTokens()[1]);
  179. assertEquals("123", line.getTags().get("tsirc date"));
  180. assertEquals("value", line.getTags().get("tag"));
  181. }
  182. /** Verify tokeniser with IRCv3 tags */
  183. @Test
  184. public void testReadLineIRCv3TS() throws IOException {
  185. final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  186. final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing"));
  187. assertEquals(":test", line.getTokens()[0]);
  188. assertEquals("ing", line.getTokens()[1]);
  189. assertNull(line.getTags().get("tsirc date"));
  190. assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
  191. }
  192. /** Verify tokeniser with a numeric tag timestamp */
  193. @Test
  194. public void testReadLineNumericTag() throws IOException {
  195. final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123 :test ing"));
  196. assertEquals(":test", line.getTokens()[0]);
  197. assertEquals("ing", line.getTokens()[1]);
  198. assertTrue(line.getTags().containsKey("123"));
  199. }
  200. /** Verify line with TSIRC Time Stamp */
  201. @Test
  202. public void testReaderTSIRC() throws IOException {
  203. final InputStream stream = new ByteArrayInputStream("@123@:test ing\r\n".getBytes());
  204. final Encoder encoder = mock(Encoder.class);
  205. final IRCReader reader = new IRCReader(stream, encoder);
  206. final ReadLine line = reader.readLine();
  207. assertEquals(":test", line.getTokens()[0]);
  208. assertEquals("ing", line.getTokens()[1]);
  209. assertEquals("123", line.getTags().get("tsirc date"));
  210. assertNull(line.getTags().get("time"));
  211. }
  212. /** Verify line with IRCv3 timestamp */
  213. @Test
  214. public void testReaderIRCv3TS() throws IOException {
  215. final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  216. final InputStream stream = new ByteArrayInputStream(("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing\r\n").getBytes());
  217. final Encoder encoder = mock(Encoder.class);
  218. final IRCReader reader = new IRCReader(stream, encoder);
  219. final ReadLine line = reader.readLine();
  220. assertEquals(":test", line.getTokens()[0]);
  221. assertEquals("ing", line.getTokens()[1]);
  222. assertNull(line.getTags().get("tsirc date"));
  223. assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
  224. }
  225. /** Verify line with a numeric tag timestamp */
  226. @Test
  227. public void testReaderNumericTag() throws IOException {
  228. final InputStream stream = new ByteArrayInputStream("@123 :test ing\r\n".getBytes());
  229. final Encoder encoder = mock(Encoder.class);
  230. final IRCReader reader = new IRCReader(stream, encoder);
  231. final ReadLine line = reader.readLine();
  232. assertEquals(":test", line.getTokens()[0]);
  233. assertEquals("ing", line.getTokens()[1]);
  234. assertTrue(line.getTags().containsKey("123"));
  235. }
  236. }