1 /* $NetBSD: compress_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1999-2001 Internet Software Consortium. 6 * 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. 10 * 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. 18 */ 19 20 /* Id: compress_test.c,v 1.34 2007/06/18 23:47:26 tbox Exp */ 21 22 /*! \file */ 23 24 #include <config.h> 25 26 #include <stdlib.h> 27 #include <string.h> 28 29 #include <isc/buffer.h> 30 #include <isc/commandline.h> 31 #include <isc/mem.h> 32 #include <isc/util.h> 33 34 #include <dns/compress.h> 35 #include <dns/name.h> 36 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"; 42 43 /* 44 * Result concatenate (plain1, plain2, plain2, plain3). 45 */ 46 int raw = 0; 47 int verbose = 0; 48 49 void 50 test(unsigned int, dns_name_t *, dns_name_t *, dns_name_t *, 51 unsigned char *, unsigned int); 52 53 int 54 main(int argc, char *argv[]) { 55 dns_name_t name1; 56 dns_name_t name2; 57 dns_name_t name3; 58 isc_region_t region; 59 int c; 60 61 while ((c = isc_commandline_parse(argc, argv, "rv")) != -1) { 62 switch (c) { 63 case 'r': 64 raw++; 65 break; 66 case 'v': 67 verbose++; 68 break; 69 } 70 } 71 72 dns_name_init(&name1, NULL); 73 region.base = plain1; 74 region.length = sizeof(plain1); 75 dns_name_fromregion(&name1, ®ion); 76 77 dns_name_init(&name2, NULL); 78 region.base = plain2; 79 region.length = sizeof(plain2); 80 dns_name_fromregion(&name2, ®ion); 81 82 dns_name_init(&name3, NULL); 83 region.base = plain3; 84 region.length = sizeof(plain3); 85 dns_name_fromregion(&name3, ®ion); 86 87 test(DNS_COMPRESS_NONE, &name1, &name2, &name3, plain, sizeof(plain)); 88 test(DNS_COMPRESS_GLOBAL14, &name1, &name2, &name3, plain, 89 sizeof(plain)); 90 test(DNS_COMPRESS_ALL, &name1, &name2, &name3, plain, sizeof(plain)); 91 92 return (0); 93 } 94 95 void 96 test(unsigned int allowed, dns_name_t *name1, dns_name_t *name2, 97 dns_name_t *name3, unsigned char *result, unsigned int length) 98 { 99 isc_mem_t *mctx = NULL; 100 dns_compress_t cctx; 101 dns_decompress_t dctx; 102 isc_buffer_t source; 103 isc_buffer_t target; 104 dns_name_t name; 105 unsigned char buf1[1024]; 106 unsigned char buf2[1024]; 107 108 if (verbose) { 109 const char *s; 110 switch (allowed) { 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; 115 } 116 fprintf(stdout, "Allowed = %s\n", s); 117 } 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); 121 122 RUNTIME_CHECK(dns_name_towire(name1, &cctx, &source) == ISC_R_SUCCESS); 123 124 /* 125 RUNTIME_CHECK(dns_compress_localinit(&cctx, name1, &source) == 126 ISC_R_SUCCESS); 127 */ 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); 132 133 /* 134 dns_compress_localinvalidate(&cctx); 135 */ 136 dns_compress_rollback(&cctx, 0); /* testing only */ 137 dns_compress_invalidate(&cctx); 138 139 if (raw) { 140 unsigned int i; 141 for (i = 0; i < source.used; /* */ ) { 142 fprintf(stdout, "%02x", 143 ((unsigned char *)source.base)[i]); 144 if ((++i % 20) == 0) 145 fputs("\n", stdout); 146 else 147 if (i == source.used) 148 fputs("\n", stdout); 149 else 150 fputs(" ", stdout); 151 } 152 } 153 154 isc_buffer_setactive(&source, source.used); 155 isc_buffer_init(&target, buf2, sizeof(buf2)); 156 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT); 157 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); 162 /* 163 dns_decompress_localinit(&dctx, &name, &source); 164 */ 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); 171 /* 172 dns_decompress_localinvalidate(&dctx); 173 */ 174 dns_decompress_invalidate(&dctx); 175 176 if (raw) { 177 unsigned int i; 178 for (i = 0; i < target.used; /* */ ) { 179 fprintf(stdout, "%02x", 180 ((unsigned char *)target.base)[i]); 181 if ((++i % 20) == 0) 182 fputs("\n", stdout); 183 else 184 if (i == target.used) 185 fputs("\n", stdout); 186 else 187 fputs(" ", stdout); 188 } 189 fputs("\n", stdout); 190 fflush(stdout); 191 } 192 193 RUNTIME_CHECK(target.used == length); 194 RUNTIME_CHECK(memcmp(target.base, result, target.used) == 0); 195 isc_mem_destroy(&mctx); 196 } 197