Skip to content

Commit 6fb7857

Browse files
committed
Split TestCustomMessage into Request and Response
This will allow for testing onion message replies.
1 parent 3078059 commit 6fb7857

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
@@ -68,20 +68,31 @@ impl ResponseErrorHandler for TestOffersMessageHandler {
6868
}
6969

7070
#[derive(Clone)]
71-
struct TestCustomMessage {}
71+
enum TestCustomMessage {
72+
Request,
73+
Response,
74+
}
7275

73-
const CUSTOM_MESSAGE_TYPE: u64 = 4242;
74-
const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
76+
const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
77+
const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
78+
const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
79+
const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
7580

7681
impl CustomOnionMessageContents for TestCustomMessage {
7782
fn tlv_type(&self) -> u64 {
78-
CUSTOM_MESSAGE_TYPE
83+
match self {
84+
TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
85+
TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
86+
}
7987
}
8088
}
8189

8290
impl Writeable for TestCustomMessage {
8391
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
84-
Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
92+
match self {
93+
TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
94+
TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
95+
}
8596
}
8697
}
8798

@@ -108,17 +119,27 @@ impl Drop for TestCustomMessageHandler {
108119

109120
impl CustomOnionMessageHandler for TestCustomMessageHandler {
110121
type CustomMessage = TestCustomMessage;
111-
fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
122+
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
112123
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
113-
None
124+
match msg {
125+
TestCustomMessage::Request => Some(TestCustomMessage::Response),
126+
TestCustomMessage::Response => None,
127+
}
114128
}
115129
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
116-
if message_type == CUSTOM_MESSAGE_TYPE {
117-
let buf = read_to_end(buffer)?;
118-
assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS);
119-
return Ok(Some(TestCustomMessage {}))
130+
match message_type {
131+
CUSTOM_REQUEST_MESSAGE_TYPE => {
132+
let buf = read_to_end(buffer)?;
133+
assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
134+
Ok(Some(TestCustomMessage::Request))
135+
},
136+
CUSTOM_RESPONSE_MESSAGE_TYPE => {
137+
let buf = read_to_end(buffer)?;
138+
assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
139+
Ok(Some(TestCustomMessage::Response))
140+
},
141+
_ => Ok(None),
120142
}
121-
Ok(None)
122143
}
123144
}
124145

@@ -176,7 +197,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
176197
#[test]
177198
fn one_hop() {
178199
let nodes = create_nodes(2);
179-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
200+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
180201

181202
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
182203
pass_along_path(&nodes);
@@ -185,7 +206,7 @@ fn one_hop() {
185206
#[test]
186207
fn two_unblinded_hops() {
187208
let nodes = create_nodes(3);
188-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
209+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
189210

190211
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
191212
pass_along_path(&nodes);
@@ -194,7 +215,7 @@ fn two_unblinded_hops() {
194215
#[test]
195216
fn two_unblinded_two_blinded() {
196217
let nodes = create_nodes(5);
197-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
218+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
198219

199220
let secp_ctx = Secp256k1::new();
200221
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -206,7 +227,7 @@ fn two_unblinded_two_blinded() {
206227
#[test]
207228
fn three_blinded_hops() {
208229
let nodes = create_nodes(4);
209-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
230+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
210231

211232
let secp_ctx = Secp256k1::new();
212233
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();
@@ -219,7 +240,7 @@ fn three_blinded_hops() {
219240
fn too_big_packet_error() {
220241
// Make sure we error as expected if a packet is too big to send.
221242
let nodes = create_nodes(2);
222-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
243+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
223244

224245
let hop_node_id = nodes[1].get_node_pk();
225246
let hops = [hop_node_id; 400];
@@ -232,7 +253,7 @@ fn we_are_intro_node() {
232253
// If we are sending straight to a blinded path and we are the introduction node, we need to
233254
// advance the blinded path by 1 hop so the second hop is the new introduction node.
234255
let mut nodes = create_nodes(3);
235-
let test_msg = TestCustomMessage {};
256+
let test_msg = TestCustomMessage::Response;
236257

237258
let secp_ctx = Secp256k1::new();
238259
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();
@@ -251,7 +272,7 @@ fn we_are_intro_node() {
251272
fn invalid_blinded_path_error() {
252273
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
253274
let nodes = create_nodes(3);
254-
let test_msg = TestCustomMessage {};
275+
let test_msg = TestCustomMessage::Response;
255276

256277
// 0 hops
257278
let secp_ctx = Secp256k1::new();
@@ -271,7 +292,7 @@ fn invalid_blinded_path_error() {
271292
#[test]
272293
fn reply_path() {
273294
let nodes = create_nodes(4);
274-
let test_msg = TestCustomMessage {};
295+
let test_msg = TestCustomMessage::Response;
275296
let secp_ctx = Secp256k1::new();
276297

277298
// Destination::Node
@@ -318,7 +339,7 @@ fn invalid_custom_message_type() {
318339
#[test]
319340
fn peer_buffer_full() {
320341
let nodes = create_nodes(2);
321-
let test_msg = TestCustomMessage {};
342+
let test_msg = TestCustomMessage::Response;
322343
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
323344
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
324345
}
@@ -332,13 +353,13 @@ fn many_hops() {
332353
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
333354
let num_nodes: usize = 25;
334355
let nodes = create_nodes(num_nodes as u8);
335-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
356+
let test_msg = TestCustomMessage::Response;
336357

337358
let mut intermediates = vec![];
338359
for i in 1..(num_nodes-1) {
339360
intermediates.push(nodes[i].get_node_pk());
340361
}
341362

342-
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), test_msg, None).unwrap();
363+
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap();
343364
pass_along_path(&nodes);
344365
}

0 commit comments

Comments
 (0)