123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.parser.irc;
-
- import com.dmdirc.parser.interfaces.Encoder;
- import com.dmdirc.parser.irc.IRCReader.ReadLine;
- import java.io.ByteArrayInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.charset.Charset;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.junit.Assert;
- import org.junit.Test;
-
- import static org.junit.Assert.assertArrayEquals;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertTrue;
- import static org.mockito.Mockito.any;
- import static org.mockito.Mockito.anyInt;
- import static org.mockito.Mockito.anyString;
- import static org.mockito.Mockito.eq;
- import static org.mockito.Mockito.isNull;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
-
- public class IRCReaderTest {
-
- /** Reads and verifies a single line. */
- @Test
- public void testReadLine() throws IOException {
- final InputStream stream = new ByteArrayInputStream("test\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertEquals("test", line.getLine());
- }
-
- /** Reads and verifies a single line with only a trailing LF. */
- @Test
- public void testReadLineWithOnlyLF() throws IOException {
- final InputStream stream = new ByteArrayInputStream("test\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertEquals("test", line.getLine());
- }
-
- /** Reads and verifies a single line with a trailing parameter. */
- @Test
- public void testReadLineWithParameter() throws IOException {
- final InputStream stream = new ByteArrayInputStream("te t :foo\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- when(encoder.encode(isNull(), isNull(),
- any(), anyInt(), eq(3)))
- .thenReturn("encoded");
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertArrayEquals(new String[]{"te", "t", "encoded",},
- line.getTokens());
- }
-
- /** Reads and verifies a single line with a trailing parameter. */
- @Test
- public void testReadLineWithMultipleSpaces() throws IOException {
- final InputStream stream = new ByteArrayInputStream("foo bar baz :qux baz\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- when(encoder.encode(isNull(), isNull(), any(), eq(15), eq(8))).thenReturn("qux baz");
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertArrayEquals(new String[]{"foo", "bar", "baz", "qux baz",}, line.getTokens());
- }
-
- /** Verifies that a source is extracted properly. */
- @Test
- public void testGetSource() throws IOException {
- final InputStream stream = new ByteArrayInputStream(":src x :o\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(eq("src"), isNull(), any(), anyInt(), anyInt());
- }
-
- /** Verifies that no source is passed if the source is empty. */
- @Test
- public void testGetEmptySource() throws IOException {
- final InputStream stream = new ByteArrayInputStream(": rc x :o\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(isNull(), isNull(), any(), anyInt(), anyInt());
- }
-
- /** Verifies that a destination is extracted properly. */
- @Test
- public void testGetDestination() throws IOException {
- final InputStream stream = new ByteArrayInputStream(":src x y :z\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(anyString(), eq("y"), any(), anyInt(), anyInt());
- }
-
- /** Verifies that no destination is extracted if there's no source. */
- @Test
- public void testGetDestinationNoSource() throws IOException {
- final InputStream stream = new ByteArrayInputStream("_src x y :z\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(isNull(), isNull(), any(), anyInt(), anyInt());
- }
-
- /** Verifies that no destination is extracted if there are too few args. */
- @Test
- public void testGetDestinationTooShort() throws IOException {
- final InputStream stream = new ByteArrayInputStream(":src x :abz\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(anyString(), isNull(), any(), anyInt(), anyInt());
- }
-
- /** Verifies that a numeric's destination is extracted properly. */
- @Test
- public void testGetDestinationWithNumeric() throws IOException {
- final InputStream stream = new ByteArrayInputStream(":src 1 x y z :x\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).readLine();
-
- verify(encoder).encode(anyString(), eq("y"), any(), anyInt(), anyInt());
- }
-
- /** Verifies that the close call is proxied to the stream. */
- @Test
- public void testClose() throws IOException {
- final InputStream stream = mock(InputStream.class);
- final Encoder encoder = mock(Encoder.class);
-
- new IRCReader(stream, encoder).close();
-
- verify(stream).close();
- }
-
- /** Verifies that the reader works with improperly coded unicode. */
- @Test
- public void testHandlesBadCoding() throws IOException {
- final InputStream stream = mock(InputStream.class);
- final Encoder encoder = mock(Encoder.class);
-
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r', (int) 'c', (int) ' ',
- (int) '1', (int) ' ', 0xF6, (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
- (int) ':', (int) 'x', (int) '\r', (int) '\n');
-
- when(encoder.encode(anyString(), anyString(), any(), anyInt(), anyInt())).thenReturn("x");
-
- final ReadLine line = new IRCReader(stream, encoder, Charset.forName("UTF-8")).readLine();
-
- Assert.assertArrayEquals(new String[]{":src", "1", "\uFFFD", "y", "z", "x"}, line.getTokens());
- }
-
- /** Verify tokeniser with TSIRC Time Stamp */
- @Test
- public void testReadLineTSIRC() throws IOException {
- final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123@:test ing"));
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertEquals("123", line.getTags().get("tsirc date"));
- assertNull(line.getTags().get("time"));
- }
-
- /** Verify tokeniser with TSIRC Time Stamp and Message Tags (tags first). */
- @Test
- public void testReadLineTSIRCandTag() throws IOException {
- final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@tag=value @123@:test ing"));
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertEquals("123", line.getTags().get("tsirc date"));
- assertEquals("value", line.getTags().get("tag"));
- }
-
- /** Verify tokeniser with TSIRC Time Stamp and Message Tags (tags second). */
- @Test
- public void testReadLineTSIRCandTag2() throws IOException {
- final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123@@tag=value :test ing"));
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertEquals("123", line.getTags().get("tsirc date"));
- assertEquals("value", line.getTags().get("tag"));
- }
-
- /** Verify tokeniser with IRCv3 tags */
- @Test
- public void testReadLineIRCv3TS() throws IOException {
- final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
- final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing"));
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertNull(line.getTags().get("tsirc date"));
- assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
- }
-
-
- /** Verify tokeniser with a numeric tag timestamp */
- @Test
- public void testReadLineNumericTag() throws IOException {
- final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123 :test ing"));
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertTrue(line.getTags().containsKey("123"));
- }
-
-
- /** Verify line with TSIRC Time Stamp */
- @Test
- public void testReaderTSIRC() throws IOException {
- final InputStream stream = new ByteArrayInputStream("@123@:test ing\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertEquals("123", line.getTags().get("tsirc date"));
- assertNull(line.getTags().get("time"));
- }
-
- /** Verify line with IRCv3 timestamp */
- @Test
- public void testReaderIRCv3TS() throws IOException {
- final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
- final InputStream stream = new ByteArrayInputStream(("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing\r\n").getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertNull(line.getTags().get("tsirc date"));
- assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
- }
-
- /** Verify line with a numeric tag timestamp */
- @Test
- public void testReaderNumericTag() throws IOException {
- final InputStream stream = new ByteArrayInputStream("@123 :test ing\r\n".getBytes());
- final Encoder encoder = mock(Encoder.class);
-
- final IRCReader reader = new IRCReader(stream, encoder);
- final ReadLine line = reader.readLine();
-
- assertEquals(":test", line.getTokens()[0]);
- assertEquals("ing", line.getTokens()[1]);
- assertTrue(line.getTags().containsKey("123"));
- }
- }
|