Skip to content

Commit 742a504

Browse files
committed
Split TestCustomMessage into Request and Response
1 parent 2caccac commit 742a504

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

lightning/src/onion_message/functional_tests.rs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,60 @@ impl ResponseErrorHandler 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

8495
struct TestCustomMessageHandler {}
8596

8697
impl CustomOnionMessageHandler for TestCustomMessageHandler {
8798
type CustomMessage = TestCustomMessage;
88-
fn handle_custom_message(&self, _msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
89-
None
99+
fn handle_custom_message(&self, msg: Self::CustomMessage) -> Option<Self::CustomMessage> {
100+
match msg {
101+
TestCustomMessage::Request => Some(TestCustomMessage::Response),
102+
TestCustomMessage::Response => None,
103+
}
90104
}
91105
fn read_custom_message<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, DecodeError> where Self: Sized {
92-
if message_type == CUSTOM_MESSAGE_TYPE {
93-
let mut buf = Vec::new();
94-
buffer.read_to_end(&mut buf)?;
95-
assert_eq!(buf, CUSTOM_MESSAGE_CONTENTS);
96-
return Ok(Some(TestCustomMessage {}))
106+
match message_type {
107+
CUSTOM_REQUEST_MESSAGE_TYPE => {
108+
let mut buf = Vec::new();
109+
buffer.read_to_end(&mut buf)?;
110+
assert_eq!(buf, CUSTOM_REQUEST_MESSAGE_CONTENTS);
111+
Ok(Some(TestCustomMessage::Request))
112+
},
113+
CUSTOM_RESPONSE_MESSAGE_TYPE => {
114+
let mut buf = Vec::new();
115+
buffer.read_to_end(&mut buf)?;
116+
assert_eq!(buf, CUSTOM_RESPONSE_MESSAGE_CONTENTS);
117+
Ok(Some(TestCustomMessage::Response))
118+
},
119+
_ => Ok(None),
97120
}
98-
Ok(None)
99121
}
100122
}
101123

@@ -155,7 +177,7 @@ fn pass_along_path(path: &Vec<MessengerNode>, expected_path_id: Option<[u8; 32]>
155177
#[test]
156178
fn one_hop() {
157179
let nodes = create_nodes(2);
158-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
180+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
159181

160182
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), test_msg, None).unwrap();
161183
pass_along_path(&nodes, None);
@@ -164,7 +186,7 @@ fn one_hop() {
164186
#[test]
165187
fn two_unblinded_hops() {
166188
let nodes = create_nodes(3);
167-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
189+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
168190

169191
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk()], Destination::Node(nodes[2].get_node_pk()), test_msg, None).unwrap();
170192
pass_along_path(&nodes, None);
@@ -173,7 +195,7 @@ fn two_unblinded_hops() {
173195
#[test]
174196
fn two_unblinded_two_blinded() {
175197
let nodes = create_nodes(5);
176-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
198+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
177199

178200
let secp_ctx = Secp256k1::new();
179201
let blinded_path = BlindedPath::new(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
@@ -185,7 +207,7 @@ fn two_unblinded_two_blinded() {
185207
#[test]
186208
fn three_blinded_hops() {
187209
let nodes = create_nodes(4);
188-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
210+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
189211

190212
let secp_ctx = Secp256k1::new();
191213
let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
@@ -198,7 +220,7 @@ fn three_blinded_hops() {
198220
fn too_big_packet_error() {
199221
// Make sure we error as expected if a packet is too big to send.
200222
let nodes = create_nodes(2);
201-
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
223+
let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
202224

203225
let hop_node_id = nodes[1].get_node_pk();
204226
let hops = [hop_node_id; 400];
@@ -211,7 +233,7 @@ fn we_are_intro_node() {
211233
// If we are sending straight to a blinded path and we are the introduction node, we need to
212234
// advance the blinded path by 1 hop so the second hop is the new introduction node.
213235
let mut nodes = create_nodes(3);
214-
let test_msg = TestCustomMessage {};
236+
let test_msg = TestCustomMessage::Response;
215237

216238
let secp_ctx = Secp256k1::new();
217239
let blinded_path = BlindedPath::new(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
@@ -230,7 +252,7 @@ fn we_are_intro_node() {
230252
fn invalid_blinded_path_error() {
231253
// Make sure we error as expected if a provided blinded path has 0 or 1 hops.
232254
let nodes = create_nodes(3);
233-
let test_msg = TestCustomMessage {};
255+
let test_msg = TestCustomMessage::Response;
234256

235257
// 0 hops
236258
let secp_ctx = Secp256k1::new();
@@ -250,7 +272,7 @@ fn invalid_blinded_path_error() {
250272
#[test]
251273
fn reply_path() {
252274
let nodes = create_nodes(4);
253-
let test_msg = TestCustomMessage {};
275+
let test_msg = TestCustomMessage::Response;
254276
let secp_ctx = Secp256k1::new();
255277

256278
// Destination::Node
@@ -297,7 +319,7 @@ fn invalid_custom_message_type() {
297319
#[test]
298320
fn peer_buffer_full() {
299321
let nodes = create_nodes(2);
300-
let test_msg = TestCustomMessage {};
322+
let test_msg = TestCustomMessage::Response;
301323
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
302324
nodes[0].messenger.send_onion_message(&[], Destination::Node(nodes[1].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
303325
}

0 commit comments

Comments
 (0)