-
Notifications
You must be signed in to change notification settings - Fork 689
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
Conversation
@@ -3,5 +3,5 @@ use nix::sys::pthread::*; | |||
#[test] | |||
fn test_pthread_self() { | |||
let tid = pthread_self(); | |||
assert!(tid > 0); | |||
assert!(tid != 0); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
test/test_unistd.rs
Outdated
@@ -64,7 +64,12 @@ fn test_wait() { | |||
|
|||
#[test] | |||
fn test_mkstemp() { | |||
let result = mkstemp("/tmp/nix_tempfile.XXXXXX"); | |||
#[cfg(target_os = "android")] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"))] |
There was a problem hiding this comment.
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.
This is a great start, thanks @ndusart!
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 Regarding some points you brought up:
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 |
test/test_unistd.rs
Outdated
Err(_) => {} | ||
} | ||
#[test] | ||
#[should_panic] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
test/test_unistd.rs
Outdated
@@ -64,22 +64,27 @@ fn test_wait() { | |||
|
|||
#[test] | |||
fn test_mkstemp() { | |||
let result = mkstemp("/tmp/nix_tempfile.XXXXXX"); | |||
use std::env; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -3,5 +3,5 @@ use nix::sys::pthread::*; | |||
#[test] | |||
fn test_pthread_self() { | |||
let tid = pthread_self(); | |||
assert!(tid > 0); | |||
assert!(tid != 0); |
There was a problem hiding this comment.
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.
@asomers Wanna take another look at this? Looks like small solid fixes to me! |
bors r+ |
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 ?
Build succeeded |
I tested the crate on an Android emulator and quite all the tests passed. Only 6 were failing.
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
: constantLOOPBACK
is the same than on Linuxtest_mkstemp
: directory/tmp
does not exist in AndroidTwo are still failing (
test_unixdomain
andtest_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 becauseO_LARGEFILE
is hardcoded and is not the right value in Android. Thenfcntl::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 allO_*
flags. (rust-lang/libc#683)Do you prefer to wait for this one to be fixed for this PR to merged as well ?