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