Skip to content

Commit 3beed62

Browse files
committed
Split TestCustomMessage into Request and Response
This will allow for testing onion message replies.
1 parent 80c98a5 commit 3beed62

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

lightning/src/onion_message/functional_tests.rs

+44-23
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,31 @@ impl OffersMessageHandler for TestOffersMessageHandler {
6464
}
6565

6666
#[derive(Clone)]
67-
struct TestCustomMessage {}
67+
enum TestCustomMessage {
68+
Request,
69+
Response,
70+
}
6871

69-
const CUSTOM_MESSAGE_TYPE: u64 = 4242;
70-
const CUSTOM_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
72+
const CUSTOM_REQUEST_MESSAGE_TYPE: u64 = 4242;
73+
const CUSTOM_RESPONSE_MESSAGE_TYPE: u64 = 4343;
74+
const CUSTOM_REQUEST_MESSAGE_CONTENTS: [u8; 32] = [42; 32];
75+
const CUSTOM_RESPONSE_MESSAGE_CONTENTS: [u8; 32] = [43; 32];
7176

7277
impl CustomOnionMessageContents for TestCustomMessage {
7378
fn tlv_type(&self) -> u64 {
74-
CUSTOM_MESSAGE_TYPE
79+
match self {
80+
TestCustomMessage::Request => CUSTOM_REQUEST_MESSAGE_TYPE,
81+
TestCustomMessage::Response => CUSTOM_RESPONSE_MESSAGE_TYPE,
82+
}
7583
}
7684
}
7785

7886
impl Writeable for TestCustomMessage {
7987
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
80-
Ok(CUSTOM_MESSAGE_CONTENTS.write(w)?)
88+
match self {
89+
TestCustomMessage::Request => Ok(CUSTOM_REQUEST_MESSAGE_CONTENTS.write(w)?),
90+
TestCustomMessage::Response => Ok(CUSTOM_RESPONSE_MESSAGE_CONTENTS.write(w)?),
91+
}
8192
}
8293
}
8394

@@ -104,17 +115,27 @@ impl Drop for TestCustomMessageHandler {
104115

105116
impl CustomOnionMessageHandler for TestCustomMessageHandler {
106117
type CustomMessage = TestCustomMessage;
107-
fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
118+
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
108119
self.num_messages_expected.fetch_sub(1, Ordering::SeqCst);
109-
None
120+
match msg {
121+
TestCustomMessage::Request => Some(TestCustomMessage::Response),
122+
TestCustomMessage::Response => None,
123+
}
110124
}
111125
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
112-
if message_type == CUSTOM_MESSAGE_TYPE {
113-
let buf = read_to_end(buffer)?;
114-
assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS);
115-
return Ok(Some(TestCustomMessage {}))
126+
match message_type {
127+
CUSTOM_REQUEST_MESSAGE_TYPE => {
128+
let buf = read_to_end(buffer)?;
129+
assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
130+
Ok(Some(TestCustomMessage::Request))
131+
},
132+
CUSTOM_RESPONSE_MESSAGE_TYPE => {
133+
let buf = read_to_end(buffer)?;
134+
assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
135+
Ok(Some(TestCustomMessage::Response))
136+
},
137+
_ => Ok(None),
116138
}
117-
Ok(None)
118139
}
119140
}
120141

@@ -166,7 +187,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
166187
#[test]
167188
fn one_hop() {
168189
let nodes = create_nodes(2);
169-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
190+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
170191

171192
let path = OnionMessagePath {
172193
intermediate_nodes: vec![],
@@ -179,7 +200,7 @@ fn one_hop() {
179200
#[test]
180201
fn two_unblinded_hops() {
181202
let nodes = create_nodes(3);
182-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
203+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
183204

184205
let path = OnionMessagePath {
185206
intermediate_nodes: vec![nodes[1].get_node_pk()],
@@ -192,7 +213,7 @@ fn two_unblinded_hops() {
192213
#[test]
193214
fn two_unblinded_two_blinded() {
194215
let nodes = create_nodes(5);
195-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
216+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
196217

197218
let secp_ctx = Secp256k1::new();
198219
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();
@@ -225,7 +246,7 @@ fn three_blinded_hops() {
225246
fn too_big_packet_error() {
226247
// Make sure we error as expected if a packet is too big to send.
227248
let nodes = create_nodes(2);
228-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
249+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
229250

230251
let hop_node_id = nodes[1].get_node_pk();
231252
let hops = vec![hop_node_id; 400];
@@ -242,7 +263,7 @@ fn we_are_intro_node() {
242263
// If we are sending straight to a blinded path and we are the introduction node, we need to
243264
// advance the blinded path by 1 hop so the second hop is the new introduction node.
244265
let mut nodes = create_nodes(3);
245-
let test_msg = TestCustomMessage {};
266+
let test_msg = TestCustomMessage::Response;
246267

247268
let secp_ctx = Secp256k1::new();
248269
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();
@@ -269,7 +290,7 @@ fn we_are_intro_node() {
269290
fn invalid_blinded_path_error() {
270291
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
271292
let nodes = create_nodes(3);
272-
let test_msg = TestCustomMessage {};
293+
let test_msg = TestCustomMessage::Response;
273294

274295
// 0 hops
275296
let secp_ctx = Secp256k1::new();
@@ -297,7 +318,7 @@ fn invalid_blinded_path_error() {
297318
#[test]
298319
fn reply_path() {
299320
let nodes = create_nodes(4);
300-
let test_msg = TestCustomMessage {};
321+
let test_msg = TestCustomMessage::Response;
301322
let secp_ctx = Secp256k1::new();
302323

303324
// Destination::Node
@@ -356,7 +377,7 @@ fn invalid_custom_message_type() {
356377
#[test]
357378
fn peer_buffer_full() {
358379
let nodes = create_nodes(2);
359-
let test_msg = TestCustomMessage {};
380+
let test_msg = TestCustomMessage::Response;
360381
let path = OnionMessagePath {
361382
intermediate_nodes: vec![],
362383
destination: Destination::Node(nodes[1].get_node_pk()),
@@ -374,7 +395,7 @@ fn many_hops() {
374395
// of size [`crate::onion_message::packet::BIG_PACKET_HOP_DATA_LEN`].
375396
let num_nodes: usize = 25;
376397
let nodes = create_nodes(num_nodes as u8);
377-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
398+
let test_msg = TestCustomMessage::Response;
378399

379400
let mut intermediate_nodes = vec![];
380401
for i in 1..(num_nodes-1) {
@@ -385,6 +406,6 @@ fn many_hops() {
385406
intermediate_nodes,
386407
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
387408
};
388-
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
409+
nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
389410
pass_along_path(&nodes);
390411
}

0 commit comments

Comments
 (0)