1 /*
2  * Tools info_handle type test program
3  *
4  * Copyright (C) 2011-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 "bde_test_libcerror.h"
32 #include "bde_test_macros.h"
33 #include "bde_test_memory.h"
34 #include "bde_test_unused.h"
35 
36 #include "../bdetools/info_handle.h"
37 
38 /* Tests the info_handle_initialize function
39  * Returns 1 if successful or 0 if not
40  */
bde_test_tools_info_handle_initialize(void)41 int bde_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_BDE_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 	          &error );
59 
60 	BDE_TEST_ASSERT_EQUAL_INT(
61 	 "result",
62 	 result,
63 	 1 );
64 
65 	BDE_TEST_ASSERT_IS_NOT_NULL(
66 	 "info_handle",
67 	 info_handle );
68 
69 	BDE_TEST_ASSERT_IS_NULL(
70 	 "error",
71 	 error );
72 
73 	result = info_handle_free(
74 	          &info_handle,
75 	          &error );
76 
77 	BDE_TEST_ASSERT_EQUAL_INT(
78 	 "result",
79 	 result,
80 	 1 );
81 
82 	BDE_TEST_ASSERT_IS_NULL(
83 	 "info_handle",
84 	 info_handle );
85 
86 	BDE_TEST_ASSERT_IS_NULL(
87 	 "error",
88 	 error );
89 
90 	/* Test error cases
91 	 */
92 	result = info_handle_initialize(
93 	          NULL,
94 	          &error );
95 
96 	BDE_TEST_ASSERT_EQUAL_INT(
97 	 "result",
98 	 result,
99 	 -1 );
100 
101 	BDE_TEST_ASSERT_IS_NOT_NULL(
102 	 "error",
103 	 error );
104 
105 	libcerror_error_free(
106 	 &error );
107 
108 	info_handle = (info_handle_t *) 0x12345678UL;
109 
110 	result = info_handle_initialize(
111 	          &info_handle,
112 	          &error );
113 
114 	info_handle = NULL;
115 
116 	BDE_TEST_ASSERT_EQUAL_INT(
117 	 "result",
118 	 result,
119 	 -1 );
120 
121 	BDE_TEST_ASSERT_IS_NOT_NULL(
122 	 "error",
123 	 error );
124 
125 	libcerror_error_free(
126 	 &error );
127 
128 #if defined( HAVE_BDE_TEST_MEMORY )
129 
130 	for( test_number = 0;
131 	     test_number < number_of_malloc_fail_tests;
132 	     test_number++ )
133 	{
134 		/* Test info_handle_initialize with malloc failing
135 		 */
136 		bde_test_malloc_attempts_before_fail = test_number;
137 
138 		result = info_handle_initialize(
139 		          &info_handle,
140 		          &error );
141 
142 		if( bde_test_malloc_attempts_before_fail != -1 )
143 		{
144 			bde_test_malloc_attempts_before_fail = -1;
145 
146 			if( info_handle != NULL )
147 			{
148 				info_handle_free(
149 				 &info_handle,
150 				 NULL );
151 			}
152 		}
153 		else
154 		{
155 			BDE_TEST_ASSERT_EQUAL_INT(
156 			 "result",
157 			 result,
158 			 -1 );
159 
160 			BDE_TEST_ASSERT_IS_NULL(
161 			 "info_handle",
162 			 info_handle );
163 
164 			BDE_TEST_ASSERT_IS_NOT_NULL(
165 			 "error",
166 			 error );
167 
168 			libcerror_error_free(
169 			 &error );
170 		}
171 	}
172 	for( test_number = 0;
173 	     test_number < number_of_memset_fail_tests;
174 	     test_number++ )
175 	{
176 		/* Test info_handle_initialize with memset failing
177 		 */
178 		bde_test_memset_attempts_before_fail = test_number;
179 
180 		result = info_handle_initialize(
181 		          &info_handle,
182 		          &error );
183 
184 		if( bde_test_memset_attempts_before_fail != -1 )
185 		{
186 			bde_test_memset_attempts_before_fail = -1;
187 
188 			if( info_handle != NULL )
189 			{
190 				info_handle_free(
191 				 &info_handle,
192 				 NULL );
193 			}
194 		}
195 		else
196 		{
197 			BDE_TEST_ASSERT_EQUAL_INT(
198 			 "result",
199 			 result,
200 			 -1 );
201 
202 			BDE_TEST_ASSERT_IS_NULL(
203 			 "info_handle",
204 			 info_handle );
205 
206 			BDE_TEST_ASSERT_IS_NOT_NULL(
207 			 "error",
208 			 error );
209 
210 			libcerror_error_free(
211 			 &error );
212 		}
213 	}
214 #endif /* defined( HAVE_BDE_TEST_MEMORY ) */
215 
216 	return( 1 );
217 
218 on_error:
219 	if( error != NULL )
220 	{
221 		libcerror_error_free(
222 		 &error );
223 	}
224 	if( info_handle != NULL )
225 	{
226 		info_handle_free(
227 		 &info_handle,
228 		 NULL );
229 	}
230 	return( 0 );
231 }
232 
233 /* Tests the info_handle_free function
234  * Returns 1 if successful or 0 if not
235  */
bde_test_tools_info_handle_free(void)236 int bde_test_tools_info_handle_free(
237      void )
238 {
239 	libcerror_error_t *error = NULL;
240 	int result               = 0;
241 
242 	/* Test error cases
243 	 */
244 	result = info_handle_free(
245 	          NULL,
246 	          &error );
247 
248 	BDE_TEST_ASSERT_EQUAL_INT(
249 	 "result",
250 	 result,
251 	 -1 );
252 
253 	BDE_TEST_ASSERT_IS_NOT_NULL(
254 	 "error",
255 	 error );
256 
257 	libcerror_error_free(
258 	 &error );
259 
260 	return( 1 );
261 
262 on_error:
263 	if( error != NULL )
264 	{
265 		libcerror_error_free(
266 		 &error );
267 	}
268 	return( 0 );
269 }
270 
271 /* The main program
272  */
273 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc BDE_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]BDE_TEST_ATTRIBUTE_UNUSED)274 int wmain(
275      int argc BDE_TEST_ATTRIBUTE_UNUSED,
276      wchar_t * const argv[] BDE_TEST_ATTRIBUTE_UNUSED )
277 #else
278 int main(
279      int argc BDE_TEST_ATTRIBUTE_UNUSED,
280      char * const argv[] BDE_TEST_ATTRIBUTE_UNUSED )
281 #endif
282 {
283 	BDE_TEST_UNREFERENCED_PARAMETER( argc )
284 	BDE_TEST_UNREFERENCED_PARAMETER( argv )
285 
286 	BDE_TEST_RUN(
287 	 "info_handle_initialize",
288 	 bde_test_tools_info_handle_initialize );
289 
290 	BDE_TEST_RUN(
291 	 "info_handle_free",
292 	 bde_test_tools_info_handle_free );
293 
294 	return( EXIT_SUCCESS );
295 
296 on_error:
297 	return( EXIT_FAILURE );
298 }
299 
300