mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
Use DNS implementation from Musl Libc
Now that our socket system call polyfills are good enough to support Musl's DNS library we should be using that rather than the barebones domain name system implementation we rolled on our own. There's many benefits to making this change. So many, that I myself wouldn't feel qualified to enumerate them all. The Musl DNS code had to be changed in order to support Windows of course, which looks very solid so far
This commit is contained in:
parent
1a28e35c62
commit
43fe5956ad
146 changed files with 2646 additions and 7190 deletions
|
@ -4,7 +4,6 @@
|
|||
.PHONY: o/$(MODE)/test/libc
|
||||
o/$(MODE)/test/libc: \
|
||||
o/$(MODE)/test/libc/calls \
|
||||
o/$(MODE)/test/libc/dns \
|
||||
o/$(MODE)/test/libc/fmt \
|
||||
o/$(MODE)/test/libc/intrin \
|
||||
o/$(MODE)/test/libc/log \
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘
|
||||
|
||||
PKGS += TEST_LIBC_DNS
|
||||
|
||||
TEST_LIBC_DNS_SRCS := $(wildcard test/libc/dns/*.c)
|
||||
TEST_LIBC_DNS_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_DNS_SRCS))
|
||||
|
||||
TEST_LIBC_DNS_OBJS = \
|
||||
$(TEST_LIBC_DNS_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TEST_LIBC_DNS_COMS = \
|
||||
$(TEST_LIBC_DNS_SRCS:%.c=o/$(MODE)/%.com)
|
||||
|
||||
TEST_LIBC_DNS_BINS = \
|
||||
$(TEST_LIBC_DNS_COMS) \
|
||||
$(TEST_LIBC_DNS_COMS:%=%.dbg)
|
||||
|
||||
TEST_LIBC_DNS_TESTS = \
|
||||
$(TEST_LIBC_DNS_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
|
||||
TEST_LIBC_DNS_CHECKS = \
|
||||
$(TEST_LIBC_DNS_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_LIBC_DNS_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_DNS \
|
||||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_SOCK \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_X
|
||||
|
||||
TEST_LIBC_DNS_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_DNS_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/libc/dns/dns.pkg: \
|
||||
$(TEST_LIBC_DNS_OBJS) \
|
||||
$(foreach x,$(TEST_LIBC_DNS_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/libc/dns/%.com.dbg: \
|
||||
$(TEST_LIBC_DNS_DEPS) \
|
||||
o/$(MODE)/test/libc/dns/%.o \
|
||||
o/$(MODE)/test/libc/dns/dns.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/dns
|
||||
o/$(MODE)/test/libc/dns: \
|
||||
$(TEST_LIBC_DNS_BINS) \
|
||||
$(TEST_LIBC_DNS_CHECKS)
|
|
@ -1,110 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
void SetUpOnce(void) {
|
||||
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testEmpty) {
|
||||
char *A = strcpy(malloc(1), "");
|
||||
char *B = strcpy(malloc(1), "");
|
||||
EXPECT_EQ(CompareDnsNames(A, B), 0);
|
||||
EXPECT_EQ(CompareDnsNames(A, A), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testDotless_caseInsensitiveBehavior) {
|
||||
char *A = malloc(2);
|
||||
char *B = malloc(2);
|
||||
EXPECT_EQ(CompareDnsNames(strcpy(A, "a"), strcpy(B, "a")), 0);
|
||||
EXPECT_EQ(CompareDnsNames(A, A), 0);
|
||||
EXPECT_EQ(CompareDnsNames(strcpy(A, "a"), strcpy(B, "A")), 0);
|
||||
EXPECT_EQ(CompareDnsNames(strcpy(A, "A"), strcpy(B, "a")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(A, "a"), strcpy(B, "b")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(A, "a"), strcpy(B, "B")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "d"), strcpy(B, "a")), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testMultiLabel_lexiReverse) {
|
||||
char *A = malloc(16);
|
||||
char *B = malloc(16);
|
||||
EXPECT_EQ(CompareDnsNames(strcpy(A, "a.example"), strcpy(B, "a.example")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "b.example"), strcpy(B, "a.example")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(A, "b.example"), strcpy(B, "a.examplz")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "a.zxample"), strcpy(B, "a.examplz")), 0);
|
||||
EXPECT_EQ(CompareDnsNames(strcpy(A, "c.a.example"), strcpy(B, "c.a.example")),
|
||||
0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "d.a.example"), strcpy(B, "c.a.example")),
|
||||
0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(A, "cat.example"), strcpy(B, "lol.example")),
|
||||
0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testTldDotQualifier_canBeEqualToDottedNames) {
|
||||
char *A = malloc(16);
|
||||
char *B = malloc(16);
|
||||
EXPECT_EQ(
|
||||
CompareDnsNames(strcpy(B, "aaa.example."), strcpy(A, "aaa.example")), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testFullyQualified_alwaysComesFirst) {
|
||||
char *A = malloc(16);
|
||||
char *B = malloc(16);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "aaa.example."), strcpy(A, "zzz")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "zzz.example."), strcpy(A, "aaa")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "zzz"), strcpy(B, "aaa.example.")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "aaa"), strcpy(B, "zzz.example.")), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testLikelySld_alwaysComesBeforeLocalName) {
|
||||
char *A = malloc(16);
|
||||
char *B = malloc(16);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "z.e"), strcpy(A, "a")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "aaa.example"), strcpy(A, "zzz")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "zzz.example"), strcpy(A, "aaa")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "zzz"), strcpy(B, "aaa.example")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "aaa"), strcpy(B, "zzz.example")), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
||||
|
||||
TEST(CompareDnsNames, testLikelySubdomain_alwaysComesAfterSld) {
|
||||
char *A = malloc(16);
|
||||
char *B = malloc(16);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "a.e"), strcpy(A, "z.a.e")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "z.a.e"), strcpy(B, "a.e")), 0);
|
||||
EXPECT_LT(CompareDnsNames(strcpy(B, "b.e"), strcpy(A, "a.b.e")), 0);
|
||||
EXPECT_GT(CompareDnsNames(strcpy(A, "a.b.e"), strcpy(B, "b.e")), 0);
|
||||
free(B);
|
||||
free(A);
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/dns/dnsheader.h"
|
||||
#include "libc/mem/gc.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(SerializeDnsHeader, test) {
|
||||
uint8_t buf[12];
|
||||
struct DnsHeader header;
|
||||
memset(&header, 0, sizeof(header));
|
||||
header.id = 255;
|
||||
header.bf1 = true;
|
||||
header.qdcount = 1;
|
||||
SerializeDnsHeader(buf, &header);
|
||||
EXPECT_BINEQ(u" λ☺ ☺ ", buf);
|
||||
}
|
||||
|
||||
TEST(SerializeDnsHeader, fuzzSymmetry) {
|
||||
uint8_t buf[12];
|
||||
struct DnsHeader in, out;
|
||||
rngset(&in, sizeof(in), _rand64, -1);
|
||||
rngset(&out, sizeof(out), _rand64, -1);
|
||||
SerializeDnsHeader(buf, &in);
|
||||
DeserializeDnsHeader(&out, buf);
|
||||
ASSERT_EQ(0, memcmp(&in, &out, 12), "%#.*s\n\t%#.*s", 12, in, 12, buf);
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/dns/dnsquestion.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(SerializeDnsQuestion, test) {
|
||||
struct DnsQuestion dq;
|
||||
char name[] = "foo.bar";
|
||||
uint8_t buf[1 + 3 + 1 + 3 + 1 + 4];
|
||||
dq.qname = name;
|
||||
dq.qtype = 0x0201;
|
||||
dq.qclass = 0x0102;
|
||||
EXPECT_EQ(1 + 3 + 1 + 3 + 1 + 4,
|
||||
SerializeDnsQuestion(buf, 1 + 3 + 1 + 3 + 1 + 4, &dq));
|
||||
EXPECT_BINEQ(u"♥foo♥bar ☻☺☺☻", buf);
|
||||
}
|
||||
|
||||
TEST(SerializeDnsQuestion, testNoSpace) {
|
||||
struct DnsQuestion dq;
|
||||
char name[] = "foo.bar";
|
||||
uint8_t buf[1 + 3 + 1 + 3 + 1 + 3];
|
||||
dq.qname = name;
|
||||
dq.qtype = 0x0201;
|
||||
dq.qclass = 0x0102;
|
||||
EXPECT_EQ(-1, SerializeDnsQuestion(buf, 1 + 3 + 1 + 3 + 1 + 3, &dq));
|
||||
EXPECT_EQ(ENOSPC, errno);
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/hoststxt.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
static const char *parseip(unsigned char ip[4]) {
|
||||
static char g_ipbuf[16];
|
||||
return inet_ntop(AF_INET, ip, g_ipbuf, sizeof(g_ipbuf));
|
||||
}
|
||||
|
||||
TEST(ParseHostsTxt, testEmpty) {
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(0, ht->entries.i);
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseHostsTxt, testCorrectlyTokenizesAndSorts) {
|
||||
const char kInput[] = "# this is a comment\n"
|
||||
"# IP HOST1 HOST2\n"
|
||||
"203.0.113.1 lol.example. lol\n"
|
||||
"203.0.113.2 cat.example. cat\n";
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(1, fwrite(kInput, strlen(kInput), 1, f));
|
||||
rewind(f);
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(4, ht->entries.i);
|
||||
EXPECT_STREQ("lol.example.", &ht->strings.p[ht->entries.p[0].name]);
|
||||
EXPECT_STREQ("lol.example.", &ht->strings.p[ht->entries.p[0].canon]);
|
||||
EXPECT_STREQ("203.0.113.1", parseip(ht->entries.p[0].ip));
|
||||
EXPECT_STREQ("lol", &ht->strings.p[ht->entries.p[1].name]);
|
||||
EXPECT_STREQ("lol.example.", &ht->strings.p[ht->entries.p[1].canon]);
|
||||
EXPECT_STREQ("203.0.113.1", parseip(ht->entries.p[1].ip));
|
||||
EXPECT_STREQ("cat.example.", &ht->strings.p[ht->entries.p[2].name]);
|
||||
EXPECT_STREQ("cat.example.", &ht->strings.p[ht->entries.p[2].canon]);
|
||||
EXPECT_STREQ("203.0.113.2", parseip(ht->entries.p[2].ip));
|
||||
EXPECT_STREQ("cat", &ht->strings.p[ht->entries.p[3].name]);
|
||||
EXPECT_STREQ("cat.example.", &ht->strings.p[ht->entries.p[3].canon]);
|
||||
EXPECT_STREQ("203.0.113.2", parseip(ht->entries.p[3].ip));
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseHostsTxt, testIpv6_isIgnored) {
|
||||
const char kInput[] = "::1 boop\n"
|
||||
"203.0.113.2 cat # ignore me\n";
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen((void *)kInput, strlen(kInput), "r+");
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(1, ht->entries.i);
|
||||
EXPECT_STREQ("cat", &ht->strings.p[ht->entries.p[0].name]);
|
||||
EXPECT_STREQ("cat", &ht->strings.p[ht->entries.p[0].canon]);
|
||||
EXPECT_STREQ("203.0.113.2", parseip(ht->entries.p[0].ip));
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/dns/hoststxt.h"
|
||||
#include "libc/dns/resolvconf.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
static const char *FormatIp(struct sockaddr_in *ip) {
|
||||
static char g_ipbuf[16];
|
||||
return inet_ntop(ip->sin_family, &ip->sin_addr.s_addr, g_ipbuf, 16);
|
||||
}
|
||||
|
||||
TEST(ParseResolvConf, testEmpty) {
|
||||
struct ResolvConf *rv = calloc(1, sizeof(struct ResolvConf));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(0, ParseResolvConf(rv, f));
|
||||
ASSERT_EQ(0, rv->nameservers.i);
|
||||
FreeResolvConf(&rv);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseResolvConf, testCorrectlyTokenizes) {
|
||||
const char kInput[] = "# this is a comment\n"
|
||||
"nameserver 203.0.113.2 \n"
|
||||
" nameserver 203.0.113.1\n";
|
||||
struct ResolvConf *rv = calloc(1, sizeof(struct ResolvConf));
|
||||
FILE *f = fmemopen((void *)kInput, strlen(kInput), "r+");
|
||||
ASSERT_EQ(2, ParseResolvConf(rv, f));
|
||||
ASSERT_EQ(2, rv->nameservers.i);
|
||||
EXPECT_EQ(AF_INET, rv->nameservers.p[0].sin_family);
|
||||
EXPECT_EQ(DNS_PORT, ntohs(rv->nameservers.p[0].sin_port));
|
||||
EXPECT_STREQ("203.0.113.2", FormatIp(&rv->nameservers.p[0]));
|
||||
EXPECT_EQ(AF_INET, rv->nameservers.p[1].sin_family);
|
||||
EXPECT_EQ(DNS_PORT, ntohs(rv->nameservers.p[1].sin_port));
|
||||
EXPECT_STREQ("203.0.113.1", FormatIp(&rv->nameservers.p[1]));
|
||||
FreeResolvConf(&rv);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseResolvConf, testMulticastDnsThing_getsIgnored) {
|
||||
const char kInput[] = "search local # boop\n";
|
||||
struct ResolvConf *rv = calloc(1, sizeof(struct ResolvConf));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
ASSERT_EQ(0, ParseResolvConf(rv, f));
|
||||
ASSERT_EQ(0, rv->nameservers.i);
|
||||
FreeResolvConf(&rv);
|
||||
fclose(f);
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(PascalifyDnsName, testEmpty) {
|
||||
uint8_t *buf = malloc(1);
|
||||
char *name = strdup("");
|
||||
EXPECT_EQ(0, PascalifyDnsName(buf, 1, name));
|
||||
EXPECT_BINEQ(u" ", buf);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
TEST(PascalifyDnsName, testOneLabel) {
|
||||
uint8_t *buf = malloc(1 + 3 + 1);
|
||||
char *name = strdup("foo");
|
||||
EXPECT_EQ(1 + 3, PascalifyDnsName(buf, 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo ", buf);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
TEST(PascalifyDnsName, testTwoLabels) {
|
||||
uint8_t *buf = malloc(1 + 3 + 1 + 3 + 1);
|
||||
char *name = strdup("foo.bar");
|
||||
EXPECT_EQ(1 + 3 + 1 + 3, PascalifyDnsName(buf, 1 + 3 + 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo♥bar ", buf);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
TEST(PascalifyDnsName, testFqdnDot_isntIncluded) {
|
||||
uint8_t *buf = malloc(1 + 3 + 1 + 3 + 1);
|
||||
char *name = strdup("foo.bar.");
|
||||
EXPECT_EQ(1 + 3 + 1 + 3, PascalifyDnsName(buf, 1 + 3 + 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo♥bar ", buf);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
TEST(PascalifyDnsName, testTooLong) {
|
||||
uint8_t *buf = malloc(1);
|
||||
char *name = malloc(1000);
|
||||
memset(name, '.', 999);
|
||||
name[999] = '\0';
|
||||
EXPECT_EQ(-1, PascalifyDnsName(buf, 1, name));
|
||||
EXPECT_EQ(ENAMETOOLONG, errno);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
TEST(PascalifyDnsName, testNoSpace) {
|
||||
uint8_t *buf = malloc(1);
|
||||
char *name = strdup("foo");
|
||||
EXPECT_EQ(-1, PascalifyDnsName(buf, 1, name));
|
||||
EXPECT_EQ(ENOSPC, errno);
|
||||
free(name);
|
||||
free(buf);
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ This is free and unencumbered software released into the public domain. │
|
||||
│ │
|
||||
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
|
||||
│ distribute this software, either in source code form or as a compiled │
|
||||
│ binary, for any purpose, commercial or non-commercial, and by any │
|
||||
│ means. │
|
||||
│ │
|
||||
│ In jurisdictions that recognize copyright laws, the author or authors │
|
||||
│ of this software dedicate any and all copyright interest in the │
|
||||
│ software to the public domain. We make this dedication for the benefit │
|
||||
│ of the public at large and to the detriment of our heirs and │
|
||||
│ successors. We intend this dedication to be an overt act of │
|
||||
│ relinquishment in perpetuity of all present and future rights to this │
|
||||
│ software under copyright law. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
|
||||
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
|
||||
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
|
||||
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
|
||||
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
|
||||
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
|
||||
│ OTHER DEALINGS IN THE SOFTWARE. │
|
||||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dns/prototxt.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dns/dns.h"
|
||||
#include "libc/dns/ent.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
}
|
||||
|
||||
void SetUp() {
|
||||
int fd;
|
||||
const char *sample = "\
|
||||
# skip comment string\n\
|
||||
rspf 73 RSPF CPHB \n\
|
||||
ggp 3 GGP ";
|
||||
ASSERT_NE(-1, (fd = creat("protocols", 0755)));
|
||||
ASSERT_NE(-1, write(fd, sample, strlen(sample)));
|
||||
ASSERT_NE(-1, close(fd));
|
||||
}
|
||||
|
||||
TEST(LookupProtoByNumber, GetNameWhenNumberCorrect) {
|
||||
char name[16]; /* sample has only names of length 3-4 */
|
||||
strcpy(name, "");
|
||||
ASSERT_EQ(-1, /*non-existent number */
|
||||
LookupProtoByNumber(24, name, sizeof(name), "protocols"));
|
||||
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
|
||||
LookupProtoByNumber(73, name, 1, "protocols"));
|
||||
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
|
||||
ASSERT_EQ(0, /* works with valid number */
|
||||
LookupProtoByNumber(73, name, sizeof(name), "protocols"));
|
||||
ASSERT_STREQ(name, "rspf"); /* official name written */
|
||||
}
|
||||
|
||||
TEST(LookupProtoByName, GetNumberWhenNameOrAlias) {
|
||||
char name[16]; /* sample has only names of length 3-4 */
|
||||
strcpy(name, "");
|
||||
ASSERT_EQ(-1, /* non-existent name or alias */
|
||||
LookupProtoByName("tcp", name, sizeof(name), "protocols"));
|
||||
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
|
||||
LookupProtoByName("ggp", name, 1, "protocols"));
|
||||
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
|
||||
ASSERT_EQ(3, /* works with valid name */
|
||||
LookupProtoByName("ggp", name, sizeof(name), "protocols"));
|
||||
ASSERT_STREQ(name, "ggp");
|
||||
ASSERT_EQ(73, /* works with valid alias */
|
||||
LookupProtoByName("CPHB", name, sizeof(name), "protocols"));
|
||||
ASSERT_STREQ(name, "rspf"); /* official name written */
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 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/dns/hoststxt.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(ParseHostsTxt, testNotFound) {
|
||||
const char kInput[] = "# this is a comment\n"
|
||||
"# IP HOST1 HOST2\n"
|
||||
"203.0.113.1 lol.example lol\n"
|
||||
"203.0.113.2 cat.example cat\n";
|
||||
char name[256];
|
||||
uint8_t ip[4] = {127, 0, 113, 1};
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(1, fwrite(kInput, strlen(kInput), 1, f));
|
||||
rewind(f);
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(4, ht->entries.i);
|
||||
ASSERT_EQ(0, ResolveHostsReverse(ht, AF_INET, ip, name, sizeof(name)));
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseHostsTxt, testFirstLookup) {
|
||||
const char kInput[] = "# this is a comment\n"
|
||||
"# IP HOST1 HOST2\n"
|
||||
"203.0.113.1 lol.example lol\n"
|
||||
"203.0.113.2 cat.example cat\n";
|
||||
char name[256];
|
||||
uint8_t ip[4] = {203, 0, 113, 1};
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(1, fwrite(kInput, strlen(kInput), 1, f));
|
||||
rewind(f);
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(4, ht->entries.i);
|
||||
ASSERT_EQ(1, ResolveHostsReverse(ht, AF_INET, ip, name, sizeof(name)));
|
||||
EXPECT_STREQ("lol.example", name);
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ParseHostsTxt, testSecondLookup) {
|
||||
const char kInput[] = "# this is a comment\n"
|
||||
"# IP HOST1 HOST2\n"
|
||||
"203.0.113.1 lol.example lol\n"
|
||||
"203.0.113.2 cat.example cat\n";
|
||||
char name[256];
|
||||
uint8_t ip[4] = {203, 0, 113, 2};
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(1, fwrite(kInput, strlen(kInput), 1, f));
|
||||
rewind(f);
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(4, ht->entries.i);
|
||||
ASSERT_EQ(1, ResolveHostsReverse(ht, AF_INET, ip, name, sizeof(name)));
|
||||
EXPECT_STREQ("cat.example", name);
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2020 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/dns/dns.h"
|
||||
#include "libc/dns/hoststxt.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
static const char *EzIp4Lookup(const struct HostsTxt *ht, const char *name) {
|
||||
struct sockaddr_in addr4;
|
||||
if (ResolveHostsTxt(ht, AF_INET, name, (void *)&addr4,
|
||||
sizeof(struct sockaddr_in), NULL) > 0) {
|
||||
static char g_ipbuf[16];
|
||||
return inet_ntop(AF_INET, &addr4.sin_addr, g_ipbuf, sizeof(g_ipbuf));
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *EzCanonicalize(const struct HostsTxt *ht, const char *name) {
|
||||
const char *res;
|
||||
return ResolveHostsTxt(ht, AF_INET, name, NULL, 0, &res) > 0 ? res : NULL;
|
||||
}
|
||||
|
||||
static const char kInput[] = "127.0.0.1 localhost\n"
|
||||
"203.0.113.1 lol.example. lol\n"
|
||||
"203.0.113.2 cat.example. cat\n";
|
||||
|
||||
TEST(ResolveHostsTxt, testBasicLookups) {
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen((void *)kInput, strlen(kInput), "r+");
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(5, ht->entries.i);
|
||||
EXPECT_STREQ("127.0.0.1", EzIp4Lookup(ht, "localhost"));
|
||||
EXPECT_STREQ("203.0.113.1", EzIp4Lookup(ht, "lol"));
|
||||
EXPECT_STREQ("203.0.113.1", EzIp4Lookup(ht, "lol.example"));
|
||||
EXPECT_STREQ("203.0.113.1", EzIp4Lookup(ht, "lol.example."));
|
||||
EXPECT_STREQ("203.0.113.2", EzIp4Lookup(ht, "cat"));
|
||||
EXPECT_STREQ("203.0.113.2", EzIp4Lookup(ht, "cat.example."));
|
||||
EXPECT_EQ(NULL, EzIp4Lookup(ht, "boop"));
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
TEST(ResolveHostsTxt, testCanonicalize) {
|
||||
struct HostsTxt *ht = calloc(1, sizeof(struct HostsTxt));
|
||||
FILE *f = fmemopen((void *)kInput, strlen(kInput), "r+");
|
||||
ASSERT_EQ(0, ParseHostsTxt(ht, f));
|
||||
ASSERT_EQ(5, ht->entries.i);
|
||||
EXPECT_STREQ("localhost", EzCanonicalize(ht, "localhost"));
|
||||
EXPECT_STREQ("lol.example.", EzCanonicalize(ht, "lol"));
|
||||
EXPECT_STREQ("lol.example.", EzCanonicalize(ht, "lol.example"));
|
||||
EXPECT_STREQ("lol.example.", EzCanonicalize(ht, "lol.example."));
|
||||
EXPECT_STREQ("cat.example.", EzCanonicalize(ht, "cat"));
|
||||
EXPECT_STREQ("cat.example.", EzCanonicalize(ht, "cat.example."));
|
||||
EXPECT_EQ(NULL, EzCanonicalize(ht, "boop"));
|
||||
FreeHostsTxt(&ht);
|
||||
fclose(f);
|
||||
}
|
|
@ -1,184 +0,0 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ This is free and unencumbered software released into the public domain. │
|
||||
│ │
|
||||
│ Anyone is free to copy, modify, publish, use, compile, sell, or │
|
||||
│ distribute this software, either in source code form or as a compiled │
|
||||
│ binary, for any purpose, commercial or non-commercial, and by any │
|
||||
│ means. │
|
||||
│ │
|
||||
│ In jurisdictions that recognize copyright laws, the author or authors │
|
||||
│ of this software dedicate any and all copyright interest in the │
|
||||
│ software to the public domain. We make this dedication for the benefit │
|
||||
│ of the public at large and to the detriment of our heirs and │
|
||||
│ successors. We intend this dedication to be an overt act of │
|
||||
│ relinquishment in perpetuity of all present and future rights to this │
|
||||
│ software under copyright law. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
|
||||
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
|
||||
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
|
||||
│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │
|
||||
│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
|
||||
│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │
|
||||
│ OTHER DEALINGS IN THE SOFTWARE. │
|
||||
│ │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dns/servicestxt.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dns/dns.h"
|
||||
#include "libc/dns/ent.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
}
|
||||
|
||||
void SetUp() {
|
||||
int fd;
|
||||
const char* sample = "\
|
||||
# skip comment string\n\
|
||||
chargen 19/tcp ttytst source\n\
|
||||
chargen 19/udp ttytst source\n\
|
||||
ssh 22/tcp # SSH Remote Login Protocol";
|
||||
|
||||
ASSERT_NE(-1, (fd = creat("services", 0755)));
|
||||
ASSERT_NE(-1, write(fd, sample, strlen(sample)));
|
||||
ASSERT_NE(-1, close(fd));
|
||||
}
|
||||
|
||||
TEST(LookupServicesByPort, GetNameWhenPortCorrect) {
|
||||
char name[8]; /* service names are of length 3 */
|
||||
char eitherproto[8]; /* protocol names are of length 3 */
|
||||
char proto1[] = "tcp";
|
||||
char proto2[] = "udp";
|
||||
char* localproto;
|
||||
strcpy(eitherproto, "");
|
||||
strcpy(name, "");
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(-1, /* non existent port */
|
||||
LookupServicesByPort(965, localproto, sizeof(eitherproto), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_EQ('\0', localproto[0]);
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(-1, /* port in network byte order */
|
||||
LookupServicesByPort(htons(22), localproto, sizeof(eitherproto),
|
||||
name, sizeof(name), "services"));
|
||||
ASSERT_EQ('\0', localproto[0]);
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(-1, /* port ok but wrong protocol */
|
||||
LookupServicesByPort(22, localproto, sizeof(proto2), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(proto2, "udp");
|
||||
|
||||
localproto = proto1;
|
||||
ASSERT_EQ(
|
||||
-1, /* protocol is non-NULL/length must be nonzero */
|
||||
LookupServicesByPort(22, localproto, 0, name, sizeof(name), "services"));
|
||||
ASSERT_STREQ(proto1, "tcp");
|
||||
|
||||
localproto = proto1;
|
||||
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
|
||||
LookupServicesByPort(22, localproto, sizeof(proto1), name, 1,
|
||||
"services"));
|
||||
ASSERT_STREQ(proto1, "tcp");
|
||||
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(
|
||||
-1, /* sizeof(proto) insufficient, memccpy failure */
|
||||
LookupServicesByPort(22, localproto, 1, name, sizeof(name), "services"));
|
||||
ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */
|
||||
|
||||
localproto = proto1;
|
||||
ASSERT_EQ(0, LookupServicesByPort(22, localproto, sizeof(proto1), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "ssh");
|
||||
ASSERT_STREQ(proto1, "tcp");
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(0, LookupServicesByPort(19, localproto, sizeof(proto2), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "chargen");
|
||||
ASSERT_STREQ(proto2, "udp");
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(0, /* pick first matching protocol */
|
||||
LookupServicesByPort(19, localproto, sizeof(eitherproto), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "chargen");
|
||||
ASSERT_NE('\0', localproto[0]); /* buffer filled during the call */
|
||||
ASSERT_STREQ(eitherproto, "tcp");
|
||||
}
|
||||
|
||||
TEST(LookupServicesByName, GetPortWhenNameOrAlias) {
|
||||
char name[8]; /* service names are of length 3 */
|
||||
char eitherproto[8]; /* protocol names are of length 3 */
|
||||
char proto1[] = "tcp";
|
||||
char proto2[] = "udp";
|
||||
char* localproto;
|
||||
strcpy(eitherproto, "");
|
||||
strcpy(name, "");
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(-1, /* non-existent name */
|
||||
LookupServicesByName("http", localproto, sizeof(eitherproto), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_EQ('\0', localproto[0]);
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(-1, /* name exists but wrong protocol */
|
||||
LookupServicesByName("ssh", localproto, sizeof(proto2), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(proto2, "udp");
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(-1, /* protocol is non-NULL/length must be nonzero */
|
||||
LookupServicesByName("ssh", localproto, sizeof(proto2), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(proto2, "udp");
|
||||
|
||||
localproto = proto1;
|
||||
ASSERT_EQ(-1, /* sizeof(name) insufficient, memccpy failure */
|
||||
LookupServicesByName("ssh", localproto, sizeof(proto1), name, 1,
|
||||
"services"));
|
||||
ASSERT_STREQ(proto1, "tcp");
|
||||
ASSERT_STREQ(name, ""); /* cleaned up after memccpy failed */
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(-1, /* sizeof(proto) insufficient, memccpy failure */
|
||||
LookupServicesByName("ssh", localproto, 1, name, sizeof(name),
|
||||
"services"));
|
||||
ASSERT_STREQ(eitherproto, ""); /* cleaned up after memccpy failed */
|
||||
|
||||
localproto = proto1;
|
||||
ASSERT_EQ(22, LookupServicesByName("ssh", localproto, sizeof(proto1), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "ssh"); /* official name written to buffer */
|
||||
ASSERT_STREQ(proto1, "tcp");
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(19, /* works if alias provided */
|
||||
LookupServicesByName("ttytst", localproto, sizeof(proto2), name,
|
||||
sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "chargen"); /* official name written to buffer */
|
||||
|
||||
localproto = proto2;
|
||||
ASSERT_EQ(19, /* can get port returned without official name */
|
||||
LookupServicesByName("ttytst", localproto, sizeof(proto2), NULL, 0,
|
||||
"services"));
|
||||
|
||||
localproto = eitherproto;
|
||||
ASSERT_EQ(19, /* pick first matching protocol */
|
||||
LookupServicesByName("source", localproto, sizeof(eitherproto),
|
||||
name, sizeof(name), "services"));
|
||||
ASSERT_STREQ(name, "chargen");
|
||||
ASSERT_STREQ(localproto, "tcp");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue