use of streamer.debug.MockServer in project cloudstack by apache.
the class MockServerTest method testIsMockServerCanRespond.
@Test
public void testIsMockServerCanRespond() throws Exception {
final byte[] mockClientData = new byte[] { 0x01, 0x02, 0x03 };
final byte[] mockServerData = new byte[] { 0x03, 0x02, 0x01 };
MockServer server = new MockServer(new Packet[] { new Packet("Client hello") {
{
type = CLIENT;
data = mockClientData;
}
}, new Packet("Server hello") {
{
type = SERVER;
data = mockServerData;
}
} });
server.start();
// Connect to server and send and receive mock data
Socket socket = SocketFactory.getDefault().createSocket();
try {
socket.connect(new InetSocketAddress("127.0.0.1", server.getAddress().getPort()));
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
// Write mock data to server
os.write(mockClientData);
// Read data from server
byte[] actualData = new byte[mockServerData.length];
int actualDataLength = is.read(actualData);
// Compare mock data with actual data
assertEquals("Unexpected length of actual data read from server.", mockServerData.length, actualDataLength);
for (int i = 0; i < actualDataLength; i++) {
assertEquals("Unexpected byte #" + i + " in response", mockServerData[i], actualData[i]);
}
server.waitUntilShutdowned(1 * 1000);
assertNull("Unexpected exception at mock server side.", server.getException());
assertTrue("Server is not shutdowned at after conversation.", server.isShutdowned());
} finally {
socket.close();
}
}
use of streamer.debug.MockServer in project cloudstack by apache.
the class MockServerTest method testIsMockServerCanUpgradeConnectionToSsl.
@Test
public void testIsMockServerCanUpgradeConnectionToSsl() throws Exception {
final byte[] mockClientData1 = new byte[] { 0x01, 0x02, 0x03 };
final byte[] mockServerData1 = new byte[] { 0x03, 0x02, 0x01 };
final byte[] mockClientData2 = new byte[] { 0x02, 0x04, 0x02, 0x03 };
final byte[] mockServerData2 = new byte[] { 0x02, 0x02, 0x01, 0x04 };
MockServer server = new MockServer(new Packet[] { new Packet("Client hello") {
{
type = CLIENT;
data = mockClientData1;
}
}, new Packet("Server hello") {
{
type = SERVER;
data = mockServerData1;
}
}, new Packet("Upgrade connection to SSL") {
{
type = UPGRADE_TO_SSL;
}
}, new Packet("Client data over SSL") {
{
type = CLIENT;
data = mockClientData2;
}
}, new Packet("Server data over SSL") {
{
type = SERVER;
data = mockServerData2;
}
} });
server.start();
// Connect to server and send and receive mock data
Socket socket = SocketFactory.getDefault().createSocket();
try {
InetSocketAddress address = new InetSocketAddress("127.0.0.1", server.getAddress().getPort());
socket.connect(address);
// Send hello data over plain connection
{
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
// Write mock data to server
os.write(mockClientData1);
// Read data from server
byte[] actualData = new byte[mockServerData1.length];
int actualDataLength = is.read(actualData);
// Compare mock data with actual data
assertEquals("Unexpected length of actual data read from server.", mockServerData1.length, actualDataLength);
for (int i = 0; i < actualDataLength; i++) {
assertEquals("Unexpected byte #" + i + " in response", mockServerData1[i], actualData[i]);
}
}
// Upgrade connection to SSL and send mock data
{
//System.setProperty("javax.net.debug", "ssl");
final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, address.getHostName(), address.getPort(), true);
//sslSocket.setEnabledCipherSuites(sslSocket.getSupportedCipherSuites());
sslSocket.setEnabledCipherSuites(new String[] { "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA" });
sslSocket.startHandshake();
InputStream is = sslSocket.getInputStream();
OutputStream os = sslSocket.getOutputStream();
// Write mock data to server
os.write(mockClientData2);
// Read data from server
byte[] actualData = new byte[mockServerData2.length];
int actualDataLength = is.read(actualData);
// Compare mock data with actual data
assertEquals("Unexpected length of actual data read from server.", mockServerData2.length, actualDataLength);
for (int i = 0; i < actualDataLength; i++) {
assertEquals("Unexpected byte #" + i + " in response", mockServerData2[i], actualData[i]);
}
}
server.waitUntilShutdowned(1 * 1000);
assertNull("Unexpected exception at mock server side.", server.getException());
assertTrue("Server is not shutdowned at after conversation.", server.isShutdowned());
} finally {
socket.close();
}
}
use of streamer.debug.MockServer in project cloudstack by apache.
the class AprSocketWrapperImpl method main.
/**
* Example.
*/
public static void main(String[] args) {
try {
System.setProperty("streamer.Link.debug", "true");
System.setProperty("streamer.Element.debug", "true");
System.setProperty("rdpclient.MockServer.debug", "true");
Pipeline pipeline = new PipelineImpl("echo client");
AprSocketWrapperImpl socketWrapper = new AprSocketWrapperImpl("socket", null);
pipeline.add(socketWrapper);
pipeline.add(new BaseElement("echo"));
// To decouple input and output
pipeline.add(new Queue("queue"));
pipeline.link("socket", "echo", "queue", "socket");
final byte[] mockData = new byte[] { 0x01, 0x02, 0x03 };
MockServer server = new MockServer(new Packet[] { new Packet("Server hello") {
{
type = SERVER;
data = mockData;
}
}, new Packet("Client hello") {
{
type = CLIENT;
data = mockData;
}
}, new Packet("Server hello") {
{
type = SERVER;
data = mockData;
}
}, new Packet("Client hello") {
{
type = CLIENT;
data = mockData;
}
} });
server.start();
InetSocketAddress address = server.getAddress();
socketWrapper.connect(address);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
use of streamer.debug.MockServer in project cloudstack by apache.
the class SocketWrapperImpl method main.
/**
* Example.
*/
public static void main(String[] args) {
try {
System.setProperty("streamer.Link.debug", "true");
System.setProperty("streamer.Element.debug", "true");
System.setProperty("rdpclient.MockServer.debug", "true");
Pipeline pipeline = new PipelineImpl("echo client");
SocketWrapperImpl socketWrapper = new SocketWrapperImpl("socket", null);
pipeline.add(socketWrapper);
pipeline.add(new BaseElement("echo"));
// To decouple input and output
pipeline.add(new Queue("queue"));
pipeline.link("socket", "echo", "queue", "socket");
final byte[] mockData = new byte[] { 0x01, 0x02, 0x03 };
MockServer server = new MockServer(new Packet[] { new Packet("Server hello") {
{
type = SERVER;
data = mockData;
}
}, new Packet("Client hello") {
{
type = CLIENT;
data = mockData;
}
}, new Packet("Server hello") {
{
type = SERVER;
data = mockData;
}
}, new Packet("Client hello") {
{
type = CLIENT;
data = mockData;
}
} });
server.start();
InetSocketAddress address = server.getAddress();
/*DEBUG*/
System.out.println("Address: " + address);
socketWrapper.connect(address);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
Aggregations