File: tst_coding.c

package info (click to toggle)
oath-toolkit 2.6.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,268 kB
  • sloc: ansic: 52,602; sh: 7,618; yacc: 1,846; xml: 1,841; makefile: 573
file content (344 lines) | stat: -rw-r--r-- 7,964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * tst_coding.c - self-tests for liboath data coding functions
 * Copyright (C) 2009-2026 Simon Josefsson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <https://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "oath.h"

#include <stdio.h>
#include <stdlib.h>

int
main (void)
{
  oath_rc rc;
  const char *hexsecret = "ABCDEF3435363738393031323334353637abcdef";
  char secret[100];
  size_t secretlen;
  char *tmp;
  size_t len;

  /* Hex decoding. */

  secretlen = 0;
  rc = oath_hex2bin (hexsecret, secret, &secretlen);
  if (rc != OATH_TOO_SMALL_BUFFER)
    {
      printf ("oath_hex2bin too small: %d\n", rc);
      return 1;
    }
  if (secretlen != 20)
    {
      printf ("oath_hex2bin too small: 20 != %lu\n",
	      (long unsigned) secretlen);
      return 1;
    }

  rc = oath_hex2bin ("abcd", secret, &secretlen);
  if (rc != OATH_OK)
    {
      printf ("oath_hex2bin lower case failed: %d\n", rc);
      return 1;
    }

  rc = oath_hex2bin ("ABCD", secret, &secretlen);
  if (rc != OATH_OK)
    {
      printf ("oath_hex2bin upper case failed: %d\n", rc);
      return 1;
    }

  rc = oath_hex2bin ("ABC", secret, &secretlen);
  if (rc != OATH_INVALID_HEX)
    {
      printf ("oath_hex2bin too small failed: %d\n", rc);
      return 1;
    }

  rc = oath_hex2bin ("JUNK", secret, &secretlen);
  if (rc != OATH_INVALID_HEX)
    {
      printf ("oath_hex2bin junk failed: %d\n", rc);
      return 1;
    }

  secretlen = sizeof (secret);
  rc = oath_hex2bin (hexsecret, secret, &secretlen);
  if (rc != OATH_OK)
    {
      printf ("oath_hex2bin: %d\n", rc);
      return 1;
    }
  if (secretlen != 20)
    {
      printf ("oath_hex2bin: 20 != %lu\n", (long unsigned) secretlen);
      return 1;
    }
  if (memcmp (secret, "\xAB\xCD\xEF\x34\x35\x36\x37\x38\x39\x30"
	      "\x31\x32\x33\x34\x35\x36\x37\xab\xcd\xef", 20) != 0)
    {
      printf ("oath_hex2bin: decode mismatch\n");
      return 1;
    }

  /* Hex decoding */

  oath_bin2hex (NULL, 0, secret);
  oath_bin2hex ("", 0, secret);

  oath_bin2hex ("x", 1, secret);
  if (strcmp (secret, "78") != 0)
    {
      printf ("oath_bin2hex: decode mismatch\n");
      return 1;
    }
  oath_bin2hex ("xx", 2, secret);
  if (strcmp (secret, "7878") != 0)
    {
      printf ("oath_bin2hex: decode mismatch\n");
      return 1;
    }

  /* Base32 encoding. */

  rc = oath_base32_encode (NULL, 0, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_encode ("", 0, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_encode ("", 1, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_encode ("foo", 3, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_encode ("foo", 3, NULL, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  if (len != strlen ("MZXW6==="))
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_encode ("foo", 3, &tmp, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  if (strcmp (tmp, "MZXW6===") != 0)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  free (tmp);

  len = 42;
  rc = oath_base32_encode ("foo", 3, &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  if (len != strlen ("MZXW6===") || strcmp (tmp, "MZXW6===") != 0)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  free (tmp);

  len = 42;
  rc = oath_base32_encode ("foobar", 6, &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  if (len != strlen ("MZXW6YTBOI======")
      || strcmp (tmp, "MZXW6YTBOI======") != 0)
    {
      printf ("oath_base32_encode: %d\n", rc);
      return 1;
    }
  free (tmp);

  /* Base32 decoding. */

  rc = oath_base32_decode (NULL, 0, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_decode ("", 0, NULL, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_decode ("", 1, NULL, NULL);
  if (rc != OATH_INVALID_BASE32)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_decode ("NIXnix", 6, &tmp, &len);
  if (rc != OATH_INVALID_BASE32)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }

  rc = oath_base32_decode ("MZXW6===", 8, NULL, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != strlen ("foo"))
    {
      printf ("oath_base32_decode length mismatch: %lu\n",
	      (long unsigned) len);
      return 1;
    }

  rc = oath_base32_decode ("mzxw6===", 8, NULL, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != strlen ("foo"))
    {
      printf ("oath_base32_decode length mismatch: %lu\n",
	      (long unsigned) len);
      return 1;
    }

  rc = oath_base32_decode ("MZXW6===", 8, &tmp, NULL);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (memcmp (tmp, "foo", 3) != 0)
    {
      printf ("oath_base32_decode strcmp failure: %s\n", tmp);
      return 1;
    }
  free (tmp);

  rc = oath_base32_decode ("MZXW6===", 8, &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != 3 || memcmp (tmp, "foo", len) != 0)
    {
      printf ("oath_base32_decode failure: %lu/%s\n",
	      (long unsigned) len, tmp);
      return 1;
    }
  free (tmp);

#define FOOBAR "MZXW6YTBOI======"
  rc = oath_base32_decode (FOOBAR, strlen (FOOBAR), &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != 6 || memcmp (tmp, "foobar", len) != 0)
    {
      printf ("oath_base32_decode failure: %lu/%s\n",
	      (long unsigned) len, tmp);
      return 1;
    }
  free (tmp);

  rc = oath_base32_decode ("MZ XW 6===", 10, &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != 3 || memcmp (tmp, "foo", len) != 0)
    {
      printf ("oath_base32_decode failure: %lu/%s\n",
	      (long unsigned) len, tmp);
      return 1;
    }
  free (tmp);

  rc = oath_base32_decode ("MZ XW 6", 7, &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != 3 || memcmp (tmp, "foo", len) != 0)
    {
      printf ("oath_base32_decode failure: %lu/%s\n",
	      (long unsigned) len, tmp);
      return 1;
    }
  free (tmp);

#define DROPBOX "gr6d 5br7 25s6 vnck v4vl hlao re"
  rc = oath_base32_decode (DROPBOX, strlen (DROPBOX), &tmp, &len);
  if (rc != OATH_OK)
    {
      printf ("oath_base32_decode: %d\n", rc);
      return 1;
    }
  if (len != 16)
    {
      printf ("oath_base32_decode failure: %lu\n", (unsigned long) len);
      return 1;
    }
  free (tmp);

  return 0;
}