Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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