xref: /freebsd/contrib/openpam/t/t_openpam_subst.c (revision abd87254)
1 /*-
2  * Copyright (c) 2023 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 <err.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <cryb/test.h>
42 
43 #include <security/pam_appl.h>
44 #include <security/openpam.h>
45 
46 #include "openpam_impl.h"
47 
48 #define T_FUNC(n, d)							\
49 	static const char *t_ ## n ## _desc = d;			\
50 	static int t_ ## n ## _func(OPENPAM_UNUSED(char **desc),	\
51 	    OPENPAM_UNUSED(void *arg))
52 
53 #define T(n)								\
54 	t_add_test(&t_ ## n ## _func, NULL, "%s", t_ ## n ## _desc)
55 
56 const char *pam_return_so;
57 
58 T_FUNC(final_percent, "template ends with %")
59 {
60 	char template[] = "test%\0deadbeef";
61 	char buf[] = "Squeamish Ossifrage";
62 	size_t bufsize = sizeof(buf);
63 	int pam_err, ret;
64 
65 	pam_err = openpam_subst(NULL, buf, &bufsize, template);
66 	ret = (pam_err == PAM_SUCCESS);
67 	ret &= t_compare_sz(sizeof("test%"), bufsize);
68 	ret &= t_compare_str("test%", buf);
69 	return (ret);
70 }
71 
72 
73 /***************************************************************************
74  * Boilerplate
75  */
76 
77 static int
78 t_prepare(int argc, char *argv[])
79 {
80 
81 	(void)argc;
82 	(void)argv;
83 
84 	if ((pam_return_so = getenv("PAM_RETURN_SO")) == NULL) {
85 		t_printv("define PAM_RETURN_SO before running these tests\n");
86 		return (0);
87 	}
88 
89 	openpam_set_feature(OPENPAM_RESTRICT_MODULE_NAME, 0);
90 	openpam_set_feature(OPENPAM_VERIFY_MODULE_FILE, 0);
91 	openpam_set_feature(OPENPAM_RESTRICT_SERVICE_NAME, 0);
92 	openpam_set_feature(OPENPAM_VERIFY_POLICY_FILE, 0);
93 	openpam_set_feature(OPENPAM_FALLBACK_TO_OTHER, 0);
94 
95 	T(final_percent);
96 
97 	return (0);
98 }
99 
100 int
101 main(int argc, char *argv[])
102 {
103 
104 	t_main(t_prepare, NULL, argc, argv);
105 }
106