xref: /netbsd-src/external/bsd/openpam/dist/t/t_openpam_subst.c (revision 024a2e8c049a7132c2274c1f5271ae54063f3c4d)
1*024a2e8cSchristos /*-
2*024a2e8cSchristos  * Copyright (c) 2023 Dag-Erling Smørgrav
3*024a2e8cSchristos  * All rights reserved.
4*024a2e8cSchristos  *
5*024a2e8cSchristos  * Redistribution and use in source and binary forms, with or without
6*024a2e8cSchristos  * modification, are permitted provided that the following conditions
7*024a2e8cSchristos  * are met:
8*024a2e8cSchristos  * 1. Redistributions of source code must retain the above copyright
9*024a2e8cSchristos  *    notice, this list of conditions and the following disclaimer.
10*024a2e8cSchristos  * 2. Redistributions in binary form must reproduce the above copyright
11*024a2e8cSchristos  *    notice, this list of conditions and the following disclaimer in the
12*024a2e8cSchristos  *    documentation and/or other materials provided with the distribution.
13*024a2e8cSchristos  * 3. The name of the author may not be used to endorse or promote
14*024a2e8cSchristos  *    products derived from this software without specific prior written
15*024a2e8cSchristos  *    permission.
16*024a2e8cSchristos  *
17*024a2e8cSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*024a2e8cSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*024a2e8cSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*024a2e8cSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*024a2e8cSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*024a2e8cSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*024a2e8cSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*024a2e8cSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*024a2e8cSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*024a2e8cSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*024a2e8cSchristos  * SUCH DAMAGE.
28*024a2e8cSchristos  */
29*024a2e8cSchristos 
30*024a2e8cSchristos #ifdef HAVE_CONFIG_H
31*024a2e8cSchristos # include "config.h"
32*024a2e8cSchristos #endif
33*024a2e8cSchristos 
34*024a2e8cSchristos #include <err.h>
35*024a2e8cSchristos #include <stdint.h>
36*024a2e8cSchristos #include <stdio.h>
37*024a2e8cSchristos #include <stdlib.h>
38*024a2e8cSchristos #include <string.h>
39*024a2e8cSchristos #include <unistd.h>
40*024a2e8cSchristos 
41*024a2e8cSchristos #include <cryb/test.h>
42*024a2e8cSchristos 
43*024a2e8cSchristos #include <security/pam_appl.h>
44*024a2e8cSchristos #include <security/openpam.h>
45*024a2e8cSchristos 
46*024a2e8cSchristos #include "openpam_impl.h"
47*024a2e8cSchristos 
48*024a2e8cSchristos #define T_FUNC(n, d)							\
49*024a2e8cSchristos 	static const char *t_ ## n ## _desc = d;			\
50*024a2e8cSchristos 	static int t_ ## n ## _func(OPENPAM_UNUSED(char **desc),	\
51*024a2e8cSchristos 	    OPENPAM_UNUSED(void *arg))
52*024a2e8cSchristos 
53*024a2e8cSchristos #define T(n)								\
54*024a2e8cSchristos 	t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc)
55*024a2e8cSchristos 
56*024a2e8cSchristos const char *pam_return_so;
57*024a2e8cSchristos 
58*024a2e8cSchristos T_FUNC(final_percent, "template ends with %")
59*024a2e8cSchristos {
60*024a2e8cSchristos 	char template[] = "test%\0deadbeef";
61*024a2e8cSchristos 	char buf[] = "Squeamish Ossifrage";
62*024a2e8cSchristos 	size_t bufsize = sizeof(buf);
63*024a2e8cSchristos 	int pam_err, ret;
64*024a2e8cSchristos 
65*024a2e8cSchristos 	pam_err = openpam_subst(NULL, buf, &bufsize, template);
66*024a2e8cSchristos 	ret = (pam_err == PAM_SUCCESS);
67*024a2e8cSchristos 	ret &= t_compare_sz(sizeof("test%"), bufsize);
68*024a2e8cSchristos 	ret &= t_compare_str("test%", buf);
69*024a2e8cSchristos 	return (ret);
70*024a2e8cSchristos }
71*024a2e8cSchristos 
72*024a2e8cSchristos 
73*024a2e8cSchristos /***************************************************************************
74*024a2e8cSchristos  * Boilerplate
75*024a2e8cSchristos  */
76*024a2e8cSchristos 
77*024a2e8cSchristos static int
t_prepare(int argc,char * argv[])78*024a2e8cSchristos t_prepare(int argc, char *argv[])
79*024a2e8cSchristos {
80*024a2e8cSchristos 
81*024a2e8cSchristos 	(void)argc;
82*024a2e8cSchristos 	(void)argv;
83*024a2e8cSchristos 
84*024a2e8cSchristos 	if ((pam_return_so = getenv("PAM_RETURN_SO")) == NULL) {
85*024a2e8cSchristos 		t_printv("define PAM_RETURN_SO before running these tests\n");
86*024a2e8cSchristos 		return (0);
87*024a2e8cSchristos 	}
88*024a2e8cSchristos 
89*024a2e8cSchristos 	openpam_set_feature(OPENPAM_RESTRICT_MODULE_NAME, 0);
90*024a2e8cSchristos 	openpam_set_feature(OPENPAM_VERIFY_MODULE_FILE, 0);
91*024a2e8cSchristos 	openpam_set_feature(OPENPAM_RESTRICT_SERVICE_NAME, 0);
92*024a2e8cSchristos 	openpam_set_feature(OPENPAM_VERIFY_POLICY_FILE, 0);
93*024a2e8cSchristos 	openpam_set_feature(OPENPAM_FALLBACK_TO_OTHER, 0);
94*024a2e8cSchristos 
95*024a2e8cSchristos 	T(final_percent);
96*024a2e8cSchristos 
97*024a2e8cSchristos 	return (0);
98*024a2e8cSchristos }
99*024a2e8cSchristos 
100*024a2e8cSchristos int
main(int argc,char * argv[])101*024a2e8cSchristos main(int argc, char *argv[])
102*024a2e8cSchristos {
103*024a2e8cSchristos 
104*024a2e8cSchristos 	t_main(t_prepare, NULL, argc, argv);
105*024a2e8cSchristos }
106