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
use std::fmt::Debug;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum RegionValidationError {
#[error(
"Region length exceeds capacity. Length {}, capacity {}",
length,
capacity
)]
LengthExceedsCapacity { length: u32, capacity: u32 },
#[error(
"Region exceeds address space. Offset {}, capacity {}",
offset,
capacity
)]
OutOfRange { offset: u32, capacity: u32 },
#[error("Got a zero Wasm address in the offset")]
ZeroOffset {},
}
impl RegionValidationError {
pub(crate) fn length_exceeds_capacity(length: u32, capacity: u32) -> Self {
RegionValidationError::LengthExceedsCapacity { length, capacity }
}
pub(crate) fn out_of_range(offset: u32, capacity: u32) -> Self {
RegionValidationError::OutOfRange { offset, capacity }
}
pub(crate) fn zero_offset() -> Self {
RegionValidationError::ZeroOffset {}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn length_exceeds_capacity_works() {
let error = RegionValidationError::length_exceeds_capacity(50, 20);
match error {
RegionValidationError::LengthExceedsCapacity {
length, capacity, ..
} => {
assert_eq!(length, 50);
assert_eq!(capacity, 20);
}
e => panic!("Unexpected error: {:?}", e),
}
}
#[test]
fn out_of_range_works() {
let error = RegionValidationError::out_of_range(u32::MAX, 1);
match error {
RegionValidationError::OutOfRange {
offset, capacity, ..
} => {
assert_eq!(offset, u32::MAX);
assert_eq!(capacity, 1);
}
e => panic!("Unexpected error: {:?}", e),
}
}
#[test]
fn zero_offset() {
let error = RegionValidationError::zero_offset();
match error {
RegionValidationError::ZeroOffset { .. } => {}
e => panic!("Unexpected error: {:?}", e),
}
}
}