add reexport test

This commit is contained in:
Jianfeng Jiang 2023-03-06 17:12:39 +08:00 committed by Tate, Hongliang Tian
parent 99e6ffb7f8
commit 394e146f3a
11 changed files with 143 additions and 1 deletions

View File

@ -0,0 +1,25 @@
//! This test checks that if cargo-component can control reexported entry points.
#![feature(once_cell)]
use std::path::PathBuf;
use test_utils::{cargo_clean, cargo_component, clean_after_test};
mod test_utils;
#[test]
fn reexport() {
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let target_dir = root_dir.join("target").join("reexport_test");
let cwd = root_dir.join("tests").join("reexport_test");
let output = cargo_clean(&cwd, &target_dir);
assert!(output.status.success());
let output = cargo_component(&cwd, &target_dir);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("stderr: {stderr}");
assert!(output.status.success());
assert!(!stderr.contains("access controlled entry point is disallowed"));
clean_after_test(&cwd);
}

View File

@ -0,0 +1,2 @@
[workspace]
members = ["foo", "bar", "baz"]

View File

@ -0,0 +1,20 @@
[components]
foo = { name = "foo" }
bar = { name = "bar" }
baz = { name = "baz" }
[whitelist]
[whitelist.foo.f1]
baz = true
[whitelist.foo.Foo.new]
baz = true
[whitelist.foo.Foo.get]
baz = true
[whitelist.foo.FooTrait.t_new]
baz = true
[whitelist.foo.FooTrait.t_get]
baz = true

View File

@ -0,0 +1,9 @@
[package]
name = "bar"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
foo = {path ="../foo"}

View File

@ -0,0 +1,3 @@
pub use foo::f1;
pub use foo::Foo;
pub use foo::FooTrait;

View File

@ -0,0 +1,10 @@
[package]
name = "baz"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
foo = {path ="../foo"}
bar = {path ="../bar"}

View File

@ -0,0 +1,24 @@
pub fn reexport_fn() {
foo::f1();
bar::f1();
}
pub fn reexport_method() {
let foo_struct = foo::Foo::new();
foo_struct.get();
let another_foo = bar::Foo::new();
another_foo.get();
}
pub fn reexport_trait_1() {
use foo::FooTrait;
let foo_struct = foo::Foo::t_new();
foo_struct.t_get();
}
pub fn reexport_trait_2() {
use bar::FooTrait;
let foo_struct = bar::Foo::t_new();
foo_struct.t_get();
}

View File

@ -0,0 +1,9 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
controlled = {path = "../../../../controlled"}

View File

@ -0,0 +1,39 @@
#![feature(register_tool)]
#![register_tool(component_access_control)]
#[macro_use]
extern crate controlled;
#[controlled]
pub fn f1() {}
pub struct Foo;
impl Foo {
#[controlled]
pub fn new() -> Self {
Foo
}
#[controlled]
pub fn get(&self) -> usize {
42
}
}
pub trait FooTrait {
#[controlled]
fn t_new() -> Self;
#[controlled]
fn t_get(&self) -> usize;
}
impl FooTrait for Foo {
fn t_new() -> Self {
Foo
}
fn t_get(&self) -> usize {
43
}
}

View File

@ -31,6 +31,7 @@ pub fn cargo_component(cwd: &PathBuf, target_dir: &PathBuf) -> Output {
}
pub fn clean_after_test(cwd: &PathBuf) {
Command::new("cargo").arg("clean").current_dir(cwd).status().unwrap();
let cargo_lock = cwd.join("Cargo.lock");
std::fs::remove_file(cargo_lock);
}

View File

@ -17,7 +17,7 @@ fn trait_method() {
let output = cargo_component(&cwd, &target_dir);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("stderr: {stderr}");
assert!(output.status.success());
assert!(stderr.contains("access controlled entry point is disallowed"));
assert!(stderr.contains("access foo::Foo::method in bar"));