use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method test_channel_is_flushed_after_receiving_flush_request.
@Test
public void test_channel_is_flushed_after_receiving_flush_request() throws Exception {
SQLOperations sqlOperations = mock(SQLOperations.class);
Session session = mock(Session.class);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
AtomicBoolean flushed = new AtomicBoolean(false);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler) {
@Override
public Channel flush() {
flushed.set(true);
return super.flush();
}
};
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "", "select ?", new int[0]);
ClientMessages.sendFlush(buffer);
channel.writeInbound(buffer);
channel.releaseInbound();
assertThat(flushed.get(), is(true));
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method test_row_description_for_statement_on_single_table_includes_table_oid.
@Test
public void test_row_description_for_statement_on_single_table_includes_table_oid() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendParseMessage(buffer, "S1", "SELECT name FROM users", new int[0]);
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.STATEMENT, "S1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a ParameterDescription message, but not interesting for this test case
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
response.release();
// we should get back a RowDescription message
response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(29));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("name"));
assertThat("table_oid", response.readInt(), is(893280107));
assertThat("attr_num", response.readShort(), is((short) 1));
var pgType = PGTypes.get(DataTypes.STRING);
assertThat(response.readInt(), is(pgType.oid()));
assertThat(response.readShort(), is(pgType.typeLen()));
assertThat(response.readInt(), is(pgType.typeMod()));
assertThat("format_code", response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method testSessionCloseOnTerminationMessage.
@Test
public void testSessionCloseOnTerminationMessage() throws Exception {
SQLOperations sqlOperations = mock(SQLOperations.class);
Session session = mock(Session.class);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendStartupMessage(buffer, "doc");
ClientMessages.sendTermination(buffer);
channel.writeInbound(buffer);
channel.releaseInbound();
verify(session, times(1)).close();
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method submitQueriesThroughSimpleQueryMode.
private void submitQueriesThroughSimpleQueryMode(String statements, @Nullable Throwable failure) {
SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
Session session = mock(Session.class);
SessionContext sessionContext = new SessionContext(User.CRATE_USER);
when(session.sessionContext()).thenReturn(sessionContext);
when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
DescribeResult describeResult = mock(DescribeResult.class);
when(describeResult.getFields()).thenReturn(null);
when(session.describe(anyChar(), anyString())).thenReturn(describeResult);
when(session.transactionState()).thenReturn(TransactionState.IDLE);
PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionCtx -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
if (failure != null) {
when(session.sync()).thenAnswer(invocationOnMock -> {
Messages.sendErrorResponse(channel, AccessControl.DISABLED, failure);
return CompletableFuture.failedFuture(failure);
});
} else {
when(session.sync()).thenReturn(CompletableFuture.completedFuture(null));
}
sendStartupMessage(channel);
readAuthenticationOK(channel);
skipParameterMessages(channel);
readReadyForQueryMessage(channel);
ByteBuf query = Unpooled.buffer();
try {
// the actual statements don't have to be valid as they are not executed
Messages.writeCString(query, statements.getBytes(StandardCharsets.UTF_8));
ctx.handleSimpleQuery(query, new DelayableWriteChannel(channel));
} finally {
query.release();
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class SslReqHandlerTest method testSslReqHandler.
@Test
public void testSslReqHandler() {
PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> null), // use a simple ssl context
getSelfSignedSslContextProvider());
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
sendSslRequest(channel);
// We should get back an 'S'...
ByteBuf responseBuffer = channel.readOutbound();
byte response = responseBuffer.readByte();
assertEquals(response, 'S');
responseBuffer.release();
// ...and continue encrypted (ssl handler)
assertThat(channel.pipeline().first(), instanceOf(SslHandler.class));
}
Aggregations