mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Improve docs of more system calls
This change also found a few POSIX compliance bugs with errnos. Another bug was discovered where, on Windows, pread() and pwrite() could modify the file position in cases where ReadFile() returned an error e.g. when seeking past the end of file. We also have more tests!
This commit is contained in:
parent
af24c19db3
commit
ccbae7799e
39 changed files with 589 additions and 175 deletions
|
@ -16,18 +16,22 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/safemacros.internal.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
int64_t fd;
|
||||
struct stat st;
|
||||
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
@ -36,15 +40,67 @@ void SetUpOnce(void) {
|
|||
ASSERT_SYS(0, 0, pledge("stdio rpath wpath cpath", 0));
|
||||
}
|
||||
|
||||
TEST(ftruncate, test) {
|
||||
ASSERT_NE(-1, (fd = creat("foo", 0755)));
|
||||
ASSERT_EQ(5, write(fd, "hello", 5));
|
||||
errno = 31337;
|
||||
ASSERT_NE(-1, ftruncate(fd, 31337));
|
||||
EXPECT_EQ(31337, errno);
|
||||
ASSERT_EQ(5, write(fd, "world", 5));
|
||||
ASSERT_NE(-1, close(fd));
|
||||
ASSERT_NE(-1, stat("foo", &st));
|
||||
ASSERT_EQ(31337, st.st_size);
|
||||
ASSERT_BINEQ(u"helloworld", gc(xslurp("foo", 0)));
|
||||
TEST(ftruncate, ebadf) {
|
||||
ASSERT_SYS(EBADF, -1, ftruncate(-1, 0));
|
||||
ASSERT_SYS(EBADF, -1, ftruncate(+3, 0));
|
||||
}
|
||||
|
||||
TEST(ftruncate, negativeLength_einval) {
|
||||
ASSERT_SYS(0, 3, creat("foo", 0755));
|
||||
ASSERT_SYS(EINVAL, -1, ftruncate(3, -1));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(ftruncate, doesntHaveWritePermission_einval) {
|
||||
ASSERT_SYS(0, 3, creat("foo", 0755));
|
||||
ASSERT_SYS(0, 5, write(3, "hello", 5));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, open("foo", O_RDONLY));
|
||||
ASSERT_SYS(EINVAL, -1, ftruncate(3, 0));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(ftruncate, pipeFd_einval) {
|
||||
int fds[2];
|
||||
ASSERT_SYS(0, 0, pipe(fds));
|
||||
ASSERT_SYS(EINVAL, -1, ftruncate(3, 0));
|
||||
EXPECT_SYS(0, 0, close(4));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(ftruncate, efbig) {
|
||||
// FreeBSD and RHEL7 return 0 (why??)
|
||||
if (IsLinux() || IsFreebsd()) return;
|
||||
sighandler_t old = signal(SIGXFSZ, SIG_IGN);
|
||||
ASSERT_SYS(0, 3, creat("foo", 0755));
|
||||
ASSERT_SYS(IsWindows() ? EINVAL : EFBIG, -1, ftruncate(3, INT64_MAX));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
signal(SIGXFSZ, old);
|
||||
}
|
||||
|
||||
TEST(ftruncate, test) {
|
||||
char got[512], want[512] = "helloworld"; // tests zero-extending
|
||||
ASSERT_SYS(0, 3, open("foo", O_CREAT | O_TRUNC | O_RDWR, 0755));
|
||||
ASSERT_SYS(0, 5, write(3, "hello", 5));
|
||||
ASSERT_SYS(0, 0, ftruncate(3, 8192));
|
||||
ASSERT_SYS(0, 5, lseek(3, 0, SEEK_CUR)); // doesn't change position
|
||||
ASSERT_SYS(0, 5, write(3, "world", 5));
|
||||
ASSERT_SYS(0, 0, fstat(3, &st));
|
||||
ASSERT_EQ(8192, st.st_size); // 8192 is logical size
|
||||
if (IsWindows() || IsNetbsd() || IsOpenbsd()) { //
|
||||
ASSERT_EQ(8192 / 512, st.st_blocks); // 8192 is physical size
|
||||
} else if (IsFreebsd()) { //
|
||||
ASSERT_EQ(512 / 512, st.st_blocks); // 512 is physical size
|
||||
} else if (IsLinux() || IsXnu()) { //
|
||||
ASSERT_EQ(4096 / 512, st.st_blocks); // 4096 is physical size
|
||||
} else {
|
||||
notpossible;
|
||||
}
|
||||
ASSERT_SYS(0, 512, pread(3, got, 512, 0));
|
||||
ASSERT_EQ(0, memcmp(want, got, 512));
|
||||
ASSERT_SYS(0, 0, ftruncate(3, 0)); // shrink file to be empty
|
||||
ASSERT_SYS(0, 0, pread(3, got, 512, 0));
|
||||
ASSERT_SYS(0, 0, read(3, got, 512));
|
||||
ASSERT_SYS(0, 10, lseek(3, 0, SEEK_CUR)); // position stays past eof
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue