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