use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method testDescribePortalMessage.
@Test
public void testDescribePortalMessage() 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 ? in (1, 2, 3)", new int[] { PGTypes.get(DataTypes.INTEGER).oid() });
ClientMessages.sendBindMessage(buffer, "P1", "S1", Collections.singletonList(1));
channel.writeInbound(buffer);
channel.releaseInbound();
// we're not interested in the startup, parse, or bind replies
channel.flushOutbound();
channel.releaseOutbound();
channel.outboundMessages().clear();
}
{
// try portal describe message
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendDescribeMessage(buffer, ClientMessages.DescribeType.PORTAL, "P1");
channel.writeInbound(buffer);
channel.releaseInbound();
// we should get back a RowDescription message
channel.flushOutbound();
ByteBuf response = channel.readOutbound();
try {
assertThat(response.readByte(), is((byte) 'T'));
assertThat(response.readInt(), is(46));
assertThat(response.readShort(), is((short) 1));
assertThat(PostgresWireProtocol.readCString(response), is("($1 = ANY([1, 2, 3]))"));
assertThat(response.readInt(), is(0));
assertThat(response.readShort(), is((short) 0));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.BOOLEAN).oid()));
assertThat(response.readShort(), is(PGTypes.get(DataTypes.BOOLEAN).typeLen()));
assertThat(response.readInt(), is(PGTypes.get(DataTypes.LONG).typeMod()));
assertThat(response.readShort(), is((short) 0));
} finally {
response.release();
}
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method testSslRejection.
@Test
public void testSslRejection() {
PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> null), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
ClientMessages.sendSslReqMessage(buffer);
channel.writeInbound(buffer);
// We should get back an 'N'...
ByteBuf responseBuffer = channel.readOutbound();
try {
byte response = responseBuffer.readByte();
assertEquals(response, 'N');
} finally {
responseBuffer.release();
}
// ...and continue unencrypted (no ssl handler)
for (Map.Entry<String, ChannelHandler> entry : channel.pipeline()) {
assertThat(entry.getValue(), isOneOf(ctx.decoder, ctx.handler));
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method testCrateServerVersionIsReceivedOnStartup.
@Test
public void testCrateServerVersionIsReceivedOnStartup() 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 buf = Unpooled.buffer();
ClientMessages.sendStartupMessage(buf, "doc");
channel.writeInbound(buf);
channel.releaseInbound();
ByteBuf respBuf;
respBuf = channel.readOutbound();
try {
// Auth OK
assertThat((char) respBuf.readByte(), is('R'));
} finally {
respBuf.release();
}
respBuf = channel.readOutbound();
try {
// ParameterStatus
assertThat((char) respBuf.readByte(), is('S'));
// length
respBuf.readInt();
String key = PostgresWireProtocol.readCString(respBuf);
String value = PostgresWireProtocol.readCString(respBuf);
assertThat(key, is("crate_version"));
assertThat(value, is(Version.CURRENT.externalNumber()));
} finally {
respBuf.release();
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresWireProtocolTest method testHandleEmptySimpleQuery.
@Test
public void testHandleEmptySimpleQuery() throws Exception {
PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class), sessionContext -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> null), null);
channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
ByteBuf buffer = Unpooled.buffer();
try {
Messages.writeCString(buffer, ";".getBytes(StandardCharsets.UTF_8));
ctx.handleSimpleQuery(buffer, new DelayableWriteChannel(channel));
} finally {
buffer.release();
}
ByteBuf firstResponse = channel.readOutbound();
byte[] responseBytes = new byte[5];
try {
firstResponse.readBytes(responseBytes);
// EmptyQueryResponse: 'I' | int32 len
assertThat(responseBytes, is(new byte[] { 'I', 0, 0, 0, 4 }));
} finally {
firstResponse.release();
}
ByteBuf secondResponse = channel.readOutbound();
try {
responseBytes = new byte[6];
secondResponse.readBytes(responseBytes);
// ReadyForQuery: 'Z' | int32 len | 'I'
assertThat(responseBytes, is(new byte[] { 'Z', 0, 0, 0, 5, 'I' }));
} finally {
secondResponse.release();
}
}
use of io.crate.auth.AlwaysOKAuthentication in project crate by crate.
the class PostgresNettyPublishPortTest method testGeneralBindAndPublishAddressOverrideSetting.
@Test
public void testGeneralBindAndPublishAddressOverrideSetting() {
// Check override for network.host
Settings settingsWithCustomHost = Settings.builder().put("network.host", "cantbindtothis").build();
NetworkService networkService = new NetworkService(Collections.emptyList());
StubUserManager userManager = new StubUserManager();
PostgresNetty psql = new PostgresNetty(settingsWithCustomHost, mock(SQLOperations.class), userManager, networkService, new AlwaysOKAuthentication(userManager), new NettyBootstrap(), mock(SslContextProvider.class));
try {
psql.doStart();
fail("Should have failed due to custom hostname");
} catch (BindPostgresException e) {
// that's what we want
assertThat(e.getCause(), instanceOf(UnknownHostException.class));
} finally {
psql.doStop();
psql.close();
}
}
Aggregations