diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-02-12 23:59:49 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-02-12 23:59:49 -0900 |
| commit | d072b028fc8962805a3cace00ff34b503325dc34 (patch) | |
| tree | a44e66a7542c77a24526af03bf146c8482b0c0be /tests | |
| parent | 697b9deb5528c3b2f94a4259cde8345b4191f7e6 (diff) | |
| download | tinyrad-d072b028fc8962805a3cace00ff34b503325dc34.tar.gz tinyrad-d072b028fc8962805a3cace00ff34b503325dc34.tar.bz2 tinyrad-d072b028fc8962805a3cace00ff34b503325dc34.tar.xz tinyrad-d072b028fc8962805a3cace00ff34b503325dc34.zip | |
tests: adding test-pckt-byte-order
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-pckt-byte-order.c | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/tests/test-pckt-byte-order.c b/tests/test-pckt-byte-order.c new file mode 100644 index 0000000..9f63ec5 --- /dev/null +++ b/tests/test-pckt-byte-order.c | |||
| @@ -0,0 +1,331 @@ | |||
| 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_PCKT_BYTE_ORDER_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 | #include <sys/socket.h> | ||
| 48 | |||
| 49 | |||
| 50 | /////////////////// | ||
| 51 | // // | ||
| 52 | // Definitions // | ||
| 53 | // // | ||
| 54 | /////////////////// | ||
| 55 | #pragma mark - Definitions | ||
| 56 | |||
| 57 | #undef PROGRAM_NAME | ||
| 58 | #define PROGRAM_NAME "test-pckt-byte-order" | ||
| 59 | |||
| 60 | |||
| 61 | ////////////////// | ||
| 62 | // // | ||
| 63 | // Prototypes // | ||
| 64 | // // | ||
| 65 | ////////////////// | ||
| 66 | #pragma mark - Prototypes | ||
| 67 | |||
| 68 | int main( | ||
| 69 | int argc, | ||
| 70 | char * argv[] ); | ||
| 71 | |||
| 72 | |||
| 73 | void | ||
| 74 | my_print_header( | ||
| 75 | unsigned opts, | ||
| 76 | size_t width ); | ||
| 77 | |||
| 78 | |||
| 79 | int | ||
| 80 | my_test_value( | ||
| 81 | unsigned opts, | ||
| 82 | size_t width, | ||
| 83 | size_t offset, | ||
| 84 | uint64_t value ); | ||
| 85 | |||
| 86 | |||
| 87 | ///////////////// | ||
| 88 | // // | ||
| 89 | // Functions // | ||
| 90 | // // | ||
| 91 | ///////////////// | ||
| 92 | #pragma mark - Functions | ||
| 93 | |||
| 94 | int main(int argc, char * argv[]) | ||
| 95 | { | ||
| 96 | int opt; | ||
| 97 | int c; | ||
| 98 | int opt_index; | ||
| 99 | unsigned opts; | ||
| 100 | size_t pos; | ||
| 101 | uint64_t value; | ||
| 102 | int errs; | ||
| 103 | |||
| 104 | // getopt options | ||
| 105 | static char short_opt[] = "dhVvq"; | ||
| 106 | static struct option long_opt[] = | ||
| 107 | { | ||
| 108 | {"debug", no_argument, NULL, 'd' }, | ||
| 109 | {"help", no_argument, NULL, 'h' }, | ||
| 110 | {"quiet", no_argument, NULL, 'q' }, | ||
| 111 | {"silent", no_argument, NULL, 'q' }, | ||
| 112 | {"version", no_argument, NULL, 'V' }, | ||
| 113 | {"verbose", no_argument, NULL, 'v' }, | ||
| 114 | { NULL, 0, NULL, 0 } | ||
| 115 | }; | ||
| 116 | |||
| 117 | our_initialize(PROGRAM_NAME); | ||
| 118 | |||
| 119 | opts = 0; | ||
| 120 | errs = 0; | ||
| 121 | |||
| 122 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 123 | { | ||
| 124 | switch(c) | ||
| 125 | { | ||
| 126 | case -1: /* no more arguments */ | ||
| 127 | case 0: /* long options toggles */ | ||
| 128 | break; | ||
| 129 | |||
| 130 | case 'd': | ||
| 131 | opt = TRAD_DEBUG_ANY; | ||
| 132 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | ||
| 133 | break; | ||
| 134 | |||
| 135 | case 'h': | ||
| 136 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 137 | printf("OPTIONS:\n"); | ||
| 138 | printf(" -d, --debug print debug messages\n"); | ||
| 139 | printf(" -h, --help print this help and exit\n"); | ||
| 140 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 141 | printf(" -V, --version print version number and exit\n"); | ||
| 142 | printf(" -v, --verbose print verbose messages\n"); | ||
| 143 | printf("\n"); | ||
| 144 | return(0); | ||
| 145 | |||
| 146 | case 'q': | ||
| 147 | opts |= TRAD_TEST_QUIET; | ||
| 148 | opts &= ~TRAD_TEST_VERBOSE; | ||
| 149 | break; | ||
| 150 | |||
| 151 | case 'V': | ||
| 152 | our_version(PROGRAM_NAME); | ||
| 153 | return(0); | ||
| 154 | |||
| 155 | case 'v': | ||
| 156 | opts |= TRAD_TEST_VERBOSE; | ||
| 157 | opts &= ~TRAD_TEST_QUIET; | ||
| 158 | break; | ||
| 159 | |||
| 160 | case '?': | ||
| 161 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 162 | return(1); | ||
| 163 | |||
| 164 | default: | ||
| 165 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 166 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 167 | return(1); | ||
| 168 | }; | ||
| 169 | }; | ||
| 170 | |||
| 171 | my_print_header(opts, sizeof(uint16_t)); | ||
| 172 | value = 0x2211; | ||
| 173 | for(pos = 0; (pos < (sizeof(uint64_t)+1)); pos++) | ||
| 174 | errs += my_test_value( opts, sizeof(uint16_t), pos, value ); | ||
| 175 | if ((opts & TRAD_TEST_VERBOSE)) printf("\n"); | ||
| 176 | |||
| 177 | my_print_header(opts, sizeof(uint32_t)); | ||
| 178 | value = 0x44332211; | ||
| 179 | for(pos = 0; (pos < (sizeof(uint64_t)+1)); pos++) | ||
| 180 | errs += my_test_value( opts, sizeof(uint32_t), pos, value ); | ||
| 181 | if ((opts & TRAD_TEST_VERBOSE)) printf("\n"); | ||
| 182 | |||
| 183 | my_print_header(opts, sizeof(uint64_t)); | ||
| 184 | value = 0x8877665544332211; | ||
| 185 | for(pos = 0; (pos < (sizeof(uint64_t)+1)); pos++) | ||
| 186 | errs += my_test_value( opts, sizeof(uint64_t), pos, value ); | ||
| 187 | if ((opts & TRAD_TEST_VERBOSE)) printf("\n"); | ||
| 188 | |||
| 189 | return( (((errs)) ? 1 : 0) ); | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | void | ||
| 194 | my_print_header( | ||
| 195 | unsigned opts, | ||
| 196 | size_t width ) | ||
| 197 | { | ||
| 198 | const char * htop_str; | ||
| 199 | const char * hton_str; | ||
| 200 | const char * ptoh_str; | ||
| 201 | |||
| 202 | if (!(opts & TRAD_TEST_VERBOSE)) | ||
| 203 | return; | ||
| 204 | |||
| 205 | switch(width) | ||
| 206 | { | ||
| 207 | case 2: | ||
| 208 | hton_str = "htons(value)"; | ||
| 209 | htop_str = "tinyrad_htops(&b[x], value)"; | ||
| 210 | ptoh_str = "tinyrad_ptohs(&b[x])"; | ||
| 211 | break; | ||
| 212 | |||
| 213 | case 4: | ||
| 214 | hton_str = "htonl(value)"; | ||
| 215 | htop_str = "tinyrad_htopl(&b[x], value)"; | ||
| 216 | ptoh_str = "tinyrad_ptohl(&b[x])"; | ||
| 217 | break; | ||
| 218 | |||
| 219 | case 8: | ||
| 220 | hton_str = "n/a for 64 bit"; // htonll() is not standard | ||
| 221 | htop_str = "tinyrad_htopll(&b[x], value)"; | ||
| 222 | ptoh_str = "tinyrad_ptohll(&b[x])"; | ||
| 223 | break; | ||
| 224 | |||
| 225 | default: | ||
| 226 | hton_str = "hton(value)"; | ||
| 227 | htop_str = "tinyrad_htop(&b[x], value)"; | ||
| 228 | ptoh_str = "tinyrad_ptoh(&b[x])"; | ||
| 229 | break; | ||
| 230 | }; | ||
| 231 | printf( | ||
| 232 | " x %18s %39s %21s %18s\n", | ||
| 233 | "value", | ||
| 234 | htop_str, | ||
| 235 | ptoh_str, | ||
| 236 | hton_str | ||
| 237 | ); | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | int | ||
| 243 | my_test_value( | ||
| 244 | unsigned opts, | ||
| 245 | size_t width, | ||
| 246 | size_t offset, | ||
| 247 | uint64_t value ) | ||
| 248 | { | ||
| 249 | size_t pos; | ||
| 250 | uint64_t ptoh; | ||
| 251 | uint64_t hton; | ||
| 252 | uint8_t buff[sizeof(uint64_t)*2]; | ||
| 253 | uint8_t byte; | ||
| 254 | const char * title; | ||
| 255 | |||
| 256 | memset(buff, 0xff, sizeof(buff)); | ||
| 257 | |||
| 258 | switch(width) | ||
| 259 | { | ||
| 260 | case 2: | ||
| 261 | title = "uint16_t:"; | ||
| 262 | hton = htons(value); | ||
| 263 | tinyrad_htops(&buff[offset], value); | ||
| 264 | ptoh = tinyrad_ptohs(&buff[offset]); | ||
| 265 | break; | ||
| 266 | |||
| 267 | case 4: | ||
| 268 | title = "uint32_t:"; | ||
| 269 | hton = htonl(value); | ||
| 270 | tinyrad_htopl(&buff[offset], value); | ||
| 271 | ptoh = tinyrad_ptohl(&buff[offset]); | ||
| 272 | break; | ||
| 273 | |||
| 274 | case 8: | ||
| 275 | title = "uint64_t:"; | ||
| 276 | hton = 0; // htonll() is not standard | ||
| 277 | tinyrad_htopll(&buff[offset], value); | ||
| 278 | ptoh = tinyrad_ptohll(&buff[offset]); | ||
| 279 | break; | ||
| 280 | |||
| 281 | default: | ||
| 282 | return(1); | ||
| 283 | }; | ||
| 284 | |||
| 285 | if ((opts & TRAD_TEST_VERBOSE)) printf("%s", title); | ||
| 286 | if ((opts & TRAD_TEST_VERBOSE)) printf(" %zu", offset); | ||
| 287 | if ((opts & TRAD_TEST_VERBOSE)) printf(" 0x%016" PRIx64, value); | ||
| 288 | if ((opts & TRAD_TEST_VERBOSE)) printf(" "); | ||
| 289 | if ((opts & TRAD_TEST_VERBOSE)) | ||
| 290 | { | ||
| 291 | for(pos = 0; (pos < sizeof(buff)); pos++) | ||
| 292 | { | ||
| 293 | if (!(pos & 0x01)) | ||
| 294 | printf(" "); | ||
| 295 | printf("%02" PRIx8, buff[pos]); | ||
| 296 | }; | ||
| 297 | }; | ||
| 298 | if ((opts & TRAD_TEST_VERBOSE)) printf(" 0x%016" PRIx64, ptoh); | ||
| 299 | if ((opts & TRAD_TEST_VERBOSE)) printf(" 0x%016" PRIx64, hton); | ||
| 300 | if ((opts & TRAD_TEST_VERBOSE)) printf("\n"); | ||
| 301 | |||
| 302 | if ( value != ptoh) | ||
| 303 | { | ||
| 304 | our_error(opts, NULL, "value parsed from buffer does not match original"); | ||
| 305 | return(1); | ||
| 306 | }; | ||
| 307 | |||
| 308 | for(pos = 0; (pos < sizeof(buff)); pos++) | ||
| 309 | { | ||
| 310 | if ( (pos >= offset) && (pos < (offset+width)) ) | ||
| 311 | { | ||
| 312 | byte = ((offset+width-pos) << 4) | ((offset+width-pos) << 0); | ||
| 313 | if (buff[pos] != byte) | ||
| 314 | { | ||
| 315 | our_error(opts, NULL, "unexpected byte in buffer at offset %zu", pos); | ||
| 316 | return(1); | ||
| 317 | }; | ||
| 318 | } else { | ||
| 319 | if (buff[pos] != 0xff) | ||
| 320 | { | ||
| 321 | our_error(opts, NULL, "unexpected byte in buffer at offset %zu", pos); | ||
| 322 | return(1); | ||
| 323 | }; | ||
| 324 | }; | ||
| 325 | }; | ||
| 326 | |||
| 327 | return(0); | ||
| 328 | } | ||
| 329 | |||
| 330 | |||
| 331 | /* end of source */ | ||
