t_openpam_straddch.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 2021 Dag-Erling Smrgrav
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that the following conditions
7 1.1 christos * are met:
8 1.1 christos * 1. Redistributions of source code must retain the above copyright
9 1.1 christos * notice, this list of conditions and the following disclaimer.
10 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer in the
12 1.1 christos * documentation and/or other materials provided with the distribution.
13 1.1 christos * 3. The name of the author may not be used to endorse or promote
14 1.1 christos * products derived from this software without specific prior written
15 1.1 christos * permission.
16 1.1 christos *
17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 christos * SUCH DAMAGE.
28 1.1 christos */
29 1.1 christos
30 1.1 christos #ifdef HAVE_CONFIG_H
31 1.1 christos # include "config.h"
32 1.1 christos #endif
33 1.1 christos
34 1.1 christos #include <sys/types.h>
35 1.1 christos
36 1.1 christos #include <errno.h>
37 1.1 christos #include <stdint.h>
38 1.1 christos #include <stdlib.h>
39 1.1 christos
40 1.1 christos #include <cryb/test.h>
41 1.1 christos
42 1.1 christos #include <security/pam_appl.h>
43 1.1 christos #include "openpam_impl.h"
44 1.1 christos
45 1.1 christos static int
46 1.1 christos t_straddch_empty(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
47 1.1 christos {
48 1.1 christos char *str;
49 1.1 christos size_t size, len;
50 1.1 christos int ret;
51 1.1 christos
52 1.1 christos str = NULL;
53 1.1 christos size = len = SIZE_MAX;
54 1.1 christos ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0'));
55 1.1 christos ret &= t_is_not_null(str);
56 1.1 christos ret &= t_is_not_zero_sz(size);
57 1.1 christos ret &= t_is_zero_sz(len);
58 1.1 christos free(str);
59 1.1 christos return ret;
60 1.1 christos }
61 1.1 christos
62 1.1 christos static int
63 1.1 christos t_straddch_alloc_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
64 1.1 christos {
65 1.1 christos char *str;
66 1.1 christos size_t size, len;
67 1.1 christos int ret;
68 1.1 christos
69 1.1 christos str = NULL;
70 1.1 christos size = len = SIZE_MAX;
71 1.1 christos errno = 0;
72 1.1 christos t_malloc_fail = 1;
73 1.1 christos ret = t_compare_i(-1, openpam_straddch(&str, &size, &len, '\0'));
74 1.1 christos t_malloc_fail = 0;
75 1.1 christos ret &= t_compare_i(ENOMEM, errno);
76 1.1 christos ret &= t_is_null(str);
77 1.1 christos ret &= t_compare_sz(SIZE_MAX, size);
78 1.1 christos ret &= t_compare_sz(SIZE_MAX, len);
79 1.1 christos free(str);
80 1.1 christos return ret;
81 1.1 christos }
82 1.1 christos
83 1.1 christos static int
84 1.1 christos t_straddch_realloc_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
85 1.1 christos {
86 1.1 christos char *str, *_str;
87 1.1 christos size_t size, _size, len, _len;
88 1.1 christos int i, ret;
89 1.1 christos
90 1.1 christos // start with an empty string
91 1.1 christos str = NULL;
92 1.1 christos size = len = SIZE_MAX;
93 1.1 christos ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0'));
94 1.1 christos ret &= t_is_not_null(str);
95 1.1 christos ret &= t_is_not_zero_sz(size);
96 1.1 christos ret &= t_is_zero_sz(len);
97 1.1 christos if (!ret)
98 1.1 christos goto end;
99 1.1 christos // repeatedly append to it until allocation fails
100 1.1 christos errno = 0;
101 1.1 christos _str = str;
102 1.1 christos _size = size;
103 1.1 christos _len = len;
104 1.1 christos t_malloc_fail = 1;
105 1.1 christos for (i = 0; i < 4096; i++) {
106 1.1 christos if ((ret = openpam_straddch(&str, &size, &len, 'x')) != 0)
107 1.1 christos break;
108 1.1 christos _size = size;
109 1.1 christos _len = len;
110 1.1 christos }
111 1.1 christos t_malloc_fail = 0;
112 1.1 christos ret = t_compare_i(-1, ret);
113 1.1 christos ret &= t_compare_i(ENOMEM, errno);
114 1.1 christos ret &= t_compare_ptr(_str, str);
115 1.1 christos ret &= t_compare_sz(_size, size);
116 1.1 christos ret &= t_compare_sz(_len, len);
117 1.1 christos end:
118 1.1 christos free(str);
119 1.1 christos return ret;
120 1.1 christos }
121 1.1 christos
122 1.1 christos static int
123 1.1 christos t_straddch_realloc_ok(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
124 1.1 christos {
125 1.1 christos char *str;
126 1.1 christos size_t size, _size, len, _len;
127 1.1 christos int i, ret;
128 1.1 christos
129 1.1 christos // start with an empty string
130 1.1 christos str = NULL;
131 1.1 christos size = len = SIZE_MAX;
132 1.1 christos ret = t_is_zero_i(openpam_straddch(&str, &size, &len, '\0'));
133 1.1 christos ret &= t_is_not_null(str);
134 1.1 christos ret &= t_is_not_zero_sz(size);
135 1.1 christos ret &= t_is_zero_sz(len);
136 1.1 christos if (!ret)
137 1.1 christos goto end;
138 1.1 christos // repeatedly append to it until size changes
139 1.1 christos _size = size;
140 1.1 christos _len = len;
141 1.1 christos for (i = ' '; i <= '~'; i++) { // assume ascii
142 1.1 christos if ((ret = openpam_straddch(&str, &size, &len, i)) != 0)
143 1.1 christos break;
144 1.1 christos if (size != _size)
145 1.1 christos break;
146 1.1 christos if (len != _len + 1)
147 1.1 christos break;
148 1.1 christos _len = len;
149 1.1 christos }
150 1.1 christos ret = t_is_zero_i(ret);
151 1.1 christos if (!ret)
152 1.1 christos goto end;
153 1.1 christos ret &= t_compare_sz(_len + 1, len);
154 1.1 christos ret &= t_compare_sz(_size * 2, size);
155 1.1 christos ret &= t_compare_i(i, str[_len]);
156 1.1 christos ret &= t_is_zero_i(str[len]);
157 1.1 christos end:
158 1.1 christos free(str);
159 1.1 christos return ret;
160 1.1 christos }
161 1.1 christos
162 1.1 christos
163 1.1 christos /***************************************************************************
165 1.1 christos * Boilerplate
166 1.1 christos */
167 1.1 christos
168 1.1 christos static int
169 1.1 christos t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
170 1.1 christos {
171 1.1 christos
172 1.1 christos t_add_test(t_straddch_empty, NULL, "empty string");
173 1.1 christos t_add_test(t_straddch_alloc_fail, NULL, "allocation failure");
174 1.1 christos t_add_test(t_straddch_realloc_fail, NULL, "reallocation failure");
175 1.1 christos t_add_test(t_straddch_realloc_ok, NULL, "reallocation success");
176 1.1 christos return (0);
177 1.1 christos }
178 1.1 christos
179 1.1 christos int
180 1.1 christos main(int argc, char *argv[])
181 1.1 christos {
182 1.1 christos
183 1.1 christos t_main(t_prepare, NULL, argc, argv);
184 }
185