diff options
| author | David M. Syzdek <david@syzdek.net> | 2023-01-09 23:45:05 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2023-01-09 23:45:05 -0900 |
| commit | 0edded79e3027427ae6b7e2e3cb4235ab8bf9d24 (patch) | |
| tree | 70e105b685864d6c8d708dd7b3b4716cc9164f7e | |
| parent | f7b1ff631c1331667b73458ce6711adf0ea9d859 (diff) | |
| download | otputils-0edded79e3027427ae6b7e2e3cb4235ab8bf9d24.tar.gz otputils-0edded79e3027427ae6b7e2e3cb4235ab8bf9d24.tar.bz2 otputils-0edded79e3027427ae6b7e2e3cb4235ab8bf9d24.tar.xz otputils-0edded79e3027427ae6b7e2e3cb4235ab8bf9d24.zip | |
test-otp-decode: adding test
| -rw-r--r-- | Makefile.am | 10 | ||||
| -rw-r--r-- | tests/test-otp-decode.c | 265 |
2 files changed, 275 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index 5c1be5c..9284247 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -84,6 +84,7 @@ EXTRA_PROGRAMS = examples/totp-example \ | |||
| 84 | src/otputil \ | 84 | src/otputil \ |
| 85 | tests/test-dict-otp \ | 85 | tests/test-dict-otp \ |
| 86 | tests/test-hotp \ | 86 | tests/test-hotp \ |
| 87 | tests/test-otp-decode \ | ||
| 87 | tests/test-otp-encode \ | 88 | tests/test-otp-encode \ |
| 88 | tests/test-totp | 89 | tests/test-totp |
| 89 | EXTRA = include/otputil.h \ | 90 | EXTRA = include/otputil.h \ |
| @@ -152,6 +153,7 @@ if ENABLE_TESTS | |||
| 152 | TESTS += tests/test-otputil-hotp.sh | 153 | TESTS += tests/test-otputil-hotp.sh |
| 153 | TESTS += tests/test-otputil-totp.sh | 154 | TESTS += tests/test-otputil-totp.sh |
| 154 | TESTS += tests/test-hotp | 155 | TESTS += tests/test-hotp |
| 156 | TESTS += tests/test-otp-decode | ||
| 155 | TESTS += tests/test-otp-encode | 157 | TESTS += tests/test-otp-encode |
| 156 | TESTS += tests/test-totp | 158 | TESTS += tests/test-totp |
| 157 | endif | 159 | endif |
| @@ -217,6 +219,14 @@ tests_test_hotp_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | |||
| 217 | tests_test_hotp_SOURCES = tests/test-hotp.c | 219 | tests_test_hotp_SOURCES = tests/test-hotp.c |
| 218 | 220 | ||
| 219 | 221 | ||
| 222 | # macros for tests/test-otp-decode | ||
| 223 | tests_test_otp_decode_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | ||
| 224 | tests_test_otp_decode_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | ||
| 225 | tests_test_otp_decode_SOURCES = tests/test-otp-decode.c \ | ||
| 226 | tests/otp-data.c \ | ||
| 227 | tests/otp-data.h | ||
| 228 | |||
| 229 | |||
| 220 | # macros for tests/test-otp-encode | 230 | # macros for tests/test-otp-encode |
| 221 | tests_test_otp_encode_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | 231 | tests_test_otp_encode_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) |
| 222 | tests_test_otp_encode_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) | 232 | tests_test_otp_encode_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) |
diff --git a/tests/test-otp-decode.c b/tests/test-otp-decode.c new file mode 100644 index 0000000..af2cb18 --- /dev/null +++ b/tests/test-otp-decode.c | |||
| @@ -0,0 +1,265 @@ | |||
| 1 | /* | ||
| 2 | * OTP Utilities | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are | ||
| 7 | * met: | ||
| 8 | * | ||
| 9 | * * Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * * Redistributions in binary form must reproduce the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer in the | ||
| 13 | * documentation and/or other materials provided with the distribution. | ||
| 14 | * * Neither the name of David M. Syzdek nor the | ||
| 15 | * names of its contributors may be used to endorse or promote products | ||
| 16 | * derived from this software without specific prior written permission. | ||
| 17 | * | ||
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 19 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR | ||
| 22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 28 | * SUCH DAMAGE. | ||
| 29 | */ | ||
| 30 | /* | ||
| 31 | * @file tests/test-otp-decode.c | ||
| 32 | */ | ||
| 33 | #define _TESTS_TEST_OTP_DECODE_C 1 | ||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #ifdef HAVE_CONFIG_H | ||
| 43 | #include <config.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #include <errno.h> | ||
| 47 | #include <assert.h> | ||
| 48 | #include <getopt.h> | ||
| 49 | #include <stdio.h> | ||
| 50 | #include <string.h> | ||
| 51 | #include <strings.h> | ||
| 52 | #include <inttypes.h> | ||
| 53 | |||
| 54 | #include <otputil.h> | ||
| 55 | |||
| 56 | #include "otp-data.h" | ||
| 57 | |||
| 58 | |||
| 59 | /////////////////// | ||
| 60 | // // | ||
| 61 | // Definitions // | ||
| 62 | // // | ||
| 63 | /////////////////// | ||
| 64 | #pragma mark - Definitions | ||
| 65 | |||
| 66 | #ifndef PROGRAM_NAME | ||
| 67 | #define PROGRAM_NAME "test-otp-decode" | ||
| 68 | #endif | ||
| 69 | #ifndef PACKAGE_BUGREPORT | ||
| 70 | #define PACKAGE_BUGREPORT "david@syzdek.net" | ||
| 71 | #endif | ||
| 72 | #ifndef PACKAGE_COPYRIGHT | ||
| 73 | #define PACKAGE_COPYRIGHT "" | ||
| 74 | #endif | ||
| 75 | #ifndef PACKAGE_NAME | ||
| 76 | #define PACKAGE_NAME "" | ||
| 77 | #endif | ||
| 78 | #ifndef PACKAGE_VERSION | ||
| 79 | #define PACKAGE_VERSION "" | ||
| 80 | #endif | ||
| 81 | |||
| 82 | |||
| 83 | ///////////////// | ||
| 84 | // // | ||
| 85 | // Variables // | ||
| 86 | // // | ||
| 87 | ///////////////// | ||
| 88 | #pragma mark - Variables | ||
| 89 | |||
| 90 | static int verbose = 0; | ||
| 91 | static int quiet = 0; | ||
| 92 | static int ignore_errors = 0; | ||
| 93 | |||
| 94 | |||
| 95 | ////////////////// | ||
| 96 | // // | ||
| 97 | // Prototypes // | ||
| 98 | // // | ||
| 99 | ////////////////// | ||
| 100 | #pragma mark - Prototypes | ||
| 101 | |||
| 102 | int | ||
| 103 | main( | ||
| 104 | int argc, | ||
| 105 | char * argv[] ); | ||
| 106 | |||
| 107 | |||
| 108 | static void | ||
| 109 | my_bverror( | ||
| 110 | const char * desc, | ||
| 111 | const otputil_bv_t * good, | ||
| 112 | const otputil_bv_t * bad ); | ||
| 113 | |||
| 114 | |||
| 115 | ///////////////// | ||
| 116 | // // | ||
| 117 | // Functions // | ||
| 118 | // // | ||
| 119 | ///////////////// | ||
| 120 | #pragma mark - Functions | ||
| 121 | |||
| 122 | int | ||
| 123 | main( | ||
| 124 | int argc, | ||
| 125 | char * argv[] ) | ||
| 126 | { | ||
| 127 | int c; | ||
| 128 | int opt_index; | ||
| 129 | int pos; | ||
| 130 | otputil_bv_t * bv; | ||
| 131 | const otputil_bv_t * dat; | ||
| 132 | otptest_t * rec; | ||
| 133 | int errs; | ||
| 134 | |||
| 135 | // getopt options | ||
| 136 | static const char * short_opt = "chqVv"; | ||
| 137 | static struct option long_opt[] = | ||
| 138 | { | ||
| 139 | {"help", no_argument, NULL, 'h' }, | ||
| 140 | {"quiet", no_argument, NULL, 'q' }, | ||
| 141 | {"silent", no_argument, NULL, 'q' }, | ||
| 142 | {"version", no_argument, NULL, 'V' }, | ||
| 143 | {"verbose", no_argument, NULL, 'v' }, | ||
| 144 | { NULL, 0, NULL, 0 } | ||
| 145 | }; | ||
| 146 | |||
| 147 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 148 | { | ||
| 149 | switch(c) | ||
| 150 | { | ||
| 151 | case -1: /* no more arguments */ | ||
| 152 | case 0: /* long options toggles */ | ||
| 153 | break; | ||
| 154 | |||
| 155 | case 'c': | ||
| 156 | ignore_errors = 1; | ||
| 157 | break; | ||
| 158 | |||
| 159 | case 'h': | ||
| 160 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 161 | printf("OPTIONS:\n"); | ||
| 162 | printf(" -c report errors, but continue\n"); | ||
| 163 | printf(" -h, --help print this help and exit\n"); | ||
| 164 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 165 | printf(" -V, --version print version number and exit\n"); | ||
| 166 | printf(" -v, --verbose print verbose messages\n"); | ||
| 167 | printf("\n"); | ||
| 168 | return(0); | ||
| 169 | |||
| 170 | case 'q': | ||
| 171 | quiet++; | ||
| 172 | break; | ||
| 173 | |||
| 174 | case 'V': | ||
| 175 | printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION); | ||
| 176 | printf("Written by David M. Syzdek.\n"); | ||
| 177 | return(0); | ||
| 178 | |||
| 179 | case 'v': | ||
| 180 | verbose++; | ||
| 181 | break; | ||
| 182 | |||
| 183 | case '?': | ||
| 184 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 185 | return(1); | ||
| 186 | |||
| 187 | default: | ||
| 188 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 189 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 190 | return(1); | ||
| 191 | }; | ||
| 192 | }; | ||
| 193 | |||
| 194 | errs = 0; | ||
| 195 | |||
| 196 | if (!(quiet)) | ||
| 197 | { | ||
| 198 | printf("Hash Pass Phrase Seed Cnt Hex Six Word Format\n"); | ||
| 199 | printf("=========================================================================================\n"); | ||
| 200 | }; | ||
| 201 | for(pos = 0; ((otp_test_data[pos].hex)); pos++) | ||
| 202 | { | ||
| 203 | rec = &otp_test_data[pos]; | ||
| 204 | dat = &rec->dat; | ||
| 205 | |||
| 206 | if (!(quiet)) | ||
| 207 | { | ||
| 208 | printf("%-5s ", otputil_md2str(rec->method)); | ||
| 209 | printf("%-17s ", rec->pass); | ||
| 210 | printf("%-9s ", rec->seed); | ||
| 211 | printf("%2i ", rec->count); | ||
| 212 | printf("%-20s ", rec->hex); | ||
| 213 | printf("%s\n", rec->six); | ||
| 214 | }; | ||
| 215 | |||
| 216 | // test HEX decoding | ||
| 217 | if ((bv = otputil_otp_decode(rec->hex, NULL)) == NULL) | ||
| 218 | { | ||
| 219 | printf("%s: unable to decode\n", rec->hex); | ||
| 220 | if (!(ignore_errors)) | ||
| 221 | return(1); | ||
| 222 | errs++; | ||
| 223 | continue; | ||
| 224 | }; | ||
| 225 | if (otputil_bvcmp(&dat, &bv) != 0) | ||
| 226 | { | ||
| 227 | my_bverror("hex decoding", dat, bv); | ||
| 228 | if (!(ignore_errors)) | ||
| 229 | return(1); | ||
| 230 | errs++; | ||
| 231 | }; | ||
| 232 | }; | ||
| 233 | |||
| 234 | if (!(quiet)) | ||
| 235 | printf("%i errors encountered\n", errs); | ||
| 236 | |||
| 237 | return( ((errs)) ? 1 : 0 ); | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | void | ||
| 242 | my_bverror( | ||
| 243 | const char * desc, | ||
| 244 | const otputil_bv_t * good, | ||
| 245 | const otputil_bv_t * bad ) | ||
| 246 | { | ||
| 247 | int desc_len; | ||
| 248 | size_t pos; | ||
| 249 | uint8_t * vals; | ||
| 250 | |||
| 251 | desc_len = (int)strlen(desc) + 1; | ||
| 252 | |||
| 253 | printf("%s: expected \"", desc); | ||
| 254 | for(pos = 0, vals = good->bv_val; (pos < good->bv_len); pos++) | ||
| 255 | printf("%02x", vals[pos]); | ||
| 256 | printf("\" (%zu)\n%*s received \"", good->bv_len, desc_len, " "); | ||
| 257 | for(pos = 0, vals = bad->bv_val; (pos < bad->bv_len); pos++) | ||
| 258 | printf("%02x", vals[pos]); | ||
| 259 | printf("\" (%zu)\n", bad->bv_len); | ||
| 260 | |||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | /* end of source file */ | ||
