Files
wasmtime/wasi-misc-tests/src/bin/path_rename.rs
Jakub Konka 3793fa3b09 Merge wasi-misc-tests repo as a subdir (#174)
* Initial checkin.

* Update to rust-lang libc.

* Add a .gitignore file.

* Factor out functions for cleaning up files and directories.

* Fix a typo in a comment.

* Print a "Success!" message if all tests passed.

* Factor out code for creating directories.

* Add wrappers around WASI functions.

These wrappers handle converting from &str to pointer+length and handle
unsafe.

* More refactoring.

* Refactor a fd_close helper.

* Move utility functions into a separate file.

* cargo update

* Add a basic test for random_get.

* Test that directories aren't resizable.

* Test clearing __WASI_RIGHT_PATH_FILESTAT_SET_SIZE.

Ensure that clearing __WASI_RIGHT_PATH_FILESTAT_SET_SIZE succeeds before
testing file truncation.

* cargo update

* Modularise tests for easier use with wasi-common crate

* Add a Code of Conduct and CONTRIBUTING.md.

* Fix typo

* Add testcase for fd_allocate

* Add positive test for fd_renumber

* Assert bufused in readlink_no_buffer testcase

* Add positive readlink testcase

* Add testcase for fd_seek and fd_tell

* Add fd_p{read, write} test

* Add README

* Add cases with trailing slashes to interesting_paths

* Split nofollow_errors testcase into two

* nofollow_errors now operators on symlinks to existing resources
* dangling_symlink covers danling symlinks tests

* Factor out a `create_file` helper function.

* Switch from the error crate to `std::io::Error::last_os_error()`.

* Use `create_file` in the readlink test too.

* Add a test for fd_filestat_set_*

* Minor refactoring

Add missing cleanup_file calls to file_pread_pwrite and
file_seek_tell.

* Add testcase for unbuffered fd_write; fixes #11

* Add testcase for path_rename

* Use the wasi crate.

Switch from depending on libc to depending on the new wasi crate to provide
the low-level WASI interfaces.

See also https://github.com/rust-lang/libc/pull/1461.

* Add a test for path_filestat_*

* Add a test for fd_readdir

* Use expect instead of unwrap

* Add a check for ino.

* Fix the build

* Don't assume a specific order of dirents

* Better test

* Test cookie value

* Fix file types

* Fix the test

* Fix the test

* Fix the test

* Cleanup

* Minor formatting tidying in README.md.

* Fix miscellaneous clippy warnings.

* Rename the crate to wasi-misc-tests.

* Update to wasi 0.7.0.

This switches from using the libc wasi bindings to using the wasi
crate's bindings. This eliminates a git dependency on libc, updates
to the new-style bindings which use Result where possible, and treats
functions that operate on raw file descriptors as unsafe.

* Add various tests for trailing-slash behavior.

* Sync new testcases with latest upstream

* Fix path_filestat testcase

* Add smoke test for fd_advise

This test is a true smoke test as it only tests whether issuing
an advise call to the host's kernel doesn't yield an error. The
consequence of issuing such a syscall is not tested.

* Check if CLOCK_MONOTONIC is actually monotonic

* Refactor the inequality assertions for more debuggable errors.

* Bump libc from 0.2.62 to 0.2.65

Bumps [libc](https://github.com/rust-lang/libc) from 0.2.62 to 0.2.65.
- [Release notes](https://github.com/rust-lang/libc/releases)
- [Commits](https://github.com/rust-lang/libc/compare/0.2.62...0.2.65)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Fix compilation error

* Enable Actions and add rust.yml (#35)

* Enable Actions and add rust.yml

This commit enables Github Actions and adds corresponding configuration in rust.yml file.

* Update rust.yml

* Fix formatting

* Add empty .rustfmt.toml config file

* Add badge to README

* Update README

* Clean up Github Actions and README

* Add test case for `poll_oneoff` syscall (#38)

* Add test case for `poll_oneoff` syscall

This commit adds a test case for `poll_oneoff` syscall. In particular,
it builds on the excellent test use case provided by @dunnock in their
repo [poll_oneoff_tests] (thanks!), and tests:
* simple timeout
* stdin read with timeout
* fd read and fd write polls

[poll_oneoff_tests]: https://github.com/dunnock/poll_oneoff_tests

* Apply suggestions and negative test for bad fd

Co-authored-by: Maxim Vorobjov <maxim.vorobjov@gmail.com>

* Add smoke test for STDOUT/ERR readwrite poll

* Add comment on stdin/out/err

* Add a test for `*at`-style functions returning `ENOTDIR` when `dirfd` is not a dir.

* Remove misc_testsuite submodule

* Add "publish=false" to Cargo.toml; remove LICENSE
2019-11-07 13:58:57 +01:00

260 lines
6.5 KiB
Rust

use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi::wasi_unstable;
use wasi_misc_tests::open_scratch_directory;
use wasi_misc_tests::utils::{cleanup_dir, cleanup_file, close_fd, create_dir, create_file};
use wasi_misc_tests::wasi_wrappers::{wasi_path_open, wasi_path_rename};
unsafe fn test_path_rename(dir_fd: wasi_unstable::Fd) {
// First, try renaming a dir to nonexistent path
// Create source directory
create_dir(dir_fd, "source");
// Try renaming the directory
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a directory"
);
// Check that source directory doesn't exist anymore
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"source",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path as a directory"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
// Check that target directory exists
status = wasi_path_open(
dir_fd,
0,
"target",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path as a directory"
);
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_dir(dir_fd, "target");
// Now, try renaming renaming a dir to existing empty dir
create_dir(dir_fd, "source");
create_dir(dir_fd, "target");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a directory"
);
// Check that source directory doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"source",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path as a directory"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
// Check that target directory exists
status = wasi_path_open(
dir_fd,
0,
"target",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path as a directory"
);
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_dir(dir_fd, "target");
// Now, try renaming a dir to existing non-empty dir
create_dir(dir_fd, "source");
create_dir(dir_fd, "target");
create_file(dir_fd, "target/file");
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target"),
Err(wasi_unstable::ENOTEMPTY),
"renaming directory to a nonempty directory"
);
// Try renaming dir to a file
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target/file"),
Err(wasi_unstable::ENOTDIR),
"renaming directory to a file"
);
cleanup_file(dir_fd, "target/file");
cleanup_dir(dir_fd, "target");
cleanup_dir(dir_fd, "source");
// Now, try renaming a file to a nonexistent path
create_file(dir_fd, "source");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a file"
);
// Check that source file doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(dir_fd, 0, "source", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
// Check that target file exists
status = wasi_path_open(dir_fd, 0, "target", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path"
);
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_file(dir_fd, "target");
// Now, try renaming file to an existing file
create_file(dir_fd, "source");
create_file(dir_fd, "target");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming file to another existing file"
);
// Check that source file doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(dir_fd, 0, "source", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
// Check that target file exists
status = wasi_path_open(dir_fd, 0, "target", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path"
);
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_file(dir_fd, "target");
// Try renaming to an (empty) directory instead
create_file(dir_fd, "source");
create_dir(dir_fd, "target");
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target"),
Err(wasi_unstable::EISDIR),
"renaming file to existing directory"
);
cleanup_dir(dir_fd, "target");
cleanup_file(dir_fd, "source");
}
fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};
// Run the tests.
unsafe { test_path_rename(dir_fd) }
}