🚧 Parse token from DCC SEND
[xudocci.git] / src / test / java / net / pterodactylus / irc / connection / CtcpHandlerTest.java
1 package net.pterodactylus.irc.connection;
2
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;
14
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.event.DccAcceptReceived;
17 import net.pterodactylus.irc.event.DccSendReceived;
18
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22
23 /**
24  * Unit test for {@link CtcpHandler}.
25  *
26  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
27  */
28 public class CtcpHandlerTest {
29
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);
33
34         @Test
35         public void handlerWillHandleCtcpMessagesSentAsNotice() {
36                 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION\u0001")), is(true));
37         }
38
39         @Test
40         public void handlerWillHandleCtcpMessagesSentAsPrivmsg() {
41                 assertThat(handler.willHandle(createReply("PRIVMSG", "\u0001ACTION\u0001")), is(true));
42         }
43
44         @Test
45         public void handlerWillNotHandleNormalMessages() {
46                 assertThat(handler.willHandle(createReply("NOTICE", "ACTION")), is(false));
47         }
48
49         @Test
50         public void handlerWillNotHandleMalformedMessages() {
51                 assertThat(handler.willHandle(createReply("NOTICE", "\u0001ACTION")), is(false));
52         }
53
54         @Test
55         public void dccSendCommandAsNoticeIsRecognizedCorrectly() {
56                 dccSendCommandIsRecognizedCorrectly("NOTICE", "23456", 23456L);
57         }
58
59         private void dccSendCommandIsRecognizedCorrectly(String command,
60                         Object sizeAsString, long expectedSize) {
61                 handler.handleReply(createReply(parseSource("User!user@host"),
62                                 command,
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(),
71                                 is("127.0.0.1"));
72                 assertThat(dccSendReceived.port(), is(12345));
73                 assertThat(dccSendReceived.filesize(), is(expectedSize));
74                 assertThat(dccSendReceived.source().nick().get(), is("User"));
75         }
76
77         @Test
78         public void dccSendCommandAsPrivmsgIsRecognizedCorrectly() {
79                 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "23456", 23456L);
80         }
81
82         @Test
83         public void nonDccCommandDoesNotCauseEvent() {
84                 handler.handleReply(createReply(parseSource("User!user@host"),
85                                 "NOTICE",
86                                 "\u0001FOO SEND test.dat 2130706433 12345 23456\u0001"));
87                 verify(eventBus, never()).post(anyObject());
88         }
89
90         @Test
91         public void nonSendDccCommandDoesNotCauseEvent() {
92                 handler.handleReply(createReply(parseSource("User!user@host"),
93                                 "NOTICE",
94                                 "\u0001DCC FOO test.dat 2130706433 12345 23456\u0001"));
95                 verify(eventBus, never()).post(anyObject());
96         }
97
98         @Test
99         public void dccCommandWithoutPortNumberDoesNotCauseEvent() {
100                 handler.handleReply(createReply(parseSource("User!user@host"),
101                                 "NOTICE",
102                                 "\u0001DCC SEND test.dat 2130706433\u0001"));
103                 verify(eventBus, never()).post(anyObject());
104         }
105
106         @Test
107         public void dccCommandWithInvalidPortNumberDoesNotCauseEvent() {
108                 handler.handleReply(createReply(parseSource("User!user@host"),
109                                 "NOTICE",
110                                 "\u0001DCC SEND test.dat 2130706433 abc\u0001"));
111                 verify(eventBus, never()).post(anyObject());
112         }
113
114         @Test
115         public void dccCommandWithoutIpAddressDoesNotCauseEvent() {
116                 handler.handleReply(createReply(parseSource("User!user@host"),
117                                 "NOTICE",
118                                 "\u0001DCC SEND test.dat\u0001"));
119                 verify(eventBus, never()).post(anyObject());
120         }
121
122         @Test
123         public void dccCommandWithInvalidIpAddressDoesNotCauseEvent() {
124                 handler.handleReply(createReply(parseSource("User!user@host"),
125                                 "NOTICE",
126                                 "\u0001DCC SEND test.dat abc abc\u0001"));
127                 verify(eventBus, never()).post(anyObject());
128         }
129
130         @Test
131         public void dccCommandWithoutLengthIsRecognizedCorrectly() {
132                 dccSendCommandIsRecognizedCorrectly("PRIVMSG", "", -1);
133         }
134
135         @Test
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"));
143         }
144
145         @Test
146         public void dccAcceptCommandIsRecognized() {
147                 handler.handleReply(createReply(parseSource("User!user@host"),
148                                 "NOTICE",
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"));
159         }
160
161         @Test
162         public void dccAcceptCommandWithoutParametersDoesNotCauseEvent() {
163                 handler.handleReply(createReply(parseSource("User!user@host"),
164                                 "NOTICE",
165                                 "\u0001DCC ACCEPT\u0001"));
166                 verify(eventBus,never()).post(anyObject());
167         }
168
169         @Test
170         public void dccAcceptCommandWithInvalidPortDoesNotCauseEvent() {
171                 handler.handleReply(createReply(parseSource("User!user@host"),
172                                 "NOTICE",
173                                 "\u0001DCC ACCEPT test.dat abc\u0001"));
174                 verify(eventBus,never()).post(anyObject());
175         }
176
177         @Test
178         public void dccAcceptCommandWithMissingPositionIsRecognized() {
179                 handler.handleReply(createReply(parseSource("User!user@host"),
180                                 "NOTICE",
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"));
191         }
192
193 }