mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 00:32:29 +00:00
Do some work on redbean
- Rewrite Slurp() API to be like string.sub() - Introduce a new Barf() API for creating files - Update Redbean `-S` sandbox flag to do unveiling
This commit is contained in:
parent
742251dd92
commit
48ce3ad7cc
9 changed files with 296 additions and 33 deletions
|
@ -29,13 +29,13 @@ __attribute__((__constructor__)) static void init(void) {
|
|||
errno = 0;
|
||||
}
|
||||
|
||||
TEST(dog, testReadPastEof_returnsZero) {
|
||||
TEST(pread, testReadPastEof_returnsZero) {
|
||||
EXPECT_NE(-1, (fd = open("a", O_RDWR | O_CREAT | O_TRUNC, 0644)));
|
||||
EXPECT_EQ(0, pread(fd, buf, 8, 0));
|
||||
EXPECT_EQ(0, close(fd));
|
||||
}
|
||||
|
||||
TEST(dog, testReadOverlapsEof_returnsShortNumber) {
|
||||
TEST(pread, testReadOverlapsEof_returnsShortNumber) {
|
||||
EXPECT_NE(-1, (fd = open("b", O_RDWR | O_CREAT | O_TRUNC, 0644)));
|
||||
EXPECT_EQ(4, pwrite(fd, buf, 4, 0));
|
||||
EXPECT_EQ(4, pread(fd, buf, 8, 0));
|
||||
|
|
39
test/libc/calls/pwrite_test.c
Normal file
39
test/libc/calls/pwrite_test.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
char buf[8];
|
||||
struct stat st;
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
__attribute__((__constructor__)) static void init(void) {
|
||||
pledge("stdio rpath wpath cpath fattr", 0);
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
TEST(pwrite, testWritePastEof_extendsFile) {
|
||||
EXPECT_SYS(0, 3, creat("foo", 0644));
|
||||
EXPECT_SYS(0, 8, pwrite(3, buf, 8, 100));
|
||||
EXPECT_SYS(0, 0, fstat(3, &st));
|
||||
EXPECT_EQ(108, st.st_size);
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
50
test/tool/net/slurp_test.lua
Normal file
50
test/tool/net/slurp_test.lua
Normal file
|
@ -0,0 +1,50 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
tmpdir = "o/tmp/lunix_test.%d" % {unix.getpid()}
|
||||
|
||||
local function Path(name)
|
||||
return tmpdir .. '/' .. name
|
||||
end
|
||||
|
||||
local function SlurpTest()
|
||||
|
||||
data = 'abc123' * 5000
|
||||
assert(Barf(Path('foo'), data))
|
||||
assert(assert(Slurp(Path('foo'))) == data)
|
||||
assert(assert(Slurp(Path('foo'), 2, 3)) == 'bc')
|
||||
assert(assert(Slurp(Path('foo'), 2)) == data:sub(2))
|
||||
assert(assert(Slurp(Path('foo'), 2, 1)) == data:sub(2, 1))
|
||||
assert(assert(Slurp(Path('foo'), 2, 2)) == data:sub(2, 2))
|
||||
assert(assert(Slurp(Path('foo'), 1, 2)) == data:sub(1, 2))
|
||||
assert(assert(Slurp(Path('foo'), -3, -1)) == data:sub(-3, -1))
|
||||
|
||||
assert(Barf(Path('foo'), 'XX', 0, 0, 3))
|
||||
assert(assert(Slurp(Path('foo'), 1, 6)) == 'abXX23')
|
||||
|
||||
end
|
||||
|
||||
local function main()
|
||||
assert(unix.makedirs(tmpdir))
|
||||
ok, err = pcall(SlurpTest)
|
||||
if ok then
|
||||
assert(unix.rmrf(tmpdir))
|
||||
else
|
||||
print(err)
|
||||
error('SlurpTest failed (%s)' % {tmpdir})
|
||||
end
|
||||
end
|
||||
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue