1 package net.pterodactylus.irc.connection;
3 import static net.pterodactylus.irc.Source.parseSource;
4 import static net.pterodactylus.irc.connection.Replies.createReply;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.equalTo;
7 import static org.hamcrest.Matchers.hasSize;
8 import static org.hamcrest.Matchers.is;
9 import static org.mockito.ArgumentCaptor.forClass;
10 import static org.mockito.Matchers.anyObject;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.event.DccAcceptReceived;
17 import net.pterodactylus.irc.event.DccSendReceived;
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
24 * Unit test for {@link CtcpHandler}.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class CtcpHandlerTest {
30 private final EventBus eventBus = mock(EventBus.class);
31 private final Connection connection = mock(Connection.class);
32 private final CtcpHandler handler = new CtcpHandler(eventBus, connection);
35 public void handlerWillHandleCtcpMessagesSentAsNotice() {
36 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION\u0001")), is(true));
40 public void handlerWillHandleCtcpMessagesSentAsPrivmsg() {
41 assertThat(handler.willHandle(createReply("PRIVMSG", "\u0001ACTION\u0001")), is(true));
45 public void handlerWillNotHandleNormalMessages() {
46 assertThat(handler.willHandle(createReply("NOTICE", "ACTION")), is(false));
50 public void handlerWillNotHandleMalformedMessages() {
51 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION")), is(false));
55 public void dccSendCommandAsNoticeIsRecognizedCorrectly() {
56 dccSendCommandIsRecognizedCorrectly("NOTICE", "23456", 23456L);
59 private void dccSendCommandIsRecognizedCorrectly(String command,
60 Object sizeAsString, long expectedSize) {
61 handler.handleReply(createReply(parseSource("User!user@host"),
63 "\u0001DCC SEND test.dat 2130706433 12345 " + sizeAsString + "\u0001"));
64 ArgumentCaptor<DccSendReceived> eventCaptor = forClass(
65 DccSendReceived.class);
66 verify(eventBus).post(eventCaptor.capture());
67 DccSendReceived dccSendReceived = eventCaptor.getValue();
68 assertThat(dccSendReceived.connection(), is(connection));
69 assertThat(dccSendReceived.filename(), is("test.dat"));
70 assertThat(dccSendReceived.inetAddress().getHostAddress(),
72 assertThat(dccSendReceived.port(), is(12345));
73 assertThat(dccSendReceived.filesize(), is(expectedSize));
74 assertThat(dccSendReceived.source().nick().get(), is("User"));
78 public void dccSendCommandAsPrivmsgIsRecognizedCorrectly() {
79 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "23456", 23456L);
83 public void nonDccCommandDoesNotCauseEvent() {
84 handler.handleReply(createReply(parseSource("User!user@host"),
86 "\u0001FOO SEND test.dat 2130706433 12345 23456\u0001"));
87 verify(eventBus, never()).post(anyObject());
91 public void nonSendDccCommandDoesNotCauseEvent() {
92 handler.handleReply(createReply(parseSource("User!user@host"),
94 "\u0001DCC FOO test.dat 2130706433 12345 23456\u0001"));
95 verify(eventBus, never()).post(anyObject());
99 public void dccCommandWithoutPortNumberDoesNotCauseEvent() {
100 handler.handleReply(createReply(parseSource("User!user@host"),
102 "\u0001DCC SEND test.dat 2130706433\u0001"));
103 verify(eventBus, never()).post(anyObject());
107 public void dccCommandWithInvalidPortNumberDoesNotCauseEvent() {
108 handler.handleReply(createReply(parseSource("User!user@host"),
110 "\u0001DCC SEND test.dat 2130706433 abc\u0001"));
111 verify(eventBus, never()).post(anyObject());
115 public void dccCommandWithoutIpAddressDoesNotCauseEvent() {
116 handler.handleReply(createReply(parseSource("User!user@host"),
118 "\u0001DCC SEND test.dat\u0001"));
119 verify(eventBus, never()).post(anyObject());
123 public void dccCommandWithInvalidIpAddressDoesNotCauseEvent() {
124 handler.handleReply(createReply(parseSource("User!user@host"),
126 "\u0001DCC SEND test.dat abc abc\u0001"));
127 verify(eventBus, never()).post(anyObject());
131 public void dccCommandWithoutLengthIsRecognizedCorrectly() {
132 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "", -1);
136 public void dccCommandWithTokenIsRecognizedCorrectly() {
137 handler.handleReply(createReply(parseSource("User!user@host"), "PRIVMSG", "\u0001DCC SEND filename 1234 0 12345 T123\u0001"));
138 ArgumentCaptor<DccSendReceived> dccSendReceivedCaptor = forClass(DccSendReceived.class);
139 verify(eventBus).post(dccSendReceivedCaptor.capture());
140 assertThat(dccSendReceivedCaptor.getAllValues(), hasSize(1));
141 DccSendReceived dccSendReceived = dccSendReceivedCaptor.getValue();
142 assertThat(dccSendReceived.token(), equalTo("T123"));
146 public void dccAcceptCommandIsRecognized() {
147 handler.handleReply(createReply(parseSource("User!user@host"),
149 "\u0001DCC ACCEPT test.dat 12345 23456\u0001"));
150 ArgumentCaptor<DccAcceptReceived> eventCaptor = forClass(
151 DccAcceptReceived.class);
152 verify(eventBus).post(eventCaptor.capture());
153 DccAcceptReceived dccAcceptReceived = eventCaptor.getValue();
154 assertThat(dccAcceptReceived.connection(), is(connection));
155 assertThat(dccAcceptReceived.filename(), is("test.dat"));
156 assertThat(dccAcceptReceived.port(), is(12345));
157 assertThat(dccAcceptReceived.position(), is(23456L));
158 assertThat(dccAcceptReceived.source().nick().get(), is("User"));
162 public void dccAcceptCommandWithoutParametersDoesNotCauseEvent() {
163 handler.handleReply(createReply(parseSource("User!user@host"),
165 "\u0001DCC ACCEPT\u0001"));
166 verify(eventBus,never()).post(anyObject());
170 public void dccAcceptCommandWithInvalidPortDoesNotCauseEvent() {
171 handler.handleReply(createReply(parseSource("User!user@host"),
173 "\u0001DCC ACCEPT test.dat abc\u0001"));
174 verify(eventBus,never()).post(anyObject());
178 public void dccAcceptCommandWithMissingPositionIsRecognized() {
179 handler.handleReply(createReply(parseSource("User!user@host"),
181 "\u0001DCC ACCEPT test.dat 12345\u0001"));
182 ArgumentCaptor<DccAcceptReceived> eventCaptor = forClass(
183 DccAcceptReceived.class);
184 verify(eventBus).post(eventCaptor.capture());
185 DccAcceptReceived dccAcceptReceived = eventCaptor.getValue();
186 assertThat(dccAcceptReceived.connection(), is(connection));
187 assertThat(dccAcceptReceived.filename(), is("test.dat"));
188 assertThat(dccAcceptReceived.port(), is(12345));
189 assertThat(dccAcceptReceived.position(), is(-1L));
190 assertThat(dccAcceptReceived.source().nick().get(), is("User"));