KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > CLIHowHardIsToGuessKeyTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans;
20
21 import java.io.DataInputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.Socket JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import org.fakepkg.FakeHandler;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.util.RequestProcessor;
32
33 /** Tests that handler can set netbeans.mainclass property in its constructor.
34  *
35  * @author Jaroslav Tulach
36  */

37 public class CLIHowHardIsToGuessKeyTest extends NbTestCase {
38     private static Object JavaDoc LOCK = new Object JavaDoc();
39     
40     public CLIHowHardIsToGuessKeyTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     @Override JavaDoc
45     protected Level JavaDoc logLevel() {
46         return Level.ALL;
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         clearWorkDir();
51         System.setProperty("netbeans.mainclass", "org.netbeans.CLIHowHardIsToGuessKeyTest");
52         System.setProperty("netbeans.user", getWorkDirPath());
53 // System.setProperty("org.netbeans.CLIHandler", "-1");
54
}
55     
56     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
57         org.netbeans.MainImpl.finishInitialization();
58         synchronized (LOCK) {
59             LOCK.notifyAll();
60             LOCK.wait();
61         }
62     }
63
64     public void testGuessTheKey() throws Exception JavaDoc {
65         class R implements Runnable JavaDoc {
66             public int cnt;
67             
68             public void run() {
69                 cnt++;
70             }
71         }
72         
73         R run = new R();
74         FakeHandler.toRun = run;
75         
76         class Main implements Runnable JavaDoc {
77             Exception JavaDoc ex;
78             public void run() {
79                 try {
80                     org.netbeans.MainImpl.main(new String JavaDoc[] { });
81                 } catch (Exception JavaDoc ex) {
82                     this.ex = ex;
83                 }
84             }
85         }
86         Main main = new Main();
87         synchronized (LOCK) {
88             RequestProcessor.getDefault().post(main);
89             LOCK.wait();
90         }
91         
92         assertEquals("One call", 1, run.cnt);
93         
94         if (main.ex != null) {
95             throw main.ex;
96         }
97         
98         File JavaDoc lock = new File JavaDoc(getWorkDir(), "lock");
99         assertTrue("Lock is created", lock.canRead());
100         for (int i = 0; i < 50; i++) {
101             if (lock.length() >= 14) {
102                 break;
103             }
104             Thread.sleep(500);
105         }
106         assertTrue("Lock must contain the key now: " + lock.length(), lock.length() >= 14);//fail("Ok");
107

108         final byte[] arr = new byte[10]; // CLIHandler.KEY_LENGTH
109
DataInputStream JavaDoc is = new DataInputStream JavaDoc(new FileInputStream JavaDoc(lock));
110         final int port = is.readInt();
111         int read = is.read(arr);
112         assertEquals("All read", arr.length, read);
113
114         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(lock);
115         os.write(arr);
116         os.close();
117         
118         class Connect implements Runnable JavaDoc {
119             int times;
120             Exception JavaDoc ex;
121             
122             public void run() {
123                 
124                 while(times++ < 100) {
125                     // making the key incorrect
126
arr[5]++;
127                     try {
128                         Socket JavaDoc s = new Socket JavaDoc(localHostAddress(), port);
129                         OutputStream JavaDoc os = s.getOutputStream();
130                         os.write(arr);
131                         os.flush();
132                         int reply = s.getInputStream().read();
133                         if (reply == 0) { // CLIHandler.REPLY_FAIL
134
continue;
135                         }
136                         fail("The reply should be fail: " + reply);
137                     } catch (Exception JavaDoc ex) {
138                         this.ex = ex;
139                         return;
140                     }
141                 }
142             }
143         }
144         
145         Connect c = new Connect();
146         RequestProcessor.getDefault().post(c).waitFinished(5000);
147         
148         if (c.ex != null) {
149             throw c.ex;
150         }
151         
152         if (c.times > 10) {
153             fail("Too many allowed connections, the responce has to be slow to prevent secure attacks: " + c.times);
154         }
155         
156     }
157     static InetAddress JavaDoc localHostAddress () throws Exception JavaDoc {
158         java.net.NetworkInterface JavaDoc net = java.net.NetworkInterface.getByName ("lo");
159         if (net == null || !net.getInetAddresses().hasMoreElements()) {
160             return InetAddress.getLocalHost();
161         }
162         else {
163             return net.getInetAddresses().nextElement();
164         }
165     }
166 }
167
Popular Tags