-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathordered_index_operations.rs
More file actions
182 lines (163 loc) · 5.14 KB
/
ordered_index_operations.rs
File metadata and controls
182 lines (163 loc) · 5.14 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![no_main]
use arbitrary::Arbitrary;
use commonware_runtime::{deterministic, Runner, Supervisor as _};
use commonware_storage::{
index::{ordered::Index, Cursor as _, Unordered as _},
translator::TwoCap,
};
use libfuzzer_sys::fuzz_target;
#[derive(Arbitrary, Debug, Clone)]
enum IndexOperation {
Insert {
key: Vec<u8>,
value: u64,
},
Get {
key: Vec<u8>,
},
GetMut {
key: Vec<u8>,
},
GetMutOrInsert {
key: Vec<u8>,
value: u64,
},
Remove {
key: Vec<u8>,
},
Prune {
key: Vec<u8>,
prune_value: u64,
},
InsertAndPrune {
key: Vec<u8>,
value: u64,
prune_value: u64,
},
// Edge case operations
InsertLargeKey {
value: u64,
},
InsertMany {
key: Vec<u8>,
count: u8,
},
PruneAll {
key: Vec<u8>,
},
// Cursor operations
CursorIterate {
key: Vec<u8>,
},
CursorUpdate {
key: Vec<u8>,
new_value: u64,
},
CursorDelete {
key: Vec<u8>,
},
CursorInsert {
key: Vec<u8>,
value: u64,
},
}
#[derive(Arbitrary, Debug)]
struct FuzzInput {
operations: Vec<IndexOperation>,
}
fn fuzz(input: FuzzInput) {
let runner = deterministic::Runner::default();
runner.start(|context| async move {
let mut index = Index::new(context.child("storage"), TwoCap);
for op in input.operations.iter() {
match op {
IndexOperation::Insert { key, value } => {
index.insert(key, *value);
}
IndexOperation::Get { key } => {
let _values: Vec<_> = index.get(key).collect();
}
IndexOperation::GetMut { key } => {
if let Some(mut cursor) = index.get_mut(key) {
// Iterate through all values
while cursor.next().is_some() {
// Just iterate, don't modify
}
}
}
IndexOperation::GetMutOrInsert { key, value } => {
if let Some(mut cursor) = index.get_mut_or_insert(key, *value) {
// Iterate through existing values
while cursor.next().is_some() {
// Just iterate
}
}
}
IndexOperation::Remove { key } => {
index.remove(key);
}
IndexOperation::Prune { key, prune_value } => {
index.prune(key, |v| *v == *prune_value);
}
IndexOperation::InsertAndPrune {
key,
value,
prune_value,
} => {
index.insert_and_prune(key, *value, |v| *v == *prune_value);
}
IndexOperation::InsertLargeKey { value } => {
// Create a large key to test translator behavior
let large_key = vec![0u8; 1000];
index.insert(&large_key, *value);
}
IndexOperation::InsertMany { key, count } => {
// Insert multiple values for the same key to test collisions
for i in 0..*count {
index.insert(key, i as u64);
}
}
IndexOperation::PruneAll { key } => {
// Remove all values for a key
index.prune(key, |_| true);
}
IndexOperation::CursorIterate { key } => {
if let Some(mut cursor) = index.get_mut(key) {
// Iterate through all values
while cursor.next().is_some() {
// Just iterate
}
}
}
IndexOperation::CursorUpdate { key, new_value } => {
if let Some(mut cursor) = index.get_mut(key) {
if cursor.next().is_some() {
cursor.update(*new_value);
}
}
}
IndexOperation::CursorDelete { key } => {
if let Some(mut cursor) = index.get_mut(key) {
if cursor.next().is_some() {
cursor.delete();
}
}
}
IndexOperation::CursorInsert { key, value } => {
if index.get(key).next().is_some() {
let mut cursor = index.get_mut(key).unwrap();
if cursor.next().is_some() {
cursor.insert(*value);
let _ = cursor.next();
}
} else {
index.insert(key, *value);
}
}
}
}
});
}
fuzz_target!(|input: FuzzInput| {
fuzz(input);
});