12 char debug_filename
[] = ".\\gammu-debug-test.log";
14 # define NUL "/dev/null"
15 char debug_filename
[] = "./gammu-debug-test.log";
18 NORETURN
void fail(int error_code
)
20 unlink(debug_filename
);
24 void check_log(FILE * f
, gboolean match
, const char *test_name
)
27 char test_message
[] = "T3ST M3S5AG3";
28 char cleaner
[] = "XXXXXXXXXXXXXXXXX";
32 GSM_LogError(s
, test_message
, ERR_MOREMEMORY
);
34 result
= fread(buff
, 1, sizeof(test_message
), f
);
35 if (match
&& result
!= sizeof(test_message
)) {
36 printf("%s: Read failed (%d)!\n", test_name
, result
);
39 if (!match
&& result
!= sizeof(test_message
)) {
42 result
= strncmp(test_message
, buff
, sizeof(test_message
) - 1);
43 if (match
&& result
!= 0) {
44 printf("%s: Match failed!\n", test_name
);
47 if (!match
&& result
== 0) {
48 printf("%s: Matchech but should not!\n", test_name
);
53 result
= fwrite(cleaner
, 1, sizeof(cleaner
), f
);
54 test_result(result
== sizeof(cleaner
));
58 void Log_Function(const char *text
, void *data UNUSED
)
60 printf("msg: %s", text
);
63 int main(int argc UNUSED
, char **argv UNUSED
)
66 GSM_Debug_Info
*di_sm
, *di_global
;
69 /* Allocates state machine */
70 s
= GSM_AllocStateMachine();
71 test_result(s
!= NULL
);
73 /* Get debug handles */
74 di_sm
= GSM_GetDebug(s
);
75 di_global
= GSM_GetGlobalDebug();
78 * Test 1 - setting debug level.
80 test_result(GSM_SetDebugLevel("NONSENSE", di_sm
) == FALSE
);
81 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
82 test_result(GSM_SetDebugLevel("textall", di_sm
) == TRUE
);
83 test_result(GSM_SetDebugLevel("textall", di_global
) == TRUE
);
86 * Test 2 - global /dev/null, local tempfile, do not use global
88 debug_file
= fopen(debug_filename
, "w+");
89 test_result(debug_file
!= NULL
);
90 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
91 error
= GSM_SetDebugFile(NUL
, di_global
);
92 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_global)");
93 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_sm
);
94 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_sm)");
95 check_log(debug_file
, TRUE
, "2. global_file=NULL, sm_file=TEMP, use_global=FALSE");
96 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
97 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
98 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
99 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
102 * Test 3 - global /dev/null, local tempfile, use global
104 debug_file
= fopen(debug_filename
, "w+");
105 test_result(debug_file
!= NULL
);
106 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
107 error
= GSM_SetDebugFile(NUL
, di_global
);
108 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_global)");
109 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_sm
);
110 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_sm)");
111 check_log(debug_file
, FALSE
, "3. global_file=NULL, sm_file=TEMP, use_global=TRUE");
112 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
113 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
114 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
115 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
118 * Test 4 - global tempfile, local /dev/null, use global
120 debug_file
= fopen(debug_filename
, "w+");
121 test_result(debug_file
!= NULL
);
122 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
123 error
= GSM_SetDebugFile(NUL
, di_sm
);
124 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_sm)");
125 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
126 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
127 check_log(debug_file
, TRUE
, "4. global_file=TEMP, sm_file=NULL, use_global=TRUE");
128 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
129 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
130 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
131 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
134 * Test 5 - global tempfile, local /dev/null, do not use global
136 debug_file
= fopen(debug_filename
, "w+");
137 test_result(debug_file
!= NULL
);
138 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
139 error
= GSM_SetDebugFile(NUL
, di_sm
);
140 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_sm)");
141 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
142 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
143 check_log(debug_file
, FALSE
, "5. global_file=TEMP, sm_file=NULL, use_global=FALSE");
144 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
145 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
146 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
147 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
150 * Test 6 - global /dev/null, local tempfile, do not use global
152 debug_file
= fopen(debug_filename
, "w+");
153 test_result(debug_file
!= NULL
);
154 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
155 error
= GSM_SetDebugFile(NUL
, di_global
);
156 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_global)");
157 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_sm
);
158 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_sm)");
159 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
160 check_log(debug_file
, TRUE
, "6. global_file=NULL, sm_file=TEMP, use_global=FALSE");
161 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
162 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
163 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
164 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
167 * Test 7 - global /dev/null, local tempfile, use global
169 debug_file
= fopen(debug_filename
, "w+");
170 test_result(debug_file
!= NULL
);
171 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
172 error
= GSM_SetDebugFile(NUL
, di_global
);
173 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_global)");
174 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_sm
);
175 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_sm)");
176 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
177 check_log(debug_file
, FALSE
, "7. global_file=NULL, sm_file=TEMP, use_global=TRUE");
178 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
179 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
180 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
181 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
184 * Test 8 - global tempfile, local /dev/null, use global
186 debug_file
= fopen(debug_filename
, "w+");
187 test_result(debug_file
!= NULL
);
188 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
189 error
= GSM_SetDebugFile(NUL
, di_sm
);
190 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_sm)");
191 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
192 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
193 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
194 check_log(debug_file
, TRUE
, "8. global_file=TEMP, sm_file=NULL, use_global=TRUE");
195 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
196 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
197 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
198 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
201 * Test 9 - global tempfile, local /dev/null, do not use global
203 debug_file
= fopen(debug_filename
, "w+");
204 test_result(debug_file
!= NULL
);
205 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
206 error
= GSM_SetDebugFile(NUL
, di_sm
);
207 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_sm)");
208 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
209 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
210 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
211 check_log(debug_file
, FALSE
, "9. global_file=TEMP, sm_file=NULL, use_global=FALSE");
212 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
213 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
214 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
215 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
218 * Test 10 - functional logging, do not use global
220 debug_file
= fopen(debug_filename
, "w+");
221 test_result(debug_file
!= NULL
);
222 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
223 error
= GSM_SetDebugFile(NUL
, di_sm
);
224 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_sm)");
225 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
226 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
227 error
= GSM_SetDebugFunction(Log_Function
, NULL
, di_sm
);
228 gammu_test_result(error
, "GSM_SetDebugFunction(Log_Function, NULL, di_sm)");
229 test_result(GSM_SetDebugGlobal(FALSE
, di_sm
) == TRUE
);
230 check_log(debug_file
, FALSE
, "10. global_file=TEMP, sm_file=function, use_global=FALSE");
231 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
232 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
233 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
234 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
237 * Test 11 - functional logging, use global
239 debug_file
= fopen(debug_filename
, "w+");
240 test_result(debug_file
!= NULL
);
241 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
242 error
= GSM_SetDebugFile(NUL
, di_global
);
243 gammu_test_result(error
, "GSM_SetDebugFile(NUL, di_global)");
244 error
= GSM_SetDebugFileDescriptor(debug_file
, TRUE
, di_global
);
245 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(debug_file, TRUE, di_global)");
246 error
= GSM_SetDebugFunction(Log_Function
, NULL
, di_global
);
247 gammu_test_result(error
, "GSM_SetDebugFunction(Log_Function, NULL, di_global)");
248 test_result(GSM_SetDebugGlobal(TRUE
, di_sm
) == TRUE
);
249 check_log(debug_file
, FALSE
, "11. global_file=function, sm_file=NULL, use_global=TRUE");
250 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_sm
);
251 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_sm)");
252 error
= GSM_SetDebugFileDescriptor(NULL
, FALSE
, di_global
);
253 gammu_test_result(error
, "GSM_SetDebugFileDescriptor(NULL, FALSE, di_global)");
255 /* Free state machine */
256 GSM_FreeStateMachine(s
);
261 /* Editor configuration
262 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72: