Skip to content

Commit 0ee0cbe

Browse files
committed
ETCM-167: Use the same examples for decoding as well.
1 parent 199e05d commit 0ee0cbe

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/test/scala/io/iohk/ethereum/network/discovery/codecs/RLPCodecsSpec.scala

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
8181

8282
// The following tests demonstrate what each payload looks like when encoded to RLP,
8383
// because the auto-derivation makes it opaque.
84-
abstract class EncodeFixture[T <: Payload: RLPEncoder: ClassTag] {
84+
abstract class RLPFixture[T <: Payload: RLPEncoder: RLPDecoder: ClassTag] {
8585
// Structrual equality checker for RLPEncodeable.
8686
// It has different wrappers for items based on whether it was hand crafted or generated
8787
// by codecs, and the RLPValue has mutable arrays inside.
@@ -104,11 +104,12 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
104104
def p: T
105105
def e: RLPEncodeable
106106

107-
def test = RLPEncoder.encode(p) should equal(e)
107+
def testEncode = RLPEncoder.encode(p) should equal(e)
108+
def testDecode = RLPDecoder.decode[T](e) should equal(p)
108109
}
109110

110111
val examples = List(
111-
new EncodeFixture[Payload.Ping] {
112+
new RLPFixture[Payload.Ping] {
112113
override val p = Payload.Ping(
113114
version = 4,
114115
from = Node.Address(localhost, 30000, 40000),
@@ -125,7 +126,7 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
125126
p.enrSeq.get
126127
)
127128
},
128-
new EncodeFixture[Payload.Pong] {
129+
new RLPFixture[Payload.Pong] {
129130
override val p = Payload.Pong(
130131
to = Node.Address(localhost, 30001, 0),
131132
pingHash = Hash(randomBytes(32)),
@@ -144,15 +145,15 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
144145
p.enrSeq.get
145146
)
146147
},
147-
new EncodeFixture[Payload.FindNode] {
148+
new RLPFixture[Payload.FindNode] {
148149
override val p = Payload.FindNode(
149150
target = PublicKey(randomBytes(64)),
150151
expiration = System.currentTimeMillis
151152
)
152153

153154
override val e = RLPList(p.target, p.expiration)
154155
},
155-
new EncodeFixture[Payload.Neighbors] {
156+
new RLPFixture[Payload.Neighbors] {
156157
override val p = Payload.Neighbors(
157158
nodes = List(
158159
Node(id = PublicKey(randomBytes(64)), address = Node.Address(localhost, 30001, 40001)),
@@ -169,7 +170,7 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
169170
p.expiration
170171
)
171172
},
172-
new EncodeFixture[Payload.ENRRequest] {
173+
new RLPFixture[Payload.ENRRequest] {
173174
override val p = Payload.ENRRequest(
174175
expiration = System.currentTimeMillis
175176
)
@@ -178,7 +179,7 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
178179
p.expiration
179180
)
180181
},
181-
new EncodeFixture[Payload.ENRResponse] {
182+
new RLPFixture[Payload.ENRResponse] {
182183
val (publicKey, privateKey) = sigalg.newKeyPair
183184
val node = Node(
184185
id = publicKey,
@@ -215,7 +216,11 @@ class RLPCodecsSpec extends AnyFlatSpec with Matchers {
215216

216217
examples.foreach { example =>
217218
it should s"encode the example ${example.name}" in {
218-
example.test
219+
example.testEncode
220+
}
221+
222+
it should s"decode the example ${example.name}" in {
223+
example.testDecode
219224
}
220225
}
221226
}

0 commit comments

Comments
 (0)