This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PAN-2811] Be more lenient with discovery message deserialization (#1580
- Loading branch information
Showing
12 changed files
with
369 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...va/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/FindNeighborsPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import org.junit.Test; | ||
|
||
public class FindNeighborsPacketDataTest { | ||
@Test | ||
public void serializeDeserialize() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
final FindNeighborsPacketData packet = FindNeighborsPacketData.create(target); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(time); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeBytesValue(target); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
final BytesValue encoded = out.encoded(); | ||
|
||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_withExtraFields() { | ||
final long time = System.currentTimeMillis(); | ||
final BytesValue target = Peer.randomId(); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeBytesValue(target); | ||
out.writeLongScalar(time); | ||
// Add extra list elements | ||
out.writeLong(123L); | ||
out.endList(); | ||
final BytesValue encoded = out.encoded(); | ||
|
||
final FindNeighborsPacketData deserialized = | ||
FindNeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getTarget()).isEqualTo(target); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...t/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/NeighborsPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static tech.pegasys.pantheon.ethereum.p2p.peers.PeerTestHelper.enode; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.discovery.DiscoveryPeer; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class NeighborsPacketDataTest { | ||
|
||
@Test | ||
public void serializeDeserialize() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
final NeighborsPacketData packet = NeighborsPacketData.create(peers); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(time); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeList(peers, DiscoveryPeer::writeTo); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
BytesValue encoded = out.encoded(); | ||
|
||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_extraFields() { | ||
final long time = System.currentTimeMillis(); | ||
final List<DiscoveryPeer> peers = | ||
Arrays.asList(DiscoveryPeer.fromEnode(enode()), DiscoveryPeer.fromEnode(enode())); | ||
|
||
BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeList(peers, DiscoveryPeer::writeTo); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
BytesValue encoded = out.encoded(); | ||
|
||
final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(encoded)); | ||
assertThat(deserialized.getNodes()).isEqualTo(peers); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...c/test/java/tech/pegasys/pantheon/ethereum/p2p/discovery/internal/PingPacketDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.ethereum.p2p.discovery.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import tech.pegasys.pantheon.ethereum.p2p.discovery.Endpoint; | ||
import tech.pegasys.pantheon.ethereum.rlp.BytesValueRLPOutput; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import org.junit.Test; | ||
|
||
public class PingPacketDataTest { | ||
|
||
@Test | ||
public void serializeDeserialize() { | ||
final long currentTime = System.currentTimeMillis(); | ||
|
||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final PingPacketData packet = PingPacketData.create(from, to); | ||
final BytesValue serialized = RLP.encode(packet::writeTo); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isGreaterThan(currentTime); | ||
} | ||
|
||
@Test | ||
public void readFrom() { | ||
final int version = 4; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_withExtraFields() { | ||
final int version = 4; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
// Add extra field | ||
out.writeLongScalar(11); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
|
||
@Test | ||
public void readFrom_unknownVersion() { | ||
final int version = 99; | ||
final Endpoint from = new Endpoint("127.0.0.1", 30303, OptionalInt.of(30303)); | ||
final Endpoint to = new Endpoint("127.0.0.2", 30303, OptionalInt.empty()); | ||
final long time = System.currentTimeMillis(); | ||
|
||
final BytesValueRLPOutput out = new BytesValueRLPOutput(); | ||
out.startList(); | ||
out.writeIntScalar(version); | ||
from.encodeStandalone(out); | ||
to.encodeStandalone(out); | ||
out.writeLongScalar(time); | ||
out.endList(); | ||
|
||
final BytesValue serialized = out.encoded(); | ||
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized)); | ||
|
||
assertThat(deserialized.getFrom()).isEqualTo(from); | ||
assertThat(deserialized.getTo()).isEqualTo(to); | ||
assertThat(deserialized.getExpiration()).isEqualTo(time); | ||
} | ||
} |
Oops, something went wrong.