93a363767bb1282185f1af8af31c11c889fc51c2
[jFCPlib.git] / src / net / pterodactylus / fcp / FcpTest.java
1 /*
2  * jSite2 - FcpTest.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fcp;
21
22 import java.io.IOException;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Tests various commands and the FCP connection.
28  * 
29  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30  */
31 public class FcpTest extends TestCase {
32
33         /** The FCP connection. */
34         private FcpConnection fcpConnection;
35
36         /**
37          * {@inheritDoc}
38          */
39         @Override
40         protected void setUp() throws Exception {
41                 fcpConnection = new FcpConnection("wing");
42                 fcpConnection.connect();
43                 fcpConnection.sendMessage(new ClientHello("FcpTest"));
44         }
45
46         /**
47          * {@inheritDoc}
48          */
49         @Override
50         protected void tearDown() throws Exception {
51                 fcpConnection.close();
52         }
53
54         /**
55          * Tests the FCP connection be simply {@link #setUp() setting it up} and
56          * {@link #tearDown() tearing it down} again.
57          */
58         public void testFcpConnection() {
59                 /* do nothing. */
60         }
61
62         /**
63          * Generates an SSK key pair.
64          * 
65          * @throws IOException
66          *             if an I/O error occurs
67          * @throws InterruptedException
68          *             if {@link Object#wait()} wakes up spuriously
69          */
70         public void testGenerateSSK() throws IOException, InterruptedException {
71                 final SSKKeypair[] keypair = new SSKKeypair[1];
72                 FcpAdapter fcpAdapter = new FcpAdapter() {
73
74                         /**
75                          * {@inheritDoc}
76                          */
77                         @Override
78                         public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
79                                 keypair[0] = sskKeypair;
80                                 synchronized (this) {
81                                         notify();
82                                 }
83                         }
84                 };
85                 fcpConnection.addFcpListener(fcpAdapter);
86                 synchronized (fcpAdapter) {
87                         fcpConnection.sendMessage(new GenerateSSK());
88                         fcpAdapter.wait();
89                 }
90                 assertNotNull("ssk keypair", keypair[0]);
91         }
92
93 }