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
use alloc::borrow::ToOwned;
use core::marker::PhantomData;
use crate::{
corpus::{Corpus, CorpusScheduler},
inputs::Input,
state::HasCorpus,
Error,
};
pub struct QueueCorpusScheduler<C, I, S>
where
S: HasCorpus<C, I>,
C: Corpus<I>,
I: Input,
{
phantom: PhantomData<(C, I, S)>,
}
impl<C, I, S> CorpusScheduler<I, S> for QueueCorpusScheduler<C, I, S>
where
S: HasCorpus<C, I>,
C: Corpus<I>,
I: Input,
{
fn next(&self, state: &mut S) -> Result<usize, Error> {
if state.corpus().count() == 0 {
Err(Error::Empty("No entries in corpus".to_owned()))
} else {
let id = match state.corpus().current() {
Some(cur) => {
if *cur + 1 >= state.corpus().count() {
0
} else {
*cur + 1
}
}
None => 0,
};
*state.corpus_mut().current_mut() = Some(id);
Ok(id)
}
}
}
impl<C, I, S> QueueCorpusScheduler<C, I, S>
where
S: HasCorpus<C, I>,
C: Corpus<I>,
I: Input,
{
#[must_use]
pub fn new() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<C, I, S> Default for QueueCorpusScheduler<C, I, S>
where
S: HasCorpus<C, I>,
C: Corpus<I>,
I: Input,
{
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[cfg(feature = "std")]
mod tests {
use std::{fs, path::PathBuf};
use crate::{
corpus::{Corpus, CorpusScheduler, OnDiskCorpus, QueueCorpusScheduler, Testcase},
inputs::bytes::BytesInput,
state::{HasCorpus, State},
utils::StdRand,
};
#[test]
fn test_queuecorpus() {
let rand = StdRand::with_seed(4);
let scheduler = QueueCorpusScheduler::new();
let mut q =
OnDiskCorpus::<BytesInput>::new(PathBuf::from("target/.test/fancy/path")).unwrap();
let t = Testcase::with_filename(
BytesInput::new(vec![0_u8; 4]),
"target/.test/fancy/path/fancyfile".into(),
);
q.add(t).unwrap();
let objective_q =
OnDiskCorpus::<BytesInput>::new(PathBuf::from("target/.test/fancy/objective/path"))
.unwrap();
let mut state = State::new(rand, q, (), objective_q, ());
let next_idx = scheduler.next(&mut state).unwrap();
let filename = state
.corpus()
.get(next_idx)
.unwrap()
.borrow()
.filename()
.as_ref()
.unwrap()
.to_owned();
assert_eq!(filename, "target/.test/fancy/path/fancyfile");
fs::remove_dir_all("target/.test/fancy").unwrap();
}
}