xref: /freebsd/contrib/openpam/t/t_pam_env.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2019 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$
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 
40 #include <cryb/test.h>
41 
42 #include <security/pam_appl.h>
43 #include <security/openpam.h>
44 
45 #include "t_pam_err.h"
46 
47 #define T_ENV_NAME	"MAGIC_WORDS"
48 #define T_ENV_VALUE	"SQUEAMISH OSSIFRAGE"
49 #define T_ENV_NAMEVALUE	T_ENV_NAME "=" T_ENV_VALUE
50 
51 struct pam_conv t_null_pamc;
52 
53 
54 /***************************************************************************
55  * Tests
56  */
57 
58 static int
59 t_env_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
60 {
61 	pam_handle_t *pamh;
62 	char **envlist;
63 	int pam_err, ret;
64 
65 	ret = 1;
66 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
67 	t_assert(pam_err == PAM_SUCCESS);
68 	envlist = pam_getenvlist(pamh);
69 	ret &= t_is_not_null(envlist);
70 	if (envlist != NULL) {
71 		ret &= t_is_null(*envlist);
72 		openpam_free_envlist(envlist);
73 	}
74 	pam_end(pamh, pam_err);
75 	return (ret);
76 }
77 
78 static int
79 t_putenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
80 {
81 	pam_handle_t *pamh;
82 	char **envlist;
83 	int pam_err, ret;
84 
85 	ret = 1;
86 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
87 	t_assert(pam_err == PAM_SUCCESS);
88 	pam_err = pam_putenv(pamh, T_ENV_NAMEVALUE);
89 	ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
90 	envlist = pam_getenvlist(pamh);
91 	ret &= t_is_not_null(envlist);
92 	if (envlist != NULL) {
93 		ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
94 		    & t_is_null(envlist[1]);
95 		openpam_free_envlist(envlist);
96 	}
97 	pam_end(pamh, pam_err);
98 	return (ret);
99 }
100 
101 static int
102 t_setenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
103 {
104 	pam_handle_t *pamh;
105 	char **envlist;
106 	int pam_err, ret;
107 
108 	ret = 1;
109 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
110 	t_assert(pam_err == PAM_SUCCESS);
111 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
112 	ret &= t_compare_pam_err(PAM_SUCCESS, pam_err);
113 	envlist = pam_getenvlist(pamh);
114 	ret &= t_is_not_null(envlist);
115 	if (envlist != NULL) {
116 		ret &= t_compare_str(T_ENV_NAMEVALUE, envlist[0])
117 		    & t_is_null(envlist[1]);
118 		openpam_free_envlist(envlist);
119 	}
120 	pam_end(pamh, pam_err);
121 	return (ret);
122 }
123 
124 static int
125 t_getenv_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
126 {
127 	pam_handle_t *pamh;
128 	const char *value;
129 	int pam_err, ret;
130 
131 	ret = 1;
132 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
133 	t_assert(pam_err == PAM_SUCCESS);
134 	value = pam_getenv(pamh, T_ENV_NAME);
135 	ret &= t_compare_str(NULL, value);
136 	pam_end(pamh, pam_err);
137 	return (ret);
138 }
139 
140 static int
141 t_getenv_simple_miss(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
142 {
143 	pam_handle_t *pamh;
144 	const char *value;
145 	int pam_err, ret;
146 
147 	ret = 1;
148 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
149 	t_assert(pam_err == PAM_SUCCESS);
150 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
151 	t_assert(pam_err == PAM_SUCCESS);
152 	value = pam_getenv(pamh, "XYZZY");
153 	ret &= t_compare_str(NULL, value);
154 	pam_end(pamh, pam_err);
155 	return (ret);
156 }
157 
158 static int
159 t_getenv_simple_hit(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
160 {
161 	pam_handle_t *pamh;
162 	const char *value;
163 	int pam_err, ret;
164 
165 	ret = 1;
166 	pam_err = pam_start("t_pam_env", "test", &t_null_pamc, &pamh);
167 	t_assert(pam_err == PAM_SUCCESS);
168 	pam_err = pam_setenv(pamh, T_ENV_NAME, T_ENV_VALUE, 0);
169 	t_assert(pam_err == PAM_SUCCESS);
170 	value = pam_getenv(pamh, T_ENV_NAME);
171 	ret &= t_compare_str(T_ENV_VALUE, value);
172 	pam_end(pamh, pam_err);
173 	return (ret);
174 }
175 
176 
177 /***************************************************************************
178  * Boilerplate
179  */
180 
181 static int
182 t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
183 {
184 
185 	openpam_set_feature(OPENPAM_FALLBACK_TO_OTHER, 0);
186 
187 	t_add_test(t_env_empty, NULL, "initially empty");
188 	t_add_test(t_putenv_simple, NULL, "put - simple");
189 	t_add_test(t_setenv_simple, NULL, "set - simple");
190 	t_add_test(t_getenv_empty, NULL, "get - empty");
191 	t_add_test(t_getenv_simple_miss, NULL, "get - simple (miss)");
192 	t_add_test(t_getenv_simple_hit, NULL, "get - simple (hit)");
193 
194 	return (0);
195 }
196 
197 int
198 main(int argc, char *argv[])
199 {
200 
201 	t_main(t_prepare, NULL, argc, argv);
202 }
203