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.is;
7 import static org.mockito.ArgumentCaptor.forClass;
8 import static org.mockito.Matchers.anyObject;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.never;
11 import static org.mockito.Mockito.verify;
13 import net.pterodactylus.irc.Connection;
14 import net.pterodactylus.irc.event.DccAcceptReceived;
15 import net.pterodactylus.irc.event.DccSendReceived;
17 import com.google.common.eventbus.EventBus;
18 import org.junit.Test;
19 import org.mockito.ArgumentCaptor;
22 * Unit test for {@link CtcpHandler}.
24 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
26 public class CtcpHandlerTest {
28 private final EventBus eventBus = mock(EventBus.class);
29 private final Connection connection = mock(Connection.class);
30 private final CtcpHandler handler = new CtcpHandler(eventBus, connection);
33 public void handlerWillHandleCtcpMessagesSentAsNotice() {
34 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION\u0001")), is(true));
38 public void handlerWillHandleCtcpMessagesSentAsPrivmsg() {
39 assertThat(handler.willHandle(createReply("PRIVMSG", "\u0001ACTION\u0001")), is(true));
43 public void handlerWillNotHandleNormalMessages() {
44 assertThat(handler.willHandle(createReply("NOTICE", "ACTION")), is(false));
48 public void handlerWillNotHandleMalformedMessages() {
49 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION")), is(false));
53 public void dccSendCommandAsNoticeIsRecognizedCorrectly() {
54 dccSendCommandIsRecognizedCorrectly("NOTICE", "23456", 23456L);
57 private void dccSendCommandIsRecognizedCorrectly(String command,
58 Object sizeAsString, long expectedSize) {
59 handler.handleReply(createReply(parseSource("User!user@host"),
61 "\u0001DCC SEND test.dat 2130706433 12345 " + sizeAsString + "\u0001"));
62 ArgumentCaptor<DccSendReceived> eventCaptor = forClass(
63 DccSendReceived.class);
64 verify(eventBus).post(eventCaptor.capture());
65 DccSendReceived dccSendReceived = eventCaptor.getValue();
66 assertThat(dccSendReceived.connection(), is(connection));
67 assertThat(dccSendReceived.filename(), is("test.dat"));
68 assertThat(dccSendReceived.inetAddress().getHostAddress(),
70 assertThat(dccSendReceived.port(), is(12345));
71 assertThat(dccSendReceived.filesize(), is(expectedSize));
72 assertThat(dccSendReceived.source().nick().get(), is("User"));
76 public void dccSendCommandAsPrivmsgIsRecognizedCorrectly() {
77 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "23456", 23456L);
81 public void nonDccCommandDoesNotCauseEvent() {
82 handler.handleReply(createReply(parseSource("User!user@host"),
84 "\u0001FOO SEND test.dat 2130706433 12345 23456\u0001"));
85 verify(eventBus, never()).post(anyObject());
89 public void nonSendDccCommandDoesNotCauseEvent() {
90 handler.handleReply(createReply(parseSource("User!user@host"),
92 "\u0001DCC FOO test.dat 2130706433 12345 23456\u0001"));
93 verify(eventBus, never()).post(anyObject());
97 public void dccCommandWithoutPortNumberDoesNotCauseEvent() {
98 handler.handleReply(createReply(parseSource("User!user@host"),
100 "\u0001DCC SEND test.dat 2130706433\u0001"));
101 verify(eventBus, never()).post(anyObject());
105 public void dccCommandWithInvalidPortNumberDoesNotCauseEvent() {
106 handler.handleReply(createReply(parseSource("User!user@host"),
108 "\u0001DCC SEND test.dat 2130706433 abc\u0001"));
109 verify(eventBus, never()).post(anyObject());
113 public void dccCommandWithoutIpAddressDoesNotCauseEvent() {
114 handler.handleReply(createReply(parseSource("User!user@host"),
116 "\u0001DCC SEND test.dat\u0001"));
117 verify(eventBus, never()).post(anyObject());
121 public void dccCommandWithInvalidIpAddressDoesNotCauseEvent() {
122 handler.handleReply(createReply(parseSource("User!user@host"),
124 "\u0001DCC SEND test.dat abc abc\u0001"));
125 verify(eventBus, never()).post(anyObject());
129 public void dccCommandWithoutLengthIsRecognizedCorrectly() {
130 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "", -1);
134 public void dccAcceptCommandIsRecognized() {
135 handler.handleReply(createReply(parseSource("User!user@host"),
137 "\u0001DCC ACCEPT test.dat 12345 23456\u0001"));
138 ArgumentCaptor<DccAcceptReceived> eventCaptor = forClass(
139 DccAcceptReceived.class);
140 verify(eventBus).post(eventCaptor.capture());
141 DccAcceptReceived dccAcceptReceived = eventCaptor.getValue();
142 assertThat(dccAcceptReceived.connection(), is(connection));
143 assertThat(dccAcceptReceived.filename(), is("test.dat"));
144 assertThat(dccAcceptReceived.port(), is(12345));
145 assertThat(dccAcceptReceived.position(), is(23456L));
146 assertThat(dccAcceptReceived.source().nick().get(), is("User"));
150 public void dccAcceptCommandWithoutParametersDoesNotCauseEvent() {
151 handler.handleReply(createReply(parseSource("User!user@host"),
153 "\u0001DCC ACCEPT\u0001"));
154 verify(eventBus,never()).post(anyObject());
158 public void dccAcceptCommandWithInvalidPortDoesNotCauseEvent() {
159 handler.handleReply(createReply(parseSource("User!user@host"),
161 "\u0001DCC ACCEPT test.dat abc\u0001"));
162 verify(eventBus,never()).post(anyObject());
166 public void dccAcceptCommandWithMissingPositionIsRecognized() {
167 handler.handleReply(createReply(parseSource("User!user@host"),
169 "\u0001DCC ACCEPT test.dat 12345\u0001"));
170 ArgumentCaptor<DccAcceptReceived> eventCaptor = forClass(
171 DccAcceptReceived.class);
172 verify(eventBus).post(eventCaptor.capture());
173 DccAcceptReceived dccAcceptReceived = eventCaptor.getValue();
174 assertThat(dccAcceptReceived.connection(), is(connection));
175 assertThat(dccAcceptReceived.filename(), is("test.dat"));
176 assertThat(dccAcceptReceived.port(), is(12345));
177 assertThat(dccAcceptReceived.position(), is(-1L));
178 assertThat(dccAcceptReceived.source().nick().get(), is("User"));