Skip to content

Commit 74e6b57

Browse files
committed
Refactor attribution data mutation test
Prepares for a DropAttributionData mutation.
1 parent 8aae34e commit 74e6b57

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,10 +3122,16 @@ mod tests {
31223122
const FAILURE_MESSAGE_LEN: usize = 1060;
31233123

31243124
for mutating_node in 0..5 {
3125-
for mutated_index in
3126-
0..FAILURE_MESSAGE_LEN + HOLD_TIME_LEN * MAX_HOPS + HMAC_LEN * HMAC_COUNT
3125+
let attribution_data_mutations = (0..HOLD_TIME_LEN * MAX_HOPS)
3126+
.map(AttributionDataMutationType::HoldTimes)
3127+
.chain((0..HMAC_LEN * HMAC_COUNT).map(AttributionDataMutationType::Hmacs));
3128+
3129+
let failure_mutations = (0..FAILURE_MESSAGE_LEN).map(MutationType::FailureMessage);
3130+
3131+
for mutation_type in failure_mutations
3132+
.chain(attribution_data_mutations.map(MutationType::AttributionData))
31273133
{
3128-
let mutation = Mutation { node: mutating_node, index: mutated_index };
3134+
let mutation = Mutation { node: mutating_node, mutation_type };
31293135
let decrypted_failure =
31303136
test_attributable_failure_packet_onion_with_mutation(Some(mutation));
31313137

@@ -3151,9 +3157,19 @@ mod tests {
31513157
assert_eq!(decrypted_failure.hold_times, [5, 4, 3, 2, 1]);
31523158
}
31533159

3160+
enum AttributionDataMutationType {
3161+
HoldTimes(usize),
3162+
Hmacs(usize),
3163+
}
3164+
3165+
enum MutationType {
3166+
FailureMessage(usize),
3167+
AttributionData(AttributionDataMutationType),
3168+
}
3169+
31543170
struct Mutation {
31553171
node: usize,
3156-
index: usize,
3172+
mutation_type: MutationType,
31573173
}
31583174

31593175
fn test_attributable_failure_packet_onion_with_mutation(
@@ -3222,24 +3238,26 @@ mod tests {
32223238
EXPECTED_MESSAGES[0].assert_eq(&onion_error);
32233239

32243240
let mut mutated = false;
3225-
let mutate_packet = |packet: &mut OnionErrorPacket, mutated_index| {
3226-
let data_len = packet.data.len();
3227-
if mutated_index < data_len {
3228-
// Mutate legacy failure message.
3229-
packet.data[mutated_index] ^= 1;
3230-
} else if mutated_index < data_len + HOLD_TIME_LEN * MAX_HOPS {
3231-
// Mutate hold times.
3232-
packet.attribution_data.as_mut().unwrap().hold_times[mutated_index - data_len] ^= 1;
3233-
} else {
3234-
// Mutate HMACs.
3235-
packet.attribution_data.as_mut().unwrap().hmacs
3236-
[mutated_index - data_len - HOLD_TIME_LEN * MAX_HOPS] ^= 1;
3241+
let mutate_packet = |packet: &mut OnionErrorPacket, mutation_type: &MutationType| {
3242+
match mutation_type {
3243+
MutationType::FailureMessage(i) => {
3244+
// Mutate legacy failure message.
3245+
packet.data[*i] ^= 1;
3246+
},
3247+
MutationType::AttributionData(AttributionDataMutationType::HoldTimes(i)) => {
3248+
// Mutate hold times.
3249+
packet.attribution_data.as_mut().unwrap().hold_times[*i] ^= 1;
3250+
},
3251+
MutationType::AttributionData(AttributionDataMutationType::Hmacs(i)) => {
3252+
// Mutate hold times.
3253+
packet.attribution_data.as_mut().unwrap().hmacs[*i] ^= 1;
3254+
},
32373255
}
32383256
};
32393257

3240-
if let Some(Mutation { node, index }) = mutation {
3258+
if let Some(Mutation { node, ref mutation_type }) = mutation {
32413259
if node == 4 {
3242-
mutate_packet(&mut onion_error, index);
3260+
mutate_packet(&mut onion_error, mutation_type);
32433261
mutated = true;
32443262
}
32453263
}
@@ -3250,9 +3268,9 @@ mod tests {
32503268
process_failure_packet(&mut onion_error, shared_secret, hold_time);
32513269
super::crypt_failure_packet(shared_secret, &mut onion_error);
32523270

3253-
if let Some(Mutation { node, index }) = mutation {
3271+
if let Some(Mutation { node, ref mutation_type }) = mutation {
32543272
if node == idx {
3255-
mutate_packet(&mut onion_error, index);
3273+
mutate_packet(&mut onion_error, mutation_type);
32563274
mutated = true;
32573275
}
32583276
}

0 commit comments

Comments
 (0)