Skip to content

fix some tests for Android #693

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

Merged
merged 2 commits into from
Jul 24, 2017
Merged

fix some tests for Android #693

merged 2 commits into from
Jul 24, 2017

Conversation

ndusart
Copy link
Contributor

@ndusart ndusart commented Jul 20, 2017

I tested the crate on an Android emulator and quite all the tests passed. Only 6 were failing.

failures:

---- sys::test_pthread::test_pthread_self stdout ----
thread 'sys::test_pthread::test_pthread_self' panicked at 'assertion failed: tid > 0', test/sys/test_pthread.rs:6
note: Run with RUST_BACKTRACE=1 for a backtrace.

---- sys::test_socket::test_getsockname stdout ----
thread 'sys::test_socket::test_getsockname' panicked at 'bind failed: Sys(EACCES)', /checkout/src/libcore/result.rs:859

---- sys::test_socket::test_unixdomain stdout ----
thread 'sys::test_socket::test_unixdomain' panicked at 'bind failed: Sys(EACCES)', /checkout/src/libcore/result.rs:859

---- sys::test_termios::test_local_flags stdout ----
thread 'sys::test_termios::test_local_flags' panicked at 'called Option::unwrap() on a None value', /checkout/src/libcore/option.rs:329

---- test_net::test_if_nametoindex stdout ----
thread 'test_net::test_if_nametoindex' panicked at 'assertion failed: if_nametoindex(&LOOPBACK[..]).is_ok()', test/test_net.rs:11

---- test_unistd::test_mkstemp stdout ----
thread 'test_unistd::test_mkstemp' panicked at 'mkstemp failed: ENOENT: No such file or directory', test/test_unistd.rs:73

This PR fixes 3 of those:

  • test_pthread_self: pthread_t is unsigned, so it can be negative (and it is often the case)
  • test_if_nametoindex: constant LOOPBACK is the same than on Linux
  • test_mkstemp: directory /tmp does not exist in Android

Two are still failing (test_unixdomain and test_getsockname) because we need some special permissions on Android for playing with sockets. On a rooted physical device, they passed. So, if tests for Android are integrated in CI, we should try to embed the tests to run in an APK with requested permissions or find a way to give the permission to a native program.

