1 /*-
2 * Copyright (c) 2014-2017 Dag-Erling Smørgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <cryb/test.h>
39
40 #include "openpam_ctype.h"
41
42 #define OC_DIGIT "0123456789"
43 #define OC_XDIGIT OC_DIGIT "ABCDEFabcdef"
44 #define OC_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 #define OC_LOWER "abcdefghijklmnopqrstuvwxyz"
46 #define OC_LETTER OC_UPPER OC_LOWER
47 #define OC_LWS " \t\f\r"
48 #define OC_WS OC_LWS "\n"
49 #define OC_P "!\"#$%&'()*+,-./" OC_DIGIT ":;<=>?@" OC_UPPER "[\\]^_`" OC_LOWER "{|}~"
50 #define OC_PFCS OC_DIGIT OC_LETTER "._-"
51
52 static const char oc_digit[] = OC_DIGIT;
53 static const char oc_xdigit[] = OC_XDIGIT;
54 static const char oc_upper[] = OC_UPPER;
55 static const char oc_lower[] = OC_LOWER;
56 static const char oc_letter[] = OC_LETTER;
57 static const char oc_lws[] = OC_LWS;
58 static const char oc_ws[] = OC_WS;
59 static const char oc_p[] = OC_P;
60 static const char oc_pfcs[] = OC_PFCS;
61
62 #define T_OC(set) \
63 static int \
64 t_oc_##set(char **desc, void *arg) \
65 { \
66 char crib[256]; \
67 unsigned int i, ret; \
68 \
69 (void)desc; \
70 (void)arg; \
71 memset(crib, 0, sizeof crib); \
72 for (i = 0; oc_##set[i]; ++i) \
73 crib[(int)oc_##set[i]] = 1; \
74 for (i = ret = 0; i < sizeof crib; ++i) { \
75 if (is_##set(i) != crib[i]) { \
76 t_printv("is_%s() incorrect " \
77 "for %#02x\n", #set, i); \
78 ++ret; \
79 } \
80 } \
81 return (ret == 0); \
82 }
83 #define T_OC_ADD(set) t_add_test(&t_oc_##set, NULL, "is_"#set)
84
85 T_OC(digit)
T_OC(xdigit)86 T_OC(xdigit)
87 T_OC(upper)
88 T_OC(lower)
89 T_OC(letter)
90 T_OC(lws)
91 T_OC(ws)
92 T_OC(p)
93 T_OC(pfcs)
94
95
96 /***************************************************************************
97 * Boilerplate
98 */
99
100 static int
101 t_prepare(int argc, char *argv[])
102 {
103
104 (void)argc;
105 (void)argv;
106 T_OC_ADD(digit);
107 T_OC_ADD(xdigit);
108 T_OC_ADD(upper);
109 T_OC_ADD(lower);
110 T_OC_ADD(letter);
111 T_OC_ADD(lws);
112 T_OC_ADD(ws);
113 T_OC_ADD(p);
114 T_OC_ADD(pfcs);
115 return (0);
116 }
117
118 int
main(int argc,char * argv[])119 main(int argc, char *argv[])
120 {
121
122 t_main(t_prepare, NULL, argc, argv);
123 }
124