Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external files in test support on iOS #429

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ macro_rules! bssl_test {
}

init::init_once();
::std::env::set_current_dir(::test::ring_src_path()).unwrap();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure why this is needed on iOS but not Android. Here's how we do it on Android; can we do the same thing on iOS?

ring/mk/travis.sh

Lines 82 to 90 in c84e411

adb wait-for-device
adb push $target_dir/ring-* /data/ring-test
for testfile in `find src crypto -name "*_test*.txt"`; do
adb shell mkdir -p /data/`dirname $testfile`
adb push $testfile /data/$testfile
done
adb shell mkdir -p /data/third-party/NIST
adb push third-party/NIST/SHAVS /data/third-party/NIST/SHAVS
adb shell 'cd /data && ./ring-test' 2>&1 | tee /tmp/ring-test

Also, why is it important to switch the current directory, instead of just changing file accesses to use the full path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a non-jailbroken iOS, the only (known) way to start a process is to make it an app, and lauching it as an app. It will only be started with the working directory set to /, I could not find a way around that.

So we have two ways to solve this: using absolute paths (as I did for the pure Rust tests) or cd-ing to the right place just before running tests and keep relative paths. On principle, the first approach feels better as you're not altering what is basically a process-wide global variable. I used the cd strategy for the bn tests because I felt the required changes would be too invasive (as we need to pass the path through the rust-to-c test machinery), but if you feel otherwise, I can give it a shot.


let result = unsafe {
$bssl_test_main_fn_name()
Expand All @@ -61,6 +62,7 @@ macro_rules! bssl_test_rng {
}

init::init_once();
::std::env::set_current_dir(::test::ring_src_path()).unwrap();

let rng = rand::SystemRandom::new();
let mut rng = rand::RAND { rng: &rng };
Expand Down
19 changes: 18 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,23 @@ impl TestCase {
}
}

/// Returns the path for *ring* source code root.
///
/// On iOS, source are assumed to be copied in the application bundle, as
/// a "src" directory along the test runner.
#[cfg(target_os = "ios")]
pub fn ring_src_path() -> std::path::PathBuf {
std::env::current_exe().unwrap().parent().unwrap().join("src")
}

/// Returns the path for *ring* source code root.
///
/// On most platforms, the tests are run by cargo, so it's just the current
/// working directory.
#[cfg(not(target_os = "ios"))]
pub fn ring_src_path() -> std::path::PathBuf {
std::path::PathBuf::from(".")
}

/// Reads test cases out of the file with the path given by
/// `test_data_relative_file_path`, calling `f` on each vector until `f` fails
Expand All @@ -225,7 +242,7 @@ impl TestCase {
pub fn from_file<F>(test_data_relative_file_path: &str, mut f: F)
where F: FnMut(&str, &mut TestCase)
-> Result<(), error::Unspecified> {
let path = std::path::Path::new(test_data_relative_file_path);
let path = ring_src_path().join(test_data_relative_file_path);
let file = std::fs::File::open(path).unwrap();
let mut lines = std::io::BufReader::new(&file).lines();

Expand Down