Skip to content

Commit 87f44c1

Browse files
committed
Split TestCustomMessage into Request and Response
This will allow for testing onion message replies.
1 parent 68bde15 commit 87f44c1

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,31 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
7070
}
7171

7272
#[derive(Clone)]
73-
struct TestCustomMessage {}
73+
enum TestCustomMessage {
74+
Request,
75+
Response,
76+
}
7477

75-
const CUSTOM_MESSAGE_TYPE: u64 = 4242;
76-
const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
78+
const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
79+
const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
80+
const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
81+
const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
7782

7883
impl CustomOnionMessageContents for TestCustomMessage {
7984
fn tlv_type(&self) -> u64 {
80-
CUSTOM_MESSAGE_TYPE
85+
match self {
86+
TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
87+
TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
88+
}
8189
}
8290
}
8391

8492
impl Writeable for TestCustomMessage {
8593
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
86-
Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
94+
match self {
95+
TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
96+
TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
97+
}
8798
}
8899
}
89100

@@ -110,17 +121,27 @@ impl Drop for TestCustomMessageHandler {
110121

111122
impl CustomOnionMessageHandler for TestCustomMessageHandler {
112123
type CustomMessage = TestCustomMessage;
113-
fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
124+
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
114125
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
115-
None
126+
match msg {
127+
TestCustomMessage::Request => Some(TestCustomMessage::Response),
128+
TestCustomMessage::Response => None,
129+
}
116130
}
117131
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
118-
if message_type == CUSTOM_MESSAGE_TYPE {
119-
let buf = read_to_end(buffer)?;
120-
assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS);
121-
return Ok(Some(TestCustomMessage {}))
132+
match message_type {
133+
CUSTOM_REQUEST_MESSAGE_TYPE => {
134+
let buf = read_to_end(buffer)?;
135+
assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
136+
Ok(Some(TestCustomMessage::Request))
137+
},
138+
CUSTOM_RESPONSE_MESSAGE_TYPE => {
139+
let buf = read_to_end(buffer)?;
140+
assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
141+
Ok(Some(TestCustomMessage::Response))
142+
},
143+
_ => Ok(None),
122144
}
123-
Ok(None)
124145
}
125146
}
126147

@@ -178,7 +199,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
178199
#[test]
179200
fn one_hop() {
180201
let nodes = create_nodes(2);
181-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
202+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
182203

183204
let path = OnionMessagePath {
184205
intermediate_nodes: vec![],
@@ -191,7 +212,7 @@ fn one_hop() {
191212
#[test]
192213
fn two_unblinded_hops() {
193214
let nodes = create_nodes(3);
194-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
215+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
195216

196217
let path = OnionMessagePath {
197218
intermediate_nodes: vec![nodes[1].get_node_pk()],
@@ -204,7 +225,7 @@ fn two_unblinded_hops() {
204225
#[test]
205226
fn two_unblinded_two_blinded() {
206227
let nodes = create_nodes(5);
207-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
228+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
208229

209230
let secp_ctx = Secp256k1::new();
210231
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -220,7 +241,7 @@ fn two_unblinded_two_blinded() {
220241
#[test]
221242
fn three_blinded_hops() {
222243
let nodes = create_nodes(4);
223-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
244+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
224245

225246
let secp_ctx = Secp256k1::new();
226247
let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -237,7 +258,7 @@ fn three_blinded_hops() {
237258
fn too_big_packet_error() {
238259
// Make sure we error as expected if a packet is too big to send.
239260
let nodes = create_nodes(2);
240-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
261+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
241262

242263
let hop_node_id = nodes[1].get_node_pk();
243264
let hops = vec![hop_node_id; 400];
@@ -254,7 +275,7 @@ fn we_are_intro_node() {
254275
// If we are sending straight to a blinded path and we are the introduction node, we need to
255276
// advance the blinded path by 1 hop so the second hop is the new introduction node.
256277
let mut nodes = create_nodes(3);
257-
let test_msg = TestCustomMessage {};
278+
let test_msg = TestCustomMessage::Response;
258279

259280
let secp_ctx = Secp256k1::new();
260281
let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
@@ -281,7 +302,7 @@ fn we_are_intro_node() {
281302
fn invalid_blinded_path_error() {
282303
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
283304
let nodes = create_nodes(3);
284-
let test_msg = TestCustomMessage {};
305+
let test_msg = TestCustomMessage::Response;
285306

286307
// 0 hops
287308
let secp_ctx = Secp256k1::new();
@@ -309,7 +330,7 @@ fn invalid_blinded_path_error() {
309330
#[test]
310331
fn reply_path() {
311332
let nodes = create_nodes(4);
312-
let test_msg = TestCustomMessage {};
333+
let test_msg = TestCustomMessage::Response;
313334
let secp_ctx = Secp256k1::new();
314335

315336
// Destination::Node
@@ -368,7 +389,7 @@ fn invalid_custom_message_type() {
368389
#[test]
369390
fn peer_buffer_full() {
370391
let nodes = create_nodes(2);
371-
let test_msg = TestCustomMessage {};
392+
let test_msg = TestCustomMessage::Response;
372393
let path = OnionMessagePath {
373394
intermediate_nodes: vec![],
374395
destination: Destination::Node(nodes[1].get_node_pk()),
@@ -386,7 +407,7 @@ fn many_hops() {
386407
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
387408
let num_nodes: usize = 25;
388409
let nodes = create_nodes(num_nodes as u8);
389-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
410+
let test_msg = TestCustomMessage::Response;
390411

391412
let mut intermediate_nodes = vec![];
392413
for i in 1..(num_nodes-1) {
@@ -397,6 +418,6 @@ fn many_hops() {
397418
intermediate_nodes,
398419
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
399420
};
400-
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
421+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
401422
pass_along_path(&nodes);
402423
}

0 commit comments

Comments
 (0)