aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile.am10
-rw-r--r--tests/test-otp.c247
2 files changed, 257 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index ed10090..32bcbfc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -85,6 +85,7 @@ EXTRA_PROGRAMS = examples/totp-example \
85 tests/otp-data-convert \ 85 tests/otp-data-convert \
86 tests/test-dict-otp \ 86 tests/test-dict-otp \
87 tests/test-hotp \ 87 tests/test-hotp \
88 tests/test-otp \
88 tests/test-otp-decode \ 89 tests/test-otp-decode \
89 tests/test-otp-encode \ 90 tests/test-otp-encode \
90 tests/test-totp 91 tests/test-totp
@@ -159,6 +160,7 @@ if ENABLE_TESTS
159 TESTS += tests/test-otputil-hotp.sh 160 TESTS += tests/test-otputil-hotp.sh
160 TESTS += tests/test-otputil-totp.sh 161 TESTS += tests/test-otputil-totp.sh
161 TESTS += tests/test-hotp 162 TESTS += tests/test-hotp
163 TESTS += tests/test-otp
162 TESTS += tests/test-otp-decode 164 TESTS += tests/test-otp-decode
163 TESTS += tests/test-otp-encode 165 TESTS += tests/test-otp-encode
164 TESTS += tests/test-totp 166 TESTS += tests/test-totp
@@ -251,6 +253,14 @@ tests_test_hotp_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES)
251tests_test_hotp_SOURCES = tests/test-hotp.c 253tests_test_hotp_SOURCES = tests/test-hotp.c
252 254
253 255
256# macros for tests/test-otp
257tests_test_otp_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES)
258tests_test_otp_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES)
259tests_test_otp_SOURCES = tests/test-otp.c \
260 tests/otp-data.c \
261 tests/otp-data.h
262
263
254# macros for tests/test-otp-decode 264# macros for tests/test-otp-decode
255tests_test_otp_decode_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES) 265tests_test_otp_decode_DEPENDENCIES = Makefile $(noinst_LIBRARIES) $(lib_LTLIBRARIES)
256tests_test_otp_decode_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES) 266tests_test_otp_decode_LDADD = $(AM_LIBS) $(noinst_LIBRARIES) $(lib_LTLIBRARIES)
diff --git a/tests/test-otp.c b/tests/test-otp.c
new file mode 100644
index 0000000..f9f4f43
--- /dev/null
+++ b/tests/test-otp.c
@@ -0,0 +1,247 @@
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.c
32 */
33#define _TESTS_TEST_OTP_C 1
34
35///////////////
36// //
37// Headers //
38// //
39///////////////
40#pragma mark - Headers
41
42#include <otputil_compat.h>
43
44#ifdef HAVE_CONFIG_H
45# include <config.h>
46#endif
47
48#include <errno.h>
49#include <assert.h>
50#include <getopt.h>
51#include <stdio.h>
52#include <string.h>
53#include <strings.h>
54#include <inttypes.h>
55
56#include <otputil.h>
57
58#include "otp-data.h"
59
60
61///////////////////
62// //
63// Definitions //
64// //
65///////////////////
66#pragma mark - Definitions
67
68#ifndef PROGRAM_NAME
69#define PROGRAM_NAME "test-otp"
70#endif
71#ifndef PACKAGE_BUGREPORT
72#define PACKAGE_BUGREPORT "david@syzdek.net"
73#endif
74#ifndef PACKAGE_COPYRIGHT
75#define PACKAGE_COPYRIGHT ""
76#endif
77#ifndef PACKAGE_NAME
78#define PACKAGE_NAME ""
79#endif
80#ifndef PACKAGE_VERSION
81#define PACKAGE_VERSION ""
82#endif
83
84
85/////////////////
86// //
87// Variables //
88// //
89/////////////////
90#pragma mark - Variables
91
92static int verbose = 0;
93static int quiet = 0;
94static int ignore_errors = 0;
95
96
97//////////////////
98// //
99// Prototypes //
100// //
101//////////////////
102#pragma mark - Prototypes
103
104int
105main(
106 int argc,
107 char * argv[] );
108
109
110/////////////////
111// //
112// Functions //
113// //
114/////////////////
115#pragma mark - Functions
116
117int
118main(
119 int argc,
120 char * argv[] )
121{
122 int c;
123 int opt_index;
124 int pos;
125 const otputil_bv_t * dat;
126 otptest_t * rec;
127 int errs;
128 char * res;
129
130 // getopt options
131 static const char * short_opt = "chqVv";
132 static struct option long_opt[] =
133 {
134 {"help", no_argument, NULL, 'h' },
135 {"quiet", no_argument, NULL, 'q' },
136 {"silent", no_argument, NULL, 'q' },
137 {"version", no_argument, NULL, 'V' },
138 {"verbose", no_argument, NULL, 'v' },
139 { NULL, 0, NULL, 0 }
140 };
141
142 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
143 {
144 switch(c)
145 {
146 case -1: /* no more arguments */
147 case 0: /* long options toggles */
148 break;
149
150 case 'c':
151 ignore_errors = 1;
152 break;
153
154 case 'h':
155 printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME);
156 printf("OPTIONS:\n");
157 printf(" -c report errors, but continue\n");
158 printf(" -h, --help print this help and exit\n");
159 printf(" -q, --quiet, --silent do not print messages\n");
160 printf(" -V, --version print version number and exit\n");
161 printf(" -v, --verbose print verbose messages\n");
162 printf("\n");
163 return(0);
164
165 case 'q':
166 quiet++;
167 break;
168
169 case 'V':
170 printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
171 printf("Written by David M. Syzdek.\n");
172 return(0);
173
174 case 'v':
175 verbose++;
176 break;
177
178 case '?':
179 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
180 return(1);
181
182 default:
183 fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
184 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
185 return(1);
186 };
187 };
188
189 errs = 0;
190
191 if (!(quiet))
192 {
193 printf("Hash Pass Phrase Seed Cnt Hex Six Word Format\n");
194 printf("=========================================================================================\n");
195 };
196 for(pos = 0; ((otp_test_data[pos].hex)); pos++)
197 {
198 rec = &otp_test_data[pos];
199 dat = &rec->dat;
200
201 if ( (!(rec->pass)) || (!(rec->seed)) )
202 continue;
203 switch(rec->method)
204 {
205 case OTPUTIL_MD_MD4: break;
206 case OTPUTIL_MD_MD5: break;
207 case OTPUTIL_MD_SHA1: break;
208 default: continue;
209 };
210
211 if (!(quiet))
212 {
213 printf("%-5s ", otputil_md2str(rec->method));
214 printf("%-17s ", rec->pass);
215 printf("%-9s ", rec->seed);
216 printf("%2i ", rec->count);
217 printf("%-20s ", rec->hex);
218 printf("%s\n", rec->six);
219 };
220
221 if ((res = otputil_otp_str(rec->pass, rec->seed, rec->count, rec->method, NULL, 0)) == NULL)
222 {
223 printf("otputil_otp_str(): unknown error\n");
224 if (!(ignore_errors))
225 return(1);
226 errs++;
227 };
228 if ((res))
229 {
230 if ((strcasecmp(res, rec->six)))
231 {
232 printf("otputil_otp_str(): expected \"%s\"\n", rec->six);
233 printf(" received \"%s\"\n", res);
234 if (!(ignore_errors))
235 return(1);
236 errs++;
237 };
238 };
239 };
240
241 if (!(quiet))
242 printf("%i errors encountered\n", errs);
243
244 return( ((errs)) ? 1 : 0 );
245}
246
247/* end of source file */