|
| 1 | +/// Test to demonstrate the RelLockTime issue in dsl.rs |
| 2 | +/// |
| 3 | +/// The issue: older() macro uses .expect() on RelLockTime::from_consensus(), |
| 4 | +/// which can panic if given an invalid value (> 16,777,215). |
| 5 | +/// |
| 6 | +/// This file shows the problem and how to catch it. |
| 7 | +use bdk_wallet::descriptor; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_older_with_invalid_rellocktime_too_large() { |
| 11 | + // Value with high bit set causes RelLockTime::from_consensus() to fail |
| 12 | + let invalid_value = 0x80000000; // 2147483648 |
| 13 | + |
| 14 | + // This should now return an error instead of panicking |
| 15 | + let result = descriptor!(wsh(older(invalid_value))); |
| 16 | + assert!( |
| 17 | + result.is_err(), |
| 18 | + "Invalid RelLockTime {} should return an error", |
| 19 | + invalid_value |
| 20 | + ); |
| 21 | + |
| 22 | + // Check that it's the right kind of error |
| 23 | + if let Err(descriptor::DescriptorError::RelLockTime(_)) = result { |
| 24 | + // Good, it's the expected error type |
| 25 | + } else { |
| 26 | + panic!("Expected RelLockTime error, got {:?}", result); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[test] |
| 31 | +fn test_older_with_valid_rellocktime_max() { |
| 32 | + // Max valid value is 16,777,215 (0xFFFFFF) |
| 33 | + let max_valid_value = 16_777_215; |
| 34 | + let result = descriptor!(wsh(older(max_valid_value))); |
| 35 | + assert!(result.is_ok(), "Max valid RelLockTime should work"); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn test_older_with_valid_rellocktime_min() { |
| 40 | + // Min valid value is 1 (0 is not valid for RelLockTime) |
| 41 | + let min_value = 1; |
| 42 | + |
| 43 | + let result = descriptor!(wsh(older(min_value))); |
| 44 | + assert!(result.is_ok(), "Min valid RelLockTime should work"); |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn test_older_with_valid_common_values() { |
| 49 | + // Common usage: blocks or seconds |
| 50 | + let test_cases = vec![ |
| 51 | + 1, // 1 block/second |
| 52 | + 144, // ~1 day in blocks |
| 53 | + 1000, // Common value |
| 54 | + 65535, // Max 16-bit value (common for CSV) |
| 55 | + 4_209_713, // Valid but large |
| 56 | + ]; |
| 57 | + |
| 58 | + for value in test_cases { |
| 59 | + let result = descriptor!(wsh(older(value))); |
| 60 | + assert!(result.is_ok(), "Valid RelLockTime {} should work", value); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Alternative: Show how proper error handling should look |
| 65 | +#[test] |
| 66 | +fn test_demonstrate_proper_error_handling() { |
| 67 | + // This is what the fix should look like - proper Result handling |
| 68 | + // instead of .expect() which panics |
| 69 | + |
| 70 | + use miniscript::RelLockTime; |
| 71 | + |
| 72 | + // Valid case |
| 73 | + match RelLockTime::from_consensus(144) { |
| 74 | + Ok(lock_time) => println!("Valid: {:?}", lock_time), |
| 75 | + Err(e) => println!("Invalid: {}", e), |
| 76 | + } |
| 77 | + |
| 78 | + // Invalid case - properly handled |
| 79 | + match RelLockTime::from_consensus(16_777_216) { |
| 80 | + Ok(lock_time) => println!("Valid: {:?}", lock_time), |
| 81 | + Err(e) => { |
| 82 | + println!("Invalid RelLockTime value: {}", e); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn test_rel_lock_time_error() { |
| 89 | + // Create a RelLockTimeError by trying to create an invalid RelLockTime |
| 90 | + let invalid_value = 0x80000000u32; // Value that causes RelLockTime::from_consensus to fail |
| 91 | + let rel_lock_time_result = miniscript::RelLockTime::from_consensus(invalid_value); |
| 92 | + assert!(rel_lock_time_result.is_err()); |
| 93 | + let rel_lock_time_error = rel_lock_time_result.unwrap_err(); |
| 94 | + |
| 95 | + // Test the From impl |
| 96 | + let error: descriptor::DescriptorError = rel_lock_time_error.into(); |
| 97 | + assert!(matches!(error, descriptor::DescriptorError::RelLockTime(_))); |
| 98 | + |
| 99 | + // Test the Display impl |
| 100 | + let display_string = format!("{}", error); |
| 101 | + assert!(display_string.starts_with("RelLockTime error:")); |
| 102 | +} |
0 commit comments