Skip to content

Commit 3e3a7b2

Browse files
1c3t3arcvalle
authored andcommitted
Add tests for the scudo-proc-macro crate
This change adds both ui-tests as well as integrations tests with scudo, via ./abort_tests.sh.
1 parent b1b835a commit 3e3a7b2

15 files changed

+216
-10
lines changed

abort_tests.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,34 @@
2222
#
2323
# [1] https://llvm.org/docs/ScudoHardenedAllocator.html#error-types
2424

25-
cargo build --release --bin crash || exit 1
25+
cargo build --release --bins || exit 1
2626

2727
# Run the `crash` binary with the first arg. This binary will crash.
2828
# If the second arg is present in the crash message, the test passes.
2929
function run_test {
3030
tmp=$(mktemp)
31-
target/release/crash $1 2>$tmp
32-
if grep -q "$2" $tmp
31+
target/release/$1 $2 2>$tmp
32+
if grep -q "$3" $tmp
3333
then
34-
echo "Test '$2' pass"
34+
echo "Test '$3' pass"
3535
else
36-
echo "Test '$2' failed"
36+
echo "Test '$3' failed"
3737
exit 1
3838
fi
3939
}
4040

41-
run_test double_free "invalid chunk state"
42-
run_test misaligned_ptr "misaligned pointer"
43-
run_test corrupted_chunk_header "corrupted chunk header"
44-
run_test delete_size_mismatch "invalid sized delete"
41+
function run_crash_test {
42+
run_test crash $1 "$2"
43+
}
44+
45+
function run_macro_test {
46+
run_test macro "" "$1"
47+
}
48+
49+
run_crash_test double_free "invalid chunk state"
50+
run_crash_test misaligned_ptr "misaligned pointer"
51+
run_crash_test corrupted_chunk_header "corrupted chunk header"
52+
run_crash_test delete_size_mismatch "invalid sized delete"
53+
run_macro_test "Scudo WARNING: found 1 unrecognized flag(s)"
4554

4655
echo "All tests pass"

scudo-proc-macros/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Google LLC
1+
// Copyright 2022 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -124,3 +124,19 @@ pub fn set_scudo_options(attr: TokenStream, item: TokenStream) -> TokenStream {
124124

125125
result
126126
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
131+
#[test]
132+
fn test_fail() {
133+
let test_cases = trybuild::TestCases::new();
134+
test_cases.compile_fail("tests/ui/*_fail.rs");
135+
}
136+
137+
#[test]
138+
fn test_pass() {
139+
let test_cases = trybuild::TestCases::new();
140+
test_cases.pass("tests/ui/*_pass.rs");
141+
}
142+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
use scudo_proc_macros::set_scudo_options;
17+
18+
#[set_scudo_options(name = 4.2, value = false)]
19+
fn main() {
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![no_main]
16+
use scudo_proc_macros::set_scudo_options;
17+
18+
#[set_scudo_options(name:true,value:3.14)]
19+
fn main() {
20+
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: expected `=`
2+
--> tests/ui/no_equal_fail.rs:18:25
3+
|
4+
18 | #[set_scudo_options(name:true,value:3.14)]
5+
| ^
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![no_main]
16+
use scudo_proc_macros::set_scudo_options;
17+
18+
#[set_scudo_options(name = foo)]
19+
fn main() {
20+
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: Expect a valid value like '3.14' or 'true'
2+
--> tests/ui/no_ident_or_literal_fail.rs:18:1
3+
|
4+
18 | #[set_scudo_options(name = foo)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the attribute macro `set_scudo_options` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#![no_main]
16+
use scudo_proc_macros::set_scudo_options;
17+
18+
#[set_scudo_options(name=true)]
19+
fn not_main() {
20+
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: This macro is only allowed to be used on the main method
2+
--> tests/ui/not_main_fail.rs:19:4
3+
|
4+
19 | fn not_main() {
5+
| ^^^^^^^^
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
use scudo_proc_macros::set_scudo_options;
17+
18+
#[set_scudo_options(name = 3.14)]
19+
fn main() {
20+
21+
}

0 commit comments

Comments
 (0)