use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.
the class AuthenticationIT method shouldFailDifferentlyIfTooManyFailedAuthAttempts.
@ParameterizedTest(name = "{displayName} {2}")
@MethodSource("argumentsProvider")
public void shouldFailDifferentlyIfTooManyFailedAuthAttempts(Class<? extends TransportConnection> connectionClass, Neo4jPack neo4jPack, String name) throws Exception {
initParameters(connectionClass, neo4jPack, name);
// Given
final long timeout = System.currentTimeMillis() + 60_000;
FailureMessage failureMessage = null;
// When
while (failureMessage == null) {
if (System.currentTimeMillis() > timeout) {
fail("Timed out waiting for the authentication failure to occur.");
}
ExecutorService executor = Executors.newFixedThreadPool(10);
// Fire up some parallel connections that all send wrong authentication tokens
List<CompletableFuture<FailureMessage>> futures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
futures.add(CompletableFuture.supplyAsync(this::collectAuthFailureOnFailedAuth, executor));
}
try {
// Wait for all tasks to complete
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(30, SECONDS);
// We want at least one of the futures to fail with our expected code
for (int i = 0; i < futures.size(); i++) {
FailureMessage recordedMessage = futures.get(i).get();
if (recordedMessage != null) {
failureMessage = recordedMessage;
break;
}
}
} catch (TimeoutException ex) {
// if jobs did not complete, let's try again
// do nothing
} finally {
executor.shutdown();
}
}
assertThat(failureMessage.status()).isEqualTo(Status.Security.AuthenticationRateLimit);
assertThat(failureMessage.message()).contains("The client has provided incorrect authentication details too many times in a row.");
}
use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.
the class FailureMessageEncoderTest method shouldEncodeFailureMessage.
@Test
void shouldEncodeFailureMessage() throws Throwable {
// Given
Neo4jPack.Packer packer = mock(Neo4jPack.Packer.class);
Log log = mock(Log.class);
FailureMessageEncoder encoder = new FailureMessageEncoder(log);
// When
encoder.encode(packer, new FailureMessage(Status.General.UnknownError, "I am an error message"));
// Then
verify(log, never()).debug(anyString(), any(FailureMessage.class));
verify(packer).packStructHeader(anyInt(), eq(FailureMessage.SIGNATURE));
verify(packer).packMapHeader(2);
verify(packer).pack("code");
verify(packer).pack("message");
}
use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.
the class BoltResponseMessageReader method read.
public void read(BoltResponseMessageWriter messageWriter) throws IOException {
try {
unpacker.unpackStructHeader();
final int signature = unpacker.unpackStructSignature();
BoltResponseMessage message = BoltResponseMessage.withSignature(signature);
try {
switch(message) {
case SUCCESS:
MapValue successMetadata = unpacker.unpackMap();
messageWriter.write(new SuccessMessage(successMetadata));
break;
case RECORD:
long length = unpacker.unpackListHeader();
final AnyValue[] fields = new AnyValue[(int) length];
for (int i = 0; i < length; i++) {
fields[i] = unpacker.unpack();
}
messageWriter.write(new RecordMessage(fields));
break;
case IGNORED:
messageWriter.write(IgnoredMessage.IGNORED_MESSAGE);
break;
case FAILURE:
MapValue failureMetadata = unpacker.unpackMap();
String code = failureMetadata.containsKey("code") ? ((StringValue) failureMetadata.get("code")).stringValue() : Status.General.UnknownError.name();
AnyValue msgValue = failureMetadata.get("message");
String msg = msgValue != NO_VALUE ? ((StringValue) msgValue).stringValue() : "<No message supplied>";
messageWriter.write(new FailureMessage(Neo4jError.codeFromString(code), msg));
break;
default:
throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not supported.", Integer.toHexString(signature)));
}
} catch (IllegalArgumentException e) {
throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not a valid message signature.", Integer.toHexString(signature)));
}
} catch (PackStream.PackStreamException e) {
throw new BoltIOException(Status.Request.InvalidFormat, String.format("Unable to read message type. Error was: %s.", e.getMessage()), e);
}
}
use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.
the class BoltResponseMessageTest method shouldHandleCommonMessages.
@Test
void shouldHandleCommonMessages() throws Throwable {
assertSerializes(new RecordMessage(new AnyValue[] { longValue(1L), stringValue("b"), longValue(2L) }));
assertSerializes(new SuccessMessage(VirtualValues.EMPTY_MAP));
assertSerializes(new FailureMessage(Status.General.UnknownError, "Err"));
assertSerializes(IGNORED_MESSAGE);
}
use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.
the class BoltResponseMessageWriterV3Test method shouldWriteFailureMessage.
@Test
void shouldWriteFailureMessage() throws Exception {
PackOutput output = mock(PackOutput.class);
Neo4jPack.Packer packer = mock(Neo4jPack.Packer.class);
var writer = newWriter(output, packer);
Status.Transaction errorStatus = Status.Transaction.DeadlockDetected;
String errorMessage = "Hi Deadlock!";
writer.write(new FailureMessage(errorStatus, errorMessage));
InOrder inOrder = inOrder(output, packer);
inOrder.verify(output).beginMessage();
inOrder.verify(packer).pack(errorStatus.code().serialize());
inOrder.verify(packer).pack(errorMessage);
inOrder.verify(output).messageSucceeded();
}
Aggregations