use of javax.net.SocketFactory in project camel by apache.
the class FtpBadLoginInProducerConnectionLeakTest method createRegistry.
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
SocketFactory sf = new AuditingSocketFactory();
jndi.bind("sf", sf);
return jndi;
}
use of javax.net.SocketFactory in project camel by apache.
the class FtpBadLoginMockNoopConnectionLeakTest method createRegistry.
@Override
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry jndi = super.createRegistry();
SocketFactory sf = new AuditingSocketFactory();
jndi.bind("sf", sf);
return jndi;
}
use of javax.net.SocketFactory in project hadoop by apache.
the class TestIPC method testRTEDuringConnectionSetup.
/**
* Test that, if a RuntimeException is thrown after creating a socket
* but before successfully connecting to the IPC server, that the
* failure is handled properly. This is a regression test for
* HADOOP-7428.
*/
@Test(timeout = 60000)
public void testRTEDuringConnectionSetup() throws IOException {
// Set up a socket factory which returns sockets which
// throw an RTE when setSoTimeout is called.
SocketFactory spyFactory = spy(NetUtils.getDefaultSocketFactory(conf));
Mockito.doAnswer(new Answer<Socket>() {
@Override
public Socket answer(InvocationOnMock invocation) throws Throwable {
Socket s = spy((Socket) invocation.callRealMethod());
doThrow(new RuntimeException("Injected fault")).when(s).setSoTimeout(anyInt());
return s;
}
}).when(spyFactory).createSocket();
Server server = new TestServer(1, true);
Client client = new Client(LongWritable.class, conf, spyFactory);
server.start();
try {
// Call should fail due to injected exception.
InetSocketAddress address = NetUtils.getConnectAddress(server);
try {
call(client, RANDOM.nextLong(), address, conf);
fail("Expected an exception to have been thrown");
} catch (Exception e) {
LOG.info("caught expected exception", e);
assertTrue(StringUtils.stringifyException(e).contains("Injected fault"));
}
// Resetting to the normal socket behavior should succeed
// (i.e. it should not have cached a half-constructed connection)
Mockito.reset(spyFactory);
call(client, RANDOM.nextLong(), address, conf);
} finally {
client.stop();
server.stop();
}
}
use of javax.net.SocketFactory in project hadoop by apache.
the class TestIPC method testSocketFactoryException.
/**
* Test that, if the socket factory throws an IOE, it properly propagates
* to the client.
*/
@Test(timeout = 60000)
public void testSocketFactoryException() throws IOException {
SocketFactory mockFactory = mock(SocketFactory.class);
doThrow(new IOException("Injected fault")).when(mockFactory).createSocket();
Client client = new Client(LongWritable.class, conf, mockFactory);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10);
try {
call(client, RANDOM.nextLong(), address, conf);
fail("Expected an exception to have been thrown");
} catch (IOException e) {
assertTrue(e.getMessage().contains("Injected fault"));
} finally {
client.stop();
}
}
use of javax.net.SocketFactory in project tomcat by apache.
the class TestNonBlockingAPI method testNonBlockingWriteInternal.
private void testNonBlockingWriteInternal(boolean keepAlive) throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
NBWriteServlet servlet = new NBWriteServlet();
String servletName = NBWriteServlet.class.getName();
Tomcat.addServlet(ctx, servletName, servlet);
ctx.addServletMappingDecoded("/", servletName);
tomcat.getConnector().setProperty("socket.txBufSize", "1024");
tomcat.start();
SocketFactory factory = SocketFactory.getDefault();
Socket s = factory.createSocket("localhost", getPort());
InputStream is = s.getInputStream();
byte[] buffer = new byte[8192];
ByteChunk result = new ByteChunk();
OutputStream os = s.getOutputStream();
if (keepAlive) {
os.write(("OPTIONS * HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.flush();
is.read(buffer);
}
os.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.flush();
int read = 0;
int readSinceLastPause = 0;
while (read != -1) {
read = is.read(buffer);
if (read > 0) {
result.append(buffer, 0, read);
}
readSinceLastPause += read;
if (readSinceLastPause > WRITE_SIZE / 16) {
readSinceLastPause = 0;
Thread.sleep(500);
}
}
os.close();
is.close();
s.close();
// Validate the result.
// Response line
String resultString = result.toString();
log.info("Client read " + resultString.length() + " bytes");
int lineStart = 0;
int lineEnd = resultString.indexOf('\n', 0);
String line = resultString.substring(lineStart, lineEnd + 1);
Assert.assertEquals("HTTP/1.1 200 \r\n", line);
// Check headers - looking to see if response is chunked (it should be)
boolean chunked = false;
while (line.length() > 2) {
lineStart = lineEnd + 1;
lineEnd = resultString.indexOf('\n', lineStart);
line = resultString.substring(lineStart, lineEnd + 1);
if (line.startsWith("Transfer-Encoding:")) {
Assert.assertEquals("Transfer-Encoding: chunked\r\n", line);
chunked = true;
}
}
Assert.assertTrue(chunked);
// Now check body size
int totalBodyRead = 0;
int chunkSize = -1;
while (chunkSize != 0) {
// Chunk size in hex
lineStart = lineEnd + 1;
lineEnd = resultString.indexOf('\n', lineStart);
line = resultString.substring(lineStart, lineEnd + 1);
Assert.assertTrue(line.endsWith("\r\n"));
line = line.substring(0, line.length() - 2);
log.info("[" + line + "]");
chunkSize = Integer.parseInt(line, 16);
// Read the chunk
lineStart = lineEnd + 1;
lineEnd = resultString.indexOf('\n', lineStart);
log.info("Start : " + lineStart + ", End: " + lineEnd);
if (lineEnd > lineStart) {
line = resultString.substring(lineStart, lineEnd + 1);
} else {
line = resultString.substring(lineStart);
}
if (line.length() > 40) {
log.info(line.substring(0, 32));
} else {
log.info(line);
}
if (chunkSize + 2 != line.length()) {
log.error("Chunk wrong length. Was " + line.length() + " Expected " + (chunkSize + 2));
byte[] resultBytes = resultString.getBytes();
// Find error
boolean found = false;
for (int i = totalBodyRead; i < (totalBodyRead + line.length()); i++) {
if (DATA[i] != resultBytes[lineStart + i - totalBodyRead]) {
int dataStart = i - 64;
if (dataStart < 0) {
dataStart = 0;
}
int dataEnd = i + 64;
if (dataEnd > DATA.length) {
dataEnd = DATA.length;
}
int resultStart = lineStart + i - totalBodyRead - 64;
if (resultStart < 0) {
resultStart = 0;
}
int resultEnd = lineStart + i - totalBodyRead + 64;
if (resultEnd > resultString.length()) {
resultEnd = resultString.length();
}
log.error("Mis-match tx: " + new String(DATA, dataStart, dataEnd - dataStart));
log.error("Mis-match rx: " + resultString.substring(resultStart, resultEnd));
found = true;
break;
}
}
if (!found) {
log.error("No mismatch. Data truncated");
}
}
Assert.assertTrue(line.endsWith("\r\n"));
Assert.assertEquals(chunkSize + 2, line.length());
totalBodyRead += chunkSize;
}
Assert.assertEquals(WRITE_SIZE, totalBodyRead);
}
Aggregations