Refactoring
[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.is;
5
6 import java.io.IOException;
7 import java.util.concurrent.ExecutionException;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.Executors;
10 import java.util.concurrent.Future;
11
12 import net.pterodactylus.fcp.FcpKeyPair;
13 import net.pterodactylus.fcp.fake.FakeTcpServer;
14
15 import org.junit.After;
16 import org.junit.Test;
17
18 /**
19  * Unit test for {@link DefaultFcpClient}.
20  *
21  * @author <a href="bombe@freenetproject.org">David ‘Bombe’ Roden</a>
22  */
23 public class DefaultFcpClientTest {
24
25         private static final String INSERT_URI = "SSK@RVCHbJdkkyTCeNN9AYukEg76eyqmiosSaNKgE3U9zUw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQECAAE/";
26         private static final String REQUEST_URI = "SSK@wtbgd2loNcJCXvtQVOftl2tuWBomDQHfqS6ytpPRhfw,7SHH53gletBVb9JD7nBsyClbLQsBubDPEIcwg908r7Y,AQACAAE/";
27
28         private final ExecutorService threadPool = Executors.newCachedThreadPool();
29         private final FakeTcpServer fcpServer;
30         private final DefaultFcpClient fcpClient;
31
32         public DefaultFcpClientTest() throws IOException {
33                 fcpServer = new FakeTcpServer(threadPool);
34                 fcpClient = new DefaultFcpClient(threadPool, "localhost", fcpServer.getPort(), () -> "Test", () -> "2.0");
35         }
36
37         @After
38         public void tearDown() throws IOException {
39                 fcpServer.close();
40         }
41
42         @Test
43         public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException {
44                 Future<FcpKeyPair> keyPairFuture = fcpClient.generateKeypair().execute();
45                 connectNode();
46                 fcpServer.collectUntil(is("EndMessage"));
47                 fcpServer.writeLine("SSKKeypair",
48                         "InsertURI=" + INSERT_URI + "",
49                         "RequestURI=" + REQUEST_URI + "",
50                         "Identifier=My Identifier from GenerateSSK",
51                         "EndMessage");
52                 FcpKeyPair keyPair = keyPairFuture.get();
53                 assertThat(keyPair.getPublicKey(), is(REQUEST_URI));
54                 assertThat(keyPair.getPrivateKey(), is(INSERT_URI));
55         }
56
57         private void connectNode() throws InterruptedException, ExecutionException, IOException {
58                 fcpServer.connect().get();
59                 fcpServer.collectUntil(is("EndMessage"));
60                 fcpServer.writeLine("NodeHello",
61                         "CompressionCodecs=4 - GZIP(0), BZIP2(1), LZMA(2), LZMA_NEW(3)",
62                         "Revision=build01466",
63                         "Testnet=false",
64                         "Version=Fred,0.7,1.0,1466",
65                         "Build=1466",
66                         "ConnectionIdentifier=14318898267048452a81b36e7f13a3f0",
67                         "Node=Fred",
68                         "ExtBuild=29",
69                         "FCPVersion=2.0",
70                         "NodeLanguage=ENGLISH",
71                         "ExtRevision=v29",
72                         "EndMessage"
73                 );
74         }
75
76 }