1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-23 08:38:49 -05:00

DOH: add test case 1650 and 2100

This commit is contained in:
Daniel Stenberg 2018-06-19 16:08:05 +02:00
parent 5ffbb63e42
commit f2b1a18975
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
7 changed files with 410 additions and 66 deletions

View File

@ -38,31 +38,6 @@
#define DNS_CLASS_IN 0x01
#define DOH_MAX_RESPONSE_SIZE 3000 /* bytes */
typedef enum {
DNS_TYPE_A = 1,
DNS_TYPE_NS = 2,
DNS_TYPE_CNAME = 5,
DNS_TYPE_AAAA = 28
} DNStype;
#define MAX_ADDR 24
typedef enum {
DOH_OK,
DOH_DNS_BAD_LABEL, /* 1 */
DOH_DNS_OUT_OF_RANGE, /* 2 */
DOH_DNS_LABEL_LOOP, /* 3 */
DOH_TOO_SMALL_BUFFER, /* 4 */
DOH_OUT_OF_MEM, /* 5 */
DOH_DNS_RDATA_LEN, /* 6 */
DOH_DNS_MALFORMAT, /* 7 */
DOH_DNS_BAD_RCODE, /* 8 - no such name */
DOH_DNS_UNEXPECTED_TYPE, /* 9 */
DOH_DNS_UNEXPECTED_CLASS, /* 10 */
DOH_NO_CONTENT, /* 11 */
DOH_DNS_BAD_ID /* 12 */
} DOHcode;
static const char * const errors[]={
"",
"Bad label",
@ -86,7 +61,13 @@ static const char *doh_strerror(DOHcode code)
return "bad error code";
}
static DOHcode doh_encode(const char *host,
#ifdef DEBUGBUILD
#define UNITTEST
#else
#define UNITTEST static
#endif
UNITTEST DOHcode doh_encode(const char *host,
DNStype dnstype,
unsigned char *dnsp, /* buffer */
size_t len, /* buffer size */
@ -123,9 +104,11 @@ static DOHcode doh_encode(const char *host,
}
else
labellen = strlen(hostp);
if(labellen > 63)
if(labellen > 63) {
/* too long label, error out */
*olen = 0;
return DOH_DNS_BAD_LABEL;
}
*dnsp++ = (unsigned char)labellen;
memcpy(dnsp, hostp, labellen);
dnsp += labellen;
@ -363,36 +346,10 @@ static unsigned int get32bit(unsigned char *doh, int index)
(doh[index + 2] << 8) | doh[index + 3];
}
struct addr6 {
unsigned char byte[16];
};
struct cnamestore {
size_t len; /* length of cname */
char *alloc; /* allocated pointer */
size_t allocsize; /* allocated size */
};
struct dohaddr {
int type;
union {
unsigned int v4;
struct addr6 v6;
} ip;
};
struct dohentry {
unsigned int ttl;
int numaddr;
struct dohaddr addr[MAX_ADDR];
int numcname;
struct cnamestore cname[MAX_ADDR];
};
static DOHcode store_a(unsigned char *doh, int index, struct dohentry *d)
{
/* silently ignore addresses over the limit */
if(d->numaddr < MAX_ADDR) {
if(d->numaddr < DOH_MAX_ADDR) {
struct dohaddr *a = &d->addr[d->numaddr];
a->type = DNS_TYPE_A;
a->ip.v4 = ntohl(get32bit(doh, index));
@ -404,7 +361,7 @@ static DOHcode store_a(unsigned char *doh, int index, struct dohentry *d)
static DOHcode store_aaaa(unsigned char *doh, int index, struct dohentry *d)
{
/* silently ignore addresses over the limit */
if(d->numaddr < MAX_ADDR) {
if(d->numaddr < DOH_MAX_ADDR) {
struct dohaddr *a = &d->addr[d->numaddr];
struct addr6 *inet6p = &a->ip.v6;
a->type = DNS_TYPE_AAAA;
@ -445,9 +402,14 @@ static DOHcode store_cname(unsigned char *doh,
unsigned int index,
struct dohentry *d)
{
struct cnamestore *c = &d->cname[d->numcname++];
struct cnamestore *c;
unsigned int loop = 128; /* a valid DNS name can never loop this much */
unsigned char length;
if(d->numcname == DOH_MAX_CNAME)
return DOH_OK; /* skip! */
c = &d->cname[d->numcname++];
do {
if(index >= dohlen)
return DOH_DNS_OUT_OF_RANGE;
@ -530,7 +492,7 @@ static DOHcode rdata(unsigned char *doh,
return DOH_OK;
}
static DOHcode doh_decode(unsigned char *doh,
UNITTEST DOHcode doh_decode(unsigned char *doh,
size_t dohlen,
DNStype dnstype,
struct dohentry *d)
@ -824,7 +786,7 @@ static const char *type2name(DNStype dnstype)
return (dnstype == DNS_TYPE_A)?"A":"AAAA";
}
static void de_cleanup(struct dohentry *d)
UNITTEST void de_cleanup(struct dohentry *d)
{
int i = 0;
for(i = 0; i < d->numcname; i++) {

View File

@ -41,4 +41,69 @@ CURLcode Curl_doh_is_resolved(struct connectdata *conn,
int Curl_doh_getsock(struct connectdata *conn, curl_socket_t *socks,
int numsocks);
typedef enum {
DOH_OK,
DOH_DNS_BAD_LABEL, /* 1 */
DOH_DNS_OUT_OF_RANGE, /* 2 */
DOH_DNS_LABEL_LOOP, /* 3 */
DOH_TOO_SMALL_BUFFER, /* 4 */
DOH_OUT_OF_MEM, /* 5 */
DOH_DNS_RDATA_LEN, /* 6 */
DOH_DNS_MALFORMAT, /* 7 */
DOH_DNS_BAD_RCODE, /* 8 - no such name */
DOH_DNS_UNEXPECTED_TYPE, /* 9 */
DOH_DNS_UNEXPECTED_CLASS, /* 10 */
DOH_NO_CONTENT, /* 11 */
DOH_DNS_BAD_ID /* 12 */
} DOHcode;
typedef enum {
DNS_TYPE_A = 1,
DNS_TYPE_NS = 2,
DNS_TYPE_CNAME = 5,
DNS_TYPE_AAAA = 28
} DNStype;
#define DOH_MAX_ADDR 24
#define DOH_MAX_CNAME 4
struct addr6 {
unsigned char byte[16];
};
struct cnamestore {
size_t len; /* length of cname */
char *alloc; /* allocated pointer */
size_t allocsize; /* allocated size */
};
struct dohaddr {
int type;
union {
unsigned int v4;
struct addr6 v6;
} ip;
};
struct dohentry {
unsigned int ttl;
int numaddr;
struct dohaddr addr[DOH_MAX_ADDR];
int numcname;
struct cnamestore cname[DOH_MAX_CNAME];
};
#ifdef DEBUGBUILD
DOHcode doh_encode(const char *host,
DNStype dnstype,
unsigned char *dnsp, /* buffer */
size_t len, /* buffer size */
size_t *olen); /* output length */
DOHcode doh_decode(unsigned char *doh,
size_t dohlen,
DNStype dnstype,
struct dohentry *d);
void de_cleanup(struct dohentry *d);
#endif
#endif

View File

@ -182,6 +182,8 @@ test1590 \
test1600 test1601 test1602 test1603 test1604 test1605 test1606 test1607 \
test1608 test1609 test1620 \
\
test1650 \
\
test1700 test1701 test1702 \
\
test1800 test1801 \
@ -201,4 +203,6 @@ test2064 test2065 test2066 test2067 test2068 test2069 \
test2070 test2071 test2072 test2073 \
test2074 test2075 \
\
test2100 \
\
test3000 test3001

26
tests/data/test1650 Normal file
View File

@ -0,0 +1,26 @@
<testcase>
<info>
<keywords>
unittest
DOH
</keywords>
</info>
#
# Client-side
<client>
<server>
none
</server>
<features>
unittest
</features>
<name>
DOH
</name>
<tool>
unit1650
</tool>
</client>
</testcase>

BIN
tests/data/test2100 Normal file

Binary file not shown.

View File

@ -10,7 +10,8 @@ UNITPROGS = unit1300 unit1301 unit1302 unit1303 unit1304 unit1305 unit1307 \
unit1330 unit1394 unit1395 unit1396 unit1397 unit1398 \
unit1399 \
unit1600 unit1601 unit1602 unit1603 unit1604 unit1605 unit1606 unit1607 \
unit1608 unit1609 unit1620
unit1608 unit1609 unit1620 \
unit1650
unit1300_SOURCES = unit1300.c $(UNITFILES)
unit1300_CPPFLAGS = $(AM_CPPFLAGS)
@ -98,3 +99,6 @@ unit1609_CPPFLAGS = $(AM_CPPFLAGS)
unit1620_SOURCES = unit1620.c $(UNITFILES)
unit1620_CPPFLAGS = $(AM_CPPFLAGS)
unit1650_SOURCES = unit1650.c $(UNITFILES)
unit1650_CPPFLAGS = $(AM_CPPFLAGS)

283
tests/unit/unit1650.c Normal file
View File

@ -0,0 +1,283 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "curlcheck.h"
#include "doh.h"
static CURLcode unit_setup(void)
{
return CURLE_OK;
}
static void unit_stop(void)
{
}
#define DNS_PREAMBLE "\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00"
#define LABEL_TEST "\x04\x74\x65\x73\x74"
#define LABEL_HOST "\x04\x68\x6f\x73\x74"
#define LABEL_NAME "\x04\x6e\x61\x6d\x65"
#define DNSA_TYPE "\x01"
#define DNSAAAA_TYPE "\x1c"
#define DNSA_EPILOGUE "\x00\x00" DNSA_TYPE "\x00\x01"
#define DNSAAAA_EPILOGUE "\x00\x00" DNSAAAA_TYPE "\x00\x01"
#define DNS_Q1 DNS_PREAMBLE LABEL_TEST LABEL_HOST LABEL_NAME DNSA_EPILOGUE
#define DNS_Q2 DNS_PREAMBLE LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE
struct dohrequest {
/* input */
const char *name;
DNStype type;
/* output */
const char *packet;
size_t size;
int rc;
};
static struct dohrequest req[] = {
{"test.host.name", DNS_TYPE_A, DNS_Q1, sizeof(DNS_Q1)-1, 0 },
{"test.host.name", DNS_TYPE_AAAA, DNS_Q2, sizeof(DNS_Q2)-1, 0 },
{"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
".host.name",
DNS_TYPE_AAAA, NULL, 0, DOH_DNS_BAD_LABEL }
};
struct dohresp {
/* input */
const char *packet;
size_t size;
DNStype type;
/* output */
int rc;
const char *out;
};
#define DNS_FOO_EXAMPLE_COM \
"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f" \
"\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00" \
"\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x37\x00\x04\x7f\x00\x00" \
"\x01"
static const char full49[] = DNS_FOO_EXAMPLE_COM;
static struct dohresp resp[] = {
{"\x00\x00", 2, DNS_TYPE_A, DOH_TOO_SMALL_BUFFER, NULL },
{"\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 12,
DNS_TYPE_A, DOH_DNS_BAD_ID, NULL },
{"\x00\x00\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 12,
DNS_TYPE_A, DOH_DNS_BAD_RCODE, NULL },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f", 16,
DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00", 17,
DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00"
"\x00\x01\x00\x01", 21,
DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00"
"\x00\x01\x00\x01"
"\x04", 18,
DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x63\x75\x72"
"\x6c\x04\x63\x75\x72\x6c\x00\x00\x05\x00\x01\xc0\x0c\x00\x05\x00"
"\x01\x00\x00\x00\x37\x00\x11\x08\x61\x6e\x79\x77\x68\x65\x72\x65"
"\x06\x72\x65\x61\x6c\x6c\x79\x00", 56,
DNS_TYPE_A, DOH_OK, "anywhere.really "},
{DNS_FOO_EXAMPLE_COM, 49, DNS_TYPE_A, DOH_OK, "127.0.0.1 "},
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x61\x61\x61"
"\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
"\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
"\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20", 62,
DNS_TYPE_AAAA, DOH_OK,
"2020:2020:0000:0000:0000:0000:0000:2020 " },
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x63\x75\x72"
"\x6c\x04\x63\x75\x72\x6c\x00\x00\x05\x00\x01\xc0\x0c\x00\x05\x00"
"\x01\x00\x00\x00\x37\x00"
"\x07\x03\x61\x6e\x79\xc0\x27\x00", 46,
DNS_TYPE_A, DOH_DNS_LABEL_LOOP, NULL},
/* packet with NSCOUNT == 1 */
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x01\x00\x00\x04\x61\x61\x61"
"\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
"\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
"\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20"
LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE "\x00\x00\x00\x01"
"\00\x04\x01\x01\x01\x01", /* RDDATA */
62 + 30,
DNS_TYPE_AAAA, DOH_OK,
"2020:2020:0000:0000:0000:0000:0000:2020 " },
/* packet with ARCOUNT == 1 */
{"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x01\x04\x61\x61\x61"
"\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
"\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
"\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20"
LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE "\x00\x00\x00\x01"
"\00\x04\x01\x01\x01\x01", /* RDDATA */
62 + 30,
DNS_TYPE_AAAA, DOH_OK,
"2020:2020:0000:0000:0000:0000:0000:2020 " },
};
UNITTEST_START
{
size_t size;
unsigned char buffer[256];
size_t i;
for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) {
int rc = doh_encode(req[i].name, req[i].type,
buffer, sizeof(buffer), &size);
if(rc != req[i].rc) {
fprintf(stderr, "req %d: Expected return code %d got %d\n", i,
req[i].rc, rc);
return 1;
}
else if(size != req[i].size) {
fprintf(stderr, "req %d: Expected size %d got %d\n", i,
(int)req[i].size, (int)size);
fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
return 2;
}
else if(memcmp(req[i].packet, buffer, size)) {
fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
fprintf(stderr, "... instead of: %s\n",
hexdump((unsigned char *)req[i].packet, size));
return 3;
}
}
for(i = 0; i < sizeof(resp) / sizeof(resp[0]); i++) {
struct dohentry d;
int rc;
char *ptr;
size_t len;
int u;
memset(&d, 0, sizeof(d));
rc = doh_decode((unsigned char *)resp[i].packet, resp[i].size,
resp[i].type, &d);
if(rc != resp[i].rc) {
fprintf(stderr, "resp %d: Expected return code %d got %d\n", i,
resp[i].rc, rc);
return 4;
}
len = sizeof(buffer);
ptr = (char *)buffer;
for(u = 0; u < d.numaddr; u++) {
size_t o;
struct dohaddr *a;
a = &d.addr[u];
if(resp[i].type == DNS_TYPE_A) {
snprintf(ptr, len, "%d.%d.%d.%d ",
a->ip.v4 & 0xff, (a->ip.v4>>8) & 0xff,
(a->ip.v4>>16) & 0xff, a->ip.v4 >>24);
o = strlen(ptr);
len -= o;
ptr += o;
}
else {
int j;
for(j = 0; j < 16; j += 2) {
size_t l;
snprintf(ptr, len, "%s%02x%02x", j?":":"", a->ip.v6.byte[j],
a->ip.v6.byte[j + 1]);
l = strlen(ptr);
len -= l;
ptr += l;
}
snprintf(ptr, len, " ");
len--;
ptr++;
}
}
for(u = 0; u < d.numcname; u++) {
size_t o;
snprintf(ptr, len, "%s ", d.cname[u].alloc);
o = strlen(ptr);
len -= o;
ptr += o;
}
de_cleanup(&d);
if(resp[i].out && strcmp((char *)buffer, resp[i].out)) {
fprintf(stderr, "resp %d: Expected %s got %s\n", i,
resp[i].out, buffer);
return 1;
}
}
{
/* pass all sizes into the decoder until full */
for(i = 0; i < sizeof(full49)-1; i++) {
struct dohentry d;
int rc;
memset(&d, 0, sizeof(d));
rc = doh_decode((unsigned char *)full49, i, DNS_TYPE_A, &d);
if(!rc) {
/* none of them should work */
fprintf(stderr, "%d: %d\n", i, rc);
return 5;
}
}
/* and try all pieces from the other end of the packet */
for(i = 1; i < sizeof(full49); i++) {
struct dohentry d;
int rc;
memset(&d, 0, sizeof(d));
rc = doh_decode((unsigned char *)&full49[i], sizeof(full49)-i-1,
DNS_TYPE_A, &d);
if(!rc) {
/* none of them should work */
fprintf(stderr, "2 %d: %d\n", i, rc);
return 7;
}
}
{
int rc;
struct dohentry d;
struct dohaddr *a;
memset(&d, 0, sizeof(d));
rc = doh_decode((unsigned char *)full49, sizeof(full49)-1,
DNS_TYPE_A, &d);
fail_if(d.numaddr != 1, "missing address");
a = &d.addr[i];
snprintf((char *)buffer, sizeof(buffer), "%d.%d.%d.%d\n",
a->ip.v4 & 0xff, (a->ip.v4>>8) & 0xff,
(a->ip.v4>>16) & 0xff, a->ip.v4 >>24);
if(rc && strcmp((char *)buffer, "127.0.0.1")) {
fprintf(stderr, "bad address decoded\n");
return 7;
}
fail_if(d.numcname, "bad cname counter");
}
}
}
UNITTEST_STOP