aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile.am9
-rw-r--r--tests/test-str-expand.c253
2 files changed, 262 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index 465b53d..0adcd03 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -98,6 +98,7 @@ EXTRA_PROGRAMS = examples/socket-example \
98 tests/test-oid-str \ 98 tests/test-oid-str \
99 tests/test-options \ 99 tests/test-options \
100 tests/test-pckt-byte-order \ 100 tests/test-pckt-byte-order \
101 tests/test-str-expand \
101 tests/test-url-desc2str \ 102 tests/test-url-desc2str \
102 tests/test-url-parse \ 103 tests/test-url-parse \
103 tests/test-url-resolve 104 tests/test-url-resolve
@@ -125,6 +126,7 @@ TESTS = tests/test-array-sorted \
125 tests/test-oid-str \ 126 tests/test-oid-str \
126 tests/test-options \ 127 tests/test-options \
127 tests/test-pckt-byte-order \ 128 tests/test-pckt-byte-order \
129 tests/test-str-expand \
128 tests/test-url-desc2str \ 130 tests/test-url-desc2str \
129 tests/test-url-parse \ 131 tests/test-url-parse \
130 tests/test-url-resolve 132 tests/test-url-resolve
@@ -390,6 +392,13 @@ tests_test_pckt_byte_order_SOURCES = $(noinst_HEADERS) $(include_HEADERS) \
390 tests/test-pckt-byte-order.c 392 tests/test-pckt-byte-order.c
391 393
392 394
395# macros for tests/test-str-expand
396tests_test_str_expand_DEPENDENCIES = $(lib_LTLIBRARIES) $(noinst_LIBRARIES)
397tests_test_str_expand_LDADD = $(lib_LTLIBRARIES) $(noinst_LIBRARIES)
398tests_test_str_expand_SOURCES = $(noinst_HEADERS) $(include_HEADERS) \
399 tests/test-str-expand.c
400
401
393# macros for tests/test-url-desc2str 402# macros for tests/test-url-desc2str
394tests_test_url_desc2str_DEPENDENCIES = $(lib_LTLIBRARIES) $(noinst_LIBRARIES) 403tests_test_url_desc2str_DEPENDENCIES = $(lib_LTLIBRARIES) $(noinst_LIBRARIES)
395tests_test_url_desc2str_LDADD = $(lib_LTLIBRARIES) $(noinst_LIBRARIES) 404tests_test_url_desc2str_LDADD = $(lib_LTLIBRARIES) $(noinst_LIBRARIES)
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
73typedef 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
87static 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
111int
112main(
113 int argc,
114 char * argv[] );
115
116
117/////////////////
118// //
119// Functions //
120// //
121/////////////////
122#pragma mark - Functions
123
124int
125main(
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