@@ -3122,10 +3122,16 @@ mod tests {
3122
3122
const FAILURE_MESSAGE_LEN : usize = 1060 ;
3123
3123
3124
3124
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 ) )
3127
3133
{
3128
- let mutation = Mutation { node : mutating_node, index : mutated_index } ;
3134
+ let mutation = Mutation { node : mutating_node, mutation_type } ;
3129
3135
let decrypted_failure =
3130
3136
test_attributable_failure_packet_onion_with_mutation ( Some ( mutation) ) ;
3131
3137
@@ -3151,9 +3157,19 @@ mod tests {
3151
3157
assert_eq ! ( decrypted_failure. hold_times, [ 5 , 4 , 3 , 2 , 1 ] ) ;
3152
3158
}
3153
3159
3160
+ enum AttributionDataMutationType {
3161
+ HoldTimes ( usize ) ,
3162
+ Hmacs ( usize ) ,
3163
+ }
3164
+
3165
+ enum MutationType {
3166
+ FailureMessage ( usize ) ,
3167
+ AttributionData ( AttributionDataMutationType ) ,
3168
+ }
3169
+
3154
3170
struct Mutation {
3155
3171
node : usize ,
3156
- index : usize ,
3172
+ mutation_type : MutationType ,
3157
3173
}
3158
3174
3159
3175
fn test_attributable_failure_packet_onion_with_mutation (
@@ -3222,24 +3238,26 @@ mod tests {
3222
3238
EXPECTED_MESSAGES [ 0 ] . assert_eq ( & onion_error) ;
3223
3239
3224
3240
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
+ } ,
3237
3255
}
3238
3256
} ;
3239
3257
3240
- if let Some ( Mutation { node, index } ) = mutation {
3258
+ if let Some ( Mutation { node, ref mutation_type } ) = mutation {
3241
3259
if node == 4 {
3242
- mutate_packet ( & mut onion_error, index ) ;
3260
+ mutate_packet ( & mut onion_error, mutation_type ) ;
3243
3261
mutated = true ;
3244
3262
}
3245
3263
}
@@ -3250,9 +3268,9 @@ mod tests {
3250
3268
process_failure_packet ( & mut onion_error, shared_secret, hold_time) ;
3251
3269
super :: crypt_failure_packet ( shared_secret, & mut onion_error) ;
3252
3270
3253
- if let Some ( Mutation { node, index } ) = mutation {
3271
+ if let Some ( Mutation { node, ref mutation_type } ) = mutation {
3254
3272
if node == idx {
3255
- mutate_packet ( & mut onion_error, index ) ;
3273
+ mutate_packet ( & mut onion_error, mutation_type ) ;
3256
3274
mutated = true ;
3257
3275
}
3258
3276
}
0 commit comments