001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans;
042:
043: import java.io.DataInputStream;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileOutputStream;
047: import java.io.OutputStream;
048: import java.net.InetAddress;
049: import java.net.Socket;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import org.fakepkg.FakeHandler;
053: import org.netbeans.junit.NbTestCase;
054: import org.openide.util.RequestProcessor;
055:
056: /** Tests that handler can set netbeans.mainclass property in its constructor.
057: *
058: * @author Jaroslav Tulach
059: */
060: public class CLIHowHardIsToGuessKeyTest extends NbTestCase {
061: private static Object LOCK = new Object();
062: private Logger LOG;
063:
064: public CLIHowHardIsToGuessKeyTest(String testName) {
065: super (testName);
066: }
067:
068: @Override
069: protected Level logLevel() {
070: return Level.ALL;
071: }
072:
073: protected void setUp() throws Exception {
074: clearWorkDir();
075:
076: LOG = Logger.getLogger("test." + getName());
077:
078: System.setProperty("netbeans.mainclass",
079: "org.netbeans.CLIHowHardIsToGuessKeyTest");
080: System.setProperty("netbeans.user", getWorkDirPath());
081: // System.setProperty("org.netbeans.CLIHandler", "-1");
082: }
083:
084: public static void main(String[] args) throws Exception {
085: org.netbeans.MainImpl.finishInitialization();
086: synchronized (LOCK) {
087: LOCK.notifyAll();
088: LOCK.wait();
089: }
090: }
091:
092: public void testGuessTheKey() throws Exception {
093: class R implements Runnable {
094: public int cnt;
095:
096: public void run() {
097: cnt++;
098: }
099: }
100:
101: R run = new R();
102: FakeHandler.toRun = run;
103:
104: class Main implements Runnable {
105: Exception ex;
106:
107: public void run() {
108: try {
109: org.netbeans.MainImpl.main(new String[] {});
110: } catch (Exception ex) {
111: this .ex = ex;
112: }
113: }
114: }
115: Main main = new Main();
116: synchronized (LOCK) {
117: RequestProcessor.getDefault().post(main);
118: LOCK.wait();
119: }
120:
121: assertEquals("One call", 1, run.cnt);
122:
123: if (main.ex != null) {
124: throw main.ex;
125: }
126:
127: File lock = new File(getWorkDir(), "lock");
128: assertTrue("Lock is created", lock.canRead());
129: LOG.info("lock file exists" + lock);
130: for (int i = 0; i < 500; i++) {
131: LOG.info(i + ": testing its size: " + lock.length());
132: if (lock.length() >= 14) {
133: break;
134: }
135: Thread.sleep(500);
136: }
137: assertTrue("Lock must contain the key now: " + lock.length(),
138: lock.length() >= 14);//fail("Ok");
139:
140: final byte[] arr = new byte[10]; // CLIHandler.KEY_LENGTH
141: DataInputStream is = new DataInputStream(new FileInputStream(
142: lock));
143: final int port = is.readInt();
144: int read = is.read(arr);
145: assertEquals("All read", arr.length, read);
146:
147: FileOutputStream os = new FileOutputStream(lock);
148: os.write(arr);
149: os.close();
150:
151: class Connect implements Runnable {
152: int times;
153: Exception ex;
154:
155: public void run() {
156:
157: while (times++ < 100) {
158: // making the key incorrect
159: arr[5]++;
160: try {
161: Socket s = new Socket(localHostAddress(), port);
162: OutputStream os = s.getOutputStream();
163: os.write(arr);
164: os.flush();
165: int reply = s.getInputStream().read();
166: if (reply == 0) { // CLIHandler.REPLY_FAIL
167: continue;
168: }
169: fail("The reply should be fail: " + reply);
170: } catch (Exception ex) {
171: this .ex = ex;
172: return;
173: }
174: }
175: }
176: }
177:
178: Connect c = new Connect();
179: RequestProcessor.getDefault().post(c).waitFinished(5000);
180:
181: if (c.ex != null) {
182: throw c.ex;
183: }
184:
185: if (c.times > 10) {
186: fail("Too many allowed connections, the responce has to be slow to prevent secure attacks: "
187: + c.times);
188: }
189: }
190:
191: static InetAddress localHostAddress() throws Exception {
192: java.net.NetworkInterface net = java.net.NetworkInterface
193: .getByName("lo");
194: if (net == null || !net.getInetAddresses().hasMoreElements()) {
195: return InetAddress.getLocalHost();
196: } else {
197: return net.getInetAddresses().nextElement();
198: }
199: }
200: }
|