2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 10:39:16 +00:00
bind/bin/tests/compress_test.c

195 lines
5.3 KiB
C
Raw Normal View History

1999-03-06 03:55:54 +00:00
/*
2007-05-15 23:46:57 +00:00
* Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
2001-01-09 22:01:04 +00:00
* Copyright (C) 1999-2001 Internet Software Consortium.
*
2007-06-18 23:47:57 +00:00
* Permission to use, copy, modify, and/or distribute this software for any
1999-03-06 03:55:54 +00:00
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 05:14:21 +00:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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.
1999-03-06 03:55:54 +00:00
*/
2007-06-18 23:47:57 +00:00
/* $Id: compress_test.c,v 1.34 2007/06/18 23:47:26 tbox Exp $ */
/*! \file */
2000-06-22 22:00:42 +00:00
1999-02-24 06:31:35 +00:00
#include <config.h>
#include <stdlib.h>
2000-11-10 05:34:16 +00:00
#include <string.h>
1999-02-24 06:31:35 +00:00
2000-04-12 17:35:37 +00:00
#include <isc/buffer.h>
#include <isc/commandline.h>
#include <isc/mem.h>
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
2000-04-12 17:35:37 +00:00
1999-02-24 06:31:35 +00:00
#include <dns/compress.h>
#include <dns/name.h>
unsigned char plain1[] = "\003yyy\003foo";
unsigned char plain2[] = "\003bar\003yyy\003foo";
1999-02-24 06:31:35 +00:00
unsigned char plain3[] = "\003xxx\003bar\003foo";
unsigned char plain[] = "\003yyy\003foo\0\003bar\003yyy\003foo\0\003"
"bar\003yyy\003foo\0\003xxx\003bar\003foo";
1999-02-24 06:31:35 +00:00
/*
* Result concatenate (plain1, plain2, plain2, plain3).
*/
1999-02-24 06:31:35 +00:00
int raw = 0;
1999-03-04 06:50:05 +00:00
int verbose = 0;
1999-02-24 06:31:35 +00:00
void
test(unsigned int, dns_name_t *, dns_name_t *, dns_name_t *,
unsigned char *, unsigned int);
1999-02-24 06:31:35 +00:00
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 = isc_commandline_parse(argc, argv, "rv")) != -1) {
1999-02-24 06:31:35 +00:00
switch (c) {
case 'r':
raw++;
break;
1999-03-04 06:50:05 +00:00
case 'v':
verbose++;
break;
1999-02-24 06:31:35 +00:00
}
}
dns_name_init(&name1, NULL);
region.base = plain1;
2001-11-27 01:56:32 +00:00
region.length = sizeof(plain1);
1999-02-24 06:31:35 +00:00
dns_name_fromregion(&name1, &region);
dns_name_init(&name2, NULL);
region.base = plain2;
2001-11-27 01:56:32 +00:00
region.length = sizeof(plain2);
1999-02-24 06:31:35 +00:00
dns_name_fromregion(&name2, &region);
dns_name_init(&name3, NULL);
region.base = plain3;
2001-11-27 01:56:32 +00:00
region.length = sizeof(plain3);
1999-02-24 06:31:35 +00:00
dns_name_fromregion(&name3, &region);
2001-11-27 01:56:32 +00:00
test(DNS_COMPRESS_NONE, &name1, &name2, &name3, plain, sizeof(plain));
1999-02-24 06:31:35 +00:00
test(DNS_COMPRESS_GLOBAL14, &name1, &name2, &name3, plain,
2001-11-27 01:56:32 +00:00
sizeof(plain));
test(DNS_COMPRESS_ALL, &name1, &name2, &name3, plain, sizeof(plain));
1999-02-24 06:31:35 +00:00
return (0);
1999-02-24 06:31:35 +00:00
}
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];
1999-03-04 06:50:05 +00:00
if (verbose) {
const char *s;
1999-03-04 06:50:05 +00:00
switch (allowed) {
case DNS_COMPRESS_NONE: s = "DNS_COMPRESS_NONE"; break;
case DNS_COMPRESS_GLOBAL14: s = "DNS_COMPRESS_GLOBAL14"; break;
2000-04-04 18:30:55 +00:00
/* case DNS_COMPRESS_ALL: s = "DNS_COMPRESS_ALL"; break; */
2006-02-26 22:57:18 +00:00
default: s = "UNKNOWN"; break;
1999-03-04 06:50:05 +00:00
}
fprintf(stdout, "Allowed = %s\n", s);
}
1999-02-24 06:31:35 +00:00
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
isc_buffer_init(&source, buf1, sizeof(buf1));
RUNTIME_CHECK(dns_compress_init(&cctx, -1, mctx) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
RUNTIME_CHECK(dns_name_towire(name1, &cctx, &source) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
2000-04-04 18:30:55 +00:00
/*
RUNTIME_CHECK(dns_compress_localinit(&cctx, name1, &source) ==
ISC_R_SUCCESS);
2000-04-04 18:30:55 +00:00
*/
1999-02-24 06:31:35 +00:00
dns_compress_setmethods(&cctx, allowed);
RUNTIME_CHECK(dns_name_towire(name2, &cctx, &source) == ISC_R_SUCCESS);
RUNTIME_CHECK(dns_name_towire(name2, &cctx, &source) == ISC_R_SUCCESS);
RUNTIME_CHECK(dns_name_towire(name3, &cctx, &source) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
2000-04-04 18:30:55 +00:00
/*
1999-02-24 06:31:35 +00:00
dns_compress_localinvalidate(&cctx);
2000-04-04 18:30:55 +00:00
*/
dns_compress_rollback(&cctx, 0); /* testing only */
1999-02-24 06:31:35 +00:00
dns_compress_invalidate(&cctx);
if (raw) {
unsigned int i;
for (i = 0; i < source.used; /* */ ) {
1999-02-24 06:31:35 +00:00
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));
dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
1999-02-24 06:31:35 +00:00
dns_name_init(&name, NULL);
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
&target) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
dns_decompress_setmethods(&dctx, allowed);
2000-04-04 18:30:55 +00:00
/*
1999-02-24 06:31:35 +00:00
dns_decompress_localinit(&dctx, &name, &source);
2000-04-04 18:30:55 +00:00
*/
1999-02-24 06:31:35 +00:00
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
&target) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
&target) == ISC_R_SUCCESS);
1999-02-24 06:31:35 +00:00
RUNTIME_CHECK(dns_name_fromwire(&name, &source, &dctx, ISC_FALSE,
&target) == ISC_R_SUCCESS);
2000-04-04 18:30:55 +00:00
/*
1999-02-24 06:31:35 +00:00
dns_decompress_localinvalidate(&dctx);
2000-04-04 18:30:55 +00:00
*/
1999-02-24 06:31:35 +00:00
dns_decompress_invalidate(&dctx);
if (raw) {
unsigned int i;
for (i = 0; i < target.used; /* */ ) {
1999-02-24 06:31:35 +00:00
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);
fflush(stdout);
1999-02-24 06:31:35 +00:00
}
RUNTIME_CHECK(target.used == length);
RUNTIME_CHECK(memcmp(target.base, result, target.used) == 0);
isc_mem_destroy(&mctx);
}