mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-24 06:12:27 +00:00
Initial import
This commit is contained in:
commit
c91b3c5006
14915 changed files with 590219 additions and 0 deletions
48
test/libc/dns/dnsheader_test.c
Normal file
48
test/libc/dns/dnsheader_test.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*-*- 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/dns/dns.h"
|
||||
#include "libc/dns/dnsheader.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(serializednsheader, test) {
|
||||
struct DnsHeader header;
|
||||
memset(&header, 0, sizeof(header));
|
||||
header.id = 255;
|
||||
header.bf1 = true;
|
||||
header.qdcount = 1;
|
||||
uint8_t *buf = tmalloc(12);
|
||||
ASSERT_EQ(12, serializednsheader(buf, 12, header));
|
||||
EXPECT_BINEQ(u" λ☺ ☺ ", buf);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(serializednsheader, fuzzSymmetry) {
|
||||
uint8_t *buf = tmalloc(12);
|
||||
struct DnsHeader *in = tmalloc(sizeof(struct DnsHeader));
|
||||
struct DnsHeader *out = tmalloc(sizeof(struct DnsHeader));
|
||||
ASSERT_EQ(12, serializednsheader(buf, 12, *in));
|
||||
ASSERT_EQ(12, deserializednsheader(out, buf, 12));
|
||||
ASSERT_EQ(0, memcmp(in, out, 12));
|
||||
tfree(out);
|
||||
tfree(in);
|
||||
tfree(buf);
|
||||
}
|
101
test/libc/dns/dnsnamecmp_test.c
Normal file
101
test/libc/dns/dnsnamecmp_test.c
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*-*- 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/dns/dns.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(dnsnamecmp, testEmpty) {
|
||||
char *A = strcpy(tmalloc(1), "");
|
||||
char *B = strcpy(tmalloc(1), "");
|
||||
EXPECT_EQ(dnsnamecmp(A, B), 0);
|
||||
EXPECT_EQ(dnsnamecmp(A, A), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testDotless_caseInsensitiveBehavior) {
|
||||
char *A = tmalloc(2);
|
||||
char *B = tmalloc(2);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(A, "a"), strcpy(B, "a")), 0);
|
||||
EXPECT_EQ(dnsnamecmp(A, A), 0);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(A, "a"), strcpy(B, "A")), 0);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(A, "A"), strcpy(B, "a")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(A, "a"), strcpy(B, "b")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(A, "a"), strcpy(B, "B")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "d"), strcpy(B, "a")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testMultiLabel_lexiReverse) {
|
||||
char *A = tmalloc(16);
|
||||
char *B = tmalloc(16);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(A, "a.example"), strcpy(B, "a.example")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "b.example"), strcpy(B, "a.example")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(A, "b.example"), strcpy(B, "a.examplz")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "a.zxample"), strcpy(B, "a.examplz")), 0);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(A, "c.a.example"), strcpy(B, "c.a.example")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "d.a.example"), strcpy(B, "c.a.example")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(A, "cat.example"), strcpy(B, "lol.example")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testTldDotQualifier_canBeEqualToDottedNames) {
|
||||
char *A = tmalloc(16);
|
||||
char *B = tmalloc(16);
|
||||
EXPECT_EQ(dnsnamecmp(strcpy(B, "aaa.example."), strcpy(A, "aaa.example")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testFullyQualified_alwaysComesFirst) {
|
||||
char *A = tmalloc(16);
|
||||
char *B = tmalloc(16);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "aaa.example."), strcpy(A, "zzz")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "zzz.example."), strcpy(A, "aaa")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "zzz"), strcpy(B, "aaa.example.")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "aaa"), strcpy(B, "zzz.example.")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testLikelySld_alwaysComesBeforeLocalName) {
|
||||
char *A = tmalloc(16);
|
||||
char *B = tmalloc(16);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "z.e"), strcpy(A, "a")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "aaa.example"), strcpy(A, "zzz")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "zzz.example"), strcpy(A, "aaa")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "zzz"), strcpy(B, "aaa.example")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "aaa"), strcpy(B, "zzz.example")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
||||
|
||||
TEST(dnsnamecmp, testLikelySubdomain_alwaysComesAfterSld) {
|
||||
char *A = tmalloc(16);
|
||||
char *B = tmalloc(16);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "a.e"), strcpy(A, "z.a.e")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "z.a.e"), strcpy(B, "a.e")), 0);
|
||||
EXPECT_LT(dnsnamecmp(strcpy(B, "b.e"), strcpy(A, "a.b.e")), 0);
|
||||
EXPECT_GT(dnsnamecmp(strcpy(A, "a.b.e"), strcpy(B, "b.e")), 0);
|
||||
tfree(B);
|
||||
tfree(A);
|
||||
}
|
50
test/libc/dns/dnsquestion_test.c
Normal file
50
test/libc/dns/dnsquestion_test.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*-*- 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/dns/dns.h"
|
||||
#include "libc/dns/dnsquestion.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(serializednsquestion, test) {
|
||||
uint8_t *buf = tmalloc(1 + 3 + 1 + 3 + 1 + 4);
|
||||
char *name = tstrdup("foo.bar");
|
||||
struct DnsQuestion dq;
|
||||
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);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(serializednsquestion, testNoSpace) {
|
||||
uint8_t *buf = tmalloc(1 + 3 + 1 + 3 + 1 + 3);
|
||||
char *name = tstrdup("foo.bar");
|
||||
struct DnsQuestion dq;
|
||||
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);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
84
test/libc/dns/parsehoststxt_test.c
Normal file
84
test/libc/dns/parsehoststxt_test.c
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*-*- 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/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(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
ASSERT_EQ(0, parsehoststxt(ht, f));
|
||||
sorthoststxt(ht);
|
||||
ASSERT_EQ(4, ht->entries.i);
|
||||
EXPECT_STREQ("cat.example.", &ht->strings.p[ht->entries.p[0].name]);
|
||||
EXPECT_STREQ("cat.example.", &ht->strings.p[ht->entries.p[0].canon]);
|
||||
EXPECT_STREQ("203.0.113.2", ParseIp(ht->entries.p[0].ip));
|
||||
EXPECT_STREQ("lol.example.", &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", &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("lol", &ht->strings.p[ht->entries.p[3].name]);
|
||||
EXPECT_STREQ("lol.example.", &ht->strings.p[ht->entries.p[3].canon]);
|
||||
EXPECT_STREQ("203.0.113.1", 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(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
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);
|
||||
}
|
76
test/libc/dns/parseresolvconf_test.c
Normal file
76
test/libc/dns/parseresolvconf_test.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*-*- 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/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(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
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, testSearchLocal_setsLoopback) {
|
||||
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(1, parseresolvconf(rv, f));
|
||||
ASSERT_EQ(1, 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("127.0.0.1", FormatIp(&rv->nameservers.p[0]));
|
||||
freeresolvconf(&rv);
|
||||
fclose(f);
|
||||
}
|
79
test/libc/dns/pascalifydnsname_test.c
Normal file
79
test/libc/dns/pascalifydnsname_test.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*-*- 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/dns/dns.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(pascalifydnsname, testEmpty) {
|
||||
uint8_t *buf = tmalloc(1);
|
||||
char *name = tstrdup("");
|
||||
EXPECT_EQ(0, pascalifydnsname(buf, 1, name));
|
||||
EXPECT_BINEQ(u" ", buf);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(pascalifydnsname, testOneLabel) {
|
||||
uint8_t *buf = tmalloc(1 + 3 + 1);
|
||||
char *name = tstrdup("foo");
|
||||
EXPECT_EQ(1 + 3, pascalifydnsname(buf, 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo ", buf);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(pascalifydnsname, testTwoLabels) {
|
||||
uint8_t *buf = tmalloc(1 + 3 + 1 + 3 + 1);
|
||||
char *name = tstrdup("foo.bar");
|
||||
EXPECT_EQ(1 + 3 + 1 + 3, pascalifydnsname(buf, 1 + 3 + 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo♥bar ", buf);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(pascalifydnsname, testFqdnDot_isntIncluded) {
|
||||
uint8_t *buf = tmalloc(1 + 3 + 1 + 3 + 1);
|
||||
char *name = tstrdup("foo.bar.");
|
||||
EXPECT_EQ(1 + 3 + 1 + 3, pascalifydnsname(buf, 1 + 3 + 1 + 3 + 1, name));
|
||||
EXPECT_BINEQ(u"♥foo♥bar ", buf);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(pascalifydnsname, testTooLong) {
|
||||
uint8_t *buf = tmalloc(1);
|
||||
char *name = tmalloc(1000);
|
||||
memset(name, '.', 999);
|
||||
name[999] = '\0';
|
||||
EXPECT_EQ(-1, pascalifydnsname(buf, 1, name));
|
||||
EXPECT_EQ(ENAMETOOLONG, errno);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
||||
|
||||
TEST(pascalifydnsname, testNoSpace) {
|
||||
uint8_t *buf = tmalloc(1);
|
||||
char *name = tstrdup("foo");
|
||||
EXPECT_EQ(-1, pascalifydnsname(buf, 1, name));
|
||||
EXPECT_EQ(ENOSPC, errno);
|
||||
tfree(name);
|
||||
tfree(buf);
|
||||
}
|
83
test/libc/dns/resolvehoststxt_test.c
Normal file
83
test/libc/dns/resolvehoststxt_test.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
/*-*- 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/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(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
ASSERT_EQ(0, parsehoststxt(ht, f));
|
||||
sorthoststxt(ht);
|
||||
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(NULL, BUFSIZ, "r+");
|
||||
ASSERT_EQ(strlen(kInput), fwrite(kInput, 1, strlen(kInput), f));
|
||||
ASSERT_EQ(0, parsehoststxt(ht, f));
|
||||
sorthoststxt(ht);
|
||||
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);
|
||||
}
|
59
test/libc/dns/test.mk
Normal file
59
test/libc/dns/test.mk
Normal file
|
@ -0,0 +1,59 @@
|
|||
#-*-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_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_COMS = $(TEST_LIBC_DNS_OBJS:%.o=%.com)
|
||||
|
||||
TEST_LIBC_DNS_OBJS = \
|
||||
$(TEST_LIBC_DNS_SRCS:%=o/$(MODE)/%.zip.o) \
|
||||
$(TEST_LIBC_DNS_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
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_HEFTY \
|
||||
LIBC_DNS \
|
||||
LIBC_FMT \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_SOCK \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
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)
|
||||
@$(APELINK)
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/dns
|
||||
o/$(MODE)/test/libc/dns: \
|
||||
$(TEST_LIBC_DNS_BINS) \
|
||||
$(TEST_LIBC_DNS_CHECKS)
|
Loading…
Add table
Add a link
Reference in a new issue