use std::path::Path;
use std::fs::File;
use std::io::{Read, Write};
use extrasafe::*;
use builtins::SystemIO;
#[test]
fn different_rulesets_same_syscall() {
SafetyContext::new()
.enable(SystemIO::nothing()
.allow_read()
.allow_stdout()
.allow_stderr()
.allow_metadata()
).unwrap()
.enable(
SystemIO::nothing()
.allow_stderr()
.allow_metadata()
.allow_close(),
)
.unwrap()
.apply_to_current_thread()
.unwrap();
let res = writeln!(std::io::stdout(), "we can print to stdout");
assert!(res.is_ok(), "failed to write to stdout: {:?}", res.unwrap_err());
let res = writeln!(std::io::stderr(), "we can print to stderr");
assert!(res.is_ok(), "failed to write to stderr: {:?}", res.unwrap_err());
}
fn create_testfile(path: &Path, filename: &str) -> File {
let path = path.join(filename);
let mut file = File::create(&path).unwrap();
file.write_all(filename.as_bytes()).unwrap();
file.sync_all().unwrap();
drop(file);
File::open(&path).unwrap()
}
#[test]
fn different_rulesets_same_syscall2() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().to_path_buf();
let mut file1 = create_testfile(&path, "testfile1.txt");
let mut file2 = create_testfile(&path, "testfile2.txt");
let mut file3 = create_testfile(&path, "testfile3.txt");
SafetyContext::new()
.enable(SystemIO::nothing()
.allow_stdout()
.allow_stderr()
).unwrap()
.enable(SystemIO::nothing()
.allow_file_read(&file1)
).unwrap()
.enable(SystemIO::nothing()
.allow_file_read(&file2)
).unwrap()
.enable(SystemIO::nothing()
.allow_file_read(&file3)
).unwrap()
.apply_to_current_thread()
.unwrap();
let mut s = String::new();
let res = file1.read_to_string(&mut s);
assert!(res.is_ok(), "Failed to read file1: {:?}", res.unwrap_err());
let res = file2.read_to_string(&mut s);
assert!(res.is_ok(), "Failed to read file2: {:?}", res.unwrap_err());
let res = file3.read_to_string(&mut s);
assert!(res.is_ok(), "Failed to read file3: {:?}", res.unwrap_err());
assert_eq!(s, "testfile1.txttestfile2.txttestfile3.txt");
}