xref: /freebsd/contrib/openpam/t/t_openpam_ctype.c (revision 42249ef2)
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  * $OpenPAM: t_openpam_ctype.c 938 2017-04-30 21:34:42Z des $
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include <cryb/test.h>
41 
42 #include "openpam_ctype.h"
43 
44 #define OC_DIGIT	"0123456789"
45 #define OC_XDIGIT	OC_DIGIT "ABCDEFabcdef"
46 #define OC_UPPER	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 #define OC_LOWER	"abcdefghijklmnopqrstuvwxyz"
48 #define OC_LETTER	OC_UPPER OC_LOWER
49 #define OC_LWS		" \t\f\r"
50 #define OC_WS		OC_LWS "\n"
51 #define OC_P		"!\"#$%&'()*+,-./" OC_DIGIT ":;<=>?@" OC_UPPER "[\\]^_`" OC_LOWER "{|}~"
52 #define OC_PFCS		OC_DIGIT OC_LETTER "._-"
53 
54 static const char oc_digit[] = OC_DIGIT;
55 static const char oc_xdigit[] = OC_XDIGIT;
56 static const char oc_upper[] = OC_UPPER;
57 static const char oc_lower[] = OC_LOWER;
58 static const char oc_letter[] = OC_LETTER;
59 static const char oc_lws[] = OC_LWS;
60 static const char oc_ws[] = OC_WS;
61 static const char oc_p[] = OC_P;
62 static const char oc_pfcs[] = OC_PFCS;
63 
64 #define T_OC(set)							\
65 	static int							\
66 	t_oc_##set(char **desc, void *arg)				\
67 	{								\
68 		char crib[256];						\
69 		unsigned int i, ret;					\
70 									\
71 		(void)desc;						\
72 		(void)arg;						\
73 		memset(crib, 0, sizeof crib);				\
74 		for (i = 0; oc_##set[i]; ++i)				\
75 			crib[(int)oc_##set[i]] = 1;			\
76 		for (i = ret = 0; i < sizeof crib; ++i) {		\
77 			if (is_##set(i) != crib[i]) {			\
78 				t_printv("is_%s() incorrect "		\
79 				    "for %#02x\n", #set, i);		\
80 				++ret;					\
81 			}						\
82 		}							\
83 		return (ret == 0);					\
84 	}
85 #define T_OC_ADD(set) t_add_test(&t_oc_##set, NULL, "is_"#set)
86 
87 T_OC(digit)
88 T_OC(xdigit)
89 T_OC(upper)
90 T_OC(lower)
91 T_OC(letter)
92 T_OC(lws)
93 T_OC(ws)
94 T_OC(p)
95 T_OC(pfcs)
96 
97 
98 /***************************************************************************
99  * Boilerplate
100  */
101 
102 static int
103 t_prepare(int argc, char *argv[])
104 {
105 
106 	(void)argc;
107 	(void)argv;
108 	T_OC_ADD(digit);
109 	T_OC_ADD(xdigit);
110 	T_OC_ADD(upper);
111 	T_OC_ADD(lower);
112 	T_OC_ADD(letter);
113 	T_OC_ADD(lws);
114 	T_OC_ADD(ws);
115 	T_OC_ADD(p);
116 	T_OC_ADD(pfcs);
117 	return (0);
118 }
119 
120 int
121 main(int argc, char *argv[])
122 {
123 
124 	t_main(t_prepare, NULL, argc, argv);
125 }
126