use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.
the class FramerTest method shouldThrowExceptionWhenFramingMessageTooLong.
@Test
public void shouldThrowExceptionWhenFramingMessageTooLong() {
final byte[] aes = { 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2 };
final byte[] mac = { 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2, 0xa, 0x2 };
final byte[] byteArray = new byte[0xFFFFFF];
new Random().nextBytes(byteArray);
final MessageData ethMessage = new RawMessage(0x00, Bytes.wrap(byteArray));
final HandshakeSecrets secrets = new HandshakeSecrets(aes, mac, mac);
final Framer framer = new Framer(secrets);
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> framer.frame(ethMessage, Unpooled.buffer())).withMessageContaining("Message size in excess of maximum length.");
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.
the class FramerTest method frameMessage.
@Test
public void frameMessage() throws IOException {
// This is a circular test.
//
// This test decrypts all messages in the test vectors; it then impersonates the sending end
// by swapping the ingress and egress MACs and frames the plaintext messages.
// We then verify if the resulting ciphertexts are equal to our test vectors.
//
final JsonNode td = MAPPER.readTree(FramerTest.class.getResource("/peer1.json"));
HandshakeSecrets secrets = secretsFrom(td, false);
Framer framer = new Framer(secrets);
final JsonNode messages = td.get("messages");
final List<MessageData> decrypted = stream(messages.spliterator(), false).map(n -> decodeHexDump(n.get("data").asText())).map(Unpooled::wrappedBuffer).map(framer::deframe).collect(toList());
secrets = secretsFrom(td, true);
framer = new Framer(secrets);
for (int i = 0; i < decrypted.size(); i++) {
final ByteBuf b = Unpooled.buffer();
framer.frame(decrypted.get(i), b);
final byte[] enc = new byte[b.readableBytes()];
b.readBytes(enc);
final byte[] expected = decodeHexDump(messages.get(i).get("data").asText());
assertThat(expected).isEqualTo(enc);
}
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.
the class DisconnectMessageTest method readFromWithReason.
@Test
public void readFromWithReason() {
MessageData messageData = new RawMessage(WireMessageCodes.DISCONNECT, Bytes.fromHexString("0xC103"));
DisconnectMessage disconnectMessage = DisconnectMessage.readFrom(messageData);
DisconnectReason reason = disconnectMessage.getReason();
assertThat(reason).isEqualTo(DisconnectReason.USELESS_PEER);
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.
the class DisconnectMessageTest method readFromWithNoReason.
@Test
public void readFromWithNoReason() {
MessageData messageData = new RawMessage(WireMessageCodes.DISCONNECT, Bytes.fromHexString("0xC180"));
DisconnectMessage disconnectMessage = DisconnectMessage.readFrom(messageData);
DisconnectReason reason = disconnectMessage.getReason();
assertThat(reason).isEqualTo(DisconnectReason.UNKNOWN);
}
use of org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData in project besu by hyperledger.
the class Istanbul99ProtocolManagerTest method respondToEth65GetHeadersUsingIstanbul99.
@Test
public void respondToEth65GetHeadersUsingIstanbul99() throws ExecutionException, InterruptedException, TimeoutException {
final CompletableFuture<Void> done = new CompletableFuture<>();
final EthScheduler ethScheduler = new DeterministicEthScheduler(() -> false);
EthPeers peers = new EthPeers(Istanbul99Protocol.NAME, TestClock.fixed(), new NoOpMetricsSystem(), 25);
EthMessages messages = new EthMessages();
final BigInteger networkId = BigInteger.ONE;
try (final EthProtocolManager ethManager = new Istanbul99ProtocolManager(blockchain, networkId, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig(), peers, messages, new EthContext(peers, messages, ethScheduler), Collections.emptyList(), false, ethScheduler)) {
final long startBlock = blockchain.getChainHeadBlockNumber() + 1;
final int blockCount = 5;
final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
assertThat(headers.size()).isEqualTo(0);
done.complete(null);
};
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(Istanbul99Protocol.ISTANBUL99, new DefaultMessage(peer, messageData));
done.get(10, TimeUnit.SECONDS);
}
}
Aggregations