-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathinvariant.rs
162 lines (143 loc) · 5.27 KB
/
invariant.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Tests for invariants
use crate::{config::*, test_helpers::filter::Filter};
use ethers::types::U256;
use forge::fuzz::CounterExample;
use std::collections::BTreeMap;
#[test]
fn test_invariant() {
let mut runner = runner();
let results = runner
.test(
&Filter::new(".*", ".*", ".*fuzz/invariant/(target|targetAbi|common)"),
None,
TEST_OPTS,
)
.unwrap();
assert_multiple(
&results,
BTreeMap::from([
(
"fuzz/invariant/common/InvariantInnerContract.t.sol:InvariantInnerContract",
vec![("invariantHideJesus", false, Some("jesus betrayed.".into()), None, None)],
),
(
"fuzz/invariant/common/InvariantReentrancy.t.sol:InvariantReentrancy",
vec![("invariantNotStolen", true, None, None, None)],
),
(
"fuzz/invariant/common/InvariantTest1.t.sol:InvariantTest",
vec![("invariant_neverFalse", false, Some("false.".into()), None, None)],
),
(
"fuzz/invariant/target/ExcludeContracts.t.sol:ExcludeContracts",
vec![("invariantTrueWorld", true, None, None, None)],
),
(
"fuzz/invariant/target/TargetContracts.t.sol:TargetContracts",
vec![("invariantTrueWorld", true, None, None, None)],
),
(
"fuzz/invariant/target/TargetSenders.t.sol:TargetSenders",
vec![("invariantTrueWorld", false, Some("false world.".into()), None, None)],
),
(
"fuzz/invariant/target/TargetSelectors.t.sol:TargetSelectors",
vec![("invariantTrueWorld", true, None, None, None)],
),
(
"fuzz/invariant/targetAbi/ExcludeArtifacts.t.sol:ExcludeArtifacts",
vec![("invariantShouldPass", true, None, None, None)],
),
(
"fuzz/invariant/targetAbi/TargetArtifacts.t.sol:TargetArtifacts",
vec![
("invariantShouldPass", true, None, None, None),
("invariantShouldFail", false, Some("false world.".into()), None, None),
],
),
(
"fuzz/invariant/targetAbi/TargetArtifactSelectors.t.sol:TargetArtifactSelectors",
vec![("invariantShouldPass", true, None, None, None)],
),
(
"fuzz/invariant/targetAbi/TargetArtifactSelectors2.t.sol:TargetArtifactSelectors2",
vec![("invariantShouldFail", false, Some("its false.".into()), None, None)],
),
]),
);
}
#[test]
fn test_invariant_override() {
let mut runner = runner();
let mut opts = TEST_OPTS;
opts.invariant_call_override = true;
runner.test_options = opts;
let results = runner
.test(
&Filter::new(".*", ".*", ".*fuzz/invariant/common/InvariantReentrancy.t.sol"),
None,
opts,
)
.unwrap();
assert_multiple(
&results,
BTreeMap::from([(
"fuzz/invariant/common/InvariantReentrancy.t.sol:InvariantReentrancy",
vec![("invariantNotStolen", false, Some("stolen.".into()), None, None)],
)]),
);
}
#[test]
fn test_invariant_storage() {
let mut runner = runner();
let mut opts = TEST_OPTS;
opts.invariant_depth = 100;
opts.fuzz_seed = Some(U256::from(6u32));
runner.test_options = opts;
let results = runner
.test(
&Filter::new(".*", ".*", ".*fuzz/invariant/storage/InvariantStorageTest.t.sol"),
None,
opts,
)
.unwrap();
assert_multiple(
&results,
BTreeMap::from([(
"fuzz/invariant/storage/InvariantStorageTest.t.sol:InvariantStorageTest",
vec![
("invariantChangeAddress", false, Some("changedAddr".into()), None, None),
("invariantChangeString", false, Some("changedStr".into()), None, None),
("invariantChangeUint", false, Some("changedUint".into()), None, None),
("invariantPush", false, Some("pushUint".into()), None, None),
],
)]),
);
}
#[test]
fn test_invariant_shrink() {
let mut runner = runner();
let mut opts = TEST_OPTS;
opts.fuzz_seed = Some(U256::from(102u32));
runner.test_options = opts;
let results = runner
.test(
&Filter::new(".*", ".*", ".*fuzz/invariant/common/InvariantInnerContract.t.sol"),
None,
opts,
)
.unwrap();
let results =
results.values().last().expect("`InvariantInnerContract.t.sol` should be testable.");
let result =
results.test_results.values().last().expect("`InvariantInnerContract` should be testable.");
let counter = result
.counterexample
.as_ref()
.expect("`InvariantInnerContract` should have failed with a counterexample.");
match counter {
CounterExample::Single(_) => assert!(false, "CounterExample should be a sequence."),
// `fuzz_seed` at 100 makes this sequence shrinkable from 4 to 2.
CounterExample::Sequence(sequence) => assert!(sequence.len() == 2),
};
}