public void testStopsDHTWhenDisabled() throws Exception { ConnectionSettings.EVER_ACCEPTED_INCOMING.setValue(true); long startTime = System.currentTimeMillis() - DHTSettings.MIN_ACTIVE_DHT_INITIAL_UPTIME.getValue(); PrivilegedAccessor.setValue(nodeAssigner, "startTime", new Long(startTime)); mockery.checking(buildBandwdithExpectations(true)); mockery.checking( buildDHTExpectations( DHTMode.ACTIVE, false, true, true, DHTSettings.MIN_ACTIVE_DHT_AVERAGE_UPTIME.getValue() + 1, false, false)); mockery.checking( new Expectations() { { one(dhtManager).stop(); } }); assignerRunnable.run(); mockery.assertIsSatisfied(); }
public void testDoesNotAssignPassiveLeafIfDisabled() throws Exception { ConnectionSettings.EVER_ACCEPTED_INCOMING.setValue(true); DHTSettings.ENABLE_PASSIVE_LEAF_DHT_MODE.setValue(false); long startTime = System.currentTimeMillis() - DHTSettings.MIN_PASSIVE_LEAF_DHT_INITIAL_UPTIME.getValue(); PrivilegedAccessor.setValue(nodeAssigner, "startTime", new Long(startTime)); mockery.checking(buildBandwdithExpectations(true)); mockery.checking( buildDHTExpectations( DHTMode.INACTIVE, true, true, false, // can't receive unsolicited DHTSettings.MIN_PASSIVE_LEAF_DHT_AVERAGE_UPTIME.getValue() + 1, false, false)); mockery.checking( new Expectations() { { never(dhtManager).start(with(Matchers.any(DHTMode.class))); } }); assignerRunnable.run(); mockery.assertIsSatisfied(); }
public void testPassiveLeafDoesNotNeedHardCore() throws Exception { // not accepted incoming previously therefore not hardcore long startTime = System.currentTimeMillis() - DHTSettings.MIN_PASSIVE_LEAF_DHT_INITIAL_UPTIME.getValue(); PrivilegedAccessor.setValue(nodeAssigner, "startTime", new Long(startTime)); mockery.checking(buildBandwdithExpectations(true)); mockery.checking( buildDHTExpectations( DHTMode.INACTIVE, true, true, false, // can't receive unsolicited DHTSettings.MIN_PASSIVE_LEAF_DHT_AVERAGE_UPTIME.getValue() + 1, false, false)); mockery.checking( new Expectations() { { one(dhtManager).start(DHTMode.PASSIVE_LEAF); } }); assignerRunnable.run(); mockery.assertIsSatisfied(); }
public void testDoesNotAssignLowAverageUptime() throws Exception { ConnectionSettings.EVER_ACCEPTED_INCOMING.setValue(true); long startTime = System.currentTimeMillis() - DHTSettings.MIN_ACTIVE_DHT_INITIAL_UPTIME.getValue(); PrivilegedAccessor.setValue(nodeAssigner, "startTime", new Long(startTime)); mockery.checking(buildBandwdithExpectations(true)); mockery.checking( buildDHTExpectations( DHTMode.INACTIVE, true, true, true, 0, // no average uptime false, false)); mockery.checking( new Expectations() { { never(dhtManager).start(with(Matchers.any(DHTMode.class))); } }); assignerRunnable.run(); mockery.assertIsSatisfied(); }
private PingRequest make(List ggeps) throws Exception { return (PingRequest) PrivilegedAccessor.invokeConstructor( PingRequestImpl.class, new Object[] {new byte[16], (byte) 1, ggeps}, new Class[] {byte[].class, byte.class, List.class}); }
@Override public void setSettings() throws Exception { // TODO change this, by either introducing a setter or overriding FileManagerController PrivilegedAccessor.setValue(QRPUpdater.class, "QRP_DELAY", 1000); SearchSettings.LIME_QRP_ENTRIES.set(new String[] {"badger"}); SearchSettings.LIME_SEARCH_TERMS.set(new String[] {"badger"}); SearchSettings.SEND_LIME_RESPONSES.setValue(1f); }
protected void setUp(Injector injector) throws Exception { // calls all doSettings() for me and my parents PrivilegedAccessor.invokeAllStaticMethods(this.getClass(), "doSettings", null); this.injector = injector; // calls all doSettings() for me and my children // rs=new RouterService(callback); assertEquals("unexpected port", SERVER_PORT, NetworkSettings.PORT.getValue()); lifecycleManager = injector.getInstance(LifecycleManager.class); connectionServices = injector.getInstance(ConnectionServices.class); blockingConnectionFactory = injector.getInstance(BlockingConnectionFactory.class); pingReplyFactory = injector.getInstance(PingReplyFactory.class); headersFactory = injector.getInstance(HeadersFactory.class); lifecycleManager.start(); connectionServices.connect(); Thread.sleep(2000); assertEquals("unexpected port", SERVER_PORT, NetworkSettings.PORT.getValue()); }
public void testPromotesFromActiveDHTIfAllowed() throws Exception { // disallow switch to ultrapeer DHTSettings.SWITCH_TO_ULTRAPEER_PROBABILITY.setValue(1f); ConnectionSettings.EVER_ACCEPTED_INCOMING.setValue(true); ApplicationSettings.AVERAGE_UPTIME.setValue(UltrapeerSettings.MIN_AVG_UPTIME.getValue() + 1); assertFalse(UltrapeerSettings.EVER_ULTRAPEER_CAPABLE.getValue()); // pretend some time passed - the uptime counter in NodeAssigner is very hacky long startTime = System.currentTimeMillis() - DHTSettings.MIN_ACTIVE_DHT_INITIAL_UPTIME.getValue(); PrivilegedAccessor.setValue(nodeAssigner, "startTime", new Long(startTime)); mockery.checking(buildBandwdithExpectations(true)); // enabled and active DHT mockery.checking(buildUltrapeerExpectations(true, true, 0l, true, DHTMode.ACTIVE, false)); // but we're not an active ultrapeer and can receive solicited // but we've been up long enough tob e active in the DHT mockery.checking( new Expectations() { { one(connectionServices).isActiveSuperNode(); will(returnValue(false)); one(networkManager).canReceiveSolicited(); will(returnValue(true)); atLeast(1).of(cManager).getCurrentAverageUptime(); will(returnValue(DHTSettings.MIN_ACTIVE_DHT_AVERAGE_UPTIME.getValue() + 1)); } }); // will get promoted mockery.checking(buildPromotionExpectations(true)); assignerRunnable.run(); assertTrue(UltrapeerSettings.EVER_ULTRAPEER_CAPABLE.getValue()); mockery.assertIsSatisfied(); }
public void testDownloadFinishes() throws Exception { BlockingConnectionUtils.keepAllAlive(testUP, pingReplyFactory); // clear up any messages before we begin the test. drainAll(); DatagramPacket pack = null; Message m = null; byte[] guid = searchServices.newQueryGUID(); searchServices.query(guid, "whatever"); // i need to pretend that the UI is showing the user the query still callback.setGUID(new GUID(guid)); QueryRequest qr = BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryRequest.class); assertNotNull(qr); assertTrue(qr.desiresOutOfBandReplies()); // ok, the leaf is sending OOB queries - good stuff, now we should send // a lot of results back and make sure it buffers the bypassed OOB ones for (int i = 0; i < testUP.length; i++) { Response[] res = new Response[200]; for (int j = 0; j < res.length; j++) res[j] = responseFactory.createResponse( 10 + j + i, 10 + j + i, "whatever " + j + i, UrnHelper.SHA1); m = queryReplyFactory.createQueryReply( qr.getGUID(), (byte) 1, 6355, myIP(), 0, res, GUID.makeGuid(), new byte[0], false, false, true, true, false, false, null); testUP[i].send(m); testUP[i].flush(); } // create a test uploader and send back that response TestUploader uploader = new TestUploader(networkManagerStub); uploader.start("whatever", UPLOADER_PORT, false); uploader.setBusy(true); URN urn = TestFile.hash(); RemoteFileDesc rfd = makeRFD(urn); // wait for processing Thread.sleep(1500); // just do it for 1 UDP guy { ReplyNumberVendorMessage vm = replyNumberVendorMessageFactory.createV3ReplyNumberVendorMessage( new GUID(qr.getGUID()), 1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); vm.write(baos); pack = new DatagramPacket( baos.toByteArray(), baos.toByteArray().length, testUP[0].getInetAddress(), SERVER_PORT); UDP_ACCESS[0].send(pack); } // wait for processing Thread.sleep(500); { // all the UDP ReplyNumberVMs should have been bypassed assertByPassedResultsCacheHasSize(qr.getGUID(), 1); } long currTime = System.currentTimeMillis(); Downloader downloader = downloadServices.download(new RemoteFileDesc[] {rfd}, false, new GUID(guid)); final int MAX_TRIES = 60; for (int i = 0; i <= MAX_TRIES; i++) { Thread.sleep(500); if (downloader.getState() == DownloadState.ITERATIVE_GUESSING) break; if (i == MAX_TRIES) fail("didn't GUESS!!"); } // we should get a query key request { boolean gotPing = false; while (!gotPing) { byte[] datagramBytes = new byte[1000]; pack = new DatagramPacket(datagramBytes, 1000); UDP_ACCESS[0].setSoTimeout(10000); // may need to wait UDP_ACCESS[0].receive(pack); InputStream in = new ByteArrayInputStream(pack.getData()); m = messageFactory.read(in, Network.TCP); m.hop(); if (m instanceof PingRequest) gotPing = ((PingRequest) m).isQueryKeyRequest(); } } // send back a query key AddressSecurityToken qk = new AddressSecurityToken(InetAddress.getLocalHost(), SERVER_PORT, macManager); { byte[] ip = new byte[] {(byte) 127, (byte) 0, (byte) 0, (byte) 1}; PingReply pr = pingReplyFactory.createQueryKeyReply( GUID.makeGuid(), (byte) 1, UDP_ACCESS[0].getLocalPort(), ip, 10, 10, false, qk); ByteArrayOutputStream baos = new ByteArrayOutputStream(); pr.write(baos); pack = new DatagramPacket( baos.toByteArray(), baos.toByteArray().length, testUP[0].getInetAddress(), SERVER_PORT); UDP_ACCESS[0].send(pack); } Thread.sleep(500); // ensure that it gets into the OnDemandUnicaster { // now we should make sure MessageRouter retains the key Map _queryKeys = (Map) PrivilegedAccessor.getValue(onDemandUnicaster, "_queryKeys"); assertNotNull(_queryKeys); assertEquals(1, _queryKeys.size()); } byte[] urnQueryGUID = null; { // confirm a URN query boolean gotQuery = false; while (!gotQuery) { byte[] datagramBytes = new byte[1000]; pack = new DatagramPacket(datagramBytes, 1000); UDP_ACCESS[0].setSoTimeout(10000); // may need to wait UDP_ACCESS[0].receive(pack); InputStream in = new ByteArrayInputStream(pack.getData()); m = messageFactory.read(in, Network.TCP); if (m instanceof QueryRequest) { QueryRequest qReq = (QueryRequest) m; Set queryURNs = qReq.getQueryUrns(); gotQuery = queryURNs.contains(urn); if (gotQuery) { gotQuery = qReq.getQueryKey().isFor(InetAddress.getLocalHost(), SERVER_PORT); if (gotQuery) urnQueryGUID = qReq.getGUID(); } } } } assertNotNull(urnQueryGUID); long timeoutVal = 8000 - (System.currentTimeMillis() - currTime); Thread.sleep(timeoutVal > 0 ? timeoutVal : 0); assertEquals(DownloadState.BUSY, downloader.getState()); // purge front end of query callback.clearGUID(); // create a new Uploader to service the download TestUploader uploader2 = new TestUploader(networkManagerStub); uploader2.start("whatever", UPLOADER_PORT + 1, false); uploader2.setRate(100); { // send back a query request, the TestUploader should service upload rfd = makeRFD(urn, UPLOADER_PORT + 1); Response[] res = new Response[] {responseFactory.createResponse(10, 10, "whatever", urn)}; m = queryReplyFactory.createQueryReply( urnQueryGUID, (byte) 1, UPLOADER_PORT + 1, myIP(), 0, res, GUID.makeGuid(), new byte[0], false, false, true, true, false, false, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); m.write(baos); pack = new DatagramPacket( baos.toByteArray(), baos.toByteArray().length, testUP[0].getInetAddress(), SERVER_PORT); UDP_ACCESS[0].send(pack); } // after a while, the download should finish, the bypassed results // should be discarded Thread.sleep(10000); assertEquals(DownloadState.COMPLETE, downloader.getState()); { // now we should make sure MessageRouter clears the map assertByPassedResultsCacheHasSize(qr.getGUID(), 0); } uploader.stopThread(); }
// RUN THIS TEST LAST!! // TODO move this test case to OnDemandUnicasterTest, sounds like a pure unit test // proabably doesn't make sense anymore since it doesn't have any data from the // previous tests to clear public void testUnicasterClearingCode() throws Exception { BlockingConnectionUtils.keepAllAlive(testUP, pingReplyFactory); // clear up any messages before we begin the test. drainAll(); { // clear all the unicaster data structures Long[] longs = new Long[] {new Long(0), new Long(1)}; Class[] classTypes = new Class[] {Long.TYPE, Long.TYPE}; // now confirm that clearing code works Object ret = PrivilegedAccessor.invokeMethod( onDemandUnicaster, "clearDataStructures", longs, classTypes); assertTrue(ret instanceof Boolean); assertTrue(((Boolean) ret).booleanValue()); } DatagramPacket pack = null; Message m = null; byte[] guid = searchServices.newQueryGUID(); searchServices.query(guid, "whatever"); // i need to pretend that the UI is showing the user the query still callback.setGUID(new GUID(guid)); QueryRequest qr = BlockingConnectionUtils.getFirstInstanceOfMessageType(testUP[0], QueryRequest.class); assertNotNull(qr); assertTrue(qr.desiresOutOfBandReplies()); // ok, the leaf is sending OOB queries - good stuff, now we should send // a lot of results back and make sure it buffers the bypassed OOB ones for (int i = 0; i < testUP.length; i++) { Response[] res = new Response[200]; for (int j = 0; j < res.length; j++) res[j] = responseFactory.createResponse( 10 + j + i, 10 + j + i, "whatever " + j + i, UrnHelper.SHA1); m = queryReplyFactory.createQueryReply( qr.getGUID(), (byte) 1, 6355, myIP(), 0, res, GUID.makeGuid(), new byte[0], false, false, true, true, false, false, null); testUP[i].send(m); testUP[i].flush(); } // create a test uploader and send back that response TestUploader uploader = new TestUploader(networkManagerStub); uploader.start("whatever", UPLOADER_PORT, false); uploader.setBusy(true); RemoteFileDesc rfd = makeRFD("GLIQY64M7FSXBSQEZY37FIM5QQSA2OUJ"); // wait for processing Thread.sleep(1500); for (int i = 0; i < UDP_ACCESS.length; i++) { ReplyNumberVendorMessage vm = replyNumberVendorMessageFactory.createV3ReplyNumberVendorMessage( new GUID(qr.getGUID()), i + 1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); vm.write(baos); pack = new DatagramPacket( baos.toByteArray(), baos.toByteArray().length, testUP[0].getInetAddress(), SERVER_PORT); UDP_ACCESS[i].send(pack); } // wait for processing Thread.sleep(500); { // all the UDP ReplyNumberVMs should have been bypassed assertByPassedResultsCacheHasSize(qr.getGUID(), UDP_ACCESS.length); } Downloader downloader = downloadServices.download(new RemoteFileDesc[] {rfd}, false, new GUID(guid)); final int MAX_TRIES = 60; for (int i = 0; i <= MAX_TRIES; i++) { Thread.sleep(500); if (downloader.getState() == DownloadState.ITERATIVE_GUESSING) break; if (i == MAX_TRIES) fail("didn't GUESS!!"); } // we should start getting guess queries on all UDP ports, actually // querykey requests for (int i = 0; i < UDP_ACCESS.length; i++) { boolean gotPing = false; while (!gotPing) { try { byte[] datagramBytes = new byte[1000]; pack = new DatagramPacket(datagramBytes, 1000); UDP_ACCESS[i].setSoTimeout(10000); // may need to wait UDP_ACCESS[i].receive(pack); InputStream in = new ByteArrayInputStream(pack.getData()); m = messageFactory.read(in, Network.TCP); m.hop(); if (m instanceof PingRequest) gotPing = ((PingRequest) m).isQueryKeyRequest(); } catch (InterruptedIOException iioe) { assertTrue("was successful for " + i, false); } } } // Prepopulate Query Keys AddressSecurityToken qk = new AddressSecurityToken(InetAddress.getLocalHost(), SERVER_PORT, macManager); for (int i = 0; i < (UDP_ACCESS.length / 2); i++) { byte[] ip = new byte[] {(byte) 127, (byte) 0, (byte) 0, (byte) 1}; PingReply pr = pingReplyFactory.createQueryKeyReply( GUID.makeGuid(), (byte) 1, UDP_ACCESS[i].getLocalPort(), ip, 10, 10, false, qk); pr.hop(); onDemandUnicaster.handleQueryKeyPong(pr); } // ensure that it gets into the OnDemandUnicaster { // now we should make sure MessageRouter retains the key Map _queryKeys = (Map) PrivilegedAccessor.getValue(onDemandUnicaster, "_queryKeys"); assertNotNull(_queryKeys); assertEquals((UDP_ACCESS.length / 2), _queryKeys.size()); // now make sure some URNs are still buffered Map _bufferedURNs = (Map) PrivilegedAccessor.getValue(onDemandUnicaster, "_bufferedURNs"); assertNotNull(_bufferedURNs); assertEquals((UDP_ACCESS.length / 2), _bufferedURNs.size()); } // now until those guys get expired Thread.sleep(60 * 1000); { Long[] longs = new Long[] {new Long(0), new Long(1)}; Class[] classTypes = new Class[] {Long.TYPE, Long.TYPE}; // now confirm that clearing code works Object ret = PrivilegedAccessor.invokeMethod( onDemandUnicaster, "clearDataStructures", longs, classTypes); assertTrue(ret instanceof Boolean); assertTrue(((Boolean) ret).booleanValue()); } // ensure that clearing worked { // now we should make sure MessageRouter retains the key Map _queryKeys = (Map) PrivilegedAccessor.getValue(onDemandUnicaster, "_queryKeys"); assertNotNull(_queryKeys); assertEquals(0, _queryKeys.size()); // now make sure some URNs are still buffered Map _bufferedURNs = (Map) PrivilegedAccessor.getValue(onDemandUnicaster, "_bufferedURNs"); assertNotNull(_bufferedURNs); assertEquals(0, _bufferedURNs.size()); } assertEquals(DownloadState.BUSY, downloader.getState()); callback.clearGUID(); downloader.stop(); Thread.sleep(1000); { // now we should make sure MessageRouter clears the map assertByPassedResultsCacheHasSize(qr.getGUID(), 0); } }
private void triggerSimppUpdate() throws Exception { List<SimppListener> l = (List<SimppListener>) PrivilegedAccessor.getValue(simppManager, "listeners"); for (SimppListener s : l) s.simppUpdated(simppVersion++); }