Skip to content

Commit 973394c

Browse files
committed
Split TestCustomMessage into Request and Response
This will allow for testing onion message replies.
1 parent 2a9b024 commit 973394c

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
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
184205
pass_along_path(&nodes);
@@ -187,7 +208,7 @@ fn one_hop() {
187208
#[test]
188209
fn two_unblinded_hops() {
189210
let nodes = create_nodes(3);
190-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
211+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
191212

192213
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
193214
pass_along_path(&nodes);
@@ -196,7 +217,7 @@ fn two_unblinded_hops() {
196217
#[test]
197218
fn two_unblinded_two_blinded() {
198219
let nodes = create_nodes(5);
199-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
220+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
200221

201222
let secp_ctx = Secp256k1::new();
202223
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -208,7 +229,7 @@ fn two_unblinded_two_blinded() {
208229
#[test]
209230
fn three_blinded_hops() {
210231
let nodes = create_nodes(4);
211-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
232+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
212233

213234
let secp_ctx = Secp256k1::new();
214235
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();
@@ -221,7 +242,7 @@ fn three_blinded_hops() {
221242
fn too_big_packet_error() {
222243
// Make sure we error as expected if a packet is too big to send.
223244
let nodes = create_nodes(2);
224-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
245+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
225246

226247
let hop_node_id = nodes[1].get_node_pk();
227248
let hops = [hop_node_id; 400];
@@ -234,7 +255,7 @@ fn we_are_intro_node() {
234255
// If we are sending straight to a blinded path and we are the introduction node, we need to
235256
// advance the blinded path by 1 hop so the second hop is the new introduction node.
236257
let mut nodes = create_nodes(3);
237-
let test_msg = TestCustomMessage {};
258+
let test_msg = TestCustomMessage::Response;
238259

239260
let secp_ctx = Secp256k1::new();
240261
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();
@@ -253,7 +274,7 @@ fn we_are_intro_node() {
253274
fn invalid_blinded_path_error() {
254275
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
255276
let nodes = create_nodes(3);
256-
let test_msg = TestCustomMessage {};
277+
let test_msg = TestCustomMessage::Response;
257278

258279
// 0 hops
259280
let secp_ctx = Secp256k1::new();
@@ -273,7 +294,7 @@ fn invalid_blinded_path_error() {
273294
#[test]
274295
fn reply_path() {
275296
let nodes = create_nodes(4);
276-
let test_msg = TestCustomMessage {};
297+
let test_msg = TestCustomMessage::Response;
277298
let secp_ctx = Secp256k1::new();
278299

279300
// Destination::Node
@@ -320,7 +341,7 @@ fn invalid_custom_message_type() {
320341
#[test]
321342
fn peer_buffer_full() {
322343
let nodes = create_nodes(2);
323-
let test_msg = TestCustomMessage {};
344+
let test_msg = TestCustomMessage::Response;
324345
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
325346
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
326347
}
@@ -334,13 +355,13 @@ fn many_hops() {
334355
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
335356
let num_nodes: usize = 25;
336357
let nodes = create_nodes(num_nodes as u8);
337-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
358+
let test_msg = TestCustomMessage::Response;
338359

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

344-
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), test_msg, None).unwrap();
365+
nodes[0].messenger.send_onion_message(&intermediates, Destination::Node(nodes[num_nodes-1].get_node_pk()), OnionMessageContents::Custom(test_msg), None).unwrap();
345366
pass_along_path(&nodes);
346367
}

0 commit comments

Comments
 (0)