Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Fix for issue #50 - support relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Mar 2, 2020
1 parent e4fb4d0 commit 8fae725
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.tuweni.devp2p.v5.packet.UdpMessage
import java.net.InetSocketAddress

/**
* Udp message handler, aimed to process it's parameters and sending result
* Udp message handler, aimed to process its parameters and sending result
*/
interface MessageHandler<T : UdpMessage> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.apache.tuweni.devp2p.v5.packet.UdpMessage
interface PacketCodec {

/**
* Encodes message, encrypting it's body
* Encodes message, encrypting its body
*
* @param message message for encoding
* @param destNodeId receiver node identifier for tag creation
Expand All @@ -47,7 +47,7 @@ interface PacketCodec {
fun encode(message: UdpMessage, destNodeId: Bytes, handshakeParams: HandshakeInitParameters? = null): EncodeResult

/**
* Decodes message, decrypting it's body
* Decodes message, decrypting its body
*
* @param message message for decoding
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public class DefaultUdpConnector(
) {
val encodeResult = packetCodec.encode(message, destNodeId, handshakeParams)
pendingMessages.put(encodeResult.authTag.toHexString(), TrackingMessage(message, destNodeId))
receiveChannel.send(ByteBuffer.wrap(encodeResult.content.toArray()), address)
receiveChannel.send(ByteBuffer.wrap(encodeResult.content.toArrayUnsafe()), address)
}

override suspend fun terminate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import org.apache.tuweni.devp2p.v5.packet.RandomMessage
import org.apache.tuweni.devp2p.v5.packet.WhoAreYouMessage
import java.net.InetSocketAddress

/**
* Handles WHOAREYOU messages.
*
* WHOAREYOU is sent by the other node after the first message is sent, asking to initiate the connection.
*/
class WhoAreYouMessageHandler : MessageHandler<WhoAreYouMessage> {

override suspend fun handle(
Expand All @@ -35,6 +40,7 @@ class WhoAreYouMessageHandler : MessageHandler<WhoAreYouMessage> {
) {
// Retrieve enr
val trackingMessage = connector.getPendingMessage(message.authTag)
// If no message was sent to the node, ignore the WHOAREYOU request.
trackingMessage?.let {
val rlpEnr = connector.getNodeRecords().find(trackingMessage.nodeId)
rlpEnr?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package org.apache.tuweni.devp2p.v5.misc

import org.apache.tuweni.bytes.Bytes

/**
* The result of encoding a message: its authentication tag, used to track responses, and its content as bytes.
*/
class EncodeResult(
val authTag: Bytes,
val content: Bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ class RandomMessage(
val authTag: Bytes = authTag(),
val data: Bytes = randomData()
) : UdpMessage() {

companion object {
fun randomData(): Bytes = Bytes.random(RANDOM_DATA_LENGTH)

fun create(authTag: Bytes, content: Bytes = randomData()): RandomMessage {
return RandomMessage(authTag, content)
}
}

override fun getMessageType(): Bytes {
throw UnsupportedOperationException("Message type unsupported for random messages")
}

override fun encode(): Bytes {
return data
}

companion object {
fun create(authTag: Bytes, content: Bytes = randomData()): RandomMessage {
return RandomMessage(authTag, content)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.tuweni.devp2p.v5.packet
import org.apache.tuweni.bytes.Bytes
import org.apache.tuweni.crypto.Hash

abstract class UdpMessage {
interface UdpMessage {

companion object {

Expand Down Expand Up @@ -51,12 +51,10 @@ abstract class UdpMessage {

fun authTag(): Bytes = Bytes.random(AUTH_TAG_LENGTH)

fun randomData(): Bytes = Bytes.random(RANDOM_DATA_LENGTH)

fun idNonce(): Bytes = Bytes.random(ID_NONCE_LENGTH)
}

abstract fun encode(): Bytes
fun encode(): Bytes

abstract fun getMessageType(): Bytes
fun getMessageType(): Bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DefaultUdpConnectorTest {
val socketChannel = CoroutineDatagramChannel.open()
socketChannel.bind(receiverAddress)

val data = UdpMessage.randomData()
val data = RandomMessage.randomData()
val randomMessage = RandomMessage(UdpMessage.authTag(), data)
connector!!.send(receiverAddress, randomMessage, destNodeId)
val buffer = ByteBuffer.allocate(UdpMessage.MAX_UDP_MESSAGE_SIZE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ class RandomMessageTest {

assert(decodingResult.data == data)
}

@Test
fun randomDataGivesRandom44Bytes() {
val firstResult = RandomMessage.randomData()

assert(UdpMessage.RANDOM_DATA_LENGTH == firstResult.size())

val secondResult = RandomMessage.randomData()

assert(secondResult != firstResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ class UdpMessageTest {
assert(secondResult != firstResult)
}

@Test
fun randomDataGivesRandom44Bytes() {
val firstResult = UdpMessage.randomData()

assert(UdpMessage.RANDOM_DATA_LENGTH == firstResult.size())

val secondResult = UdpMessage.randomData()

assert(secondResult != firstResult)
}

@Test
fun idNonceGivesRandom32Bytes() {
val firstResult = UdpMessage.idNonce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class FileBackedFingerprintRepository implements FingerprintRepository {

FileBackedFingerprintRepository(Path fingerprintFile) {
try {
createDirectories(fingerprintFile.getParent());
createDirectories(fingerprintFile.toAbsolutePath().getParent());
createFileIfMissing(fingerprintFile);
} catch (IOException e) {
throw new TLSEnvironmentException("Cannot create fingerprint file " + fingerprintFile, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.apache.tuweni.net.tls;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.tuweni.io.file.Files.deleteRecursively;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -24,6 +25,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;

import org.junit.jupiter.api.Test;
Expand All @@ -40,6 +42,15 @@ private Bytes generateFingerprint() {
return Bytes.wrap(bytes);
}

@Test
void testRelativePath() throws IOException {
try {
FileBackedFingerprintRepository repo = new FileBackedFingerprintRepository(Paths.get("tmp", "foo"));
} finally {
deleteRecursively(Paths.get("tmp"));
}
}

@Test
FileBackedFingerprintRepository testAddingNewFingerprint(@TempDirectory Path tempFolder) throws IOException {
FileBackedFingerprintRepository repo = new FileBackedFingerprintRepository(tempFolder.resolve("repo"));
Expand Down

0 comments on commit 8fae725

Please sign in to comment.