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 11KB

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