use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testSimpleWriteMethod.
@Test
public void testSimpleWriteMethod() throws Exception {
Byte[] bytes = new Byte[] { 1, 2, 3 };
MessageOutputStream out = sendChannel.writeMessage();
for (int i = 0; i < bytes.length; i++) {
out.write(bytes[i]);
}
out.close();
final CountDownLatch latch = new CountDownLatch(1);
final ArrayList<Byte> result = new ArrayList<Byte>();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received");
try {
int i = message.read();
while (i != -1) {
result.add((byte) i);
i = message.read();
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
latch.await();
assertNull(exRef.get());
Byte[] resultBytes = result.toArray(new Byte[result.size()]);
assertArrayEquals(bytes, resultBytes);
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ConnectionTestCase method rejectUnknownService.
@Test
public void rejectUnknownService() throws Exception {
final Connection connection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<Connection>() {
public Connection run() {
try {
return clientEndpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY).get();
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
});
final IoFuture<Channel> channelFuture = connection.openChannel("unknown", OptionMap.EMPTY);
try {
channelFuture.get();
fail();
} catch (CancellationException e) {
throw e;
} catch (IOException e) {
// ok
}
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class RemoteServiceWithPredicateTest method tryToConnectToService.
/**
* Method which registers a service with the provided validation predicate, then tests to see if the service
* accepts or rejects service Channel creation attempts which are expected by the predicate provided.
*
* @param validationPredicate
* @throws IOException
* @throws URISyntaxException
* @throws InterruptedException
*/
public void tryToConnectToService(Predicate<Connection> validationPredicate, boolean shouldSucceed) throws IOException {
Connection connection;
Registration serviceRegistration;
Channel sendChannel = null, recvChannel = null;
final FutureResult<Channel> passer = new FutureResult<Channel>();
// register the service with the endpoint using the provided validation predicate
serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {
public void channelOpened(final Channel channel) {
passer.setResult(channel);
}
public void registrationTerminated() {
}
}, OptionMap.EMPTY, validationPredicate);
// create a connection to the endpoint's connector
IoFuture<Connection> futureConnection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<IoFuture<Connection>>() {
public IoFuture<Connection> run() {
try {
return endpoint.connect(new URI("remote://localhost:30123"), OptionMap.EMPTY);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
});
connection = futureConnection.get();
assertNull("No SSLSession", connection.getSslSession());
// use the connection to open a channel to the service
IoFuture.Status status = IoFuture.Status.WAITING;
IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", OptionMap.EMPTY);
try {
status = futureChannel.awaitInterruptibly(2L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// verify the resukt of the service connection attempt
Logger.getLogger("TEST").infof("service returned status %s", status);
if (status == IoFuture.Status.DONE) {
Logger.getLogger("TEST").infof("Channel creation succeeded");
sendChannel = futureChannel.get();
recvChannel = passer.getIoFuture().get();
assertNotNull(recvChannel);
assertNull("No SSLSession", recvChannel.getConnection().getSslSession());
if (!shouldSucceed)
fail("Channel creation succeeded when it should have failed!");
} else if (status == IoFuture.Status.FAILED) {
Exception exception = futureChannel.getException();
Logger.getLogger("TEST").infof("Channel creation failed with exception %s", exception);
if (shouldSucceed)
fail("Channel creation failed when it should have succeeded!");
}
// service has been tested, we can shut down
safeClose(sendChannel);
safeClose(recvChannel);
safeClose(connection);
serviceRegistration.close();
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class HeartbeatTestCase method testDisableHeartbeat.
/**
* Test that heartbeat can be set and can be disabled by setting it to 0
*
* @throws Exception
*/
@Test
public void testDisableHeartbeat() throws Exception {
Channel clientChannel = null;
Channel serverChannel = null;
Closeable streamServer = null;
Connection connection = null;
Registration serviceRegistration = null;
final Endpoint endpoint = Endpoint.builder().setEndpointName("test").build();
NetworkServerProvider networkServerProvider = endpoint.getConnectionProviderInterface("remote", NetworkServerProvider.class);
final SecurityDomain.Builder domainBuilder = SecurityDomain.builder();
final SimpleMapBackedSecurityRealm mainRealm = new SimpleMapBackedSecurityRealm();
domainBuilder.addRealm("mainRealm", mainRealm).build();
domainBuilder.setDefaultRealmName("mainRealm");
domainBuilder.setPermissionMapper((permissionMappable, roles) -> PermissionVerifier.ALL);
final PasswordFactory passwordFactory = PasswordFactory.getInstance("clear");
mainRealm.setPasswordMap("bob", passwordFactory.generatePassword(new ClearPasswordSpec("pass".toCharArray())));
final SaslServerFactory saslServerFactory = new ServiceLoaderSaslServerFactory(HeartbeatTestCase.class.getClassLoader());
final SaslAuthenticationFactory.Builder builder = SaslAuthenticationFactory.builder();
builder.setSecurityDomain(domainBuilder.build());
builder.setFactory(saslServerFactory);
builder.setMechanismConfigurationSelector(mechanismInformation -> SaslMechanismInformation.Names.SCRAM_SHA_256.equals(mechanismInformation.getMechanismName()) ? MechanismConfiguration.EMPTY : null);
final SaslAuthenticationFactory saslAuthenticationFactory = builder.build();
streamServer = networkServerProvider.createServer(new InetSocketAddress("localhost", 30123), OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE), saslAuthenticationFactory, SSLContext.getDefault());
final FutureResult<Channel> passer = new FutureResult<Channel>();
serviceRegistration = endpoint.registerService("org.jboss.test", new OpenListener() {
public void channelOpened(final Channel channel) {
passer.setResult(channel);
}
public void registrationTerminated() {
}
}, OptionMap.EMPTY);
IoFuture<Connection> futureConnection = AuthenticationContext.empty().with(MatchRule.ALL, AuthenticationConfiguration.empty().useName("bob").usePassword("pass").setSaslMechanismSelector(SaslMechanismSelector.NONE.addMechanism("SCRAM-SHA-256"))).run(new PrivilegedAction<IoFuture<Connection>>() {
public IoFuture<Connection> run() {
try {
return endpoint.connect(new URI("remote://localhost:30123"), OptionMap.create(RemotingOptions.HEARTBEAT_INTERVAL, 0));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
});
connection = futureConnection.get();
IoFuture<Channel> futureChannel = connection.openChannel("org.jboss.test", OptionMap.EMPTY);
clientChannel = futureChannel.get();
serverChannel = passer.getIoFuture().get();
assertNotNull(serverChannel);
RemoteConnectionChannel remoteClientChannel = (RemoteConnectionChannel) clientChannel;
assertEquals(0, Utils.getInstanceValue(remoteClientChannel.getRemoteConnection(), "heartbeatInterval"));
RemoteWriteListener clientWriteListener = (RemoteWriteListener) Utils.getInstanceValue(remoteClientChannel.getRemoteConnection(), "writeListener");
assertNull(Utils.getInstanceValue(clientWriteListener, "heartKey"));
afterTest(clientChannel, serverChannel, connection, serviceRegistration);
destroy(endpoint, streamServer);
}
use of com.automatak.dnp3.Channel in project jboss-ejb-client by wildfly.
the class DummyServer method hardKill.
public void hardKill() throws IOException {
for (Channel i : currentConnections) {
try {
i.close();
} catch (IOException e) {
logger.error("failed to close", e);
}
}
server.close();
server = null;
endpoint.close();
}
Aggregations