|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "curlcheck.h" |
|
|
|
|
|
#include "doh.h" |
|
|
|
|
|
static CURLcode unit_setup(void) |
|
|
{ |
|
|
|
|
|
return CURLE_OK; |
|
|
} |
|
|
|
|
|
static void unit_stop(void) |
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
#ifndef CURL_DISABLE_DOH |
|
|
|
|
|
UNITTEST_START |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
static const char max[] = |
|
|
|
|
|
|
|
|
"this.is.a.maximum-length.hostname." |
|
|
"with-no-label-of-greater-length-than-the-sixty-three-characters." |
|
|
|
|
|
"specified.in.the.RFCs." |
|
|
"and.with.a.QNAME.encoding.whose.length.is.exactly." |
|
|
"the.maximum.length.allowed." |
|
|
"that.is.two-hundred.and.fifty-six." |
|
|
"including.the.last.null." |
|
|
""; |
|
|
static const char toolong[] = |
|
|
|
|
|
|
|
|
"here.is.a.hostname.which.is.just.barely.too.long." |
|
|
"to.be.encoded.as.a.QNAME.of.the.maximum.allowed.length." |
|
|
|
|
|
"which.is.256.including.a.final.zero-length.label." |
|
|
"representing.the.root.node.so.that.a.name.with." |
|
|
"a.trailing.dot.may.have.up.to." |
|
|
"255.characters.never.more." |
|
|
""; |
|
|
static const char emptylabel[] = |
|
|
"this.is.an.otherwise-valid.hostname." |
|
|
".with.an.empty.label."; |
|
|
static const char outsizelabel[] = |
|
|
"this.is.an.otherwise-valid.hostname." |
|
|
"with-a-label-of-greater-length-than-the-sixty-three-characters-" |
|
|
"specified.in.the.RFCs."; |
|
|
int i; |
|
|
|
|
|
struct test { |
|
|
const char *name; |
|
|
const DOHcode expected_result; |
|
|
}; |
|
|
|
|
|
|
|
|
struct demo { |
|
|
unsigned char dohbuffer[255 + 16]; |
|
|
unsigned char canary1; |
|
|
unsigned char canary2; |
|
|
unsigned char canary3; |
|
|
}; |
|
|
|
|
|
const struct test playlist[4] = { |
|
|
{ toolong, DOH_DNS_NAME_TOO_LONG }, |
|
|
{ emptylabel, DOH_DNS_BAD_LABEL }, |
|
|
{ outsizelabel, DOH_DNS_BAD_LABEL }, |
|
|
{ max, DOH_OK } |
|
|
}; |
|
|
|
|
|
for(i = 0; i < (int)(sizeof(playlist)/sizeof(*playlist)); i++) { |
|
|
const char *name = playlist[i].name; |
|
|
size_t olen = 100000; |
|
|
struct demo victim; |
|
|
DOHcode d; |
|
|
|
|
|
victim.canary1 = 87; |
|
|
victim.canary2 = 35; |
|
|
victim.canary3 = 41; |
|
|
d = doh_req_encode(name, DNS_TYPE_A, victim.dohbuffer, |
|
|
sizeof(struct demo), |
|
|
&olen); |
|
|
|
|
|
fail_unless(d == playlist[i].expected_result, |
|
|
"result returned was not as expected"); |
|
|
if(d == playlist[i].expected_result) { |
|
|
if(name == max) { |
|
|
fail_if(victim.canary1 == 87, |
|
|
"demo one-byte buffer overwrite did not happen"); |
|
|
} |
|
|
else { |
|
|
fail_unless(victim.canary1 == 87, |
|
|
"one-byte buffer overwrite has happened"); |
|
|
} |
|
|
fail_unless(victim.canary2 == 35, |
|
|
"two-byte buffer overwrite has happened"); |
|
|
fail_unless(victim.canary3 == 41, |
|
|
"three-byte buffer overwrite has happened"); |
|
|
} |
|
|
else { |
|
|
if(d == DOH_OK) { |
|
|
fail_unless(olen <= sizeof(victim.dohbuffer), "wrote outside bounds"); |
|
|
fail_unless(olen > strlen(name), "unrealistic low size"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} while(0); |
|
|
|
|
|
|
|
|
do { |
|
|
DNStype dnstype = DNS_TYPE_A; |
|
|
unsigned char buffer[128]; |
|
|
const size_t buflen = sizeof(buffer); |
|
|
const size_t magic1 = 9765; |
|
|
size_t olen1 = magic1; |
|
|
const char *sunshine1 = "a.com"; |
|
|
const char *dotshine1 = "a.com."; |
|
|
const char *sunshine2 = "aa.com"; |
|
|
size_t olen2; |
|
|
DOHcode ret2; |
|
|
size_t olen; |
|
|
|
|
|
DOHcode ret = doh_req_encode(sunshine1, dnstype, buffer, buflen, &olen1); |
|
|
fail_unless(ret == DOH_OK, "sunshine case 1 should pass fine"); |
|
|
fail_if(olen1 == magic1, "olen has not been assigned properly"); |
|
|
fail_unless(olen1 > strlen(sunshine1), "bad out length"); |
|
|
|
|
|
|
|
|
olen2 = magic1; |
|
|
ret2 = doh_req_encode(dotshine1, dnstype, buffer, buflen, &olen2); |
|
|
fail_unless(ret2 == DOH_OK, "dotshine case should pass fine"); |
|
|
fail_if(olen2 == magic1, "olen has not been assigned properly"); |
|
|
fail_unless(olen1 == olen2, "olen should not grow for a trailing dot"); |
|
|
|
|
|
|
|
|
olen2 = magic1; |
|
|
ret2 = doh_req_encode(sunshine2, dnstype, buffer, buflen, &olen2); |
|
|
fail_unless(ret2 == DOH_OK, "sunshine case 2 should pass fine"); |
|
|
fail_if(olen2 == magic1, "olen has not been assigned properly"); |
|
|
fail_unless(olen1 + 1 == olen2, "olen should grow with the hostname"); |
|
|
|
|
|
|
|
|
ret = doh_req_encode(sunshine1, dnstype, buffer, olen1 - 1, &olen); |
|
|
fail_if(ret == DOH_OK, "short buffer should have been noticed"); |
|
|
|
|
|
|
|
|
ret = doh_req_encode(sunshine1, dnstype, buffer, olen1, &olen); |
|
|
fail_unless(ret == DOH_OK, "minimal length buffer should be long enough"); |
|
|
fail_unless(olen == olen1, "bad buffer length"); |
|
|
} while(0); |
|
|
UNITTEST_STOP |
|
|
|
|
|
#else |
|
|
|
|
|
UNITTEST_START |
|
|
|
|
|
UNITTEST_STOP |
|
|
|
|
|
#endif |
|
|
|