1 19 package org.netbeans; 20 21 import java.io.DataInputStream ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStream ; 26 import java.net.InetAddress ; 27 import java.net.Socket ; 28 import java.util.logging.Level ; 29 import org.fakepkg.FakeHandler; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.util.RequestProcessor; 32 33 37 public class CLIHowHardIsToGuessKeyTest extends NbTestCase { 38 private static Object LOCK = new Object (); 39 40 public CLIHowHardIsToGuessKeyTest(String testName) { 41 super(testName); 42 } 43 44 @Override 45 protected Level logLevel() { 46 return Level.ALL; 47 } 48 49 protected void setUp() throws Exception { 50 clearWorkDir(); 51 System.setProperty("netbeans.mainclass", "org.netbeans.CLIHowHardIsToGuessKeyTest"); 52 System.setProperty("netbeans.user", getWorkDirPath()); 53 } 55 56 public static void main(String [] args) throws Exception { 57 org.netbeans.MainImpl.finishInitialization(); 58 synchronized (LOCK) { 59 LOCK.notifyAll(); 60 LOCK.wait(); 61 } 62 } 63 64 public void testGuessTheKey() throws Exception { 65 class R implements Runnable { 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 { 77 Exception ex; 78 public void run() { 79 try { 80 org.netbeans.MainImpl.main(new String [] { }); 81 } catch (Exception 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 lock = new File (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); 108 final byte[] arr = new byte[10]; DataInputStream is = new DataInputStream (new FileInputStream (lock)); 110 final int port = is.readInt(); 111 int read = is.read(arr); 112 assertEquals("All read", arr.length, read); 113 114 FileOutputStream os = new FileOutputStream (lock); 115 os.write(arr); 116 os.close(); 117 118 class Connect implements Runnable { 119 int times; 120 Exception ex; 121 122 public void run() { 123 124 while(times++ < 100) { 125 arr[5]++; 127 try { 128 Socket s = new Socket (localHostAddress(), port); 129 OutputStream os = s.getOutputStream(); 130 os.write(arr); 131 os.flush(); 132 int reply = s.getInputStream().read(); 133 if (reply == 0) { continue; 135 } 136 fail("The reply should be fail: " + reply); 137 } catch (Exception 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 localHostAddress () throws Exception { 158 java.net.NetworkInterface 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 |