diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-15 14:38:37 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-15 14:38:37 -0800 |
| commit | 8bb457aa0c8e31d71580203e894789186574e675 (patch) | |
| tree | ae9dd0995ec128c9d09ae7ff7a6c22868d94152d /tests | |
| parent | 3b3c2ea53cd920ae0ea8a5c578eca0b4bb63cc31 (diff) | |
| download | tinyrad-8bb457aa0c8e31d71580203e894789186574e675.tar.gz tinyrad-8bb457aa0c8e31d71580203e894789186574e675.tar.bz2 tinyrad-8bb457aa0c8e31d71580203e894789186574e675.tar.xz tinyrad-8bb457aa0c8e31d71580203e894789186574e675.zip | |
tests: adding test-str-expand
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-str-expand.c | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/tests/test-str-expand.c b/tests/test-str-expand.c new file mode 100644 index 0000000..3a36ed2 --- /dev/null +++ b/tests/test-str-expand.c | |||
| @@ -0,0 +1,253 @@ | |||
| 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_STR_EXPAND_C 1 | ||
| 32 | |||
| 33 | |||
| 34 | /////////////// | ||
| 35 | // // | ||
| 36 | // Headers // | ||
| 37 | // // | ||
| 38 | /////////////// | ||
| 39 | #pragma mark - Headers | ||
| 40 | |||
| 41 | #include <tinyrad_utils.h> | ||
| 42 | |||
| 43 | #include <stdio.h> | ||
| 44 | #include <stdarg.h> | ||
| 45 | #include <stdlib.h> | ||
| 46 | #include <string.h> | ||
| 47 | #include <strings.h> | ||
| 48 | #include <unistd.h> | ||
| 49 | #include <getopt.h> | ||
| 50 | #include <assert.h> | ||
| 51 | |||
| 52 | #include <tinyrad.h> | ||
| 53 | |||
| 54 | |||
| 55 | /////////////////// | ||
| 56 | // // | ||
| 57 | // Definitions // | ||
| 58 | // // | ||
| 59 | /////////////////// | ||
| 60 | #pragma mark - Definitions | ||
| 61 | |||
| 62 | #undef PROGRAM_NAME | ||
| 63 | #define PROGRAM_NAME "tinyrad-str-expand" | ||
| 64 | |||
| 65 | |||
| 66 | ///////////////// | ||
| 67 | // // | ||
| 68 | // Datatypes // | ||
| 69 | // // | ||
| 70 | ///////////////// | ||
| 71 | #pragma mark - Datatypes | ||
| 72 | |||
| 73 | typedef struct my_test_strings | ||
| 74 | { | ||
| 75 | const char * origin; | ||
| 76 | const char * expand; | ||
| 77 | } MyTestStr; | ||
| 78 | |||
| 79 | |||
| 80 | ///////////////// | ||
| 81 | // // | ||
| 82 | // Variables // | ||
| 83 | // // | ||
| 84 | ///////////////// | ||
| 85 | #pragma mark - Variables | ||
| 86 | |||
| 87 | static const MyTestStr test_strings[] = | ||
| 88 | { | ||
| 89 | { "asdfghj", "asdfghj" }, | ||
| 90 | { "\\141\\163\\144\\146\\147\\150\\152", "asdfghj" }, | ||
| 91 | { "\\x61\\x73\\x64\\x66\\x67\\x68\\x6a", "asdfghj" }, | ||
| 92 | { "a\\163\\x64f\\147\\x68j", "asdfghj" }, | ||
| 93 | { "a%%sdfghj", "a%sdfghj" }, | ||
| 94 | { "%u:x:%U:%G:test user:%d:/bin/false", NULL }, | ||
| 95 | { "%g:x:%G:%u,root", NULL }, | ||
| 96 | { "Is %p running successfully?", NULL }, | ||
| 97 | { "Is your email address %u@%D\?", NULL }, | ||
| 98 | { "is your email address %u@%H\?", NULL }, | ||
| 99 | { "Is your email address %u@%h.%D\?", NULL }, | ||
| 100 | { NULL, NULL } | ||
| 101 | }; | ||
| 102 | |||
| 103 | |||
| 104 | ////////////////// | ||
| 105 | // // | ||
| 106 | // Prototypes // | ||
| 107 | // // | ||
| 108 | ////////////////// | ||
| 109 | #pragma mark - Prototypes | ||
| 110 | |||
| 111 | int | ||
| 112 | main( | ||
| 113 | int argc, | ||
| 114 | char * argv[] ); | ||
| 115 | |||
| 116 | |||
| 117 | ///////////////// | ||
| 118 | // // | ||
| 119 | // Functions // | ||
| 120 | // // | ||
| 121 | ///////////////// | ||
| 122 | #pragma mark - Functions | ||
| 123 | |||
| 124 | int | ||
| 125 | main( | ||
| 126 | int argc, | ||
| 127 | char * argv[] ) | ||
| 128 | { | ||
| 129 | int opt; | ||
| 130 | int c; | ||
| 131 | int rc; | ||
| 132 | int opt_index; | ||
| 133 | size_t pos; | ||
| 134 | unsigned opts; | ||
| 135 | char buff[256]; | ||
| 136 | |||
| 137 | // getopt options | ||
| 138 | static char short_opt[] = "dhVvq"; | ||
| 139 | static struct option long_opt[] = | ||
| 140 | { | ||
| 141 | {"debug", no_argument, NULL, 'd' }, | ||
| 142 | {"help", no_argument, NULL, 'h' }, | ||
| 143 | {"quiet", no_argument, NULL, 'q' }, | ||
| 144 | {"silent", no_argument, NULL, 'q' }, | ||
| 145 | {"version", no_argument, NULL, 'V' }, | ||
| 146 | {"verbose", no_argument, NULL, 'v' }, | ||
| 147 | { NULL, 0, NULL, 0 } | ||
| 148 | }; | ||
| 149 | |||
| 150 | trutils_initialize(PROGRAM_NAME); | ||
| 151 | |||
| 152 | opts = 0; | ||
| 153 | |||
| 154 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | ||
| 155 | { | ||
| 156 | switch(c) | ||
| 157 | { | ||
| 158 | case -1: /* no more arguments */ | ||
| 159 | case 0: /* long options toggles */ | ||
| 160 | break; | ||
| 161 | |||
| 162 | case 'd': | ||
| 163 | opt = TRAD_DEBUG_ANY; | ||
| 164 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | ||
| 165 | break; | ||
| 166 | |||
| 167 | case 'h': | ||
| 168 | printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME); | ||
| 169 | printf(" %s [OPTIONS] str1 [ str2 [ ... [ strN ] ] ]\n", PROGRAM_NAME); | ||
| 170 | printf("OPTIONS:\n"); | ||
| 171 | printf(" -d, --debug print debug messages\n"); | ||
| 172 | printf(" -h, --help print this help and exit\n"); | ||
| 173 | printf(" -q, --quiet, --silent do not print messages\n"); | ||
| 174 | printf(" -V, --version print version number and exit\n"); | ||
| 175 | printf(" -v, --verbose print verbose messages\n"); | ||
| 176 | printf("\n"); | ||
| 177 | return(0); | ||
| 178 | |||
| 179 | case 'q': | ||
| 180 | opts |= TRUTILS_OPT_QUIET; | ||
| 181 | opts &= ~TRUTILS_OPT_VERBOSE; | ||
| 182 | break; | ||
| 183 | |||
| 184 | case 'V': | ||
| 185 | trutils_version(); | ||
| 186 | return(0); | ||
| 187 | |||
| 188 | case 'v': | ||
| 189 | opts |= TRUTILS_OPT_VERBOSE; | ||
| 190 | opts &= ~TRUTILS_OPT_QUIET; | ||
| 191 | break; | ||
| 192 | |||
| 193 | case '?': | ||
| 194 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 195 | return(1); | ||
| 196 | |||
| 197 | default: | ||
| 198 | fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c); | ||
| 199 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 200 | return(1); | ||
| 201 | }; | ||
| 202 | }; | ||
| 203 | |||
| 204 | // expand custom strings | ||
| 205 | if (optind < argc) | ||
| 206 | { | ||
| 207 | while (optind < argc) | ||
| 208 | { | ||
| 209 | trutils_verbose(opts, "test string: %s", argv[optind]); | ||
| 210 | if ((rc = tinyrad_strexpand(buff, argv[optind], sizeof(buff))) != TRAD_SUCCESS) | ||
| 211 | { | ||
| 212 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); | ||
| 213 | return(1); | ||
| 214 | }; | ||
| 215 | buff[sizeof(buff)-1] = '\0'; | ||
| 216 | if (!(opts & TRUTILS_OPT_VERBOSE)) | ||
| 217 | trutils_print(opts, "%s", buff); | ||
| 218 | trutils_verbose(opts, " ==> %s", buff); | ||
| 219 | optind++; | ||
| 220 | if (optind < argc) | ||
| 221 | trutils_verbose(opts, ""); | ||
| 222 | }; | ||
| 223 | return(0); | ||
| 224 | }; | ||
| 225 | |||
| 226 | // expand and verify test strings | ||
| 227 | for(pos = 0; ((test_strings[pos].origin)); pos++) | ||
| 228 | { | ||
| 229 | trutils_verbose(opts, "test string: %s", test_strings[pos].origin); | ||
| 230 | if ((rc = tinyrad_strexpand(buff, test_strings[pos].origin, sizeof(buff))) != TRAD_SUCCESS) | ||
| 231 | { | ||
| 232 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); | ||
| 233 | return(1); | ||
| 234 | }; | ||
| 235 | buff[sizeof(buff)-1] = '\0'; | ||
| 236 | trutils_verbose(opts, " ==> %s", buff); | ||
| 237 | if ((test_strings[pos].expand)) | ||
| 238 | { | ||
| 239 | if ((strcmp(buff, test_strings[pos].expand))) | ||
| 240 | { | ||
| 241 | trutils_error(opts, NULL, "expected: %s", test_strings[pos].expand); | ||
| 242 | return(1); | ||
| 243 | }; | ||
| 244 | }; | ||
| 245 | trutils_verbose(opts, ""); | ||
| 246 | }; | ||
| 247 | |||
| 248 | return(0); | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | /* end of source */ | ||
| 253 | |||
