提交 15966c3c 编写于 作者: A Aaron Turon

Remove iotest macro

This commit removes the `iotest!` macro from `std::io`. The macro was
primarily used to ensure that all io-related tests were run on both
libnative and libgreen/librustuv. However, now that the librustuv stack
is being removed, the macro is no longer needed.

See the [runtime removal
RFC](https://github.com/rust-lang/rfcs/pull/230) for more context.

[breaking-change]
上级 60b859ab
...@@ -948,9 +948,7 @@ mod test { ...@@ -948,9 +948,7 @@ mod test {
use io::{SeekSet, SeekCur, SeekEnd, Read, Open, ReadWrite}; use io::{SeekSet, SeekCur, SeekEnd, Read, Open, ReadWrite};
use io; use io;
use str; use str;
use io::fs::{File, rmdir, mkdir, readdir, rmdir_recursive, use io::fs::*;
mkdir_recursive, copy, unlink, stat, symlink, link,
readlink, chmod, lstat, change_file_times};
use path::Path; use path::Path;
use io; use io;
use ops::Drop; use ops::Drop;
...@@ -1002,7 +1000,8 @@ pub fn tmpdir() -> TempDir { ...@@ -1002,7 +1000,8 @@ pub fn tmpdir() -> TempDir {
TempDir(ret) TempDir(ret)
} }
iotest!(fn file_test_io_smoke_test() { #[test]
fn file_test_io_smoke_test() {
let message = "it's alright. have a good time"; let message = "it's alright. have a good time";
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let filename = &tmpdir.join("file_rt_io_file_test.txt"); let filename = &tmpdir.join("file_rt_io_file_test.txt");
...@@ -1020,9 +1019,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1020,9 +1019,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(read_str.as_slice(), message); assert_eq!(read_str.as_slice(), message);
} }
check!(unlink(filename)); check!(unlink(filename));
}) }
iotest!(fn invalid_path_raises() { #[test]
fn invalid_path_raises() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let filename = &tmpdir.join("file_that_does_not_exist.txt"); let filename = &tmpdir.join("file_that_does_not_exist.txt");
let result = File::open_mode(filename, Open, Read); let result = File::open_mode(filename, Open, Read);
...@@ -1032,9 +1032,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1032,9 +1032,10 @@ pub fn tmpdir() -> TempDir {
error!(result, "no such file or directory"); error!(result, "no such file or directory");
} }
error!(result, format!("path={}; mode=open; access=read", filename.display())); error!(result, format!("path={}; mode=open; access=read", filename.display()));
}) }
iotest!(fn file_test_iounlinking_invalid_path_should_raise_condition() { #[test]
fn file_test_iounlinking_invalid_path_should_raise_condition() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt"); let filename = &tmpdir.join("file_another_file_that_does_not_exist.txt");
...@@ -1045,9 +1046,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1045,9 +1046,10 @@ pub fn tmpdir() -> TempDir {
error!(result, "no such file or directory"); error!(result, "no such file or directory");
} }
error!(result, format!("path={}", filename.display())); error!(result, format!("path={}", filename.display()));
}) }
iotest!(fn file_test_io_non_positional_read() { #[test]
fn file_test_io_non_positional_read() {
let message: &str = "ten-four"; let message: &str = "ten-four";
let mut read_mem = [0, .. 8]; let mut read_mem = [0, .. 8];
let tmpdir = tmpdir(); let tmpdir = tmpdir();
...@@ -1070,9 +1072,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1070,9 +1072,10 @@ pub fn tmpdir() -> TempDir {
check!(unlink(filename)); check!(unlink(filename));
let read_str = str::from_utf8(read_mem).unwrap(); let read_str = str::from_utf8(read_mem).unwrap();
assert_eq!(read_str, message); assert_eq!(read_str, message);
}) }
iotest!(fn file_test_io_seek_and_tell_smoke_test() { #[test]
fn file_test_io_seek_and_tell_smoke_test() {
let message = "ten-four"; let message = "ten-four";
let mut read_mem = [0, .. 4]; let mut read_mem = [0, .. 4];
let set_cursor = 4 as u64; let set_cursor = 4 as u64;
...@@ -1096,9 +1099,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1096,9 +1099,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(read_str, message.slice(4, 8)); assert_eq!(read_str, message.slice(4, 8));
assert_eq!(tell_pos_pre_read, set_cursor); assert_eq!(tell_pos_pre_read, set_cursor);
assert_eq!(tell_pos_post_read, message.len() as u64); assert_eq!(tell_pos_post_read, message.len() as u64);
}) }
iotest!(fn file_test_io_seek_and_write() { #[test]
fn file_test_io_seek_and_write() {
let initial_msg = "food-is-yummy"; let initial_msg = "food-is-yummy";
let overwrite_msg = "-the-bar!!"; let overwrite_msg = "-the-bar!!";
let final_msg = "foo-the-bar!!"; let final_msg = "foo-the-bar!!";
...@@ -1119,9 +1123,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1119,9 +1123,10 @@ pub fn tmpdir() -> TempDir {
check!(unlink(filename)); check!(unlink(filename));
let read_str = str::from_utf8(read_mem).unwrap(); let read_str = str::from_utf8(read_mem).unwrap();
assert!(read_str.as_slice() == final_msg.as_slice()); assert!(read_str.as_slice() == final_msg.as_slice());
}) }
iotest!(fn file_test_io_seek_shakedown() { #[test]
fn file_test_io_seek_shakedown() {
use str; // 01234567890123 use str; // 01234567890123
let initial_msg = "qwer-asdf-zxcv"; let initial_msg = "qwer-asdf-zxcv";
let chunk_one: &str = "qwer"; let chunk_one: &str = "qwer";
...@@ -1150,9 +1155,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1150,9 +1155,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_one); assert_eq!(str::from_utf8(read_mem).unwrap(), chunk_one);
} }
check!(unlink(filename)); check!(unlink(filename));
}) }
iotest!(fn file_test_stat_is_correct_on_is_file() { #[test]
fn file_test_stat_is_correct_on_is_file() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let filename = &tmpdir.join("file_stat_correct_on_is_file.txt"); let filename = &tmpdir.join("file_stat_correct_on_is_file.txt");
{ {
...@@ -1168,9 +1174,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1168,9 +1174,10 @@ pub fn tmpdir() -> TempDir {
let stat_res_meth = check!(filename.stat()); let stat_res_meth = check!(filename.stat());
assert_eq!(stat_res_meth.kind, io::TypeFile); assert_eq!(stat_res_meth.kind, io::TypeFile);
check!(unlink(filename)); check!(unlink(filename));
}) }
iotest!(fn file_test_stat_is_correct_on_is_dir() { #[test]
fn file_test_stat_is_correct_on_is_dir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let filename = &tmpdir.join("file_stat_correct_on_is_dir"); let filename = &tmpdir.join("file_stat_correct_on_is_dir");
check!(mkdir(filename, io::UserRWX)); check!(mkdir(filename, io::UserRWX));
...@@ -1179,26 +1186,29 @@ pub fn tmpdir() -> TempDir { ...@@ -1179,26 +1186,29 @@ pub fn tmpdir() -> TempDir {
let stat_res_meth = check!(filename.stat()); let stat_res_meth = check!(filename.stat());
assert!(stat_res_meth.kind == io::TypeDirectory); assert!(stat_res_meth.kind == io::TypeDirectory);
check!(rmdir(filename)); check!(rmdir(filename));
}) }
iotest!(fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() { #[test]
fn file_test_fileinfo_false_when_checking_is_file_on_a_directory() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = &tmpdir.join("fileinfo_false_on_dir"); let dir = &tmpdir.join("fileinfo_false_on_dir");
check!(mkdir(dir, io::UserRWX)); check!(mkdir(dir, io::UserRWX));
assert!(dir.is_file() == false); assert!(dir.is_file() == false);
check!(rmdir(dir)); check!(rmdir(dir));
}) }
iotest!(fn file_test_fileinfo_check_exists_before_and_after_file_creation() { #[test]
fn file_test_fileinfo_check_exists_before_and_after_file_creation() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt"); let file = &tmpdir.join("fileinfo_check_exists_b_and_a.txt");
check!(File::create(file).write(b"foo")); check!(File::create(file).write(b"foo"));
assert!(file.exists()); assert!(file.exists());
check!(unlink(file)); check!(unlink(file));
assert!(!file.exists()); assert!(!file.exists());
}) }
iotest!(fn file_test_directoryinfo_check_exists_before_and_after_mkdir() { #[test]
fn file_test_directoryinfo_check_exists_before_and_after_mkdir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = &tmpdir.join("before_and_after_dir"); let dir = &tmpdir.join("before_and_after_dir");
assert!(!dir.exists()); assert!(!dir.exists());
...@@ -1207,9 +1217,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1207,9 +1217,10 @@ pub fn tmpdir() -> TempDir {
assert!(dir.is_dir()); assert!(dir.is_dir());
check!(rmdir(dir)); check!(rmdir(dir));
assert!(!dir.exists()); assert!(!dir.exists());
}) }
iotest!(fn file_test_directoryinfo_readdir() { #[test]
fn file_test_directoryinfo_readdir() {
use str; use str;
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = &tmpdir.join("di_readdir"); let dir = &tmpdir.join("di_readdir");
...@@ -1238,9 +1249,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1238,9 +1249,10 @@ pub fn tmpdir() -> TempDir {
check!(unlink(f)); check!(unlink(f));
} }
check!(rmdir(dir)); check!(rmdir(dir));
}) }
iotest!(fn file_test_walk_dir() { #[test]
fn file_test_walk_dir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = &tmpdir.join("walk_dir"); let dir = &tmpdir.join("walk_dir");
check!(mkdir(dir, io::UserRWX)); check!(mkdir(dir, io::UserRWX));
...@@ -1264,16 +1276,18 @@ pub fn tmpdir() -> TempDir { ...@@ -1264,16 +1276,18 @@ pub fn tmpdir() -> TempDir {
} }
check!(rmdir_recursive(dir)); check!(rmdir_recursive(dir));
}) }
iotest!(fn recursive_mkdir() { #[test]
fn recursive_mkdir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = tmpdir.join("d1/d2"); let dir = tmpdir.join("d1/d2");
check!(mkdir_recursive(&dir, io::UserRWX)); check!(mkdir_recursive(&dir, io::UserRWX));
assert!(dir.is_dir()) assert!(dir.is_dir())
}) }
iotest!(fn recursive_mkdir_failure() { #[test]
fn recursive_mkdir_failure() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let dir = tmpdir.join("d1"); let dir = tmpdir.join("d1");
let file = dir.join("f1"); let file = dir.join("f1");
...@@ -1287,15 +1301,17 @@ pub fn tmpdir() -> TempDir { ...@@ -1287,15 +1301,17 @@ pub fn tmpdir() -> TempDir {
error!(result, "couldn't create directory"); error!(result, "couldn't create directory");
error!(result, "mode=0700"); error!(result, "mode=0700");
error!(result, format!("path={}", file.display())); error!(result, format!("path={}", file.display()));
}) }
iotest!(fn recursive_mkdir_slash() { #[test]
fn recursive_mkdir_slash() {
check!(mkdir_recursive(&Path::new("/"), io::UserRWX)); check!(mkdir_recursive(&Path::new("/"), io::UserRWX));
}) }
// FIXME(#12795) depends on lstat to work on windows // FIXME(#12795) depends on lstat to work on windows
#[cfg(not(windows))] #[cfg(not(windows))]
iotest!(fn recursive_rmdir() { #[test]
fn recursive_rmdir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let d1 = tmpdir.join("d1"); let d1 = tmpdir.join("d1");
let dt = d1.join("t"); let dt = d1.join("t");
...@@ -1310,9 +1326,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1310,9 +1326,10 @@ pub fn tmpdir() -> TempDir {
assert!(!d1.is_dir()); assert!(!d1.is_dir());
assert!(canary.exists()); assert!(canary.exists());
}) }
iotest!(fn unicode_path_is_dir() { #[test]
fn unicode_path_is_dir() {
assert!(Path::new(".").is_dir()); assert!(Path::new(".").is_dir());
assert!(!Path::new("test/stdtest/fs.rs").is_dir()); assert!(!Path::new("test/stdtest/fs.rs").is_dir());
...@@ -1328,9 +1345,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1328,9 +1345,10 @@ pub fn tmpdir() -> TempDir {
check!(File::create(&filepath)); // ignore return; touch only check!(File::create(&filepath)); // ignore return; touch only
assert!(!filepath.is_dir()); assert!(!filepath.is_dir());
assert!(filepath.exists()); assert!(filepath.exists());
}) }
iotest!(fn unicode_path_exists() { #[test]
fn unicode_path_exists() {
assert!(Path::new(".").exists()); assert!(Path::new(".").exists());
assert!(!Path::new("test/nonexistent-bogus-path").exists()); assert!(!Path::new("test/nonexistent-bogus-path").exists());
...@@ -1340,9 +1358,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1340,9 +1358,10 @@ pub fn tmpdir() -> TempDir {
check!(mkdir(&unicode, io::UserRWX)); check!(mkdir(&unicode, io::UserRWX));
assert!(unicode.exists()); assert!(unicode.exists());
assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists()); assert!(!Path::new("test/unicode-bogus-path-각丁ー再见").exists());
}) }
iotest!(fn copy_file_does_not_exist() { #[test]
fn copy_file_does_not_exist() {
let from = Path::new("test/nonexistent-bogus-path"); let from = Path::new("test/nonexistent-bogus-path");
let to = Path::new("test/other-bogus-path"); let to = Path::new("test/other-bogus-path");
...@@ -1358,9 +1377,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1358,9 +1377,10 @@ pub fn tmpdir() -> TempDir {
assert!(!to.exists()); assert!(!to.exists());
} }
} }
}) }
iotest!(fn copy_file_ok() { #[test]
fn copy_file_ok() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let input = tmpdir.join("in.txt"); let input = tmpdir.join("in.txt");
let out = tmpdir.join("out.txt"); let out = tmpdir.join("out.txt");
...@@ -1371,9 +1391,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1371,9 +1391,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(contents.as_slice(), b"hello"); assert_eq!(contents.as_slice(), b"hello");
assert_eq!(check!(input.stat()).perm, check!(out.stat()).perm); assert_eq!(check!(input.stat()).perm, check!(out.stat()).perm);
}) }
iotest!(fn copy_file_dst_dir() { #[test]
fn copy_file_dst_dir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let out = tmpdir.join("out"); let out = tmpdir.join("out");
...@@ -1381,9 +1402,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1381,9 +1402,10 @@ pub fn tmpdir() -> TempDir {
match copy(&out, tmpdir.path()) { match copy(&out, tmpdir.path()) {
Ok(..) => fail!(), Err(..) => {} Ok(..) => fail!(), Err(..) => {}
} }
}) }
iotest!(fn copy_file_dst_exists() { #[test]
fn copy_file_dst_exists() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let input = tmpdir.join("in"); let input = tmpdir.join("in");
let output = tmpdir.join("out"); let output = tmpdir.join("out");
...@@ -1394,9 +1416,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1394,9 +1416,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(check!(File::open(&output).read_to_end()), assert_eq!(check!(File::open(&output).read_to_end()),
(Vec::from_slice(b"foo"))); (Vec::from_slice(b"foo")));
}) }
iotest!(fn copy_file_src_dir() { #[test]
fn copy_file_src_dir() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let out = tmpdir.join("out"); let out = tmpdir.join("out");
...@@ -1404,9 +1427,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1404,9 +1427,10 @@ pub fn tmpdir() -> TempDir {
Ok(..) => fail!(), Err(..) => {} Ok(..) => fail!(), Err(..) => {}
} }
assert!(!out.exists()); assert!(!out.exists());
}) }
iotest!(fn copy_file_preserves_perm_bits() { #[test]
fn copy_file_preserves_perm_bits() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let input = tmpdir.join("in.txt"); let input = tmpdir.join("in.txt");
let out = tmpdir.join("out.txt"); let out = tmpdir.join("out.txt");
...@@ -1418,10 +1442,11 @@ pub fn tmpdir() -> TempDir { ...@@ -1418,10 +1442,11 @@ pub fn tmpdir() -> TempDir {
check!(chmod(&input, io::UserFile)); check!(chmod(&input, io::UserFile));
check!(chmod(&out, io::UserFile)); check!(chmod(&out, io::UserFile));
}) }
#[cfg(not(windows))] // FIXME(#10264) operation not permitted? #[cfg(not(windows))] // FIXME(#10264) operation not permitted?
iotest!(fn symlinks_work() { #[test]
fn symlinks_work() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let input = tmpdir.join("in.txt"); let input = tmpdir.join("in.txt");
let out = tmpdir.join("out.txt"); let out = tmpdir.join("out.txt");
...@@ -1435,25 +1460,28 @@ pub fn tmpdir() -> TempDir { ...@@ -1435,25 +1460,28 @@ pub fn tmpdir() -> TempDir {
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size); assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
assert_eq!(check!(File::open(&out).read_to_end()), assert_eq!(check!(File::open(&out).read_to_end()),
(Vec::from_slice(b"foobar"))); (Vec::from_slice(b"foobar")));
}) }
#[cfg(not(windows))] // apparently windows doesn't like symlinks #[cfg(not(windows))] // apparently windows doesn't like symlinks
iotest!(fn symlink_noexist() { #[test]
fn symlink_noexist() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
// symlinks can point to things that don't exist // symlinks can point to things that don't exist
check!(symlink(&tmpdir.join("foo"), &tmpdir.join("bar"))); check!(symlink(&tmpdir.join("foo"), &tmpdir.join("bar")));
assert!(check!(readlink(&tmpdir.join("bar"))) == tmpdir.join("foo")); assert!(check!(readlink(&tmpdir.join("bar"))) == tmpdir.join("foo"));
}) }
iotest!(fn readlink_not_symlink() { #[test]
fn readlink_not_symlink() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
match readlink(tmpdir.path()) { match readlink(tmpdir.path()) {
Ok(..) => fail!("wanted a failure"), Ok(..) => fail!("wanted a failure"),
Err(..) => {} Err(..) => {}
} }
}) }
iotest!(fn links_work() { #[test]
fn links_work() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let input = tmpdir.join("in.txt"); let input = tmpdir.join("in.txt");
let out = tmpdir.join("out.txt"); let out = tmpdir.join("out.txt");
...@@ -1481,9 +1509,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1481,9 +1509,10 @@ pub fn tmpdir() -> TempDir {
Ok(..) => fail!("wanted a failure"), Ok(..) => fail!("wanted a failure"),
Err(..) => {} Err(..) => {}
} }
}) }
iotest!(fn chmod_works() { #[test]
fn chmod_works() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let file = tmpdir.join("in.txt"); let file = tmpdir.join("in.txt");
...@@ -1498,9 +1527,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1498,9 +1527,10 @@ pub fn tmpdir() -> TempDir {
} }
check!(chmod(&file, io::UserFile)); check!(chmod(&file, io::UserFile));
}) }
iotest!(fn sync_doesnt_kill_anything() { #[test]
fn sync_doesnt_kill_anything() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let path = tmpdir.join("in.txt"); let path = tmpdir.join("in.txt");
...@@ -1511,9 +1541,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1511,9 +1541,10 @@ pub fn tmpdir() -> TempDir {
check!(file.fsync()); check!(file.fsync());
check!(file.datasync()); check!(file.datasync());
drop(file); drop(file);
}) }
iotest!(fn truncate_works() { #[test]
fn truncate_works() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let path = tmpdir.join("in.txt"); let path = tmpdir.join("in.txt");
...@@ -1542,9 +1573,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1542,9 +1573,10 @@ pub fn tmpdir() -> TempDir {
assert_eq!(check!(File::open(&path).read_to_end()), assert_eq!(check!(File::open(&path).read_to_end()),
(Vec::from_slice(b"fo\0\0\0\0wut"))); (Vec::from_slice(b"fo\0\0\0\0wut")));
drop(file); drop(file);
}) }
iotest!(fn open_flavors() { #[test]
fn open_flavors() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
match File::open_mode(&tmpdir.join("a"), io::Open, io::Read) { match File::open_mode(&tmpdir.join("a"), io::Open, io::Read) {
...@@ -1602,9 +1634,10 @@ pub fn tmpdir() -> TempDir { ...@@ -1602,9 +1634,10 @@ pub fn tmpdir() -> TempDir {
} }
assert!(check!(stat(&tmpdir.join("h"))).size == 3, assert!(check!(stat(&tmpdir.join("h"))).size == 3,
"truncate didn't truncate"); "truncate didn't truncate");
}) }
iotest!(fn utime() { #[test]
fn utime() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let path = tmpdir.join("a"); let path = tmpdir.join("a");
check!(File::create(&path)); check!(File::create(&path));
...@@ -1613,18 +1646,20 @@ pub fn tmpdir() -> TempDir { ...@@ -1613,18 +1646,20 @@ pub fn tmpdir() -> TempDir {
check!(change_file_times(&path, 100000, 200000)); check!(change_file_times(&path, 100000, 200000));
assert_eq!(check!(path.stat()).accessed, 100000); assert_eq!(check!(path.stat()).accessed, 100000);
assert_eq!(check!(path.stat()).modified, 200000); assert_eq!(check!(path.stat()).modified, 200000);
}) }
iotest!(fn utime_noexist() { #[test]
fn utime_noexist() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
match change_file_times(&tmpdir.join("a"), 100, 200) { match change_file_times(&tmpdir.join("a"), 100, 200) {
Ok(..) => fail!(), Ok(..) => fail!(),
Err(..) => {} Err(..) => {}
} }
}) }
iotest!(fn binary_file() { #[test]
fn binary_file() {
use rand::{StdRng, Rng}; use rand::{StdRng, Rng};
let mut bytes = [0, ..1024]; let mut bytes = [0, ..1024];
...@@ -1635,13 +1670,14 @@ pub fn tmpdir() -> TempDir { ...@@ -1635,13 +1670,14 @@ pub fn tmpdir() -> TempDir {
check!(File::create(&tmpdir.join("test")).write(bytes)); check!(File::create(&tmpdir.join("test")).write(bytes));
let actual = check!(File::open(&tmpdir.join("test")).read_to_end()); let actual = check!(File::open(&tmpdir.join("test")).read_to_end());
assert!(actual.as_slice() == bytes); assert!(actual.as_slice() == bytes);
}) }
iotest!(fn unlink_readonly() { #[test]
fn unlink_readonly() {
let tmpdir = tmpdir(); let tmpdir = tmpdir();
let path = tmpdir.join("file"); let path = tmpdir.join("file");
check!(File::create(&path)); check!(File::create(&path));
check!(chmod(&path, io::UserRead)); check!(chmod(&path, io::UserRead));
check!(unlink(&path)); check!(unlink(&path));
}) }
} }
...@@ -265,9 +265,6 @@ fn file_product(p: &Path) -> IoResult<u32> { ...@@ -265,9 +265,6 @@ fn file_product(p: &Path) -> IoResult<u32> {
LineBufferedWriter}; LineBufferedWriter};
pub use self::comm_adapters::{ChanReader, ChanWriter}; pub use self::comm_adapters::{ChanReader, ChanWriter};
// this comes first to get the iotest! macro
pub mod test;
mod buffered; mod buffered;
mod comm_adapters; mod comm_adapters;
mod mem; mod mem;
...@@ -280,6 +277,7 @@ fn file_product(p: &Path) -> IoResult<u32> { ...@@ -280,6 +277,7 @@ fn file_product(p: &Path) -> IoResult<u32> {
pub mod process; pub mod process;
pub mod signal; pub mod signal;
pub mod stdio; pub mod stdio;
pub mod test;
pub mod timer; pub mod timer;
pub mod util; pub mod util;
......
...@@ -125,7 +125,13 @@ fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>) ...@@ -125,7 +125,13 @@ fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
// permission without help of apk // permission without help of apk
#[cfg(all(test, not(target_os = "android")))] #[cfg(all(test, not(target_os = "android")))]
mod test { mod test {
iotest!(fn dns_smoke_test() { use super::*;
use io::net::tcp::*;
use io::net::ip::*;
use io::net::udp::*;
#[test]
fn dns_smoke_test() {
let ipaddrs = get_host_addresses("localhost").unwrap(); let ipaddrs = get_host_addresses("localhost").unwrap();
let mut found_local = false; let mut found_local = false;
let local_addr = &Ipv4Addr(127, 0, 0, 1); let local_addr = &Ipv4Addr(127, 0, 0, 1);
...@@ -133,11 +139,13 @@ mod test { ...@@ -133,11 +139,13 @@ mod test {
found_local = found_local || addr == local_addr; found_local = found_local || addr == local_addr;
} }
assert!(found_local); assert!(found_local);
}) }
iotest!(fn issue_10663() { #[ignore]
#[test]
fn issue_10663() {
// Something should happen here, but this certainly shouldn't cause // Something should happen here, but this certainly shouldn't cause
// everything to die. The actual outcome we don't care too much about. // everything to die. The actual outcome we don't care too much about.
get_host_addresses("example.com").unwrap(); get_host_addresses("example.com").unwrap();
} #[ignore]) }
} }
...@@ -257,6 +257,8 @@ mod tests { ...@@ -257,6 +257,8 @@ mod tests {
use super::*; use super::*;
use io::*; use io::*;
use io::test::*; use io::test::*;
use io::fs::PathExtensions;
use time::Duration;
pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
let path1 = next_test_unix(); let path1 = next_test_unix();
...@@ -277,7 +279,8 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -277,7 +279,8 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
} }
} }
iotest!(fn bind_error() { #[test]
fn bind_error() {
let path = "path/to/nowhere"; let path = "path/to/nowhere";
match UnixListener::bind(&path) { match UnixListener::bind(&path) {
Ok(..) => fail!(), Ok(..) => fail!(),
...@@ -286,9 +289,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -286,9 +289,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
e.kind == InvalidInput); e.kind == InvalidInput);
} }
} }
}) }
iotest!(fn connect_error() { #[test]
fn connect_error() {
let path = if cfg!(windows) { let path = if cfg!(windows) {
r"\\.\pipe\this_should_not_exist_ever" r"\\.\pipe\this_should_not_exist_ever"
} else { } else {
...@@ -300,9 +304,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -300,9 +304,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
assert!(e.kind == FileNotFound || e.kind == OtherIoError); assert!(e.kind == FileNotFound || e.kind == OtherIoError);
} }
} }
}) }
iotest!(fn smoke() { #[test]
fn smoke() {
smalltest(proc(mut server) { smalltest(proc(mut server) {
let mut buf = [0]; let mut buf = [0];
server.read(buf).unwrap(); server.read(buf).unwrap();
...@@ -310,9 +315,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -310,9 +315,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
}, proc(mut client) { }, proc(mut client) {
client.write([99]).unwrap(); client.write([99]).unwrap();
}) })
}) }
iotest!(fn read_eof() { #[cfg_attr(windows, ignore)] // FIXME(#12516)
#[test]
fn read_eof() {
smalltest(proc(mut server) { smalltest(proc(mut server) {
let mut buf = [0]; let mut buf = [0];
assert!(server.read(buf).is_err()); assert!(server.read(buf).is_err());
...@@ -320,9 +327,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -320,9 +327,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
}, proc(_client) { }, proc(_client) {
// drop the client // drop the client
}) })
} #[cfg_attr(windows, ignore)]) // FIXME(#12516) }
iotest!(fn write_begone() { #[test]
fn write_begone() {
smalltest(proc(mut server) { smalltest(proc(mut server) {
let buf = [0]; let buf = [0];
loop { loop {
...@@ -340,9 +348,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -340,9 +348,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
}, proc(_client) { }, proc(_client) {
// drop the client // drop the client
}) })
}) }
iotest!(fn accept_lots() { #[test]
fn accept_lots() {
let times = 10; let times = 10;
let path1 = next_test_unix(); let path1 = next_test_unix();
let path2 = path1.clone(); let path2 = path1.clone();
...@@ -371,16 +380,18 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -371,16 +380,18 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
} }
assert_eq!(buf[0], 100); assert_eq!(buf[0], 100);
} }
}) }
#[cfg(unix)] #[cfg(unix)]
iotest!(fn path_exists() { #[test]
fn path_exists() {
let path = next_test_unix(); let path = next_test_unix();
let _acceptor = UnixListener::bind(&path).listen(); let _acceptor = UnixListener::bind(&path).listen();
assert!(path.exists()); assert!(path.exists());
}) }
iotest!(fn unix_clone_smoke() { #[test]
fn unix_clone_smoke() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut acceptor = UnixListener::bind(&addr).listen(); let mut acceptor = UnixListener::bind(&addr).listen();
...@@ -414,9 +425,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -414,9 +425,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
assert_eq!(s1.read(buf), Ok(1)); assert_eq!(s1.read(buf), Ok(1));
debug!("reader done"); debug!("reader done");
rx2.recv(); rx2.recv();
}) }
iotest!(fn unix_clone_two_read() { #[test]
fn unix_clone_two_read() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut acceptor = UnixListener::bind(&addr).listen(); let mut acceptor = UnixListener::bind(&addr).listen();
let (tx1, rx) = channel(); let (tx1, rx) = channel();
...@@ -446,9 +458,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -446,9 +458,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
tx1.send(()); tx1.send(());
rx.recv(); rx.recv();
}) }
iotest!(fn unix_clone_two_write() { #[test]
fn unix_clone_two_write() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut acceptor = UnixListener::bind(&addr).listen(); let mut acceptor = UnixListener::bind(&addr).listen();
...@@ -471,25 +484,30 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -471,25 +484,30 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
s1.write([2]).unwrap(); s1.write([2]).unwrap();
rx.recv(); rx.recv();
}) }
iotest!(fn drop_removes_listener_path() { #[cfg(not(windows))]
#[test]
fn drop_removes_listener_path() {
let path = next_test_unix(); let path = next_test_unix();
let l = UnixListener::bind(&path).unwrap(); let l = UnixListener::bind(&path).unwrap();
assert!(path.exists()); assert!(path.exists());
drop(l); drop(l);
assert!(!path.exists()); assert!(!path.exists());
} #[cfg(not(windows))]) }
iotest!(fn drop_removes_acceptor_path() { #[cfg(not(windows))]
#[test]
fn drop_removes_acceptor_path() {
let path = next_test_unix(); let path = next_test_unix();
let l = UnixListener::bind(&path).unwrap(); let l = UnixListener::bind(&path).unwrap();
assert!(path.exists()); assert!(path.exists());
drop(l.listen().unwrap()); drop(l.listen().unwrap());
assert!(!path.exists()); assert!(!path.exists());
} #[cfg(not(windows))]) }
iotest!(fn accept_timeout() { #[test]
fn accept_timeout() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut a = UnixListener::bind(&addr).unwrap().listen().unwrap(); let mut a = UnixListener::bind(&addr).unwrap().listen().unwrap();
...@@ -527,32 +545,37 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -527,32 +545,37 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
drop(UnixStream::connect(&addr2).unwrap()); drop(UnixStream::connect(&addr2).unwrap());
}); });
a.accept().unwrap(); a.accept().unwrap();
}) }
iotest!(fn connect_timeout_error() { #[test]
fn connect_timeout_error() {
let addr = next_test_unix(); let addr = next_test_unix();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err()); assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err());
}) }
iotest!(fn connect_timeout_success() { #[test]
fn connect_timeout_success() {
let addr = next_test_unix(); let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap(); let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok()); assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
}) }
iotest!(fn connect_timeout_zero() { #[test]
fn connect_timeout_zero() {
let addr = next_test_unix(); let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap(); let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_err()); assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_err());
}) }
iotest!(fn connect_timeout_negative() { #[test]
fn connect_timeout_negative() {
let addr = next_test_unix(); let addr = next_test_unix();
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap(); let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_err()); assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_err());
}) }
iotest!(fn close_readwrite_smoke() { #[test]
fn close_readwrite_smoke() {
let addr = next_test_unix(); let addr = next_test_unix();
let a = UnixListener::bind(&addr).listen().unwrap(); let a = UnixListener::bind(&addr).listen().unwrap();
let (_tx, rx) = channel::<()>(); let (_tx, rx) = channel::<()>();
...@@ -586,9 +609,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -586,9 +609,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
let _ = s2.close_write(); let _ = s2.close_write();
let _ = s3.close_read(); let _ = s3.close_read();
let _ = s3.close_write(); let _ = s3.close_write();
}) }
iotest!(fn close_read_wakes_up() { #[test]
fn close_read_wakes_up() {
let addr = next_test_unix(); let addr = next_test_unix();
let a = UnixListener::bind(&addr).listen().unwrap(); let a = UnixListener::bind(&addr).listen().unwrap();
let (_tx, rx) = channel::<()>(); let (_tx, rx) = channel::<()>();
...@@ -611,9 +635,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -611,9 +635,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
// this test will never finish if the child doesn't wake up // this test will never finish if the child doesn't wake up
rx.recv(); rx.recv();
}) }
iotest!(fn readwrite_timeouts() { #[test]
fn readwrite_timeouts() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut a = UnixListener::bind(&addr).listen().unwrap(); let mut a = UnixListener::bind(&addr).listen().unwrap();
let (tx, rx) = channel::<()>(); let (tx, rx) = channel::<()>();
...@@ -648,9 +673,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -648,9 +673,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
tx.send(()); tx.send(());
s.set_timeout(None); s.set_timeout(None);
assert_eq!(s.read([0, 0]), Ok(1)); assert_eq!(s.read([0, 0]), Ok(1));
}) }
iotest!(fn read_timeouts() { #[test]
fn read_timeouts() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut a = UnixListener::bind(&addr).listen().unwrap(); let mut a = UnixListener::bind(&addr).listen().unwrap();
let (tx, rx) = channel::<()>(); let (tx, rx) = channel::<()>();
...@@ -676,9 +702,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -676,9 +702,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
for _ in range(0u, 100) { for _ in range(0u, 100) {
assert!(s.write([0, ..128 * 1024]).is_ok()); assert!(s.write([0, ..128 * 1024]).is_ok());
} }
}) }
iotest!(fn write_timeouts() { #[test]
fn write_timeouts() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut a = UnixListener::bind(&addr).listen().unwrap(); let mut a = UnixListener::bind(&addr).listen().unwrap();
let (tx, rx) = channel::<()>(); let (tx, rx) = channel::<()>();
...@@ -702,9 +729,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -702,9 +729,10 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
tx.send(()); tx.send(());
assert!(s.read([0]).is_ok()); assert!(s.read([0]).is_ok());
}) }
iotest!(fn timeout_concurrent_read() { #[test]
fn timeout_concurrent_read() {
let addr = next_test_unix(); let addr = next_test_unix();
let mut a = UnixListener::bind(&addr).listen().unwrap(); let mut a = UnixListener::bind(&addr).listen().unwrap();
let (tx, rx) = channel::<()>(); let (tx, rx) = channel::<()>();
...@@ -729,10 +757,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -729,10 +757,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
tx.send(()); tx.send(());
rx2.recv(); rx2.recv();
}) }
#[cfg(not(windows))] #[cfg(not(windows))]
iotest!(fn clone_accept_smoke() { #[test]
fn clone_accept_smoke() {
let addr = next_test_unix(); let addr = next_test_unix();
let l = UnixListener::bind(&addr); let l = UnixListener::bind(&addr);
let mut a = l.listen().unwrap(); let mut a = l.listen().unwrap();
...@@ -749,10 +778,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -749,10 +778,11 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
assert!(a.accept().is_ok()); assert!(a.accept().is_ok());
drop(a); drop(a);
assert!(a2.accept().is_ok()); assert!(a2.accept().is_ok());
}) }
#[cfg(not(windows))] // FIXME #17553 #[cfg(not(windows))] // FIXME #17553
iotest!(fn clone_accept_concurrent() { #[test]
fn clone_accept_concurrent() {
let addr = next_test_unix(); let addr = next_test_unix();
let l = UnixListener::bind(&addr); let l = UnixListener::bind(&addr);
let a = l.listen().unwrap(); let a = l.listen().unwrap();
...@@ -774,18 +804,20 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -774,18 +804,20 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
assert!(rx.recv().is_ok()); assert!(rx.recv().is_ok());
assert!(rx.recv().is_ok()); assert!(rx.recv().is_ok());
}) }
iotest!(fn close_accept_smoke() { #[test]
fn close_accept_smoke() {
let addr = next_test_unix(); let addr = next_test_unix();
let l = UnixListener::bind(&addr); let l = UnixListener::bind(&addr);
let mut a = l.listen().unwrap(); let mut a = l.listen().unwrap();
a.close_accept().unwrap(); a.close_accept().unwrap();
assert_eq!(a.accept().err().unwrap().kind, EndOfFile); assert_eq!(a.accept().err().unwrap().kind, EndOfFile);
}) }
iotest!(fn close_accept_concurrent() { #[test]
fn close_accept_concurrent() {
let addr = next_test_unix(); let addr = next_test_unix();
let l = UnixListener::bind(&addr); let l = UnixListener::bind(&addr);
let a = l.listen().unwrap(); let a = l.listen().unwrap();
...@@ -799,5 +831,5 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) { ...@@ -799,5 +831,5 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
a2.close_accept().unwrap(); a2.close_accept().unwrap();
assert_eq!(rx.recv().err().unwrap().kind, EndOfFile); assert_eq!(rx.recv().err().unwrap().kind, EndOfFile);
}) }
} }
...@@ -523,26 +523,33 @@ fn clone(&self) -> TcpAcceptor { ...@@ -523,26 +523,33 @@ fn clone(&self) -> TcpAcceptor {
#[allow(experimental)] #[allow(experimental)]
mod test { mod test {
use super::*; use super::*;
use io::net::ip::SocketAddr; use io::net::tcp::*;
use io::net::ip::*;
use io::net::udp::*;
use io::*; use io::*;
use io::test::*;
use prelude::*; use prelude::*;
// FIXME #11530 this fails on android because tests are run as root // FIXME #11530 this fails on android because tests are run as root
iotest!(fn bind_error() { #[cfg_attr(any(windows, target_os = "android"), ignore)]
#[test]
fn bind_error() {
match TcpListener::bind("0.0.0.0", 1) { match TcpListener::bind("0.0.0.0", 1) {
Ok(..) => fail!(), Ok(..) => fail!(),
Err(e) => assert_eq!(e.kind, PermissionDenied), Err(e) => assert_eq!(e.kind, PermissionDenied),
} }
} #[cfg_attr(any(windows, target_os = "android"), ignore)]) }
iotest!(fn connect_error() { #[test]
fn connect_error() {
match TcpStream::connect("0.0.0.0", 1) { match TcpStream::connect("0.0.0.0", 1) {
Ok(..) => fail!(), Ok(..) => fail!(),
Err(e) => assert_eq!(e.kind, ConnectionRefused), Err(e) => assert_eq!(e.kind, ConnectionRefused),
} }
}) }
iotest!(fn listen_ip4_localhost() { #[test]
fn listen_ip4_localhost() {
let socket_addr = next_test_ip4(); let socket_addr = next_test_ip4();
let ip_str = socket_addr.ip.to_string(); let ip_str = socket_addr.ip.to_string();
let port = socket_addr.port; let port = socket_addr.port;
...@@ -558,9 +565,10 @@ mod test { ...@@ -558,9 +565,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 144); assert!(buf[0] == 144);
}) }
iotest!(fn connect_localhost() { #[test]
fn connect_localhost() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -575,9 +583,10 @@ mod test { ...@@ -575,9 +583,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 64); assert!(buf[0] == 64);
}) }
iotest!(fn connect_ip4_loopback() { #[test]
fn connect_ip4_loopback() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -592,9 +601,10 @@ mod test { ...@@ -592,9 +601,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 44); assert!(buf[0] == 44);
}) }
iotest!(fn connect_ip6_loopback() { #[test]
fn connect_ip6_loopback() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -609,9 +619,10 @@ mod test { ...@@ -609,9 +619,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 66); assert!(buf[0] == 66);
}) }
iotest!(fn smoke_test_ip4() { #[test]
fn smoke_test_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -626,9 +637,10 @@ mod test { ...@@ -626,9 +637,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 99); assert!(buf[0] == 99);
}) }
iotest!(fn smoke_test_ip6() { #[test]
fn smoke_test_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -643,9 +655,10 @@ mod test { ...@@ -643,9 +655,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert!(buf[0] == 99); assert!(buf[0] == 99);
}) }
iotest!(fn read_eof_ip4() { #[test]
fn read_eof_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -660,9 +673,10 @@ mod test { ...@@ -660,9 +673,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
let nread = stream.read(buf); let nread = stream.read(buf);
assert!(nread.is_err()); assert!(nread.is_err());
}) }
iotest!(fn read_eof_ip6() { #[test]
fn read_eof_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -677,9 +691,10 @@ mod test { ...@@ -677,9 +691,10 @@ mod test {
let mut buf = [0]; let mut buf = [0];
let nread = stream.read(buf); let nread = stream.read(buf);
assert!(nread.is_err()); assert!(nread.is_err());
}) }
iotest!(fn read_eof_twice_ip4() { #[test]
fn read_eof_twice_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -702,9 +717,10 @@ mod test { ...@@ -702,9 +717,10 @@ mod test {
"unknown kind: {}", e.kind); "unknown kind: {}", e.kind);
} }
} }
}) }
iotest!(fn read_eof_twice_ip6() { #[test]
fn read_eof_twice_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -727,9 +743,10 @@ mod test { ...@@ -727,9 +743,10 @@ mod test {
"unknown kind: {}", e.kind); "unknown kind: {}", e.kind);
} }
} }
}) }
iotest!(fn write_close_ip4() { #[test]
fn write_close_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -754,9 +771,10 @@ mod test { ...@@ -754,9 +771,10 @@ mod test {
} }
} }
} }
}) }
iotest!(fn write_close_ip6() { #[test]
fn write_close_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -781,9 +799,10 @@ mod test { ...@@ -781,9 +799,10 @@ mod test {
} }
} }
} }
}) }
iotest!(fn multiple_connect_serial_ip4() { #[test]
fn multiple_connect_serial_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -802,9 +821,10 @@ mod test { ...@@ -802,9 +821,10 @@ mod test {
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert_eq!(buf[0], 99); assert_eq!(buf[0], 99);
} }
}) }
iotest!(fn multiple_connect_serial_ip6() { #[test]
fn multiple_connect_serial_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -823,9 +843,10 @@ mod test { ...@@ -823,9 +843,10 @@ mod test {
stream.read(buf).unwrap(); stream.read(buf).unwrap();
assert_eq!(buf[0], 99); assert_eq!(buf[0], 99);
} }
}) }
iotest!(fn multiple_connect_interleaved_greedy_schedule_ip4() { #[test]
fn multiple_connect_interleaved_greedy_schedule_ip4() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -862,9 +883,10 @@ fn connect(i: int, addr: SocketAddr) { ...@@ -862,9 +883,10 @@ fn connect(i: int, addr: SocketAddr) {
stream.write([i as u8]).unwrap(); stream.write([i as u8]).unwrap();
}); });
} }
}) }
iotest!(fn multiple_connect_interleaved_greedy_schedule_ip6() { #[test]
fn multiple_connect_interleaved_greedy_schedule_ip6() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -901,9 +923,10 @@ fn connect(i: int, addr: SocketAddr) { ...@@ -901,9 +923,10 @@ fn connect(i: int, addr: SocketAddr) {
stream.write([i as u8]).unwrap(); stream.write([i as u8]).unwrap();
}); });
} }
}) }
iotest!(fn multiple_connect_interleaved_lazy_schedule_ip4() { #[test]
fn multiple_connect_interleaved_lazy_schedule_ip4() {
static MAX: int = 10; static MAX: int = 10;
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
...@@ -940,9 +963,10 @@ fn connect(i: int, addr: SocketAddr) { ...@@ -940,9 +963,10 @@ fn connect(i: int, addr: SocketAddr) {
stream.write([99]).unwrap(); stream.write([99]).unwrap();
}); });
} }
}) }
iotest!(fn multiple_connect_interleaved_lazy_schedule_ip6() { #[test]
fn multiple_connect_interleaved_lazy_schedule_ip6() {
static MAX: int = 10; static MAX: int = 10;
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
...@@ -979,7 +1003,7 @@ fn connect(i: int, addr: SocketAddr) { ...@@ -979,7 +1003,7 @@ fn connect(i: int, addr: SocketAddr) {
stream.write([99]).unwrap(); stream.write([99]).unwrap();
}); });
} }
}) }
pub fn socket_name(addr: SocketAddr) { pub fn socket_name(addr: SocketAddr) {
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
...@@ -1015,18 +1039,21 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1015,18 +1039,21 @@ pub fn peer_name(addr: SocketAddr) {
assert_eq!(addr, peer_name.unwrap()); assert_eq!(addr, peer_name.unwrap());
} }
iotest!(fn socket_and_peer_name_ip4() { #[test]
fn socket_and_peer_name_ip4() {
peer_name(next_test_ip4()); peer_name(next_test_ip4());
socket_name(next_test_ip4()); socket_name(next_test_ip4());
}) }
iotest!(fn socket_and_peer_name_ip6() { #[test]
fn socket_and_peer_name_ip6() {
// FIXME: peer name is not consistent // FIXME: peer name is not consistent
//peer_name(next_test_ip6()); //peer_name(next_test_ip6());
socket_name(next_test_ip6()); socket_name(next_test_ip6());
}) }
iotest!(fn partial_read() { #[test]
fn partial_read() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let port = addr.port; let port = addr.port;
let (tx, rx) = channel(); let (tx, rx) = channel();
...@@ -1048,9 +1075,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1048,9 +1075,10 @@ pub fn peer_name(addr: SocketAddr) {
assert_eq!(c.read(b), Ok(1)); assert_eq!(c.read(b), Ok(1));
c.write([1]).unwrap(); c.write([1]).unwrap();
rx.recv(); rx.recv();
}) }
iotest!(fn double_bind() { #[test]
fn double_bind() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1063,9 +1091,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1063,9 +1091,10 @@ pub fn peer_name(addr: SocketAddr) {
"unknown error: {} {}", e, e.kind); "unknown error: {} {}", e, e.kind);
} }
} }
}) }
iotest!(fn fast_rebind() { #[test]
fn fast_rebind() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let port = addr.port; let port = addr.port;
let (tx, rx) = channel(); let (tx, rx) = channel();
...@@ -1090,9 +1119,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1090,9 +1119,10 @@ pub fn peer_name(addr: SocketAddr) {
// Close listener // Close listener
} }
let _listener = TcpListener::bind(addr.ip.to_string().as_slice(), port); let _listener = TcpListener::bind(addr.ip.to_string().as_slice(), port);
}) }
iotest!(fn tcp_clone_smoke() { #[test]
fn tcp_clone_smoke() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1121,9 +1151,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1121,9 +1151,10 @@ pub fn peer_name(addr: SocketAddr) {
let mut buf = [0, 0]; let mut buf = [0, 0];
assert_eq!(s1.read(buf), Ok(1)); assert_eq!(s1.read(buf), Ok(1));
rx2.recv(); rx2.recv();
}) }
iotest!(fn tcp_clone_two_read() { #[test]
fn tcp_clone_two_read() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1155,9 +1186,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1155,9 +1186,10 @@ pub fn peer_name(addr: SocketAddr) {
tx1.send(()); tx1.send(());
rx.recv(); rx.recv();
}) }
iotest!(fn tcp_clone_two_write() { #[test]
fn tcp_clone_two_write() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1182,9 +1214,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1182,9 +1214,10 @@ pub fn peer_name(addr: SocketAddr) {
s1.write([2]).unwrap(); s1.write([2]).unwrap();
rx.recv(); rx.recv();
}) }
iotest!(fn shutdown_smoke() { #[test]
fn shutdown_smoke() {
use rt::rtio::RtioTcpStream; use rt::rtio::RtioTcpStream;
let addr = next_test_ip4(); let addr = next_test_ip4();
...@@ -1202,9 +1235,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1202,9 +1235,10 @@ pub fn peer_name(addr: SocketAddr) {
assert!(s.obj.close_write().is_ok()); assert!(s.obj.close_write().is_ok());
assert!(s.write([1]).is_err()); assert!(s.write([1]).is_err());
assert_eq!(s.read_to_end(), Ok(vec!(1))); assert_eq!(s.read_to_end(), Ok(vec!(1)));
}) }
iotest!(fn accept_timeout() { #[test]
fn accept_timeout() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1249,9 +1283,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1249,9 +1283,10 @@ pub fn peer_name(addr: SocketAddr) {
port).unwrap()); port).unwrap());
}); });
a.accept().unwrap(); a.accept().unwrap();
}) }
iotest!(fn close_readwrite_smoke() { #[test]
fn close_readwrite_smoke() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1287,9 +1322,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1287,9 +1322,10 @@ pub fn peer_name(addr: SocketAddr) {
let _ = s2.close_write(); let _ = s2.close_write();
let _ = s3.close_read(); let _ = s3.close_read();
let _ = s3.close_write(); let _ = s3.close_write();
}) }
iotest!(fn close_read_wakes_up() { #[test]
fn close_read_wakes_up() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1314,9 +1350,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1314,9 +1350,10 @@ pub fn peer_name(addr: SocketAddr) {
// this test will never finish if the child doesn't wake up // this test will never finish if the child doesn't wake up
rx.recv(); rx.recv();
}) }
iotest!(fn readwrite_timeouts() { #[test]
fn readwrite_timeouts() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1348,9 +1385,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1348,9 +1385,10 @@ pub fn peer_name(addr: SocketAddr) {
tx.send(()); tx.send(());
s.set_timeout(None); s.set_timeout(None);
assert_eq!(s.read([0, 0]), Ok(1)); assert_eq!(s.read([0, 0]), Ok(1));
}) }
iotest!(fn read_timeouts() { #[test]
fn read_timeouts() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1378,9 +1416,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1378,9 +1416,10 @@ pub fn peer_name(addr: SocketAddr) {
for _ in range(0i, 100) { for _ in range(0i, 100) {
assert!(s.write([0, ..128 * 1024]).is_ok()); assert!(s.write([0, ..128 * 1024]).is_ok());
} }
}) }
iotest!(fn write_timeouts() { #[test]
fn write_timeouts() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1407,9 +1446,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1407,9 +1446,10 @@ pub fn peer_name(addr: SocketAddr) {
tx.send(()); tx.send(());
assert!(s.read([0]).is_ok()); assert!(s.read([0]).is_ok());
}) }
iotest!(fn timeout_concurrent_read() { #[test]
fn timeout_concurrent_read() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let ip_str = addr.ip.to_string(); let ip_str = addr.ip.to_string();
let port = addr.port; let port = addr.port;
...@@ -1436,9 +1476,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1436,9 +1476,10 @@ pub fn peer_name(addr: SocketAddr) {
tx.send(()); tx.send(());
rx2.recv(); rx2.recv();
}) }
iotest!(fn clone_while_reading() { #[test]
fn clone_while_reading() {
let addr = next_test_ip6(); let addr = next_test_ip6();
let listen = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port); let listen = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port);
let mut accept = listen.listen().unwrap(); let mut accept = listen.listen().unwrap();
...@@ -1476,9 +1517,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1476,9 +1517,10 @@ pub fn peer_name(addr: SocketAddr) {
tx.send(()); tx.send(());
rxdone.recv(); rxdone.recv();
rxdone.recv(); rxdone.recv();
}) }
iotest!(fn clone_accept_smoke() { #[test]
fn clone_accept_smoke() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port); let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port);
let mut a = l.listen().unwrap(); let mut a = l.listen().unwrap();
...@@ -1493,9 +1535,10 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1493,9 +1535,10 @@ pub fn peer_name(addr: SocketAddr) {
assert!(a.accept().is_ok()); assert!(a.accept().is_ok());
assert!(a2.accept().is_ok()); assert!(a2.accept().is_ok());
}) }
iotest!(fn clone_accept_concurrent() { #[test]
fn clone_accept_concurrent() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port); let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port);
let a = l.listen().unwrap(); let a = l.listen().unwrap();
...@@ -1516,18 +1559,20 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1516,18 +1559,20 @@ pub fn peer_name(addr: SocketAddr) {
assert!(rx.recv().is_ok()); assert!(rx.recv().is_ok());
assert!(rx.recv().is_ok()); assert!(rx.recv().is_ok());
}) }
iotest!(fn close_accept_smoke() { #[test]
fn close_accept_smoke() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port); let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port);
let mut a = l.listen().unwrap(); let mut a = l.listen().unwrap();
a.close_accept().unwrap(); a.close_accept().unwrap();
assert_eq!(a.accept().err().unwrap().kind, EndOfFile); assert_eq!(a.accept().err().unwrap().kind, EndOfFile);
}) }
iotest!(fn close_accept_concurrent() { #[test]
fn close_accept_concurrent() {
let addr = next_test_ip4(); let addr = next_test_ip4();
let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port); let l = TcpListener::bind(addr.ip.to_string().as_slice(), addr.port);
let a = l.listen().unwrap(); let a = l.listen().unwrap();
...@@ -1541,5 +1586,5 @@ pub fn peer_name(addr: SocketAddr) { ...@@ -1541,5 +1586,5 @@ pub fn peer_name(addr: SocketAddr) {
a2.close_accept().unwrap(); a2.close_accept().unwrap();
assert_eq!(rx.recv().err().unwrap().kind, EndOfFile); assert_eq!(rx.recv().err().unwrap().kind, EndOfFile);
}) }
} }
...@@ -264,18 +264,24 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> { ...@@ -264,18 +264,24 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> {
#[allow(experimental)] #[allow(experimental)]
mod test { mod test {
use super::*; use super::*;
use io::net::ip::{SocketAddr}; use prelude::*;
use io::*;
use io::net::ip::*;
use io::test::*;
// FIXME #11530 this fails on android because tests are run as root // FIXME #11530 this fails on android because tests are run as root
iotest!(fn bind_error() { #[cfg_attr(any(windows, target_os = "android"), ignore)]
#[test]
fn bind_error() {
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 }; let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
match UdpSocket::bind(addr) { match UdpSocket::bind(addr) {
Ok(..) => fail!(), Ok(..) => fail!(),
Err(e) => assert_eq!(e.kind, PermissionDenied), Err(e) => assert_eq!(e.kind, PermissionDenied),
} }
} #[cfg_attr(any(windows, target_os = "android"), ignore)]) }
iotest!(fn socket_smoke_test_ip4() { #[test]
fn socket_smoke_test_ip4() {
let server_ip = next_test_ip4(); let server_ip = next_test_ip4();
let client_ip = next_test_ip4(); let client_ip = next_test_ip4();
let (tx1, rx1) = channel(); let (tx1, rx1) = channel();
...@@ -308,9 +314,10 @@ mod test { ...@@ -308,9 +314,10 @@ mod test {
Err(..) => fail!() Err(..) => fail!()
} }
rx2.recv(); rx2.recv();
}) }
iotest!(fn socket_smoke_test_ip6() { #[test]
fn socket_smoke_test_ip6() {
let server_ip = next_test_ip6(); let server_ip = next_test_ip6();
let client_ip = next_test_ip6(); let client_ip = next_test_ip6();
let (tx, rx) = channel::<()>(); let (tx, rx) = channel::<()>();
...@@ -340,9 +347,10 @@ mod test { ...@@ -340,9 +347,10 @@ mod test {
} }
Err(..) => fail!() Err(..) => fail!()
} }
}) }
iotest!(fn stream_smoke_test_ip4() { #[test]
fn stream_smoke_test_ip4() {
let server_ip = next_test_ip4(); let server_ip = next_test_ip4();
let client_ip = next_test_ip4(); let client_ip = next_test_ip4();
let (tx1, rx1) = channel(); let (tx1, rx1) = channel();
...@@ -378,9 +386,10 @@ mod test { ...@@ -378,9 +386,10 @@ mod test {
Err(..) => fail!() Err(..) => fail!()
} }
rx2.recv(); rx2.recv();
}) }
iotest!(fn stream_smoke_test_ip6() { #[test]
fn stream_smoke_test_ip6() {
let server_ip = next_test_ip6(); let server_ip = next_test_ip6();
let client_ip = next_test_ip6(); let client_ip = next_test_ip6();
let (tx1, rx1) = channel(); let (tx1, rx1) = channel();
...@@ -416,7 +425,7 @@ mod test { ...@@ -416,7 +425,7 @@ mod test {
Err(..) => fail!() Err(..) => fail!()
} }
rx2.recv(); rx2.recv();
}) }
pub fn socket_name(addr: SocketAddr) { pub fn socket_name(addr: SocketAddr) {
let server = UdpSocket::bind(addr); let server = UdpSocket::bind(addr);
...@@ -431,15 +440,18 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -431,15 +440,18 @@ pub fn socket_name(addr: SocketAddr) {
assert_eq!(addr, so_name.unwrap()); assert_eq!(addr, so_name.unwrap());
} }
iotest!(fn socket_name_ip4() { #[test]
fn socket_name_ip4() {
socket_name(next_test_ip4()); socket_name(next_test_ip4());
}) }
iotest!(fn socket_name_ip6() { #[test]
fn socket_name_ip6() {
socket_name(next_test_ip6()); socket_name(next_test_ip6());
}) }
iotest!(fn udp_clone_smoke() { #[test]
fn udp_clone_smoke() {
let addr1 = next_test_ip4(); let addr1 = next_test_ip4();
let addr2 = next_test_ip4(); let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap(); let mut sock1 = UdpSocket::bind(addr1).unwrap();
...@@ -467,9 +479,10 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -467,9 +479,10 @@ pub fn socket_name(addr: SocketAddr) {
let mut buf = [0, 0]; let mut buf = [0, 0];
assert_eq!(sock1.recv_from(buf), Ok((1, addr2))); assert_eq!(sock1.recv_from(buf), Ok((1, addr2)));
rx2.recv(); rx2.recv();
}) }
iotest!(fn udp_clone_two_read() { #[test]
fn udp_clone_two_read() {
let addr1 = next_test_ip4(); let addr1 = next_test_ip4();
let addr2 = next_test_ip4(); let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap(); let mut sock1 = UdpSocket::bind(addr1).unwrap();
...@@ -500,9 +513,10 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -500,9 +513,10 @@ pub fn socket_name(addr: SocketAddr) {
tx1.send(()); tx1.send(());
rx.recv(); rx.recv();
}) }
iotest!(fn udp_clone_two_write() { #[test]
fn udp_clone_two_write() {
let addr1 = next_test_ip4(); let addr1 = next_test_ip4();
let addr2 = next_test_ip4(); let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap(); let mut sock1 = UdpSocket::bind(addr1).unwrap();
...@@ -543,10 +557,11 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -543,10 +557,11 @@ pub fn socket_name(addr: SocketAddr) {
rx.recv(); rx.recv();
serv_rx.recv(); serv_rx.recv();
}) }
#[cfg(not(windows))] // FIXME #17553 #[cfg(not(windows))] // FIXME #17553
iotest!(fn recv_from_timeout() { #[test]
fn recv_from_timeout() {
let addr1 = next_test_ip4(); let addr1 = next_test_ip4();
let addr2 = next_test_ip4(); let addr2 = next_test_ip4();
let mut a = UdpSocket::bind(addr1).unwrap(); let mut a = UdpSocket::bind(addr1).unwrap();
...@@ -580,9 +595,10 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -580,9 +595,10 @@ pub fn socket_name(addr: SocketAddr) {
// Make sure the child didn't die // Make sure the child didn't die
rx2.recv(); rx2.recv();
}) }
iotest!(fn send_to_timeout() { #[test]
fn send_to_timeout() {
let addr1 = next_test_ip4(); let addr1 = next_test_ip4();
let addr2 = next_test_ip4(); let addr2 = next_test_ip4();
let mut a = UdpSocket::bind(addr1).unwrap(); let mut a = UdpSocket::bind(addr1).unwrap();
...@@ -596,5 +612,5 @@ pub fn socket_name(addr: SocketAddr) { ...@@ -596,5 +612,5 @@ pub fn socket_name(addr: SocketAddr) {
Err(e) => fail!("other error: {}", e), Err(e) => fail!("other error: {}", e),
} }
} }
}) }
} }
...@@ -118,7 +118,11 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> { ...@@ -118,7 +118,11 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
iotest!(fn partial_read() { use super::*;
use prelude::*;
#[test]
fn partial_read() {
use os; use os;
use io::pipe::PipeStream; use io::pipe::PipeStream;
...@@ -135,5 +139,5 @@ mod test { ...@@ -135,5 +139,5 @@ mod test {
let mut buf = [0, ..10]; let mut buf = [0, ..10];
input.read(buf).unwrap(); input.read(buf).unwrap();
tx.send(()); tx.send(());
}) }
} }
...@@ -662,39 +662,52 @@ fn drop(&mut self) { ...@@ -662,39 +662,52 @@ fn drop(&mut self) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#![allow(unused_imports)]
extern crate native; extern crate native;
use io::process::{Command, Process};
use super::*;
use prelude::*; use prelude::*;
use io::timer::*;
use io::*;
use io::fs::PathExtensions;
use time::Duration;
use str;
use rt::running_on_valgrind;
// FIXME(#10380) these tests should not all be ignored on android. // FIXME(#10380) these tests should not all be ignored on android.
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn smoke() { #[test]
fn smoke() {
let p = Command::new("true").spawn(); let p = Command::new("true").spawn();
assert!(p.is_ok()); assert!(p.is_ok());
let mut p = p.unwrap(); let mut p = p.unwrap();
assert!(p.wait().unwrap().success()); assert!(p.wait().unwrap().success());
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn smoke_failure() { #[test]
fn smoke_failure() {
match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() { match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
Ok(..) => fail!(), Ok(..) => fail!(),
Err(..) => {} Err(..) => {}
} }
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn exit_reported_right() { #[test]
fn exit_reported_right() {
let p = Command::new("false").spawn(); let p = Command::new("false").spawn();
assert!(p.is_ok()); assert!(p.is_ok());
let mut p = p.unwrap(); let mut p = p.unwrap();
assert!(p.wait().unwrap().matches_exit_status(1)); assert!(p.wait().unwrap().matches_exit_status(1));
drop(p.wait().clone()); drop(p.wait().clone());
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
iotest!(fn signal_reported_right() { #[test]
fn signal_reported_right() {
let p = Command::new("/bin/sh").arg("-c").arg("kill -1 $$").spawn(); let p = Command::new("/bin/sh").arg("-c").arg("kill -1 $$").spawn();
assert!(p.is_ok()); assert!(p.is_ok());
let mut p = p.unwrap(); let mut p = p.unwrap();
...@@ -702,7 +715,7 @@ mod tests { ...@@ -702,7 +715,7 @@ mod tests {
process::ExitSignal(1) => {}, process::ExitSignal(1) => {},
result => fail!("not terminated by signal 1 (instead, {})", result), result => fail!("not terminated by signal 1 (instead, {})", result),
} }
}) }
pub fn read_all(input: &mut Reader) -> String { pub fn read_all(input: &mut Reader) -> String {
input.read_to_string().unwrap() input.read_to_string().unwrap()
...@@ -719,23 +732,26 @@ pub fn run_output(cmd: Command) -> String { ...@@ -719,23 +732,26 @@ pub fn run_output(cmd: Command) -> String {
} }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn stdout_works() { #[test]
fn stdout_works() {
let mut cmd = Command::new("echo"); let mut cmd = Command::new("echo");
cmd.arg("foobar").stdout(CreatePipe(false, true)); cmd.arg("foobar").stdout(CreatePipe(false, true));
assert_eq!(run_output(cmd), "foobar\n".to_string()); assert_eq!(run_output(cmd), "foobar\n".to_string());
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
iotest!(fn set_cwd_works() { #[test]
fn set_cwd_works() {
let mut cmd = Command::new("/bin/sh"); let mut cmd = Command::new("/bin/sh");
cmd.arg("-c").arg("pwd") cmd.arg("-c").arg("pwd")
.cwd(&Path::new("/")) .cwd(&Path::new("/"))
.stdout(CreatePipe(false, true)); .stdout(CreatePipe(false, true));
assert_eq!(run_output(cmd), "/\n".to_string()); assert_eq!(run_output(cmd), "/\n".to_string());
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
iotest!(fn stdin_works() { #[test]
fn stdin_works() {
let mut p = Command::new("/bin/sh") let mut p = Command::new("/bin/sh")
.arg("-c").arg("read line; echo $line") .arg("-c").arg("read line; echo $line")
.stdin(CreatePipe(true, false)) .stdin(CreatePipe(true, false))
...@@ -746,21 +762,24 @@ pub fn run_output(cmd: Command) -> String { ...@@ -746,21 +762,24 @@ pub fn run_output(cmd: Command) -> String {
let out = read_all(p.stdout.get_mut_ref() as &mut Reader); let out = read_all(p.stdout.get_mut_ref() as &mut Reader);
assert!(p.wait().unwrap().success()); assert!(p.wait().unwrap().success());
assert_eq!(out, "foobar\n".to_string()); assert_eq!(out, "foobar\n".to_string());
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn detach_works() { #[test]
fn detach_works() {
let mut p = Command::new("true").detached().spawn().unwrap(); let mut p = Command::new("true").detached().spawn().unwrap();
assert!(p.wait().unwrap().success()); assert!(p.wait().unwrap().success());
}) }
#[cfg(windows)] #[cfg(windows)]
iotest!(fn uid_fails_on_windows() { #[test]
fn uid_fails_on_windows() {
assert!(Command::new("test").uid(10).spawn().is_err()); assert!(Command::new("test").uid(10).spawn().is_err());
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
iotest!(fn uid_works() { #[test]
fn uid_works() {
use libc; use libc;
let mut p = Command::new("/bin/sh") let mut p = Command::new("/bin/sh")
.arg("-c").arg("true") .arg("-c").arg("true")
...@@ -768,36 +787,40 @@ pub fn run_output(cmd: Command) -> String { ...@@ -768,36 +787,40 @@ pub fn run_output(cmd: Command) -> String {
.gid(unsafe { libc::getgid() as uint }) .gid(unsafe { libc::getgid() as uint })
.spawn().unwrap(); .spawn().unwrap();
assert!(p.wait().unwrap().success()); assert!(p.wait().unwrap().success());
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
iotest!(fn uid_to_root_fails() { #[test]
fn uid_to_root_fails() {
use libc; use libc;
// if we're already root, this isn't a valid test. Most of the bots run // if we're already root, this isn't a valid test. Most of the bots run
// as non-root though (android is an exception). // as non-root though (android is an exception).
if unsafe { libc::getuid() == 0 } { return } if unsafe { libc::getuid() == 0 } { return }
assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err()); assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_process_status() { #[test]
fn test_process_status() {
let mut status = Command::new("false").status().unwrap(); let mut status = Command::new("false").status().unwrap();
assert!(status.matches_exit_status(1)); assert!(status.matches_exit_status(1));
status = Command::new("true").status().unwrap(); status = Command::new("true").status().unwrap();
assert!(status.success()); assert!(status.success());
}) }
iotest!(fn test_process_output_fail_to_start() { #[test]
fn test_process_output_fail_to_start() {
match Command::new("/no-binary-by-this-name-should-exist").output() { match Command::new("/no-binary-by-this-name-should-exist").output() {
Err(e) => assert_eq!(e.kind, FileNotFound), Err(e) => assert_eq!(e.kind, FileNotFound),
Ok(..) => fail!() Ok(..) => fail!()
} }
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_process_output_output() { #[test]
fn test_process_output_output() {
let ProcessOutput {status, output, error} let ProcessOutput {status, output, error}
= Command::new("echo").arg("hello").output().unwrap(); = Command::new("echo").arg("hello").output().unwrap();
let output_str = str::from_utf8(output.as_slice()).unwrap(); let output_str = str::from_utf8(output.as_slice()).unwrap();
...@@ -808,33 +831,37 @@ pub fn run_output(cmd: Command) -> String { ...@@ -808,33 +831,37 @@ pub fn run_output(cmd: Command) -> String {
if !running_on_valgrind() { if !running_on_valgrind() {
assert_eq!(error, Vec::new()); assert_eq!(error, Vec::new());
} }
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_process_output_error() { #[test]
fn test_process_output_error() {
let ProcessOutput {status, output, error} let ProcessOutput {status, output, error}
= Command::new("mkdir").arg(".").output().unwrap(); = Command::new("mkdir").arg(".").output().unwrap();
assert!(status.matches_exit_status(1)); assert!(status.matches_exit_status(1));
assert_eq!(output, Vec::new()); assert_eq!(output, Vec::new());
assert!(!error.is_empty()); assert!(!error.is_empty());
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_finish_once() { #[test]
fn test_finish_once() {
let mut prog = Command::new("false").spawn().unwrap(); let mut prog = Command::new("false").spawn().unwrap();
assert!(prog.wait().unwrap().matches_exit_status(1)); assert!(prog.wait().unwrap().matches_exit_status(1));
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_finish_twice() { #[test]
fn test_finish_twice() {
let mut prog = Command::new("false").spawn().unwrap(); let mut prog = Command::new("false").spawn().unwrap();
assert!(prog.wait().unwrap().matches_exit_status(1)); assert!(prog.wait().unwrap().matches_exit_status(1));
assert!(prog.wait().unwrap().matches_exit_status(1)); assert!(prog.wait().unwrap().matches_exit_status(1));
}) }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_wait_with_output_once() { #[test]
fn test_wait_with_output_once() {
let prog = Command::new("echo").arg("hello").spawn().unwrap(); let prog = Command::new("echo").arg("hello").spawn().unwrap();
let ProcessOutput {status, output, error} = prog.wait_with_output().unwrap(); let ProcessOutput {status, output, error} = prog.wait_with_output().unwrap();
let output_str = str::from_utf8(output.as_slice()).unwrap(); let output_str = str::from_utf8(output.as_slice()).unwrap();
...@@ -845,7 +872,7 @@ pub fn run_output(cmd: Command) -> String { ...@@ -845,7 +872,7 @@ pub fn run_output(cmd: Command) -> String {
if !running_on_valgrind() { if !running_on_valgrind() {
assert_eq!(error, Vec::new()); assert_eq!(error, Vec::new());
} }
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
pub fn pwd_cmd() -> Command { pub fn pwd_cmd() -> Command {
...@@ -865,7 +892,8 @@ pub fn pwd_cmd() -> Command { ...@@ -865,7 +892,8 @@ pub fn pwd_cmd() -> Command {
cmd cmd
} }
iotest!(fn test_keep_current_working_dir() { #[test]
fn test_keep_current_working_dir() {
use os; use os;
let prog = pwd_cmd().spawn().unwrap(); let prog = pwd_cmd().spawn().unwrap();
...@@ -878,9 +906,10 @@ pub fn pwd_cmd() -> Command { ...@@ -878,9 +906,10 @@ pub fn pwd_cmd() -> Command {
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device); assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode); assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
}) }
iotest!(fn test_change_working_directory() { #[test]
fn test_change_working_directory() {
use os; use os;
// test changing to the parent of os::getcwd() because we know // test changing to the parent of os::getcwd() because we know
// the path exists (and os::getcwd() is not expected to be root) // the path exists (and os::getcwd() is not expected to be root)
...@@ -895,7 +924,7 @@ pub fn pwd_cmd() -> Command { ...@@ -895,7 +924,7 @@ pub fn pwd_cmd() -> Command {
assert_eq!(parent_stat.unstable.device, child_stat.unstable.device); assert_eq!(parent_stat.unstable.device, child_stat.unstable.device);
assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode); assert_eq!(parent_stat.unstable.inode, child_stat.unstable.inode);
}) }
#[cfg(all(unix, not(target_os="android")))] #[cfg(all(unix, not(target_os="android")))]
pub fn env_cmd() -> Command { pub fn env_cmd() -> Command {
...@@ -916,7 +945,8 @@ pub fn env_cmd() -> Command { ...@@ -916,7 +945,8 @@ pub fn env_cmd() -> Command {
} }
#[cfg(not(target_os="android"))] #[cfg(not(target_os="android"))]
iotest!(fn test_inherit_env() { #[test]
fn test_inherit_env() {
use os; use os;
if running_on_valgrind() { return; } if running_on_valgrind() { return; }
...@@ -930,9 +960,10 @@ pub fn env_cmd() -> Command { ...@@ -930,9 +960,10 @@ pub fn env_cmd() -> Command {
output.as_slice() output.as_slice()
.contains(format!("{}={}", *k, *v).as_slice())); .contains(format!("{}={}", *k, *v).as_slice()));
} }
}) }
#[cfg(target_os="android")] #[cfg(target_os="android")]
iotest!(fn test_inherit_env() { #[test]
fn test_inherit_env() {
use os; use os;
if running_on_valgrind() { return; } if running_on_valgrind() { return; }
...@@ -953,9 +984,10 @@ pub fn env_cmd() -> Command { ...@@ -953,9 +984,10 @@ pub fn env_cmd() -> Command {
*v).as_slice())); *v).as_slice()));
} }
} }
}) }
iotest!(fn test_override_env() { #[test]
fn test_override_env() {
use os; use os;
let mut new_env = vec![("RUN_TEST_NEW_ENV", "123")]; let mut new_env = vec![("RUN_TEST_NEW_ENV", "123")];
...@@ -978,18 +1010,20 @@ pub fn env_cmd() -> Command { ...@@ -978,18 +1010,20 @@ pub fn env_cmd() -> Command {
assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"), assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"),
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
}) }
iotest!(fn test_add_to_env() { #[test]
fn test_add_to_env() {
let prog = env_cmd().env("RUN_TEST_NEW_ENV", "123").spawn().unwrap(); let prog = env_cmd().env("RUN_TEST_NEW_ENV", "123").spawn().unwrap();
let result = prog.wait_with_output().unwrap(); let result = prog.wait_with_output().unwrap();
let output = str::from_utf8_lossy(result.output.as_slice()).into_string(); let output = str::from_utf8_lossy(result.output.as_slice()).into_string();
assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"), assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"),
"didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
}) }
iotest!(fn test_remove_from_env() { #[test]
fn test_remove_from_env() {
use os; use os;
// save original environment // save original environment
...@@ -1012,7 +1046,7 @@ pub fn env_cmd() -> Command { ...@@ -1012,7 +1046,7 @@ pub fn env_cmd() -> Command {
assert!(!output.as_slice().contains("RUN_TEST_NEW_ENV"), assert!(!output.as_slice().contains("RUN_TEST_NEW_ENV"),
"found RUN_TEST_NEW_ENV inside of:\n\n{}", output); "found RUN_TEST_NEW_ENV inside of:\n\n{}", output);
}) }
#[cfg(unix)] #[cfg(unix)]
pub fn sleeper() -> Process { pub fn sleeper() -> Process {
...@@ -1026,20 +1060,23 @@ pub fn sleeper() -> Process { ...@@ -1026,20 +1060,23 @@ pub fn sleeper() -> Process {
Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn().unwrap() Command::new("ping").arg("127.0.0.1").arg("-n").arg("1000").spawn().unwrap()
} }
iotest!(fn test_kill() { #[test]
fn test_kill() {
let mut p = sleeper(); let mut p = sleeper();
Process::kill(p.id(), PleaseExitSignal).unwrap(); Process::kill(p.id(), PleaseExitSignal).unwrap();
assert!(!p.wait().unwrap().success()); assert!(!p.wait().unwrap().success());
}) }
iotest!(fn test_exists() { #[test]
fn test_exists() {
let mut p = sleeper(); let mut p = sleeper();
assert!(Process::kill(p.id(), 0).is_ok()); assert!(Process::kill(p.id(), 0).is_ok());
p.signal_kill().unwrap(); p.signal_kill().unwrap();
assert!(!p.wait().unwrap().success()); assert!(!p.wait().unwrap().success());
}) }
iotest!(fn test_zero() { #[test]
fn test_zero() {
let mut p = sleeper(); let mut p = sleeper();
p.signal_kill().unwrap(); p.signal_kill().unwrap();
for _ in range(0i, 20) { for _ in range(0i, 20) {
...@@ -1050,9 +1087,10 @@ pub fn sleeper() -> Process { ...@@ -1050,9 +1087,10 @@ pub fn sleeper() -> Process {
timer::sleep(Duration::milliseconds(100)); timer::sleep(Duration::milliseconds(100));
} }
fail!("never saw the child go away"); fail!("never saw the child go away");
}) }
iotest!(fn wait_timeout() { #[test]
fn wait_timeout() {
let mut p = sleeper(); let mut p = sleeper();
p.set_timeout(Some(10)); p.set_timeout(Some(10));
assert_eq!(p.wait().err().unwrap().kind, TimedOut); assert_eq!(p.wait().err().unwrap().kind, TimedOut);
...@@ -1060,9 +1098,10 @@ pub fn sleeper() -> Process { ...@@ -1060,9 +1098,10 @@ pub fn sleeper() -> Process {
p.signal_kill().unwrap(); p.signal_kill().unwrap();
p.set_timeout(None); p.set_timeout(None);
assert!(p.wait().is_ok()); assert!(p.wait().is_ok());
}) }
iotest!(fn wait_timeout2() { #[test]
fn wait_timeout2() {
let (tx, rx) = channel(); let (tx, rx) = channel();
let tx2 = tx.clone(); let tx2 = tx.clone();
spawn(proc() { spawn(proc() {
...@@ -1081,19 +1120,21 @@ pub fn sleeper() -> Process { ...@@ -1081,19 +1120,21 @@ pub fn sleeper() -> Process {
}); });
rx.recv(); rx.recv();
rx.recv(); rx.recv();
}) }
iotest!(fn forget() { #[test]
fn forget() {
let p = sleeper(); let p = sleeper();
let id = p.id(); let id = p.id();
p.forget(); p.forget();
assert!(Process::kill(id, 0).is_ok()); assert!(Process::kill(id, 0).is_ok());
assert!(Process::kill(id, PleaseExitSignal).is_ok()); assert!(Process::kill(id, PleaseExitSignal).is_ok());
}) }
iotest!(fn dont_close_fd_on_command_spawn() { #[test]
fn dont_close_fd_on_command_spawn() {
use std::rt::rtio::{Truncate, Write}; use std::rt::rtio::{Truncate, Write};
use native::io::file; use self::native::io::file;
let path = if cfg!(windows) { let path = if cfg!(windows) {
Path::new("NUL") Path::new("NUL")
...@@ -1110,7 +1151,7 @@ pub fn sleeper() -> Process { ...@@ -1110,7 +1151,7 @@ pub fn sleeper() -> Process {
let _ = cmd.stdout(InheritFd(fdes.fd())); let _ = cmd.stdout(InheritFd(fdes.fd()));
assert!(cmd.status().unwrap().success()); assert!(cmd.status().unwrap().success());
assert!(fdes.inner_write("extra write\n".as_bytes()).is_ok()); assert!(fdes.inner_write("extra write\n".as_bytes()).is_ok());
}) }
#[test] #[test]
#[cfg(windows)] #[cfg(windows)]
......
...@@ -382,14 +382,17 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> { ...@@ -382,14 +382,17 @@ fn write(&mut self, buf: &[u8]) -> IoResult<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
iotest!(fn smoke() { use super::*;
use prelude::*;
fn smoke() {
// Just make sure we can acquire handles // Just make sure we can acquire handles
stdin(); stdin();
stdout(); stdout();
stderr(); stderr();
}) }
iotest!(fn capture_stdout() { fn capture_stdout() {
use io::{ChanReader, ChanWriter}; use io::{ChanReader, ChanWriter};
let (tx, rx) = channel(); let (tx, rx) = channel();
...@@ -399,9 +402,9 @@ mod tests { ...@@ -399,9 +402,9 @@ mod tests {
println!("hello!"); println!("hello!");
}); });
assert_eq!(r.read_to_string().unwrap(), "hello!\n".to_string()); assert_eq!(r.read_to_string().unwrap(), "hello!\n".to_string());
}) }
iotest!(fn capture_stderr() { fn capture_stderr() {
use realstd::comm::channel; use realstd::comm::channel;
use realstd::io::{Writer, ChanReader, ChanWriter, Reader}; use realstd::io::{Writer, ChanReader, ChanWriter, Reader};
...@@ -413,5 +416,5 @@ mod tests { ...@@ -413,5 +416,5 @@ mod tests {
}); });
let s = r.read_to_string().unwrap(); let s = r.read_to_string().unwrap();
assert!(s.as_slice().contains("my special message")); assert!(s.as_slice().contains("my special message"));
}) }
} }
...@@ -18,42 +18,6 @@ ...@@ -18,42 +18,6 @@
use std::io::net::ip::*; use std::io::net::ip::*;
use sync::atomic::{AtomicUint, INIT_ATOMIC_UINT, Relaxed}; use sync::atomic::{AtomicUint, INIT_ATOMIC_UINT, Relaxed};
macro_rules! iotest (
{ fn $name:ident() $b:block $(#[$a:meta])* } => (
mod $name {
#![allow(unused_imports)]
use super::super::*;
use super::*;
use io;
use prelude::*;
use io::*;
use io::fs::*;
use io::test::*;
use io::net::tcp::*;
use io::net::ip::*;
use io::net::udp::*;
#[cfg(unix)]
use io::net::pipe::*;
use io::timer::*;
use io::process::*;
use rt::running_on_valgrind;
use str;
use time::Duration;
fn f() $b
$(#[$a])* #[test] fn green() { f() }
$(#[$a])* #[test] fn native() {
use native;
let (tx, rx) = channel();
native::task::spawn(proc() { tx.send(f()) });
rx.recv();
}
}
)
)
/// Get a port number, starting at 9600, for use in tests /// Get a port number, starting at 9600, for use in tests
pub fn next_test_port() -> u16 { pub fn next_test_port() -> u16 {
static mut next_offset: AtomicUint = INIT_ATOMIC_UINT; static mut next_offset: AtomicUint = INIT_ATOMIC_UINT;
......
...@@ -232,55 +232,70 @@ fn in_ms_u64(d: Duration) -> u64 { ...@@ -232,55 +232,70 @@ fn in_ms_u64(d: Duration) -> u64 {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
iotest!(fn test_io_timer_sleep_simple() { use super::*;
use time::Duration;
use task::spawn;
use io::*;
use prelude::*;
#[test]
fn test_io_timer_sleep_simple() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.sleep(Duration::milliseconds(1)); timer.sleep(Duration::milliseconds(1));
}) }
iotest!(fn test_io_timer_sleep_oneshot() { #[test]
fn test_io_timer_sleep_oneshot() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.oneshot(Duration::milliseconds(1)).recv(); timer.oneshot(Duration::milliseconds(1)).recv();
}) }
iotest!(fn test_io_timer_sleep_oneshot_forget() { #[test]
fn test_io_timer_sleep_oneshot_forget() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.oneshot(Duration::milliseconds(100000000)); timer.oneshot(Duration::milliseconds(100000000));
}) }
iotest!(fn oneshot_twice() { #[test]
fn oneshot_twice() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx1 = timer.oneshot(Duration::milliseconds(10000)); let rx1 = timer.oneshot(Duration::milliseconds(10000));
let rx = timer.oneshot(Duration::milliseconds(1)); let rx = timer.oneshot(Duration::milliseconds(1));
rx.recv(); rx.recv();
assert_eq!(rx1.recv_opt(), Err(())); assert_eq!(rx1.recv_opt(), Err(()));
}) }
iotest!(fn test_io_timer_oneshot_then_sleep() { #[test]
fn test_io_timer_oneshot_then_sleep() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.oneshot(Duration::milliseconds(100000000)); let rx = timer.oneshot(Duration::milliseconds(100000000));
timer.sleep(Duration::milliseconds(1)); // this should invalidate rx timer.sleep(Duration::milliseconds(1)); // this should invalidate rx
assert_eq!(rx.recv_opt(), Err(())); assert_eq!(rx.recv_opt(), Err(()));
}) }
iotest!(fn test_io_timer_sleep_periodic() { #[test]
fn test_io_timer_sleep_periodic() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.periodic(Duration::milliseconds(1)); let rx = timer.periodic(Duration::milliseconds(1));
rx.recv(); rx.recv();
rx.recv(); rx.recv();
rx.recv(); rx.recv();
}) }
iotest!(fn test_io_timer_sleep_periodic_forget() { #[test]
fn test_io_timer_sleep_periodic_forget() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.periodic(Duration::milliseconds(100000000)); timer.periodic(Duration::milliseconds(100000000));
}) }
iotest!(fn test_io_timer_sleep_standalone() { #[test]
sleep(Duration::milliseconds(1)) fn test_io_timer_sleep_standalone() {
}) super::sleep(Duration::milliseconds(1))
}
iotest!(fn oneshot() { #[test]
fn oneshot() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.oneshot(Duration::milliseconds(1)); let rx = timer.oneshot(Duration::milliseconds(1));
...@@ -290,9 +305,10 @@ mod test { ...@@ -290,9 +305,10 @@ mod test {
let rx = timer.oneshot(Duration::milliseconds(1)); let rx = timer.oneshot(Duration::milliseconds(1));
rx.recv(); rx.recv();
assert!(rx.recv_opt().is_err()); assert!(rx.recv_opt().is_err());
}) }
iotest!(fn override() { #[test]
fn override() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let orx = timer.oneshot(Duration::milliseconds(100)); let orx = timer.oneshot(Duration::milliseconds(100));
let prx = timer.periodic(Duration::milliseconds(100)); let prx = timer.periodic(Duration::milliseconds(100));
...@@ -300,9 +316,10 @@ mod test { ...@@ -300,9 +316,10 @@ mod test {
assert_eq!(orx.recv_opt(), Err(())); assert_eq!(orx.recv_opt(), Err(()));
assert_eq!(prx.recv_opt(), Err(())); assert_eq!(prx.recv_opt(), Err(()));
timer.oneshot(Duration::milliseconds(1)).recv(); timer.oneshot(Duration::milliseconds(1)).recv();
}) }
iotest!(fn period() { #[test]
fn period() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.periodic(Duration::milliseconds(1)); let rx = timer.periodic(Duration::milliseconds(1));
rx.recv(); rx.recv();
...@@ -310,32 +327,40 @@ mod test { ...@@ -310,32 +327,40 @@ mod test {
let rx2 = timer.periodic(Duration::milliseconds(1)); let rx2 = timer.periodic(Duration::milliseconds(1));
rx2.recv(); rx2.recv();
rx2.recv(); rx2.recv();
}) }
iotest!(fn sleep() { #[test]
fn sleep() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.sleep(Duration::milliseconds(1)); timer.sleep(Duration::milliseconds(1));
timer.sleep(Duration::milliseconds(1)); timer.sleep(Duration::milliseconds(1));
}) }
iotest!(fn oneshot_fail() { #[test]
#[should_fail]
fn oneshot_fail() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let _rx = timer.oneshot(Duration::milliseconds(1)); let _rx = timer.oneshot(Duration::milliseconds(1));
fail!(); fail!();
} #[should_fail]) }
iotest!(fn period_fail() { #[test]
#[should_fail]
fn period_fail() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let _rx = timer.periodic(Duration::milliseconds(1)); let _rx = timer.periodic(Duration::milliseconds(1));
fail!(); fail!();
} #[should_fail]) }
iotest!(fn normal_fail() { #[test]
#[should_fail]
fn normal_fail() {
let _timer = Timer::new().unwrap(); let _timer = Timer::new().unwrap();
fail!(); fail!();
} #[should_fail]) }
iotest!(fn closing_channel_during_drop_doesnt_kill_everything() { #[test]
fn closing_channel_during_drop_doesnt_kill_everything() {
// see issue #10375 // see issue #10375
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let timer_rx = timer.periodic(Duration::milliseconds(1000)); let timer_rx = timer.periodic(Duration::milliseconds(1000));
...@@ -346,9 +371,10 @@ mod test { ...@@ -346,9 +371,10 @@ mod test {
// when we drop the TimerWatcher we're going to destroy the channel, // when we drop the TimerWatcher we're going to destroy the channel,
// which must wake up the task on the other end // which must wake up the task on the other end
}) }
iotest!(fn reset_doesnt_switch_tasks() { #[test]
fn reset_doesnt_switch_tasks() {
// similar test to the one above. // similar test to the one above.
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let timer_rx = timer.periodic(Duration::milliseconds(1000)); let timer_rx = timer.periodic(Duration::milliseconds(1000));
...@@ -358,9 +384,10 @@ mod test { ...@@ -358,9 +384,10 @@ mod test {
}); });
timer.oneshot(Duration::milliseconds(1)); timer.oneshot(Duration::milliseconds(1));
}) }
iotest!(fn reset_doesnt_switch_tasks2() { #[test]
fn reset_doesnt_switch_tasks2() {
// similar test to the one above. // similar test to the one above.
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let timer_rx = timer.periodic(Duration::milliseconds(1000)); let timer_rx = timer.periodic(Duration::milliseconds(1000));
...@@ -370,80 +397,90 @@ mod test { ...@@ -370,80 +397,90 @@ mod test {
}); });
timer.sleep(Duration::milliseconds(1)); timer.sleep(Duration::milliseconds(1));
}) }
iotest!(fn sender_goes_away_oneshot() { #[test]
fn sender_goes_away_oneshot() {
let rx = { let rx = {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.oneshot(Duration::milliseconds(1000)) timer.oneshot(Duration::milliseconds(1000))
}; };
assert_eq!(rx.recv_opt(), Err(())); assert_eq!(rx.recv_opt(), Err(()));
}) }
iotest!(fn sender_goes_away_period() { #[test]
fn sender_goes_away_period() {
let rx = { let rx = {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.periodic(Duration::milliseconds(1000)) timer.periodic(Duration::milliseconds(1000))
}; };
assert_eq!(rx.recv_opt(), Err(())); assert_eq!(rx.recv_opt(), Err(()));
}) }
iotest!(fn receiver_goes_away_oneshot() { #[test]
fn receiver_goes_away_oneshot() {
let mut timer1 = Timer::new().unwrap(); let mut timer1 = Timer::new().unwrap();
timer1.oneshot(Duration::milliseconds(1)); timer1.oneshot(Duration::milliseconds(1));
let mut timer2 = Timer::new().unwrap(); let mut timer2 = Timer::new().unwrap();
// while sleeping, the previous timer should fire and not have its // while sleeping, the previous timer should fire and not have its
// callback do something terrible. // callback do something terrible.
timer2.sleep(Duration::milliseconds(2)); timer2.sleep(Duration::milliseconds(2));
}) }
iotest!(fn receiver_goes_away_period() { #[test]
fn receiver_goes_away_period() {
let mut timer1 = Timer::new().unwrap(); let mut timer1 = Timer::new().unwrap();
timer1.periodic(Duration::milliseconds(1)); timer1.periodic(Duration::milliseconds(1));
let mut timer2 = Timer::new().unwrap(); let mut timer2 = Timer::new().unwrap();
// while sleeping, the previous timer should fire and not have its // while sleeping, the previous timer should fire and not have its
// callback do something terrible. // callback do something terrible.
timer2.sleep(Duration::milliseconds(2)); timer2.sleep(Duration::milliseconds(2));
}) }
iotest!(fn sleep_zero() { #[test]
fn sleep_zero() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.sleep(Duration::milliseconds(0)); timer.sleep(Duration::milliseconds(0));
}) }
iotest!(fn sleep_negative() { #[test]
fn sleep_negative() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
timer.sleep(Duration::milliseconds(-1000000)); timer.sleep(Duration::milliseconds(-1000000));
}) }
iotest!(fn oneshot_zero() { #[test]
fn oneshot_zero() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.oneshot(Duration::milliseconds(0)); let rx = timer.oneshot(Duration::milliseconds(0));
rx.recv(); rx.recv();
}) }
iotest!(fn oneshot_negative() { #[test]
fn oneshot_negative() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.oneshot(Duration::milliseconds(-1000000)); let rx = timer.oneshot(Duration::milliseconds(-1000000));
rx.recv(); rx.recv();
}) }
iotest!(fn periodic_zero() { #[test]
fn periodic_zero() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.periodic(Duration::milliseconds(0)); let rx = timer.periodic(Duration::milliseconds(0));
rx.recv(); rx.recv();
rx.recv(); rx.recv();
rx.recv(); rx.recv();
rx.recv(); rx.recv();
}) }
iotest!(fn periodic_negative() { #[test]
fn periodic_negative() {
let mut timer = Timer::new().unwrap(); let mut timer = Timer::new().unwrap();
let rx = timer.periodic(Duration::milliseconds(-1000000)); let rx = timer.periodic(Duration::milliseconds(-1000000));
rx.recv(); rx.recv();
rx.recv(); rx.recv();
rx.recv(); rx.recv();
rx.recv(); rx.recv();
}) }
} }
...@@ -20,12 +20,8 @@ ...@@ -20,12 +20,8 @@
extern crate libc; extern crate libc;
extern crate native;
use std::io::{Process, Command, timer}; use std::io::{Process, Command, timer};
use std::time::Duration; use std::time::Duration;
use libc;
use std::str; use std::str;
macro_rules! succeed( ($e:expr) => ( macro_rules! succeed( ($e:expr) => (
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
use std::time::Duration; use std::time::Duration;
fn main() { fn main() {
native::task::spawn(proc() customtask()); std::task::spawn(proc() customtask());
} }
fn customtask() { fn customtask() {
......
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// ignore-windows // ignore-test
// ignore-android
// FIXME: this test is being ignored until signals are implemented
// This test ensures that the 'detach' field on processes does the right thing. // This test ensures that the 'detach' field on processes does the right thing.
// By detaching the child process, they should be put into a separate process // By detaching the child process, they should be put into a separate process
......
...@@ -30,17 +30,13 @@ ...@@ -30,17 +30,13 @@
#[cfg_attr(target_os = "freebsd", ignore)] #[cfg_attr(target_os = "freebsd", ignore)]
fn eventual_timeout() { fn eventual_timeout() {
use native;
let addr = next_test_ip4(); let addr = next_test_ip4();
let host = addr.ip.to_string(); let host = addr.ip.to_string();
let port = addr.port; let port = addr.port;
// Use a native task to receive connections because it turns out libuv is
// really good at accepting connections and will likely run out of file
// descriptors before timing out.
let (tx1, rx1) = channel(); let (tx1, rx1) = channel();
let (_tx2, rx2) = channel::<()>(); let (_tx2, rx2) = channel::<()>();
native::task::spawn(proc() { std::task::spawn(proc() {
let _l = TcpListener::bind(host.as_slice(), port).unwrap().listen(); let _l = TcpListener::bind(host.as_slice(), port).unwrap().listen();
tx1.send(()); tx1.send(());
let _ = rx2.recv_opt(); let _ = rx2.recv_opt();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册