1 /* $NetBSD: compress_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: compress_test.c,v 1.34 2007/06/18 23:47:26 tbox Exp */
29 #include <isc/buffer.h>
30 #include <isc/commandline.h>
34 #include <dns/compress.h>
37 unsigned char plain1
[] = "\003yyy\003foo";
38 unsigned char plain2
[] = "\003bar\003yyy\003foo";
39 unsigned char plain3
[] = "\003xxx\003bar\003foo";
40 unsigned char plain
[] = "\003yyy\003foo\0\003bar\003yyy\003foo\0\003"
41 "bar\003yyy\003foo\0\003xxx\003bar\003foo";
44 * Result concatenate (plain1, plain2, plain2, plain3).
50 test(unsigned int, dns_name_t
*, dns_name_t
*, dns_name_t
*,
51 unsigned char *, unsigned int);
54 main(int argc
, char *argv
[]) {
61 while ((c
= isc_commandline_parse(argc
, argv
, "rv")) != -1) {
72 dns_name_init(&name1
, NULL
);
74 region
.length
= sizeof(plain1
);
75 dns_name_fromregion(&name1
, ®ion
);
77 dns_name_init(&name2
, NULL
);
79 region
.length
= sizeof(plain2
);
80 dns_name_fromregion(&name2
, ®ion
);
82 dns_name_init(&name3
, NULL
);
84 region
.length
= sizeof(plain3
);
85 dns_name_fromregion(&name3
, ®ion
);
87 test(DNS_COMPRESS_NONE
, &name1
, &name2
, &name3
, plain
, sizeof(plain
));
88 test(DNS_COMPRESS_GLOBAL14
, &name1
, &name2
, &name3
, plain
,
90 test(DNS_COMPRESS_ALL
, &name1
, &name2
, &name3
, plain
, sizeof(plain
));
96 test(unsigned int allowed
, dns_name_t
*name1
, dns_name_t
*name2
,
97 dns_name_t
*name3
, unsigned char *result
, unsigned int length
)
99 isc_mem_t
*mctx
= NULL
;
101 dns_decompress_t dctx
;
105 unsigned char buf1
[1024];
106 unsigned char buf2
[1024];
111 case DNS_COMPRESS_NONE
: s
= "DNS_COMPRESS_NONE"; break;
112 case DNS_COMPRESS_GLOBAL14
: s
= "DNS_COMPRESS_GLOBAL14"; break;
113 /* case DNS_COMPRESS_ALL: s = "DNS_COMPRESS_ALL"; break; */
114 default: s
= "UNKNOWN"; break;
116 fprintf(stdout
, "Allowed = %s\n", s
);
118 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
119 isc_buffer_init(&source
, buf1
, sizeof(buf1
));
120 RUNTIME_CHECK(dns_compress_init(&cctx
, -1, mctx
) == ISC_R_SUCCESS
);
122 RUNTIME_CHECK(dns_name_towire(name1
, &cctx
, &source
) == ISC_R_SUCCESS
);
125 RUNTIME_CHECK(dns_compress_localinit(&cctx, name1, &source) ==
128 dns_compress_setmethods(&cctx
, allowed
);
129 RUNTIME_CHECK(dns_name_towire(name2
, &cctx
, &source
) == ISC_R_SUCCESS
);
130 RUNTIME_CHECK(dns_name_towire(name2
, &cctx
, &source
) == ISC_R_SUCCESS
);
131 RUNTIME_CHECK(dns_name_towire(name3
, &cctx
, &source
) == ISC_R_SUCCESS
);
134 dns_compress_localinvalidate(&cctx);
136 dns_compress_rollback(&cctx
, 0); /* testing only */
137 dns_compress_invalidate(&cctx
);
141 for (i
= 0; i
< source
.used
; /* */ ) {
142 fprintf(stdout
, "%02x",
143 ((unsigned char *)source
.base
)[i
]);
147 if (i
== source
.used
)
154 isc_buffer_setactive(&source
, source
.used
);
155 isc_buffer_init(&target
, buf2
, sizeof(buf2
));
156 dns_decompress_init(&dctx
, -1, DNS_DECOMPRESS_STRICT
);
158 dns_name_init(&name
, NULL
);
159 RUNTIME_CHECK(dns_name_fromwire(&name
, &source
, &dctx
, ISC_FALSE
,
160 &target
) == ISC_R_SUCCESS
);
161 dns_decompress_setmethods(&dctx
, allowed
);
163 dns_decompress_localinit(&dctx, &name, &source);
165 RUNTIME_CHECK(dns_name_fromwire(&name
, &source
, &dctx
, ISC_FALSE
,
166 &target
) == ISC_R_SUCCESS
);
167 RUNTIME_CHECK(dns_name_fromwire(&name
, &source
, &dctx
, ISC_FALSE
,
168 &target
) == ISC_R_SUCCESS
);
169 RUNTIME_CHECK(dns_name_fromwire(&name
, &source
, &dctx
, ISC_FALSE
,
170 &target
) == ISC_R_SUCCESS
);
172 dns_decompress_localinvalidate(&dctx);
174 dns_decompress_invalidate(&dctx
);
178 for (i
= 0; i
< target
.used
; /* */ ) {
179 fprintf(stdout
, "%02x",
180 ((unsigned char *)target
.base
)[i
]);
184 if (i
== target
.used
)
193 RUNTIME_CHECK(target
.used
== length
);
194 RUNTIME_CHECK(memcmp(target
.base
, result
, target
.used
) == 0);
195 isc_mem_destroy(&mctx
);