-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathbyte_string.rs
More file actions
243 lines (197 loc) · 6.31 KB
/
Copy pathbyte_string.rs
File metadata and controls
243 lines (197 loc) · 6.31 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] byte_string_opaque_without_reveal verus_code! {
use vstd::prelude::*;
fn test() {
let bytes: &'static [u8; 4] = b"RIFF";
// The array length is part of the type.
assert(bytes@.len() == 4);
// The contents remain opaque until reveal_byteslit is called.
assert(bytes@[0] == b'R'); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] byte_string_reveal_contents verus_code! {
use vstd::prelude::*;
const ICON_PATTERN: &'static [u8; 4] =
b"\x00\x00\x02\x00";
fn direct_array_reference() {
proof {
reveal_byteslit(b"RIFF");
}
let bytes: &'static [u8; 4] = b"RIFF";
assert(bytes@.len() == 4);
assert(bytes@ =~= seq![b'R', b'I', b'F', b'F']);
}
fn direct_slice_reference() {
proof {
reveal_byteslit(b"\x00\x00\x02\x00");
}
// Exercises local coercion from &[u8; 4] to &[u8].
let bytes: &'static [u8] = b"\x00\x00\x02\x00";
assert(bytes@.len() == 4);
assert(bytes@ =~= seq![0u8, 0u8, 2u8, 0u8]);
}
fn constant_array_reference() {
proof {
reveal_byteslit(b"\x00\x00\x02\x00");
}
assert(ICON_PATTERN@.len() == 4);
assert(ICON_PATTERN@ =~= seq![0u8, 0u8, 2u8, 0u8]);
}
} => Ok(())
}
test_verify_one_file! {
#[test] byte_string_reveal_soundness verus_code! {
use vstd::prelude::*;
fn wrong_byte() {
proof {
reveal_byteslit(b"RIFF");
}
let bytes: &'static [u8] = b"RIFF";
assert(bytes@[1] == b'X'); // FAILS
}
fn wrong_length() {
proof {
reveal_byteslit(b"\x00\x00\x02\x00");
}
let bytes: &'static [u8] = b"\x00\x00\x02\x00";
assert(bytes@.len() == 5); // FAILS
}
} => Err(err) => assert_fails(err, 2)
}
test_verify_one_file! {
#[test] byte_string_distinct_same_length verus_code! {
const X: &'static [u8; 3] = b"ABC";
const Y: &'static [u8; 3] = b"XYZ";
fn test() {
// No reveal: this depends on opaque literal identity/injectivity.
assert(!(X === Y));
}
} => Ok(())
}
test_verify_one_file! {
#[test] byte_string_same_literal_equal verus_code! {
const X: &'static [u8; 3] = b"ABC";
const Y: &'static [u8; 3] = b"ABC";
fn test() {
assert(X === Y);
}
} => Ok(())
}
test_verify_one_file! {
#[test] reveal_byteslit_requires_literal verus_code! {
use vstd::prelude::*;
fn test() {
proof {
reveal_byteslit(12u32);
}
}
} => Err(err) => assert_vir_error_msg(err, "byte-string literal expected")
}
test_verify_one_file! {
#[test] reveal_byteslit_requires_one_argument verus_code! {
use vstd::prelude::*;
fn test() {
proof {
reveal_byteslit(b"a", b"b");
}
}
} => Err(err) => assert_rust_error_msg(
err,
"this function takes 1 argument but 2 arguments were supplied",
)
}
test_verify_one_file! {
#[test] reveal_byteslit_requires_proof_mode verus_code! {
use vstd::prelude::*;
fn test() {
reveal_byteslit(b"ABC");
}
} => Err(err) => assert_vir_error_msg(
err,
"cannot use reveal_byteslit in exec mode",
)
}
test_verify_one_file! {
#[test] byte_string_edge_cases verus_code! {
use vstd::prelude::*;
fn empty() {
proof {
reveal_byteslit(b"");
}
let bytes: &'static [u8] = b"";
assert(bytes@ =~= seq![]);
}
fn raw() {
proof {
reveal_byteslit(br"RIFF");
}
let bytes: &'static [u8] = br"RIFF";
assert(bytes@ =~= seq![b'R', b'I', b'F', b'F']);
}
fn non_utf8() {
proof {
reveal_byteslit(b"\x00\x80\xfe\xff");
}
let bytes: &'static [u8] = b"\x00\x80\xfe\xff";
assert(bytes@ =~= seq![0u8, 128u8, 254u8, 255u8]);
}
fn escapes() {
proof {
reveal_byteslit(b"\n\r\t\\\"");
}
let bytes: &'static [u8] = b"\n\r\t\\\"";
assert(bytes@ =~= seq![b'\n', b'\r', b'\t', b'\\', b'"']);
}
} => Ok(())
}
test_verify_one_file! {
#[test] byte_string_in_struct verus_code! {
use vstd::prelude::*;
struct ByteMatcher {
pattern: &'static [u8],
mask: &'static [u8],
leading_ignore: &'static [u8],
}
fn image_x_icon() {
proof {
reveal_byteslit(b"\x00\x00\x01\x00");
reveal_byteslit(b"\xff\xff\xff\xff");
reveal_byteslit(b"");
}
let matcher = ByteMatcher {
pattern: b"\x00\x00\x01\x00",
mask: b"\xff\xff\xff\xff",
leading_ignore: b"",
};
assert(matcher.pattern@ =~= seq![0u8, 0u8, 1u8, 0u8]);
assert(matcher.mask@ =~= seq![255u8, 255u8, 255u8, 255u8]);
assert(matcher.leading_ignore@ =~= seq![]);
}
fn video_avi() {
proof {
reveal_byteslit(b"RIFF\x00\x00\x00\x00AVI ");
reveal_byteslit(
b"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff"
);
reveal_byteslit(b"");
}
let matcher = ByteMatcher {
pattern: b"RIFF\x00\x00\x00\x00AVI ",
mask: b"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff",
leading_ignore: b"",
};
assert(matcher.pattern@.len() == 12);
assert(matcher.mask@.len() == 12);
assert(matcher.pattern@[0] == b'R');
assert(matcher.pattern@[4] == 0u8);
assert(matcher.pattern@[8] == b'A');
assert(matcher.pattern@[11] == b' ');
}
} => Ok(())
}