2 * Copyright (c) 2019 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
40 #include <cryb/test.h>
42 #include <security/pam_appl.h>
43 #include <security/openpam.h>
45 #include "t_pam_err.h"
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
51 struct pam_conv t_null_pamc;
54 /***************************************************************************
59 t_env_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
74 pam_end(pamh, pam_err);
79 t_putenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
97 pam_end(pamh, pam_err);
102 t_setenv_simple(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
120 pam_end(pamh, pam_err);
125 t_getenv_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
141 t_getenv_simple_miss(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
159 t_getenv_simple_hit(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
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);
177 /***************************************************************************
182 t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
185 openpam_set_feature(OPENPAM_FALLBACK_TO_OTHER, 0);
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)");
198 main(int argc, char *argv[])
201 t_main(t_prepare, NULL, argc, argv);