Skip to content

Commit f84e1cf

Browse files
bigpoppaofnyazahjessica49-commitsBaskarayelu
authored
Add tests for the lock-time-limit guard (#2381)
* Add tests for the lock-time-limit guard * quick fix [ci-skip] * quick fix [ci-skip] --------- Co-authored-by: azahjessica49-commits <azahjessica49@gmail.com> Co-authored-by: Baskar <baskarayelu@gmail.com>
1 parent 0e1e4bd commit f84e1cf

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

quicklendx-contracts/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ mod test_evidence_kind_guard_matrix;
193193
#[cfg(all(test, feature = "legacy-tests"))]
194194
mod test_dispute_timeline_props;
195195
#[cfg(test)]
196+
mod test_due_date_guard;
197+
#[cfg(test)]
198+
mod test_lock_time_limit_guard;
199+
#[cfg(test)]
196200
// mod test_dispute_event_invariant;
197201
#[cfg(test)]
198202
mod test_dust_transfer;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//! Lock-time-limit guard — "Within, at limit, past".
2+
//!
3+
//! Pins the time-window validation in
4+
//! `ProtocolLimitsContract::validate_invoice` across three zones:
5+
//!
6+
//! | Zone | Lower bound | Upper bound |
7+
//! |------------|------------------------------------|------------------------------------|
8+
//! | **Within** | `due_date > current_time` | `due_date < max_due_date` |
9+
//! | **At limit** | `due_date == current_time` | `due_date == max_due_date` |
10+
//! | **Past** | `due_date < current_time` | `due_date > max_due_date` |
11+
//!
12+
//! Every test is gated only by `#[cfg(test)]` so it runs on every CI matrix
13+
//! entry, not just when the `legacy-tests` feature is enabled.
14+
15+
#![cfg(test)]
16+
17+
use crate::errors::QuickLendXError;
18+
use crate::protocol_limits::ProtocolLimitsContract;
19+
use crate::QuickLendXContract;
20+
use soroban_sdk::{
21+
testutils::{Address as _, Ledger},
22+
Address, Env,
23+
};
24+
25+
const SECONDS_PER_DAY: u64 = 86_400;
26+
27+
fn setup_env() -> (Env, Address) {
28+
let env = Env::default();
29+
env.mock_all_auths();
30+
env.ledger().set_timestamp(1_000_000);
31+
let contract_id = env.register(QuickLendXContract, ());
32+
(env, contract_id)
33+
}
34+
35+
fn valid_amount() -> i128 {
36+
100i128 // > DEFAULT_MIN_AMOUNT (10 in test builds)
37+
}
38+
39+
// ---------------------------------------------------------------------------
40+
// Lower bound: due_date <= current_time → InvoiceDueDateInvalid
41+
// ---------------------------------------------------------------------------
42+
43+
#[test]
44+
fn validate_invoice_rejects_due_date_past_lower_bound() {
45+
let (env, contract_id) = setup_env();
46+
let result = env.as_contract(&contract_id, || {
47+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), 999_999u64)
48+
});
49+
assert_eq!(result, Err(QuickLendXError::InvoiceDueDateInvalid));
50+
}
51+
52+
#[test]
53+
fn validate_invoice_rejects_due_date_at_lower_bound() {
54+
let (env, contract_id) = setup_env();
55+
let result = env.as_contract(&contract_id, || {
56+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), 1_000_000u64)
57+
});
58+
assert_eq!(result, Err(QuickLendXError::InvoiceDueDateInvalid));
59+
}
60+
61+
#[test]
62+
fn validate_invoice_accepts_due_date_within_lower_bound() {
63+
let (env, contract_id) = setup_env();
64+
let result = env.as_contract(&contract_id, || {
65+
// due_date = current_time + 1, the first valid second
66+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), 1_000_001u64)
67+
});
68+
assert_eq!(result, Ok(()));
69+
}
70+
71+
// ---------------------------------------------------------------------------
72+
// Upper bound: due_date > max_due_date → InvoiceDueDateInvalid
73+
// ---------------------------------------------------------------------------
74+
75+
#[test]
76+
fn validate_invoice_accepts_due_date_within_upper_bound() {
77+
let (env, contract_id) = setup_env();
78+
let one_day_before_max = env.ledger().timestamp()
79+
+ 365 * SECONDS_PER_DAY
80+
- 1;
81+
let result = env.as_contract(&contract_id, || {
82+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), one_day_before_max)
83+
});
84+
assert_eq!(result, Ok(()));
85+
}
86+
87+
#[test]
88+
fn validate_invoice_accepts_due_date_at_upper_bound() {
89+
let (env, contract_id) = setup_env();
90+
let max_due_date = env.ledger().timestamp() + 365 * SECONDS_PER_DAY;
91+
let result = env.as_contract(&contract_id, || {
92+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), max_due_date)
93+
});
94+
assert_eq!(result, Ok(()));
95+
}
96+
97+
#[test]
98+
fn validate_invoice_rejects_due_date_past_upper_bound() {
99+
let (env, contract_id) = setup_env();
100+
let past_max = env.ledger().timestamp() + 365 * SECONDS_PER_DAY + 1;
101+
let result = env.as_contract(&contract_id, || {
102+
ProtocolLimitsContract::validate_invoice(env.clone(), valid_amount(), past_max)
103+
});
104+
assert_eq!(result, Err(QuickLendXError::InvoiceDueDateInvalid));
105+
}

0 commit comments

Comments
 (0)