e0d20e49cdffc2069b8cc27b3ab0fdbb3da0edbb
[jSite2.git] / src / net / pterodactylus / util / 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.util.fcp;
21
22 import java.io.IOException;
23
24 import junit.framework.TestCase;
25 import net.pterodactylus.util.fcp.message.ClientHello;
26 import net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName;
27 import net.pterodactylus.util.fcp.message.GenerateSSK;
28 import net.pterodactylus.util.fcp.message.NodeHello;
29
30 /**
31  * Tests various commands and the FCP connection.
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  * @version $Id$
35  */
36 public class FcpTest extends TestCase {
37
38         /** The FCP connection. */
39         private FcpConnection fcpConnection;
40
41         /**
42          * {@inheritDoc}
43          */
44         @Override
45         protected void setUp() throws Exception {
46                 fcpConnection = new FcpConnection("wing");
47                 fcpConnection.connect();
48                 fcpConnection.sendMessage(new ClientHello("FcpTest"));
49         }
50
51         /**
52          * {@inheritDoc}
53          */
54         @Override
55         protected void tearDown() throws Exception {
56                 fcpConnection.disconnect();
57         }
58
59         /**
60          * Tests the FCP connection be simply {@link #setUp() setting it up} and
61          * {@link #tearDown() tearing it down} again.
62          * 
63          * @throws IOException
64          *             if an I/O error occurs
65          */
66         public void testFcpConnection() throws IOException {
67                 /* do nothing. */
68         }
69
70 //      public void testListPeers() throws FcpException, IOException {
71 //              List<Map<String, String>> peers = fcpConnection.sendListPeers(true, true);
72 //              for (Map<String, String> peer: peers) {
73 //                      for (Map.Entry<String, String> entry: peer.entrySet()) {
74 //                              System.out.println(entry.getKey() + ": " + entry.getValue());
75 //                      }
76 //              }
77 //      }
78
79 //      public void testListPeerNotes() throws FcpException, IOException {
80 //              List<Map<String, String>> peers = fcpConnection.sendListPeerNotes("AY6rGAGjayoyQD2CkvQibf1Ct3mh6iwqyntzNpfRsiM");
81 //              for (Map<String, String> peer: peers) {
82 //                      for (Map.Entry<String, String> entry: peer.entrySet()) {
83 //                              System.out.println(entry.getKey() + ": " + entry.getValue());
84 //                      }
85 //              }
86 //      }
87
88         /**
89          * Generates an SSK key pair.
90          * 
91          * @throws IOException
92          *             if an I/O error occurs
93          * @throws InterruptedException
94          *             if {@link Object#wait()} wakes up spuriously
95          */
96         public void testGenerateSSK() throws IOException, InterruptedException {
97                 final boolean[] result = new boolean[1];
98                 FcpAdapter fcpAdapter = new FcpAdapter() {
99                         /**
100                          * @see net.pterodactylus.util.fcp.FcpAdapter#receivedNodeHello(net.pterodactylus.util.fcp.FcpConnection,
101                          *      net.pterodactylus.util.fcp.message.NodeHello)
102                          */
103                         @Override
104                         public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
105                                 result[0] = true;
106                                 synchronized (this) {
107                                         notify();
108                                 }
109                         }
110
111                         /**
112                          * @see net.pterodactylus.util.fcp.FcpAdapter#receivedCloseConnectionDuplicateClientName(net.pterodactylus.util.fcp.FcpConnection,
113                          *      net.pterodactylus.util.fcp.message.CloseConnectionDuplicateClientName)
114                          */
115                         @Override
116                         public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
117                                 result[0] = false;
118                                 synchronized (this) {
119                                         notify();
120                                 }
121                         }
122                 };
123                 synchronized (fcpAdapter) {
124                         fcpConnection.sendMessage(new GenerateSSK());
125                         fcpAdapter.wait();
126                 }
127                 assertTrue("received NodeHello", result[0]);
128         }
129
130 }