public void testAcceptorFilterChain() throws Exception { int port = AvailablePortFinder.getNextAvailable(1024 + 1000); IoFilter mockFilter = new MockFilter(); IoHandler mockHandler = new MockHandler(); acceptor.getFilterChain().addLast("mock", mockFilter); acceptor.setHandler(mockHandler); acceptor.bind(new InetSocketAddress(port)); try { connector.setHandler(new IoHandlerAdapter()); ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port)); future.awaitUninterruptibly(); WriteFuture writeFuture = future.getSession().write(IoBuffer.allocate(16).putInt(0).flip()); writeFuture.awaitUninterruptibly(); Assert.assertTrue(writeFuture.isWritten()); future.getSession().close(true); for (int i = 0; i < 30; i++) { if (result.length() == 2) { break; } Thread.sleep(100); } Assert.assertEquals("FH", result); } finally { acceptor.unbind(); } }
@Test public void updateAcceptorSocketBinding() throws IOException, CommandFailedException { cliClient.executeCommand(command); page.navigateToMessaging(); page.selectView("Connections"); page.selectInTable(NAME, 0); page.edit(); String socketBindingName = "RemoteAcceptorSB" + RandomStringUtils.randomAlphanumeric(5); try (OnlineManagementClient client = ManagementClientProvider.createOnlineManagementClient()) { int port = AvailablePortFinder.getNextAvailable(1024); log.info("Obtained port for socket binding '" + socketBindingName + "' is " + port); client.apply(new AddSocketBinding.Builder(socketBindingName).port(port).build()); } ConfigFragment editPanelFragment = page.getConfigFragment(); editPanelFragment.getEditor().text("socketBinding", socketBindingName); boolean finished = editPanelFragment.save(); assertTrue("Config should be saved and closed.", finished); verifier.verifyAttribute(address, "socket-binding", socketBindingName, 500); cliClient.executeCommand(remove); }
@Before public void setup() throws Exception { int port = AvailablePortFinder.getNextAvailable(10000); // Write port number to configuration file in target String basedir = System.getProperty("basedir"); if (basedir == null) { basedir = new File(".").getCanonicalPath(); } // Read in camel configuration file and substitute in the correct port File f = new File(basedir + "/src/test/resources/camel-scp.xml"); FileInputStream inputStream = new FileInputStream(f); String content = IOUtils.toString(inputStream, "UTF-8"); inputStream.close(); content = content.replaceAll("portno", "" + port); File f2 = new File(basedir + "/target/test-classes/camel-scp.xml"); FileOutputStream outputStream = new FileOutputStream(f2); IOUtils.write(content, outputStream, "UTF-8"); outputStream.close(); sshServer = SshServer.setUpDefaultServer(); sshServer.setPort(port); // Generate a key sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("target/generatedkey.pem")); sshServer.setCommandFactory(new ScpCommandFactory()); sshServer.setPasswordAuthenticator(new CamelPasswordAuthenticator()); sshServer.start(); setupKnownHosts(port); // Create "storage" directory (delete it first if it exists) File storageDirectory = new File(basedir + "/target/storage"); if (storageDirectory.exists()) { storageDirectory.delete(); } storageDirectory.mkdir(); }
@Test public void testWriteUsingSocketTransport() throws Exception { NioSocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.setReuseAddress(true); SocketAddress address = new InetSocketAddress("localhost", AvailablePortFinder.getNextAvailable()); NioSocketConnector connector = new NioSocketConnector(); // Generate 4MB of random data byte[] data = new byte[4 * 1024 * 1024]; new Random().nextBytes(data); byte[] expectedMd5 = MessageDigest.getInstance("MD5").digest(data); M message = createMessage(data); SenderHandler sender = new SenderHandler(message); ReceiverHandler receiver = new ReceiverHandler(data.length); acceptor.setHandler(sender); connector.setHandler(receiver); acceptor.bind(address); connector.connect(address); sender.latch.await(); receiver.latch.await(); acceptor.dispose(); assertEquals(data.length, receiver.bytesRead); byte[] actualMd5 = receiver.digest.digest(); assertEquals(expectedMd5.length, actualMd5.length); for (int i = 0; i < expectedMd5.length; i++) { assertEquals(expectedMd5[i], actualMd5[i]); } }