1 /*
2  * Library io_handle type test program
3  *
4  * Copyright (C) 2012-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 <types.h>
25 
26 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
27 #include <stdlib.h>
28 #endif
29 
30 #include "modi_test_libcerror.h"
31 #include "modi_test_libmodi.h"
32 #include "modi_test_macros.h"
33 #include "modi_test_memory.h"
34 #include "modi_test_unused.h"
35 
36 #include "../libmodi/libmodi_io_handle.h"
37 
38 #if defined( __GNUC__ ) && !defined( LIBMODI_DLL_IMPORT )
39 
40 /* Tests the libmodi_io_handle_initialize function
41  * Returns 1 if successful or 0 if not
42  */
modi_test_io_handle_initialize(void)43 int modi_test_io_handle_initialize(
44      void )
45 {
46 	libcerror_error_t *error        = NULL;
47 	libmodi_io_handle_t *io_handle  = NULL;
48 	int result                      = 0;
49 
50 #if defined( HAVE_MODI_TEST_MEMORY )
51 	int number_of_malloc_fail_tests = 1;
52 	int number_of_memset_fail_tests = 1;
53 	int test_number                 = 0;
54 #endif
55 
56 	/* Test regular cases
57 	 */
58 	result = libmodi_io_handle_initialize(
59 	          &io_handle,
60 	          &error );
61 
62 	MODI_TEST_ASSERT_EQUAL_INT(
63 	 "result",
64 	 result,
65 	 1 );
66 
67 	MODI_TEST_ASSERT_IS_NOT_NULL(
68 	 "io_handle",
69 	 io_handle );
70 
71 	MODI_TEST_ASSERT_IS_NULL(
72 	 "error",
73 	 error );
74 
75 	result = libmodi_io_handle_free(
76 	          &io_handle,
77 	          &error );
78 
79 	MODI_TEST_ASSERT_EQUAL_INT(
80 	 "result",
81 	 result,
82 	 1 );
83 
84 	MODI_TEST_ASSERT_IS_NULL(
85 	 "io_handle",
86 	 io_handle );
87 
88 	MODI_TEST_ASSERT_IS_NULL(
89 	 "error",
90 	 error );
91 
92 	/* Test error cases
93 	 */
94 	result = libmodi_io_handle_initialize(
95 	          NULL,
96 	          &error );
97 
98 	MODI_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	MODI_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	io_handle = (libmodi_io_handle_t *) 0x12345678UL;
111 
112 	result = libmodi_io_handle_initialize(
113 	          &io_handle,
114 	          &error );
115 
116 	io_handle = NULL;
117 
118 	MODI_TEST_ASSERT_EQUAL_INT(
119 	 "result",
120 	 result,
121 	 -1 );
122 
123 	MODI_TEST_ASSERT_IS_NOT_NULL(
124 	 "error",
125 	 error );
126 
127 	libcerror_error_free(
128 	 &error );
129 
130 #if defined( HAVE_MODI_TEST_MEMORY )
131 
132 	for( test_number = 0;
133 	     test_number < number_of_malloc_fail_tests;
134 	     test_number++ )
135 	{
136 		/* Test libmodi_io_handle_initialize with malloc failing
137 		 */
138 		modi_test_malloc_attempts_before_fail = test_number;
139 
140 		result = libmodi_io_handle_initialize(
141 		          &io_handle,
142 		          &error );
143 
144 		if( modi_test_malloc_attempts_before_fail != -1 )
145 		{
146 			modi_test_malloc_attempts_before_fail = -1;
147 
148 			if( io_handle != NULL )
149 			{
150 				libmodi_io_handle_free(
151 				 &io_handle,
152 				 NULL );
153 			}
154 		}
155 		else
156 		{
157 			MODI_TEST_ASSERT_EQUAL_INT(
158 			 "result",
159 			 result,
160 			 -1 );
161 
162 			MODI_TEST_ASSERT_IS_NULL(
163 			 "io_handle",
164 			 io_handle );
165 
166 			MODI_TEST_ASSERT_IS_NOT_NULL(
167 			 "error",
168 			 error );
169 
170 			libcerror_error_free(
171 			 &error );
172 		}
173 	}
174 	for( test_number = 0;
175 	     test_number < number_of_memset_fail_tests;
176 	     test_number++ )
177 	{
178 		/* Test libmodi_io_handle_initialize with memset failing
179 		 */
180 		modi_test_memset_attempts_before_fail = test_number;
181 
182 		result = libmodi_io_handle_initialize(
183 		          &io_handle,
184 		          &error );
185 
186 		if( modi_test_memset_attempts_before_fail != -1 )
187 		{
188 			modi_test_memset_attempts_before_fail = -1;
189 
190 			if( io_handle != NULL )
191 			{
192 				libmodi_io_handle_free(
193 				 &io_handle,
194 				 NULL );
195 			}
196 		}
197 		else
198 		{
199 			MODI_TEST_ASSERT_EQUAL_INT(
200 			 "result",
201 			 result,
202 			 -1 );
203 
204 			MODI_TEST_ASSERT_IS_NULL(
205 			 "io_handle",
206 			 io_handle );
207 
208 			MODI_TEST_ASSERT_IS_NOT_NULL(
209 			 "error",
210 			 error );
211 
212 			libcerror_error_free(
213 			 &error );
214 		}
215 	}
216 #endif /* defined( HAVE_MODI_TEST_MEMORY ) */
217 
218 	return( 1 );
219 
220 on_error:
221 	if( error != NULL )
222 	{
223 		libcerror_error_free(
224 		 &error );
225 	}
226 	if( io_handle != NULL )
227 	{
228 		libmodi_io_handle_free(
229 		 &io_handle,
230 		 NULL );
231 	}
232 	return( 0 );
233 }
234 
235 /* Tests the libmodi_io_handle_free function
236  * Returns 1 if successful or 0 if not
237  */
modi_test_io_handle_free(void)238 int modi_test_io_handle_free(
239      void )
240 {
241 	libcerror_error_t *error = NULL;
242 	int result               = 0;
243 
244 	/* Test error cases
245 	 */
246 	result = libmodi_io_handle_free(
247 	          NULL,
248 	          &error );
249 
250 	MODI_TEST_ASSERT_EQUAL_INT(
251 	 "result",
252 	 result,
253 	 -1 );
254 
255 	MODI_TEST_ASSERT_IS_NOT_NULL(
256 	 "error",
257 	 error );
258 
259 	libcerror_error_free(
260 	 &error );
261 
262 	return( 1 );
263 
264 on_error:
265 	if( error != NULL )
266 	{
267 		libcerror_error_free(
268 		 &error );
269 	}
270 	return( 0 );
271 }
272 
273 /* Tests the libmodi_io_handle_clear function
274  * Returns 1 if successful or 0 if not
275  */
modi_test_io_handle_clear(void)276 int modi_test_io_handle_clear(
277      void )
278 {
279 	libcerror_error_t *error       = NULL;
280 	libmodi_io_handle_t *io_handle = NULL;
281 	int result                     = 0;
282 
283 	/* Initialize test
284 	 */
285 	result = libmodi_io_handle_initialize(
286 	          &io_handle,
287 	          &error );
288 
289 	MODI_TEST_ASSERT_EQUAL_INT(
290 	 "result",
291 	 result,
292 	 1 );
293 
294 	MODI_TEST_ASSERT_IS_NOT_NULL(
295 	 "io_handle",
296 	 io_handle );
297 
298 	MODI_TEST_ASSERT_IS_NULL(
299 	 "error",
300 	 error );
301 
302 	/* Test regular cases
303 	 */
304 	result = libmodi_io_handle_clear(
305 	          io_handle,
306 	          &error );
307 
308 	MODI_TEST_ASSERT_EQUAL_INT(
309 	 "result",
310 	 result,
311 	 1 );
312 
313 	MODI_TEST_ASSERT_IS_NULL(
314 	 "error",
315 	 error );
316 
317 	/* Test error cases
318 	 */
319 	result = libmodi_io_handle_clear(
320 	          NULL,
321 	          &error );
322 
323 	MODI_TEST_ASSERT_EQUAL_INT(
324 	 "result",
325 	 result,
326 	 -1 );
327 
328 	MODI_TEST_ASSERT_IS_NOT_NULL(
329 	 "error",
330 	 error );
331 
332 	libcerror_error_free(
333 	 &error );
334 
335 #if defined( HAVE_MODI_TEST_MEMORY )
336 
337 	/* Test libmodi_io_handle_clear with memset failing
338 	 */
339 	modi_test_memset_attempts_before_fail = 0;
340 
341 	result = libmodi_io_handle_clear(
342 	          io_handle,
343 	          &error );
344 
345 	if( modi_test_memset_attempts_before_fail != -1 )
346 	{
347 		modi_test_memset_attempts_before_fail = -1;
348 	}
349 	else
350 	{
351 		MODI_TEST_ASSERT_EQUAL_INT(
352 		 "result",
353 		 result,
354 		 -1 );
355 
356 		MODI_TEST_ASSERT_IS_NOT_NULL(
357 		 "error",
358 		 error );
359 
360 		libcerror_error_free(
361 		 &error );
362 	}
363 #endif /* defined( HAVE_MODI_TEST_MEMORY ) */
364 
365 	/* Clean up
366 	 */
367 	result = libmodi_io_handle_free(
368 	          &io_handle,
369 	          &error );
370 
371 	MODI_TEST_ASSERT_EQUAL_INT(
372 	 "result",
373 	 result,
374 	 1 );
375 
376 	MODI_TEST_ASSERT_IS_NULL(
377 	 "io_handle",
378 	 io_handle );
379 
380 	MODI_TEST_ASSERT_IS_NULL(
381 	 "error",
382 	 error );
383 
384 	return( 1 );
385 
386 on_error:
387 	if( error != NULL )
388 	{
389 		libcerror_error_free(
390 		 &error );
391 	}
392 	if( io_handle != NULL )
393 	{
394 		libmodi_io_handle_free(
395 		 &io_handle,
396 		 NULL );
397 	}
398 	return( 0 );
399 }
400 
401 #endif /* defined( __GNUC__ ) && !defined( LIBMODI_DLL_IMPORT ) */
402 
403 /* The main program
404  */
405 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc MODI_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]MODI_TEST_ATTRIBUTE_UNUSED)406 int wmain(
407      int argc MODI_TEST_ATTRIBUTE_UNUSED,
408      wchar_t * const argv[] MODI_TEST_ATTRIBUTE_UNUSED )
409 #else
410 int main(
411      int argc MODI_TEST_ATTRIBUTE_UNUSED,
412      char * const argv[] MODI_TEST_ATTRIBUTE_UNUSED )
413 #endif
414 {
415 	MODI_TEST_UNREFERENCED_PARAMETER( argc )
416 	MODI_TEST_UNREFERENCED_PARAMETER( argv )
417 
418 #if defined( __GNUC__ ) && !defined( LIBMODI_DLL_IMPORT )
419 
420 	MODI_TEST_RUN(
421 	 "libmodi_io_handle_initialize",
422 	 modi_test_io_handle_initialize );
423 
424 	MODI_TEST_RUN(
425 	 "libmodi_io_handle_free",
426 	 modi_test_io_handle_free );
427 
428 	MODI_TEST_RUN(
429 	 "libmodi_io_handle_clear",
430 	 modi_test_io_handle_clear );
431 
432 #endif /* defined( __GNUC__ ) && !defined( LIBMODI_DLL_IMPORT ) */
433 
434 	return( EXIT_SUCCESS );
435 
436 on_error:
437 	return( EXIT_FAILURE );
438 }
439 
440