mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-01 06:55:30 +00:00
Add decompression.
This commit is contained in:
171
bin/tests/compress_test.c
Normal file
171
bin/tests/compress_test.c
Normal file
@@ -0,0 +1,171 @@
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <isc/assertions.h>
|
||||
#include <isc/error.h>
|
||||
#include <dns/compress.h>
|
||||
#include <dns/name.h>
|
||||
|
||||
unsigned char plain1[] = "\003foo";
|
||||
unsigned char plain2[] = "\003bar\003foo";
|
||||
unsigned char plain3[] = "\003xxx\003bar\003foo";
|
||||
unsigned char plain[] =
|
||||
"\003foo\0\003bar\003foo\0\003bar\003foo\0\003xxx\003bar\003foo";
|
||||
|
||||
unsigned char bit1[] = "\101\010b";
|
||||
unsigned char bit2[] = "\101\014b\260";
|
||||
unsigned char bit3[] = "\101\020b\264";
|
||||
unsigned char bit[] = "\101\010b\0\101\014b\260\0\101\014b\260\0\101\020b\264";
|
||||
|
||||
int raw = 0;
|
||||
|
||||
void test(unsigned int, dns_name_t *, dns_name_t *, dns_name_t *,
|
||||
unsigned char *, unsigned int);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
dns_name_t name1;
|
||||
dns_name_t name2;
|
||||
dns_name_t name3;
|
||||
isc_region_t region;
|
||||
int c;
|
||||
|
||||
while ((c = getopt(argc, argv, "r")) != -1) {
|
||||
switch (c) {
|
||||
case 'r':
|
||||
raw++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dns_name_init(&name1, NULL);
|
||||
region.base = plain1;
|
||||
region.length = sizeof plain1;
|
||||
dns_name_fromregion(&name1, ®ion);
|
||||
|
||||
dns_name_init(&name2, NULL);
|
||||
region.base = plain2;
|
||||
region.length = sizeof plain2;
|
||||
dns_name_fromregion(&name2, ®ion);
|
||||
|
||||
dns_name_init(&name3, NULL);
|
||||
region.base = plain3;
|
||||
region.length = sizeof plain3;
|
||||
dns_name_fromregion(&name3, ®ion);
|
||||
|
||||
test(DNS_COMPRESS_NONE, &name1, &name2, &name3, plain, sizeof plain);
|
||||
test(DNS_COMPRESS_GLOBAL14, &name1, &name2, &name3, plain,
|
||||
sizeof plain);
|
||||
test(DNS_COMPRESS_GLOBAL, &name1, &name2, &name3, plain, sizeof plain);
|
||||
test(DNS_COMPRESS_LOCAL, &name1, &name2, &name3, plain, sizeof plain);
|
||||
test(DNS_COMPRESS_ALL, &name1, &name2, &name3, plain, sizeof plain);
|
||||
|
||||
dns_name_init(&name1, NULL);
|
||||
region.base = bit1;
|
||||
region.length = sizeof bit1;
|
||||
dns_name_fromregion(&name1, ®ion);
|
||||
|
||||
dns_name_init(&name2, NULL);
|
||||
region.base = bit2;
|
||||
region.length = sizeof bit2;
|
||||
dns_name_fromregion(&name2, ®ion);
|
||||
|
||||
dns_name_init(&name3, NULL);
|
||||
region.base = bit3;
|
||||
region.length = sizeof bit3;
|
||||
dns_name_fromregion(&name3, ®ion);
|
||||
|
||||
test(DNS_COMPRESS_NONE, &name1, &name2, &name3, bit, sizeof bit);
|
||||
test(DNS_COMPRESS_GLOBAL14, &name1, &name2, &name3, bit, sizeof bit);
|
||||
test(DNS_COMPRESS_GLOBAL, &name1, &name2, &name3, bit, sizeof bit);
|
||||
test(DNS_COMPRESS_LOCAL, &name1, &name2, &name3, bit, sizeof bit);
|
||||
test(DNS_COMPRESS_ALL, &name1, &name2, &name3, bit, sizeof bit);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void
|
||||
test(unsigned int allowed, dns_name_t *name1, dns_name_t *name2,
|
||||
dns_name_t *name3, unsigned char *result, unsigned int length)
|
||||
{
|
||||
isc_mem_t *mctx = NULL;
|
||||
dns_compress_t cctx;
|
||||
dns_decompress_t dctx;
|
||||
isc_buffer_t source;
|
||||
isc_buffer_t target;
|
||||
dns_name_t name;
|
||||
unsigned char buf1[1024];
|
||||
unsigned char buf2[1024];
|
||||
|
||||
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
||||
isc_buffer_init(&source, buf1, sizeof buf1, ISC_BUFFERTYPE_BINARY);
|
||||
RUNTIME_CHECK(dns_compress_init(&cctx, -1, mctx) == DNS_R_SUCCESS);
|
||||
|
||||
RUNTIME_CHECK(dns_name_towire(name1, &cctx, &source) == DNS_R_SUCCESS);
|
||||
|
||||
RUNTIME_CHECK(dns_compress_localinit(&cctx, name1, &source) ==
|
||||
DNS_R_SUCCESS);
|
||||
dns_compress_setmethods(&cctx, allowed);
|
||||
RUNTIME_CHECK(dns_name_towire(name2, &cctx, &source) == DNS_R_SUCCESS);
|
||||
RUNTIME_CHECK(dns_name_towire(name2, &cctx, &source) == DNS_R_SUCCESS);
|
||||
RUNTIME_CHECK(dns_name_towire(name3, &cctx, &source) == DNS_R_SUCCESS);
|
||||
|
||||
dns_compress_localinvalidate(&cctx);
|
||||
dns_compress_invalidate(&cctx);
|
||||
|
||||
if (raw) {
|
||||
unsigned int i;
|
||||
for (i = 0 ; i < source.used ; /* */ ) {
|
||||
fprintf(stdout, "%02x",
|
||||
((unsigned char *)source.base)[i]);
|
||||
if ((++i % 20) == 0)
|
||||
fputs("\n", stdout);
|
||||
else
|
||||
if (i == source.used)
|
||||
fputs("\n", stdout);
|
||||
else
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
}
|
||||
|
||||
isc_buffer_setactive(&source, source.used);
|
||||
isc_buffer_init(&target, buf2, sizeof buf2, ISC_BUFFERTYPE_BINARY);
|
||||
dns_decompress_init(&dctx, -1, ISC_TRUE);
|
||||
|
||||
dns_name_init(&name, NULL);
|
||||
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
|
||||
&target) == DNS_R_SUCCESS);
|
||||
dns_decompress_setmethods(&dctx, allowed);
|
||||
dns_decompress_localinit(&dctx, &name, &source);
|
||||
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
|
||||
&target) == DNS_R_SUCCESS);
|
||||
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
|
||||
&target) == DNS_R_SUCCESS);
|
||||
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
|
||||
&target) == DNS_R_SUCCESS);
|
||||
dns_decompress_localinvalidate(&dctx);
|
||||
dns_decompress_invalidate(&dctx);
|
||||
|
||||
if (raw) {
|
||||
unsigned int i;
|
||||
for (i = 0 ; i < target.used ; /* */ ) {
|
||||
fprintf(stdout, "%02x",
|
||||
((unsigned char *)target.base)[i]);
|
||||
if ((++i % 20) == 0)
|
||||
fputs("\n", stdout);
|
||||
else
|
||||
if (i == target.used)
|
||||
fputs("\n", stdout);
|
||||
else
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
fputs("\n", stdout);
|
||||
}
|
||||
|
||||
RUNTIME_CHECK(target.used == length);
|
||||
RUNTIME_CHECK(memcmp(target.base, result, target.used) == 0);
|
||||
isc_mem_destroy(&mctx);
|
||||
}
|
Reference in New Issue
Block a user