test_local_flags is still failing also because O_LARGEFILE is hardcoded and is not the right value in Android. Then fcntl::OFlag::from_bits(flags).unwrap() fails because this flag is then not recognised.
I made a PR in libc so that we can rely on libc definitions for all O_* flags. (rust-lang/libc#683)

Do you prefer to wait for this one to be fixed for this PR to merged as well ?

@@ -3,5 +3,5 @@ use nix::sys::pthread::*;
#[test]
fn test_pthread_self() {
let tid = pthread_self();
assert!(tid > 0);
assert!(tid != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually the man page for pthread_self says we should only use pthread_equals for comparing pthreads. If anything we shouldn't even check this value as it's non-portable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was wondering that when I read that test.

So should we remove it ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, remove it, but leave it as a separate commit with a description of why it was removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

0 still isn't valid here, we should really fix this at some point, but this is fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I just remove the test leaving the test_pthread.rs file for a future test to be inserted later or should I completely remove the file ?

Concerning the validity of the test. Maybe we have to wait pthread_create is defined in nix, so that we could make a test where we spin a thread and test that pthread_self returns different pthread_t in the different threads.
And we shouldn't even test for an "invalid" return value as pthread_self is not meant to fail, man page says it always succeed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving this test as-is is fine.

@@ -64,7 +64,12 @@ fn test_wait() {

#[test]
fn test_mkstemp() {
let result = mkstemp("/tmp/nix_tempfile.XXXXXX");
#[cfg(target_os = "android")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this use std::env::temp_dir instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

And actually, this would be cleaner if we separated this into two tests, since they are two separate tests just rolled into one. Could you do that here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't know about std::env::temp_dir, we definitely should use it instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests are splitted and use std::env::temp_dir now :)

test/test_net.rs Outdated
@@ -1,9 +1,9 @@
use nix::net::if_::*;

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

The conditions should be alphabetized, so "android" should come first here. Same for the following #cfg attribute.

@Susurrus
Copy link
Contributor

This is a great start, thanks @ndusart!

Do you prefer to wait for this one to be fixed for this PR to merged as well?

While fixing all the tests is ideal, I'd like to limit this PR to those tests which can work with the current release of libc and also don't add any new hard-coded values. We'll wait on the rest once 0.9 is released and we're back to using libc-git. You can either raise a new issue or a separate PR to track that work; your call.

Regarding some points you brought up:

Two are still failing (test_unixdomain and test_getsockname) because we need some special permissions on Android for playing with sockets. On a rooted physical device, they passed. So, if tests for Android are integrated in CI, we should try to embed the tests to run in an APK with requested permissions or find a way to give the permission to a native program.

I get the feeling we'll run into tons of permission issues with Android as we continue to develop nix. So I'm fine with needing to figure out a rooted app to run tests on. It'll be up to the users of nix to figure out if Android supports that functionality or not for their own use cases.

Err(_) => {}
}
#[test]
#[should_panic]
Copy link
Contributor

Choose a reason for hiding this comment

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

Our test shouldn't panic, you should just not use unwrap() here. You should use assertions to check the return value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right, I tried to tied to the behavior of the test as it was written, it seemed logical at the moment.

@@ -64,22 +64,27 @@ fn test_wait() {

#[test]
fn test_mkstemp() {
let result = mkstemp("/tmp/nix_tempfile.XXXXXX");
use std::env;
Copy link
Member

Choose a reason for hiding this comment

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

stylistically, I prefer to put all use statements at the top of the file, unless there's a good reason not to.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with this, especially since this is repeated in the following test. My rule is that if there are a lot of little things that need to be imported in a function/module or if they're conditionally imported, then they're fine within the function/module. Otherwise they should be left out.

@Susurrus Susurrus mentioned this pull request Jul 21, 2017
10 tasks
@@ -3,5 +3,5 @@ use nix::sys::pthread::*;
#[test]
fn test_pthread_self() {
let tid = pthread_self();
assert!(tid > 0);
assert!(tid != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving this test as-is is fine.

@Susurrus
Copy link
Contributor

@asomers Wanna take another look at this? Looks like small solid fixes to me!

@asomers
Copy link
Member

asomers commented Jul 24, 2017

bors r+

bors bot added a commit that referenced this pull request Jul 24, 2017
693: fix some tests for Android r=asomers

I tested the crate on an Android emulator and quite all the tests passed. Only 6 were failing.

> failures:
> 
> ---- sys::test_pthread::test_pthread_self stdout ----
> 	thread 'sys::test_pthread::test_pthread_self' panicked at 'assertion failed: tid > 0', test/sys/test_pthread.rs:6
> note: Run with `RUST_BACKTRACE=1` for a backtrace.
> 
> ---- sys::test_socket::test_getsockname stdout ----
> 	thread 'sys::test_socket::test_getsockname' panicked at 'bind failed: Sys(EACCES)', /checkout/src/libcore/result.rs:859
> 
> ---- sys::test_socket::test_unixdomain stdout ----
> 	thread 'sys::test_socket::test_unixdomain' panicked at 'bind failed: Sys(EACCES)', /checkout/src/libcore/result.rs:859
> 
> ---- sys::test_termios::test_local_flags stdout ----
> 	thread 'sys::test_termios::test_local_flags' panicked at 'called `Option::unwrap()` on a `None` value', /checkout/src/libcore/option.rs:329
> 
> ---- test_net::test_if_nametoindex stdout ----
> 	thread 'test_net::test_if_nametoindex' panicked at 'assertion failed: if_nametoindex(&LOOPBACK[..]).is_ok()', test/test_net.rs:11
> 
> ---- test_unistd::test_mkstemp stdout ----
> 	thread 'test_unistd::test_mkstemp' panicked at 'mkstemp failed: ENOENT: No such file or directory', test/test_unistd.rs:73

This PR fixes 3 of those:
 - `test_pthread_self`: pthread_t is unsigned, so it can be negative (and it is often the case)
 - `test_if_nametoindex`: constant `LOOPBACK` is the same than on Linux
 - `test_mkstemp`: directory `/tmp` does not exist in Android

Two are still failing (`test_unixdomain` and `test_getsockname`) because we need some special permissions on Android for playing with sockets. On a rooted physical device, they passed. So, if tests for Android are integrated in CI, we should try to embed the tests to run in an APK with requested permissions or find a way to give the permission to a native program.

`test_local_flags` is still failing also because `O_LARGEFILE` is hardcoded and is not the right value in Android. Then `fcntl::OFlag::from_bits(flags).unwrap()` fails because this flag is then not recognised.
I made a PR in `libc` so that we can rely on libc definitions for all `O_*` flags. (rust-lang/libc#683)

Do you prefer to wait for this one to be fixed for this PR to merged as well ?
@bors
Copy link
Contributor

bors bot commented Jul 24, 2017

@bors bors bot merged commit a546ef9 into nix-rust:master Jul 24, 2017
@ndusart ndusart deleted the android-test branch July 24, 2017 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants