mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
63
test/libc/sock/inet_ntop_test.c
Normal file
63
test/libc/sock/inet_ntop_test.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/errno.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(inet_ntop, test) {
|
||||
char buf[16];
|
||||
uint8_t localhost[4] = {127, 0, 0, 1};
|
||||
EXPECT_STREQ("127.0.0.1", inet_ntop(AF_INET, localhost, buf, sizeof(buf)));
|
||||
}
|
||||
|
||||
TEST(inet_ntop, testMax) {
|
||||
char buf[16];
|
||||
uint8_t localhost[4] = {255, 255, 255, 255};
|
||||
EXPECT_STREQ("255.255.255.255",
|
||||
inet_ntop(AF_INET, localhost, buf, sizeof(buf)));
|
||||
}
|
||||
|
||||
TEST(inet_ntop, testBadFamily) {
|
||||
char buf[16] = "hi";
|
||||
uint8_t localhost[4] = {127, 0, 0, 1};
|
||||
ASSERT_EQ(NULL, inet_ntop(666, localhost, buf, sizeof(buf)));
|
||||
EXPECT_EQ(EAFNOSUPPORT, errno);
|
||||
ASSERT_STREQ("", buf);
|
||||
}
|
||||
|
||||
TEST(inet_ntop, testNoSpace) {
|
||||
char *buf = memcpy(tmalloc(16), "hi", 3);
|
||||
uint8_t localhost[4] = {127, 0, 0, 1};
|
||||
ASSERT_EQ(NULL, inet_ntop(AF_INET, localhost, buf, 0));
|
||||
EXPECT_EQ(ENOSPC, errno);
|
||||
ASSERT_STREQ("hi", buf);
|
||||
ASSERT_EQ(NULL, inet_ntop(AF_INET, localhost, buf, 7));
|
||||
ASSERT_STREQ("", buf);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(inet_ntop, testSqueeze) {
|
||||
char *buf = memcpy(tmalloc(8), "hi", 3);
|
||||
uint8_t localhost[4] = {0, 0, 0, 0};
|
||||
ASSERT_STREQ("0.0.0.0", inet_ntop(AF_INET, localhost, buf, 8));
|
||||
tfree(buf);
|
||||
}
|
42
test/libc/sock/inet_pton_test.c
Normal file
42
test/libc/sock/inet_pton_test.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/progn.h"
|
||||
#include "libc/bits/safemacros.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/inaddr.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(inet_pton, testLocalhost) {
|
||||
uint32_t addr;
|
||||
EXPECT_EQ(htonl(INADDR_LOOPBACK),
|
||||
PROGN(ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &addr)), addr));
|
||||
}
|
||||
|
||||
TEST(inet_pton, testBadAddresses) {
|
||||
uint32_t addr;
|
||||
ASSERT_EQ(0, inet_pton(AF_INET, "127.0.0", &addr));
|
||||
ASSERT_EQ(0, inet_pton(AF_INET, "256.0.0.1", &addr));
|
||||
}
|
||||
|
||||
TEST(inet_pton, testBadFamily) {
|
||||
uint32_t addr = 666;
|
||||
ASSERT_EQ(-1, inet_pton(666, "127.0.0.1", &addr));
|
||||
}
|
80
test/libc/sock/poll_test.c
Normal file
80
test/libc/sock/poll_test.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/limits.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "tool/decode/lib/flagger.h"
|
||||
#include "tool/decode/lib/pollnames.h"
|
||||
|
||||
#if 0 /* todo(jart): fix me */
|
||||
|
||||
#define POLL(FDS, TIMEOUT) \
|
||||
poll(((struct pollfd[])FDS), ARRAYLEN(((struct pollfd[])FDS)), TIMOUT)
|
||||
|
||||
nodiscard char *FormatPollFd(struct pollfd *pol) {
|
||||
return xasprintf("fd:%d revents:%s", pol->fd,
|
||||
gc(recreateflags(kPollNames, pol->revents)));
|
||||
}
|
||||
|
||||
TEST(poll, testNegativeOneFd_completelyIgnored) {
|
||||
struct pollfd fds[] = {{-1}};
|
||||
EXPECT_EQ(0, poll(fds, ARRAYLEN(fds), 0));
|
||||
EXPECT_STREQ("fd:-1 revents:0", gc(FormatPollFd(&fds[0])));
|
||||
}
|
||||
|
||||
TEST(poll, demo) {
|
||||
int rw[2];
|
||||
char buf[2] = "hi";
|
||||
ASSERT_NE(-1, pipe(rw));
|
||||
ASSERT_EQ(2, write(rw[1], buf, sizeof(buf))); /* produce */
|
||||
{
|
||||
struct pollfd fds[] = {{rw[0], POLLIN}, {rw[1], POLLOUT}};
|
||||
EXPECT_EQ(2, poll(fds, ARRAYLEN(fds), 0));
|
||||
system(gc(xasprintf("ls -l /proc/%d/fd", getpid())));
|
||||
EXPECT_STREQ("fd:3 revents:POLLIN", gc(FormatPollFd(fds + 0)));
|
||||
EXPECT_STREQ("fd:4 revents:POLLOUT", gc(FormatPollFd(&fds[1])));
|
||||
}
|
||||
ASSERT_EQ(2, read(rw[0], buf, sizeof(buf))); /* consume */
|
||||
{
|
||||
struct pollfd fds[] = {{rw[0], POLLIN}, {rw[1], POLLOUT}};
|
||||
EXPECT_EQ(1, poll(fds, ARRAYLEN(fds), 0));
|
||||
EXPECT_STREQ("fd:3 revents:0", gc(FormatPollFd(&fds[0])));
|
||||
EXPECT_STREQ("fd:4 revents:POLLOUT", gc(FormatPollFd(&fds[1])));
|
||||
}
|
||||
ASSERT_NE(-1, close(rw[1])); /* close producer */
|
||||
{
|
||||
struct pollfd fds[] = {{rw[0], POLLIN}, {rw[1], POLLOUT}};
|
||||
EXPECT_EQ(2, poll(fds, ARRAYLEN(fds), 0));
|
||||
EXPECT_STREQ("fd:3 revents:POLLHUP", gc(FormatPollFd(&fds[0])));
|
||||
EXPECT_STREQ("fd:4 revents:POLLNVAL", gc(FormatPollFd(&fds[1])));
|
||||
}
|
||||
ASSERT_NE(-1, close(rw[0])); /* close consumer */
|
||||
}
|
||||
|
||||
#endif
|
61
test/libc/sock/test.mk
Normal file
61
test/libc/sock/test.mk
Normal file
|
@ -0,0 +1,61 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
|
||||
|
||||
PKGS += TEST_LIBC_SOCK
|
||||
|
||||
TEST_LIBC_SOCK_SRCS := $(wildcard test/libc/sock/*.c)
|
||||
TEST_LIBC_SOCK_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_SOCK_SRCS))
|
||||
TEST_LIBC_SOCK_COMS = $(TEST_LIBC_SOCK_OBJS:%.o=%.com)
|
||||
|
||||
TEST_LIBC_SOCK_OBJS = \
|
||||
$(TEST_LIBC_SOCK_SRCS:%=o/$(MODE)/%.zip.o) \
|
||||
$(TEST_LIBC_SOCK_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TEST_LIBC_SOCK_BINS = \
|
||||
$(TEST_LIBC_SOCK_COMS) \
|
||||
$(TEST_LIBC_SOCK_COMS:%=%.dbg)
|
||||
|
||||
TEST_LIBC_SOCK_TESTS = \
|
||||
$(TEST_LIBC_SOCK_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
|
||||
TEST_LIBC_SOCK_CHECKS = \
|
||||
$(TEST_LIBC_SOCK_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_LIBC_SOCK_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_CALLS_HEFTY \
|
||||
LIBC_STDIO \
|
||||
LIBC_FMT \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_SOCK \
|
||||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_X \
|
||||
TOOL_DECODE_LIB
|
||||
|
||||
TEST_LIBC_SOCK_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_SOCK_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/libc/sock/sock.pkg: \
|
||||
$(TEST_LIBC_SOCK_OBJS) \
|
||||
$(foreach x,$(TEST_LIBC_SOCK_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/libc/sock/%.com.dbg: \
|
||||
$(TEST_LIBC_SOCK_DEPS) \
|
||||
o/$(MODE)/test/libc/sock/%.o \
|
||||
o/$(MODE)/test/libc/sock/sock.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE)
|
||||
@$(APELINK)
|
||||
|
||||
$(TEST_LIBC_SOCK_OBJS): \
|
||||
$(BUILD_FILES) \
|
||||
test/libc/sock/test.mk
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/sock
|
||||
o/$(MODE)/test/libc/sock: \
|
||||
$(TEST_LIBC_SOCK_BINS) \
|
||||
$(TEST_LIBC_SOCK_CHECKS)
|
Loading…
Add table
Add a link
Reference in a new issue