1 /*
2  * Tools info_handle type test program
3  *
4  * Copyright (C) 2010-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <file_stream.h>
24 #include <memory.h>
25 #include <types.h>
26 
27 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
28 #include <stdlib.h>
29 #endif
30 
31 #include "fsext_test_libcerror.h"
32 #include "fsext_test_macros.h"
33 #include "fsext_test_memory.h"
34 #include "fsext_test_unused.h"
35 
36 #include "../fsexttools/info_handle.h"
37 
38 /* Tests the info_handle_initialize function
39  * Returns 1 if successful or 0 if not
40  */
fsext_test_tools_info_handle_initialize(void)41 int fsext_test_tools_info_handle_initialize(
42      void )
43 {
44 	info_handle_t *info_handle      = NULL;
45 	libcerror_error_t *error        = NULL;
46 	int result                      = 0;
47 
48 #if defined( HAVE_FSEXT_TEST_MEMORY )
49 	int number_of_malloc_fail_tests = 1;
50 	int number_of_memset_fail_tests = 1;
51 	int test_number                 = 0;
52 #endif
53 
54 	/* Test regular cases
55 	 */
56 	result = info_handle_initialize(
57 	          &info_handle,
58 	          0,
59 	          &error );
60 
61 	FSEXT_TEST_ASSERT_EQUAL_INT(
62 	 "result",
63 	 result,
64 	 1 );
65 
66 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
67 	 "info_handle",
68 	 info_handle );
69 
70 	FSEXT_TEST_ASSERT_IS_NULL(
71 	 "error",
72 	 error );
73 
74 	result = info_handle_free(
75 	          &info_handle,
76 	          &error );
77 
78 	FSEXT_TEST_ASSERT_EQUAL_INT(
79 	 "result",
80 	 result,
81 	 1 );
82 
83 	FSEXT_TEST_ASSERT_IS_NULL(
84 	 "info_handle",
85 	 info_handle );
86 
87 	FSEXT_TEST_ASSERT_IS_NULL(
88 	 "error",
89 	 error );
90 
91 	/* Test error cases
92 	 */
93 	result = info_handle_initialize(
94 	          NULL,
95 	          0,
96 	          &error );
97 
98 	FSEXT_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	info_handle = (info_handle_t *) 0x12345678UL;
111 
112 	result = info_handle_initialize(
113 	          &info_handle,
114 	          0,
115 	          &error );
116 
117 	info_handle = NULL;
118 
119 	FSEXT_TEST_ASSERT_EQUAL_INT(
120 	 "result",
121 	 result,
122 	 -1 );
123 
124 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
125 	 "error",
126 	 error );
127 
128 	libcerror_error_free(
129 	 &error );
130 
131 #if defined( HAVE_FSEXT_TEST_MEMORY )
132 
133 	for( test_number = 0;
134 	     test_number < number_of_malloc_fail_tests;
135 	     test_number++ )
136 	{
137 		/* Test info_handle_initialize with malloc failing
138 		 */
139 		fsext_test_malloc_attempts_before_fail = test_number;
140 
141 		result = info_handle_initialize(
142 		          &info_handle,
143 		          0,
144 		          &error );
145 
146 		if( fsext_test_malloc_attempts_before_fail != -1 )
147 		{
148 			fsext_test_malloc_attempts_before_fail = -1;
149 
150 			if( info_handle != NULL )
151 			{
152 				info_handle_free(
153 				 &info_handle,
154 				 NULL );
155 			}
156 		}
157 		else
158 		{
159 			FSEXT_TEST_ASSERT_EQUAL_INT(
160 			 "result",
161 			 result,
162 			 -1 );
163 
164 			FSEXT_TEST_ASSERT_IS_NULL(
165 			 "info_handle",
166 			 info_handle );
167 
168 			FSEXT_TEST_ASSERT_IS_NOT_NULL(
169 			 "error",
170 			 error );
171 
172 			libcerror_error_free(
173 			 &error );
174 		}
175 	}
176 	for( test_number = 0;
177 	     test_number < number_of_memset_fail_tests;
178 	     test_number++ )
179 	{
180 		/* Test info_handle_initialize with memset failing
181 		 */
182 		fsext_test_memset_attempts_before_fail = test_number;
183 
184 		result = info_handle_initialize(
185 		          &info_handle,
186 		          0,
187 		          &error );
188 
189 		if( fsext_test_memset_attempts_before_fail != -1 )
190 		{
191 			fsext_test_memset_attempts_before_fail = -1;
192 
193 			if( info_handle != NULL )
194 			{
195 				info_handle_free(
196 				 &info_handle,
197 				 NULL );
198 			}
199 		}
200 		else
201 		{
202 			FSEXT_TEST_ASSERT_EQUAL_INT(
203 			 "result",
204 			 result,
205 			 -1 );
206 
207 			FSEXT_TEST_ASSERT_IS_NULL(
208 			 "info_handle",
209 			 info_handle );
210 
211 			FSEXT_TEST_ASSERT_IS_NOT_NULL(
212 			 "error",
213 			 error );
214 
215 			libcerror_error_free(
216 			 &error );
217 		}
218 	}
219 #endif /* defined( HAVE_FSEXT_TEST_MEMORY ) */
220 
221 	return( 1 );
222 
223 on_error:
224 	if( error != NULL )
225 	{
226 		libcerror_error_free(
227 		 &error );
228 	}
229 	if( info_handle != NULL )
230 	{
231 		info_handle_free(
232 		 &info_handle,
233 		 NULL );
234 	}
235 	return( 0 );
236 }
237 
238 /* Tests the info_handle_free function
239  * Returns 1 if successful or 0 if not
240  */
fsext_test_tools_info_handle_free(void)241 int fsext_test_tools_info_handle_free(
242      void )
243 {
244 	libcerror_error_t *error = NULL;
245 	int result               = 0;
246 
247 	/* Test error cases
248 	 */
249 	result = info_handle_free(
250 	          NULL,
251 	          &error );
252 
253 	FSEXT_TEST_ASSERT_EQUAL_INT(
254 	 "result",
255 	 result,
256 	 -1 );
257 
258 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
259 	 "error",
260 	 error );
261 
262 	libcerror_error_free(
263 	 &error );
264 
265 	return( 1 );
266 
267 on_error:
268 	if( error != NULL )
269 	{
270 		libcerror_error_free(
271 		 &error );
272 	}
273 	return( 0 );
274 }
275 
276 /* The main program
277  */
278 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSEXT_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSEXT_TEST_ATTRIBUTE_UNUSED)279 int wmain(
280      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
281      wchar_t * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
282 #else
283 int main(
284      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
285      char * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
286 #endif
287 {
288 	FSEXT_TEST_UNREFERENCED_PARAMETER( argc )
289 	FSEXT_TEST_UNREFERENCED_PARAMETER( argv )
290 
291 	FSEXT_TEST_RUN(
292 	 "info_handle_initialize",
293 	 fsext_test_tools_info_handle_initialize );
294 
295 	FSEXT_TEST_RUN(
296 	 "info_handle_free",
297 	 fsext_test_tools_info_handle_free );
298 
299 	return( EXIT_SUCCESS );
300 
301 on_error:
302 	return( EXIT_FAILURE );
303 }
304 
305