1 /*
2  * Tools signal functions test program
3  *
4  * Copyright (C) 2020-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 "fsxfs_test_libcerror.h"
31 #include "fsxfs_test_macros.h"
32 #include "fsxfs_test_unused.h"
33 
34 #include "../fsxfstools/fsxfstools_signal.h"
35 
fsxfs_test_tools_signal_handler_function(fsxfstools_signal_t signal FSXFS_TEST_ATTRIBUTE_UNUSED)36 void fsxfs_test_tools_signal_handler_function(
37       fsxfstools_signal_t signal FSXFS_TEST_ATTRIBUTE_UNUSED )
38 {
39 	FSXFS_TEST_UNREFERENCED_PARAMETER( signal )
40 }
41 
42 #if defined( WINAPI )
43 
44 /* Tests the fsxfstools_signal_handler function
45  * Returns 1 if successful or 0 if not
46  */
fsxfs_test_tools_signal_handler(void)47 int fsxfs_test_tools_signal_handler(
48      void )
49 {
50 	BOOL result = 0;
51 
52 	/* Test regular cases
53 	 */
54 	result = fsxfstools_signal_handler(
55 	          CTRL_C_EVENT );
56 
57 	FSXFS_TEST_ASSERT_EQUAL_INT(
58 	 "result",
59 	 result,
60 	 (int) TRUE );
61 
62 	result = fsxfstools_signal_handler(
63 	          CTRL_LOGOFF_EVENT );
64 
65 	FSXFS_TEST_ASSERT_EQUAL_INT(
66 	 "result",
67 	 result,
68 	 (int) FALSE );
69 
70 	return( 1 );
71 
72 on_error:
73 	return( 0 );
74 }
75 
76 #if defined( _MSC_VER )
77 
78 	/* TODO add tests for fsxfstools_signal_initialize_memory_debug */
79 
80 #endif /* defined( _MSC_VER ) */
81 
82 #endif /* defined( WINAPI ) */
83 
84 /* Tests the fsxfstools_signal_attach function
85  * Returns 1 if successful or 0 if not
86  */
fsxfs_test_tools_signal_attach(void)87 int fsxfs_test_tools_signal_attach(
88      void )
89 {
90 	libcerror_error_t *error = NULL;
91 	int result               = 0;
92 
93 	/* Test regular cases
94 	 */
95 	result = fsxfstools_signal_attach(
96 	          fsxfs_test_tools_signal_handler_function,
97 	          &error );
98 
99 	FSXFS_TEST_ASSERT_EQUAL_INT(
100 	 "result",
101 	 result,
102 	 1 );
103 
104 	FSXFS_TEST_ASSERT_IS_NULL(
105 	 "error",
106 	 error );
107 
108 	/* Test error cases
109 	 */
110 	result = fsxfstools_signal_attach(
111 	          NULL,
112 	          &error );
113 
114 	FSXFS_TEST_ASSERT_EQUAL_INT(
115 	 "result",
116 	 result,
117 	 -1 );
118 
119 	FSXFS_TEST_ASSERT_IS_NOT_NULL(
120 	 "error",
121 	 error );
122 
123 	libcerror_error_free(
124 	 &error );
125 
126 	return( 1 );
127 
128 on_error:
129 	if( error != NULL )
130 	{
131 		libcerror_error_free(
132 		 &error );
133 	}
134 	return( 0 );
135 }
136 
137 /* Tests the fsxfstools_signal_detach function
138  * Returns 1 if successful or 0 if not
139  */
fsxfs_test_tools_signal_detach(void)140 int fsxfs_test_tools_signal_detach(
141      void )
142 {
143 	libcerror_error_t *error = NULL;
144 	int result               = 0;
145 
146 	/* Test regular cases
147 	 */
148 	result = fsxfstools_signal_detach(
149 	          &error );
150 
151 	FSXFS_TEST_ASSERT_EQUAL_INT(
152 	 "result",
153 	 result,
154 	 1 );
155 
156 	FSXFS_TEST_ASSERT_IS_NULL(
157 	 "error",
158 	 error );
159 
160 	return( 1 );
161 
162 on_error:
163 	if( error != NULL )
164 	{
165 		libcerror_error_free(
166 		 &error );
167 	}
168 	return( 0 );
169 }
170 
171 /* The main program
172  */
173 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSXFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSXFS_TEST_ATTRIBUTE_UNUSED)174 int wmain(
175      int argc FSXFS_TEST_ATTRIBUTE_UNUSED,
176      wchar_t * const argv[] FSXFS_TEST_ATTRIBUTE_UNUSED )
177 #else
178 int main(
179      int argc FSXFS_TEST_ATTRIBUTE_UNUSED,
180      char * const argv[] FSXFS_TEST_ATTRIBUTE_UNUSED )
181 #endif
182 {
183 	FSXFS_TEST_UNREFERENCED_PARAMETER( argc )
184 	FSXFS_TEST_UNREFERENCED_PARAMETER( argv )
185 
186 #if defined( WINAPI )
187 
188 	FSXFS_TEST_RUN(
189 	 "fsxfstools_signal_handler",
190 	 fsxfs_test_tools_signal_handler )
191 
192 #if defined( _MSC_VER )
193 
194 	/* TODO add tests for fsxfstools_signal_initialize_memory_debug */
195 
196 #endif /* defined( _MSC_VER ) */
197 
198 #endif /* defined( WINAPI ) */
199 
200 	FSXFS_TEST_RUN(
201 	 "fsxfstools_signal_attach",
202 	 fsxfs_test_tools_signal_attach )
203 
204 	FSXFS_TEST_RUN(
205 	 "fsxfstools_signal_detach",
206 	 fsxfs_test_tools_signal_detach )
207 
208 	return( EXIT_SUCCESS );
209 
210 on_error:
211 	return( EXIT_FAILURE );
212 }
213 
214