diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-02-28 23:48:04 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-02-28 23:54:57 -0900 |
| commit | 875eab12781f53b7a6454b6fdee610160bd0c6ec (patch) | |
| tree | b66da1515d08141acfbea3095fa0c02ced73d62c /tests/test-oid-str.c | |
| parent | dba37afac42b2b3f8f7ed93d31dec65979ae3a2c (diff) | |
| download | tinyrad-875eab12781f53b7a6454b6fdee610160bd0c6ec.tar.gz tinyrad-875eab12781f53b7a6454b6fdee610160bd0c6ec.tar.bz2 tinyrad-875eab12781f53b7a6454b6fdee610160bd0c6ec.tar.xz tinyrad-875eab12781f53b7a6454b6fdee610160bd0c6ec.zip | |
tests: adding test-oid-str
Diffstat (limited to 'tests/test-oid-str.c')
| -rw-r--r-- | tests/test-oid-str.c | 366 |
1 files changed, 366 insertions, 0 deletions
diff --git a/tests/test-oid-str.c b/tests/test-oid-str.c new file mode 100644 index 0000000..9c5c18b --- /dev/null +++ b/tests/test-oid-str.c | |||
| @@ -0,0 +1,366 @@ | |||
| 1 | /* | ||
| 2 | * Tiny RADIUS Client Library | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _TESTS_TINYRAD_OID_STR_C 1 | ||
| 32 | |||
| 33 | /////////////// | ||
| 34 | // // | ||
| 35 | // Headers // | ||
| 36 | // // | ||
| 37 | /////////////// | ||
| 38 | #pragma mark - Headers | ||
| 39 | |||
| 40 | #include "common.h" | ||
| 41 | |||
| 42 | #include <stdio.h> | ||
| 43 | #include <stdint.h> | ||
| 44 | #include <inttypes.h> | ||
| 45 | #include <string.h> | ||
| 46 | #include <getopt.h> | ||
| 47 | |||
| 48 | |||
| 49 | /////////////////// | ||
| 50 | // // | ||
| 51 | // Definitions // | ||
| 52 | // // | ||
| 53 | /////////////////// | ||
| 54 | #pragma mark - Definitions | ||
| 55 | |||
| 56 | #undef PROGRAM_NAME | ||
| 57 | #define PROGRAM_NAME "test-oid-str" | ||
| 58 | |||
| 59 | |||
| 60 | ////////////////// | ||
| 61 | // // | ||
| 62 | // Data Types // | ||
| 63 | // // | ||
| 64 | ////////////////// | ||
| 65 | #pragma mark - Data Types | ||
| 66 | |||
| 67 | struct test_data | ||
| 68 | { | ||
| 69 | size_t len; | ||
| 70 | uint32_t vals[8]; | ||
| 71 | }; | ||
| 72 | |||
| 73 | |||
| 74 | ///////////////// | ||
| 75 | // // | ||
| 76 | // Variables // | ||
| 77 | // // | ||
| 78 | ///////////////// | ||
| 79 | #pragma mark - Variables | ||
| 80 | |||
| 81 | struct test_data test_vals[] = | ||
| 82 | { | ||
| 83 | { 1, { TRAD_ATTR_USER_NAME } }, | ||
| 84 | { 1, { TRAD_ATTR_USER_PASSWORD } }, | ||
| 85 | { 3, { TRAD_ATTR_VENDOR_SPECIFIC, 27893, 1 } }, | ||
| 86 | { 3, { TRAD_ATTR_VENDOR_SPECIFIC, 27893, 254 } }, | ||
| 87 | { 4, { TRAD_ATTR_EXTENDED_ATTRIBUTE_1, 26, 27893, 1 } }, | ||
| 88 | { 4, { TRAD_ATTR_EXTENDED_ATTRIBUTE_1, 26, 27893, 254 } }, | ||
| 89 | { 0, { 0 } } | ||
| 90 | }; | ||
| 91 | |||
| 92 | |||
| 93 | ////////////////// | ||
| 94 | // // | ||
| 95 | // Prototypes // | ||
| 96 | // // | ||
| 97 | ////////////////// | ||
| 98 | #pragma mark - Prototypes | ||
| 99 | |||
| 100 | int main( | ||
| 101 | int argc, | ||
| 102 | char * argv[] ); | ||
| 103 | |||
| 104 | |||
| 105 | char * | ||
| 106 | my_oidstr( | ||
| 107 | uint32_t * vals, | ||
| 108 | size_t len ); | ||
| 109 | |||
| 110 | |||
| 111 | int | ||
| 112 | my_oid_test( | ||
| 113 | int opts, | ||
| 114 | TinyRadOID * oldoid, | ||
| 115 | int type, | ||
| 116 | const char * title ); | ||
| 117 | |||
| 118 | |||
| 119 | ///////////////// | ||
| 120 | // // | ||
| 121 | // Functions // | ||
| 122 | // // | ||
| 123 | ///////////////// | ||
| 124 | #pragma mark - Functions | ||
| 125 | |||
| 126 | int main(int argc, char * argv[]) | ||
| 127 | { | ||
| 128 | int opt; | ||
| 129 | int c; | ||
| 130 | int opt_index; | ||
| 131 | unsigned opts; | ||
| 132 | int errs; | ||
| 133 | int rc; | ||
| 134 | size_t pos; | ||
| 135 | size_t x; | ||
| 136 | size_t y; | ||
| 137 | // char * str; | ||
| 138 | TinyRadOID * oid; | ||
| 139 | // TinyRadOID * oid2; | ||
| 140 | uint32_t * vals; | ||
| 141 | size_t len; | ||
| 142 | |||
| 143 | // getopt options | ||
| 144 | static char short_opt[] = "dhVvq"; | ||
| 145 | static struct option long_opt[] = | ||
| 146 | { | ||
| 147 | {"debug", no_argument, NULL, 'd' }, | ||
| 148 | {"help", no_argument, NULL, 'h' }, | ||
| 149 | {"quiet", no_argument, NULL, 'q' }, | ||
| 150 | {"silent", no_argument, NULL, 'q' }, | ||
| 151 | {"version", no_argument, NULL, 'V' }, | ||
| 152 | {"verbose", no_argument, NULL, 'v' }, | ||
| 153 | { NULL, 0, NULL, 0 } | ||
| 154 | }; | ||
| 155 | |||
| 156 | our_initialize(PROGRAM_NAME); | ||
| 157 | |||
| 158 | opts = 0; | ||
| 159 | errs = 0; | ||
| 160 | |||
| 161 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 162 | { | ||
| 163 | switch(c) | ||
| 164 | { | ||
| 165 | case -1: /* no more arguments */ | ||
| 166 | case 0: /* long options toggles */ | ||
| 167 | break; | ||
| 168 | |||
| 169 | case 'd': | ||
| 170 | opt = TRAD_DEBUG_ANY; | ||
| 171 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | ||
| 172 | break; | ||
| 173 | |||
| 174 | case 'h': | ||
| 175 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 176 | printf("OPTIONS:\n"); | ||
| 177 | printf(" -d, --debug print debug messages\n"); | ||
| 178 | printf(" -h, --help print this help and exit\n"); | ||
| 179 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 180 | printf(" -V, --version print version number and exit\n"); | ||
| 181 | printf(" -v, --verbose print verbose messages\n"); | ||
| 182 | printf("\n"); | ||
| 183 | return(0); | ||
| 184 | |||
| 185 | case 'q': | ||
| 186 | opts |= TRAD_TEST_QUIET; | ||
| 187 | opts &= ~TRAD_TEST_VERBOSE; | ||
| 188 | break; | ||
| 189 | |||
| 190 | case 'V': | ||
| 191 | our_version(PROGRAM_NAME); | ||
| 192 | return(0); | ||
| 193 | |||
| 194 | case 'v': | ||
| 195 | opts |= TRAD_TEST_VERBOSE; | ||
| 196 | opts &= ~TRAD_TEST_QUIET; | ||
| 197 | break; | ||
| 198 | |||
| 199 | case '?': | ||
| 200 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 201 | return(1); | ||
| 202 | |||
| 203 | default: | ||
| 204 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 205 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 206 | return(1); | ||
| 207 | }; | ||
| 208 | }; | ||
| 209 | |||
| 210 | for(x = 0; ((test_vals[x].len)); x++) | ||
| 211 | { | ||
| 212 | for(y = 1; (y <= test_vals[x].len); y++) | ||
| 213 | { | ||
| 214 | our_verbose(opts, "testing oid %s ...", my_oidstr(test_vals[x].vals, y)); | ||
| 215 | our_verbose(opts, " Initializing OID from values ..."); | ||
| 216 | if ((oid = tinyrad_oid_init(test_vals[x].vals, y)) == NULL) | ||
| 217 | { | ||
| 218 | our_error(opts, NULL, "out of virtual memory"); | ||
| 219 | errs++; | ||
| 220 | continue; | ||
| 221 | }; | ||
| 222 | |||
| 223 | our_verbose(opts, " Testing values of OID ..."); | ||
| 224 | if ((rc = tinyrad_oid_values(oid, &vals, &len)) != TRAD_SUCCESS) | ||
| 225 | { | ||
| 226 | our_error(opts, NULL, "tinyrad_oid_values(): %s", tinyrad_strerror(rc)); | ||
| 227 | errs++; | ||
| 228 | tinyrad_free(oid); | ||
| 229 | continue; | ||
| 230 | }; | ||
| 231 | if (len != y) | ||
| 232 | { | ||
| 233 | our_error(opts, NULL, "OID length does not match reference"); | ||
| 234 | errs++; | ||
| 235 | tinyrad_free(oid); | ||
| 236 | continue; | ||
| 237 | }; | ||
| 238 | for(pos = 0; (pos < y); pos++) | ||
| 239 | { | ||
| 240 | if (test_vals[x].vals[pos] != vals[pos]) | ||
| 241 | { | ||
| 242 | our_error(opts, NULL, "OID values do not match reference"); | ||
| 243 | errs++; | ||
| 244 | pos = y; | ||
| 245 | }; | ||
| 246 | }; | ||
| 247 | tinyrad_free(vals); | ||
| 248 | |||
| 249 | if ((my_oid_test(opts, oid, TRAD_OID_TYPE_ATTRIBUTE, "canonical attribute name"))) | ||
| 250 | errs++; | ||
| 251 | |||
| 252 | if ((my_oid_test(opts, oid, TRAD_OID_TYPE_VENDOR, "canonical vendor name"))) | ||
| 253 | errs++; | ||
| 254 | |||
| 255 | if ((my_oid_test(opts, oid, TRAD_OID_TYPE_VALUE, "canonical value name"))) | ||
| 256 | errs++; | ||
| 257 | |||
| 258 | if ((my_oid_test(opts, oid, TRAD_OID_TYPE_NONE, "OID string"))) | ||
| 259 | errs++; | ||
| 260 | |||
| 261 | if ((my_oid_test(opts, oid, 0xffff, "unknown canonical name"))) | ||
| 262 | errs++; | ||
| 263 | |||
| 264 | tinyrad_free(oid); | ||
| 265 | }; | ||
| 266 | }; | ||
| 267 | |||
| 268 | return( (((errs)) ? 1 : 0) ); | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | char * | ||
| 273 | my_oidstr( | ||
| 274 | uint32_t * vals, | ||
| 275 | size_t len ) | ||
| 276 | { | ||
| 277 | size_t pos; | ||
| 278 | size_t str_len; | ||
| 279 | static char str[128]; | ||
| 280 | |||
| 281 | str[0] = '\0'; | ||
| 282 | str_len = 0; | ||
| 283 | |||
| 284 | for(pos = 0; (pos < len); pos++) | ||
| 285 | { | ||
| 286 | if (str_len >= sizeof(str)) | ||
| 287 | continue; | ||
| 288 | str_len += snprintf(&str[str_len], (sizeof(str)-str_len-1), ".%"PRIu32, vals[pos]); | ||
| 289 | }; | ||
| 290 | str[sizeof(str)-1] = '\0'; | ||
| 291 | |||
| 292 | return(&str[1]); | ||
| 293 | } | ||
| 294 | |||
| 295 | |||
| 296 | int | ||
| 297 | my_oid_test( | ||
| 298 | int opts, | ||
| 299 | TinyRadOID * oldoid, | ||
| 300 | int type, | ||
| 301 | const char * title ) | ||
| 302 | { | ||
| 303 | TinyRadOID * newoid; | ||
| 304 | char * str; | ||
| 305 | size_t pos; | ||
| 306 | size_t newlen; | ||
| 307 | size_t oldlen; | ||
| 308 | uint32_t * newvals; | ||
| 309 | uint32_t * oldvals; | ||
| 310 | |||
| 311 | our_verbose(opts, " Generating %s ...", title); | ||
| 312 | if ((str = tinyrad_oid2str(oldoid, type)) == NULL) | ||
| 313 | { | ||
| 314 | our_error(opts, NULL, "out of virtual memory"); | ||
| 315 | return(1); | ||
| 316 | }; | ||
| 317 | our_verbose(opts, " %s: %s", title, str); | ||
| 318 | |||
| 319 | our_verbose(opts, " Generating OID from %s ...", title); | ||
| 320 | if ((newoid = tinyrad_str2oid(str)) == NULL) | ||
| 321 | { | ||
| 322 | our_error(opts, NULL, "error generating OID from %s", title); | ||
| 323 | tinyrad_free(str); | ||
| 324 | return(1); | ||
| 325 | }; | ||
| 326 | |||
| 327 | tinyrad_free(str); | ||
| 328 | |||
| 329 | tinyrad_oid_values(newoid, &newvals, &newlen); | ||
| 330 | tinyrad_oid_values(oldoid, &oldvals, &oldlen); | ||
| 331 | our_verbose(opts, " Generated OID: %s", my_oidstr(newvals, newlen)); | ||
| 332 | if (newlen != oldlen) | ||
| 333 | { | ||
| 334 | our_error(opts, NULL, "old and new OID lengths do not match"); | ||
| 335 | tinyrad_free(newoid); | ||
| 336 | tinyrad_free(newvals); | ||
| 337 | tinyrad_free(oldvals); | ||
| 338 | return(1); | ||
| 339 | }; | ||
| 340 | for(pos = 0; (pos < newlen); pos++) | ||
| 341 | { | ||
| 342 | if (newvals[pos] != oldvals[pos]) | ||
| 343 | { | ||
| 344 | our_error(opts, NULL, "old and new OID values do not match"); | ||
| 345 | tinyrad_free(newoid); | ||
| 346 | tinyrad_free(newvals); | ||
| 347 | tinyrad_free(oldvals); | ||
| 348 | return(1); | ||
| 349 | }; | ||
| 350 | }; | ||
| 351 | |||
| 352 | our_verbose(opts, " comparing old OID and new OID ..."); | ||
| 353 | if ((tinyrad_oid_cmp(&oldoid, &newoid))) | ||
| 354 | { | ||
| 355 | tinyrad_free(newoid); | ||
| 356 | our_error(opts, NULL, "old OID does not match new OID"); | ||
| 357 | return(1); | ||
| 358 | }; | ||
| 359 | |||
| 360 | tinyrad_free(newoid); | ||
| 361 | |||
| 362 | return(0); | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | /* end of source */ | ||
