Add tests that the identifier is correctly used
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsInAnyOrder;
5 import static org.hamcrest.Matchers.is;
6
7 import java.io.IOException;
8 import java.nio.charset.StandardCharsets;
9 import java.util.List;
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
15
16 import net.pterodactylus.fcp.FcpKeyPair;
17 import net.pterodactylus.fcp.Priority;
18 import net.pterodactylus.fcp.fake.FakeTcpServer;
19 import net.pterodactylus.fcp.quelaton.ClientGetCommand.Data;
20
21 import com.google.common.io.ByteStreams;
22 import org.junit.After;
23 import org.junit.Test;
24
25 /**
26  * Unit test for {@link DefaultFcpClient}.
27  *
28  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
29  */
30 public class DefaultFcpClientTest {
31
32         private static final String INSERT_URI = "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
33         private static final String REQUEST_URI = "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
34
35         private final ExecutorService threadPool = Executors.newCachedThreadPool();
36         private final FakeTcpServer fcpServer;
37         private final DefaultFcpClient fcpClient;
38
39         public DefaultFcpClientTest() throws IOException {
40                 fcpServer = new FakeTcpServer(threadPool);
41                 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test", () -> "2.0");
42         }
43
44         @After
45         public void tearDown() throws IOException {
46                 fcpServer.close();
47         }
48
49         @Test
50         public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
51                 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
52                 connectNode();
53                 fcpServer.collectUntil(is("EndMessage"));
54                 fcpServer.writeLine("SSKKeypair",
55                         "InsertURI=" + INSERT_URI + "",
56                         "RequestURI=" + REQUEST_URI + "",
57                         "Identifier=My Identifier from GenerateSSK",
58                         "EndMessage");
59                 FcpKeyPair keyPair = keyPairFuture.get();
60                 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
61                 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
62         }
63
64         private void connectNode() throws InterruptedException, ExecutionException, IOException {
65                 fcpServer.connect().get();
66                 fcpServer.collectUntil(is("EndMessage"));
67                 fcpServer.writeLine("NodeHello",
68                         "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
69                         "Revision=build01466",
70                         "Testnet=false",
71                         "Version=Fred,0.7,1.0,1466",
72                         "Build=1466",
73                         "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
74                         "Node=Fred",
75                         "ExtBuild=29",
76                         "FCPVersion=2.0",
77                         "NodeLanguage=ENGLISH",
78                         "ExtRevision=v29",
79                         "EndMessage"
80                 );
81         }
82
83         @Test
84         public void clientGetCanDownloadData() throws InterruptedException, ExecutionException, IOException {
85                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
86                 connectNode();
87                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
88                 assertThat(lines, containsInAnyOrder(
89                         "ClientGet",
90                         "Identifier=test",
91                         "ReturnType=direct",
92                         "URI=KSK@foo.txt",
93                         "EndMessage"
94                 ));
95                 fcpServer.writeLine(
96                         "AllData",
97                         "Identifier=test",
98                         "DataLength=6",
99                         "StartupTime=1435610539000",
100                         "CompletionTime=1435610540000",
101                         "Metadata.ContentType=text/plain;charset=utf-8",
102                         "Data",
103                         "Hello"
104                 );
105                 Optional<Data> data = dataFuture.get();
106                 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
107                 assertThat(data.get().size(), is(6L));
108                 assertThat(ByteStreams.toByteArray(data.get().getInputStream()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
109         }
110
111         @Test
112         public void clientGetDownloadsDataForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
113                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
114                 connectNode();
115                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
116                 assertThat(lines, containsInAnyOrder(
117                         "ClientGet",
118                         "Identifier=test",
119                         "ReturnType=direct",
120                         "URI=KSK@foo.txt",
121                         "EndMessage"
122                 ));
123                 fcpServer.writeLine(
124                         "AllData",
125                         "Identifier=not-test",
126                         "DataLength=12",
127                         "StartupTime=1435610539000",
128                         "CompletionTime=1435610540000",
129                         "Metadata.ContentType=text/plain;charset=latin-9",
130                         "Data",
131                         "Hello World"
132                 );
133                 fcpServer.writeLine(
134                         "AllData",
135                         "Identifier=test",
136                         "DataLength=6",
137                         "StartupTime=1435610539000",
138                         "CompletionTime=1435610540000",
139                         "Metadata.ContentType=text/plain;charset=utf-8",
140                         "Data",
141                         "Hello"
142                 );
143                 Optional<Data> data = dataFuture.get();
144                 assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
145                 assertThat(data.get().size(), is(6L));
146                 assertThat(ByteStreams.toByteArray(data.get().getInputStream()), is("Hello\n".getBytes(StandardCharsets.UTF_8)));
147         }
148
149         @Test
150         public void clientGetRecognizesGetFailed() throws InterruptedException, ExecutionException, IOException {
151                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
152                 connectNode();
153                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
154                 assertThat(lines, containsInAnyOrder(
155                         "ClientGet",
156                         "Identifier=test",
157                         "ReturnType=direct",
158                         "URI=KSK@foo.txt",
159                         "EndMessage"
160                 ));
161                 fcpServer.writeLine(
162                         "GetFailed",
163                         "Identifier=test",
164                         "Code=3",
165                         "EndMessage"
166                 );
167                 Optional<Data> data = dataFuture.get();
168                 assertThat(data.isPresent(), is(false));
169         }
170
171         @Test
172         public void clientGetRecognizesGetFailedForCorrectIdentifier() throws InterruptedException, ExecutionException, IOException {
173                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
174                 connectNode();
175                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
176                 assertThat(lines, containsInAnyOrder(
177                         "ClientGet",
178                         "Identifier=test",
179                         "ReturnType=direct",
180                         "URI=KSK@foo.txt",
181                         "EndMessage"
182                 ));
183                 fcpServer.writeLine(
184                         "GetFailed",
185                         "Identifier=not-test",
186                         "Code=3",
187                         "EndMessage"
188                 );
189                 fcpServer.writeLine(
190                         "GetFailed",
191                         "Identifier=test",
192                         "Code=3",
193                         "EndMessage"
194                 );
195                 Optional<Data> data = dataFuture.get();
196                 assertThat(data.isPresent(), is(false));
197         }
198
199         @Test
200         public void clientGetRecognizesConnectionClosed() throws InterruptedException, ExecutionException, IOException {
201                 Future<Optional<Data>> dataFuture = fcpClient.clientGet().identifier("test").uri("KSK@foo.txt");
202                 connectNode();
203                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
204                 assertThat(lines, containsInAnyOrder(
205                         "ClientGet",
206                         "Identifier=test",
207                         "ReturnType=direct",
208                         "URI=KSK@foo.txt",
209                         "EndMessage"
210                 ));
211                 fcpServer.close();
212                 Optional<Data> data = dataFuture.get();
213                 assertThat(data.isPresent(), is(false));
214         }
215
216         @Test
217         public void clientGetWithIgnoreDataStoreSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
218                 fcpClient.clientGet().ignoreDataStore().identifier("test").uri("KSK@foo.txt");
219                 connectNode();
220                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
221                 assertThat(lines, containsInAnyOrder(
222                         "ClientGet",
223                         "Identifier=test",
224                         "ReturnType=direct",
225                         "URI=KSK@foo.txt",
226                         "IgnoreDS=true",
227                         "EndMessage"
228                 ));
229         }
230
231         @Test
232         public void clientGetWithDataStoreOnlySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
233                 fcpClient.clientGet().dataStoreOnly().identifier("test").uri("KSK@foo.txt");
234                 connectNode();
235                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
236                 assertThat(lines, containsInAnyOrder(
237                         "ClientGet",
238                         "Identifier=test",
239                         "ReturnType=direct",
240                         "URI=KSK@foo.txt",
241                         "DSonly=true",
242                         "EndMessage"
243                 ));
244         }
245
246         @Test
247         public void clientGetWithMaxSizeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
248                 fcpClient.clientGet().maxSize(1048576).identifier("test").uri("KSK@foo.txt");
249                 connectNode();
250                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
251                 assertThat(lines, containsInAnyOrder(
252                         "ClientGet",
253                         "Identifier=test",
254                         "ReturnType=direct",
255                         "URI=KSK@foo.txt",
256                         "MaxSize=1048576",
257                         "EndMessage"
258                 ));
259         }
260
261         @Test
262         public void clientGetWithPrioritySettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
263                 fcpClient.clientGet().priority(Priority.interactive).identifier("test").uri("KSK@foo.txt");
264                 connectNode();
265                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
266                 assertThat(lines, containsInAnyOrder(
267                         "ClientGet",
268                         "Identifier=test",
269                         "ReturnType=direct",
270                         "URI=KSK@foo.txt",
271                         "PriorityClass=1",
272                         "EndMessage"
273                 ));
274         }
275
276         @Test
277         public void clientGetWithRealTimeSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
278                 fcpClient.clientGet().realTime().identifier("test").uri("KSK@foo.txt");
279                 connectNode();
280                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
281                 assertThat(lines, containsInAnyOrder(
282                         "ClientGet",
283                         "Identifier=test",
284                         "ReturnType=direct",
285                         "URI=KSK@foo.txt",
286                         "RealTimeFlag=true",
287                         "EndMessage"
288                 ));
289         }
290
291         @Test
292         public void clientGetWithGlobalSettingSendsCorrectCommands() throws InterruptedException, ExecutionException, IOException {
293                 fcpClient.clientGet().global().identifier("test").uri("KSK@foo.txt");
294                 connectNode();
295                 List<String> lines = fcpServer.collectUntil(is("EndMessage"));
296                 assertThat(lines, containsInAnyOrder(
297                         "ClientGet",
298                         "Identifier=test",
299                         "ReturnType=direct",
300                         "URI=KSK@foo.txt",
301                         "Global=true",
302                         "EndMessage"
303                 ));
304         }
305
306 }