1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include <setjmp.h>
19 #include "cmocka.h"
20 #include <stdio.h>
21 
22 #ifdef _WIN32
23 /* Compatibility with the Windows standard C library. */
24 #define vsnprintf _vsnprintf
25 #endif /* _WIN32 */
26 
27 #define array_length(x) (sizeof(x) / sizeof((x)[0]))
28 
29 /* To simplify this code, these functions and data structures could have been
30  * separated out from the application example.c into a header shared with
31  * test application.  However, this example illustrates how it's possible to
32  * test existing code with little modification. */
33 
34 typedef int (*BinaryOperator)(int a, int b);
35 
36 typedef struct OperatorFunction {
37 	const char* operator;
38 	BinaryOperator function;
39 } OperatorFunction;
40 
41 extern int add(int a, int b);
42 extern int subtract(int a, int b);
43 extern int multiply(int a, int b);
44 extern int divide(int a, int b);
45 extern BinaryOperator find_operator_function_by_string(
46         const size_t number_of_operator_functions,
47         const OperatorFunction * const operator_functions,
48         const char* const operator_string);
49 extern int perform_operation(
50         int number_of_arguments, char *arguments[],
51         const size_t number_of_operator_functions,
52         const OperatorFunction * const operator_functions,
53         int * const number_of_intermediate_values,
54         int ** const intermediate_values, int * const error_occurred);
55 extern int example_main(int argc, char *argv[]);
56 
57 int example_test_fprintf(FILE* const file, const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(2, 3);
58 int example_test_printf(const char *format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
59 
60 static char temporary_buffer[256];
61 
62 /* A mock fprintf function that checks the value of strings printed to the
63  * standard error stream. */
example_test_fprintf(FILE * const file,const char * format,...)64 int example_test_fprintf(FILE* const file, const char *format, ...) {
65 	int return_value;
66 	va_list args;
67 	assert_true(file == stderr);
68 	va_start(args, format);
69 	return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
70 	                         format, args);
71 	check_expected_ptr(temporary_buffer);
72 	va_end(args);
73 	return return_value;
74 }
75 
76 /* A mock printf function that checks the value of strings printed to the
77  * standard output stream. */
example_test_printf(const char * format,...)78 int example_test_printf(const char *format, ...) {
79 	int return_value;
80 	va_list args;
81 	va_start(args, format);
82 	return_value = vsnprintf(temporary_buffer, sizeof(temporary_buffer),
83 	                         format, args);
84 	check_expected_ptr(temporary_buffer);
85 	va_end(args);
86 	return return_value;
87 }
88 
89 /* A mock binary operator function. */
binary_operator(int a,int b)90 static int binary_operator(int a, int b) {
91 	check_expected(a);
92 	check_expected(b);
93 	return (int)mock();
94 }
95 
96 
97 /* Ensure add() adds two integers correctly. */
test_add(void ** state)98 static void test_add(void **state) {
99         (void) state; /* unused */
100 
101 	assert_int_equal(add(3, 3), 6);
102 	assert_int_equal(add(3, -3), 0);
103 }
104 
105 /* Ensure subtract() subtracts two integers correctly. */
test_subtract(void ** state)106 static void test_subtract(void **state) {
107         (void) state; /* unused */
108 
109 	assert_int_equal(subtract(3, 3), 0);
110 	assert_int_equal(subtract(3, -3), 6);
111 }
112 
113 /* Ensure multiple() mulitplies two integers correctly. */
test_multiply(void ** state)114 static void test_multiply(void **state) {
115         (void) state; /* unused */
116 
117 	assert_int_equal(multiply(3, 3), 9);
118 	assert_int_equal(multiply(3, 0), 0);
119 }
120 
121 /* Ensure divide() divides one integer by another correctly. */
test_divide(void ** state)122 static void test_divide(void **state) {
123         (void) state; /* unused */
124 
125 	assert_int_equal(divide(10, 2), 5);
126 	assert_int_equal(divide(2, 10), 0);
127 }
128 
129 /* Ensure divide() asserts when trying to divide by zero. */
test_divide_by_zero(void ** state)130 static void test_divide_by_zero(void **state) {
131         (void) state; /* unused */
132 
133 	expect_assert_failure(divide(100, 0));
134 }
135 
136 /* Ensure find_operator_function_by_string() asserts when a NULL pointer is
137  * specified as the table to search. */
test_find_operator_function_by_string_null_functions(void ** state)138 static void test_find_operator_function_by_string_null_functions(void **state) {
139         (void) state; /* unused */
140 
141 	expect_assert_failure(find_operator_function_by_string(1, NULL, "test"));
142 }
143 
144 /* Ensure find_operator_function_by_string() asserts when a NULL pointer is
145  * specified as the string to search for. */
test_find_operator_function_by_string_null_string(void ** state)146 static void test_find_operator_function_by_string_null_string(void **state) {
147 	const OperatorFunction operator_functions[] = {
148 		{"+", binary_operator},
149 	};
150 
151         (void) state; /* unused */
152 
153 	expect_assert_failure(find_operator_function_by_string(
154 	    array_length(operator_functions), operator_functions, NULL));
155 }
156 
157 /* Ensure find_operator_function_by_string() returns NULL when a NULL pointer
158  * is specified as the table to search when the table size is 0. */
test_find_operator_function_by_string_valid_null_functions(void ** state)159 static void test_find_operator_function_by_string_valid_null_functions(void **state) {
160         (void) state; /* unused */
161 
162 	assert_null(find_operator_function_by_string(0, NULL, "test"));
163 }
164 
165 /* Ensure find_operator_function_by_string() returns NULL when searching for
166  * an operator string that isn't in the specified table. */
test_find_operator_function_by_string_not_found(void ** state)167 static void test_find_operator_function_by_string_not_found(void **state) {
168 	const OperatorFunction operator_functions[] = {
169 		{"+", binary_operator},
170 		{"-", binary_operator},
171 		{"/", binary_operator},
172 	};
173 
174         (void) state; /* unused */
175 
176 	assert_null(find_operator_function_by_string(
177 	        array_length(operator_functions), operator_functions, "test"));
178 }
179 
180 /* Ensure find_operator_function_by_string() returns the correct function when
181  * searching for an operator string that is in the specified table. */
test_find_operator_function_by_string_found(void ** state)182 static void test_find_operator_function_by_string_found(void **state) {
183 	const OperatorFunction operator_functions[] = {
184 		{"+", (BinaryOperator)0x12345678},
185 		{"-", (BinaryOperator)0xDEADBEEF},
186 		{"/", (BinaryOperator)0xABADCAFE},
187 	};
188 
189         (void) state; /* unused */
190 
191 	assert_int_equal(
192             cast_ptr_to_largest_integral_type(
193                 find_operator_function_by_string(array_length(operator_functions),
194                                                  operator_functions,
195                                                  "-")),
196 	    0xDEADBEEF);
197 }
198 
199 /* Ensure perform_operation() asserts when a NULL arguments array is specified. */
test_perform_operation_null_args(void ** state)200 static void test_perform_operation_null_args(void **state) {
201 	const OperatorFunction operator_functions[] = {
202 		{"+", binary_operator},
203 	};
204 	int number_of_intermediate_values;
205 	int *intermediate_values;
206 	int error_occurred;
207 
208         (void) state; /* unused */
209 
210 	expect_assert_failure(perform_operation(
211 	    1, NULL, array_length(operator_functions), operator_functions,
212 	    &number_of_intermediate_values, &intermediate_values,
213 	    &error_occurred));
214 }
215 
216 /* Ensure perform_operation() asserts when a NULL operator_functions array is
217  * specified. */
test_perform_operation_null_operator_functions(void ** state)218 static void test_perform_operation_null_operator_functions(void **state) {
219 	const char *args[] = {
220 		"1", "+", "2", "*", "4"
221 	};
222 	int number_of_intermediate_values;
223 	int *intermediate_values;
224 	int error_occurred;
225 
226         (void) state; /* unused */
227 
228 	expect_assert_failure(perform_operation(
229 	    array_length(args), (char **) args, 1, NULL, &number_of_intermediate_values,
230 	    &intermediate_values, &error_occurred));
231 }
232 
233 /* Ensure perform_operation() asserts when a NULL pointer is specified for
234  * number_of_intermediate_values. */
test_perform_operation_null_number_of_intermediate_values(void ** state)235 static void test_perform_operation_null_number_of_intermediate_values(void **state) {
236 	const OperatorFunction operator_functions[] = {
237 		{"+", binary_operator},
238 	};
239 	const char *args[] = {
240 		"1", "+", "2", "*", "4"
241 	};
242 	int *intermediate_values;
243 	int error_occurred;
244 
245         (void) state; /* unused */
246 
247 	expect_assert_failure(perform_operation(
248 	    array_length(args), (char **) args, 1, operator_functions, NULL,
249 	    &intermediate_values, &error_occurred));
250 }
251 
252 /* Ensure perform_operation() asserts when a NULL pointer is specified for
253  * intermediate_values. */
test_perform_operation_null_intermediate_values(void ** state)254 static void test_perform_operation_null_intermediate_values(void **state) {
255 	const OperatorFunction operator_functions[] = {
256 		{"+", binary_operator},
257 	};
258 	const char *args[] = {
259 		"1", "+", "2", "*", "4"
260 	};
261 	int number_of_intermediate_values;
262 	int error_occurred;
263 
264         (void) state; /* unused */
265 
266 	expect_assert_failure(perform_operation(
267 	    array_length(args), (char **) args, array_length(operator_functions),
268 	    operator_functions, &number_of_intermediate_values, NULL,
269 	    &error_occurred));
270 }
271 
272 /* Ensure perform_operation() returns 0 when no arguments are specified. */
test_perform_operation_no_arguments(void ** state)273 static void test_perform_operation_no_arguments(void **state) {
274 	int number_of_intermediate_values;
275 	int *intermediate_values;
276 	int error_occurred;
277 
278         (void) state; /* unused */
279 
280 	assert_int_equal(perform_operation(
281 	    0, NULL, 0, NULL, &number_of_intermediate_values, &intermediate_values,
282 	    &error_occurred), 0);
283 	assert_int_equal(error_occurred, 0);
284 }
285 
286 /* Ensure perform_operation() returns an error if the first argument isn't
287  * an integer string. */
test_perform_operation_first_arg_not_integer(void ** state)288 static void test_perform_operation_first_arg_not_integer(void **state) {
289 	const OperatorFunction operator_functions[] = {
290 		{"+", binary_operator},
291 	};
292 	const char *args[] = {
293 		"test", "+", "2", "*", "4"
294 	};
295 	int number_of_intermediate_values;
296 	int *intermediate_values;
297 	int error_occurred;
298 
299         (void) state; /* unused */
300 
301 	expect_string(example_test_fprintf, temporary_buffer,
302 	              "Unable to parse integer from argument test\n");
303 
304 	assert_int_equal(perform_operation(
305 	    array_length(args), (char **) args, array_length(operator_functions),
306 	    operator_functions, &number_of_intermediate_values,
307 	    &intermediate_values, &error_occurred), 0);
308 	assert_int_equal(error_occurred, 1);
309 }
310 
311 /* Ensure perform_operation() returns an error when parsing an unknown
312  * operator. */
test_perform_operation_unknown_operator(void ** state)313 static void test_perform_operation_unknown_operator(void **state) {
314 	const OperatorFunction operator_functions[] = {
315 		{"+", binary_operator},
316 	};
317 	const char *args[] = {
318 		"1", "*", "2", "*", "4"
319 	};
320 	int number_of_intermediate_values;
321 	int *intermediate_values;
322 	int error_occurred;
323 
324         (void) state; /* unused */
325 
326 	expect_string(example_test_fprintf, temporary_buffer,
327 	              "Unknown operator *, argument 1\n");
328 
329 	assert_int_equal(perform_operation(
330 	    array_length(args), (char **) args, array_length(operator_functions),
331 	    operator_functions, &number_of_intermediate_values,
332 	    &intermediate_values, &error_occurred), 0);
333 	assert_int_equal(error_occurred, 1);
334 }
335 
336 /* Ensure perform_operation() returns an error when nothing follows an
337  * operator. */
test_perform_operation_missing_argument(void ** state)338 static void test_perform_operation_missing_argument(void **state) {
339 	const OperatorFunction operator_functions[] = {
340 		{"+", binary_operator},
341 	};
342 	const char *args[] = {
343 		"1", "+",
344 	};
345 	int number_of_intermediate_values;
346 	int *intermediate_values;
347 	int error_occurred;
348 
349         (void) state; /* unused */
350 
351 	expect_string(example_test_fprintf, temporary_buffer,
352 	              "Binary operator + missing argument\n");
353 
354 	assert_int_equal(perform_operation(
355 	    array_length(args), (char **) args, array_length(operator_functions),
356 	    operator_functions, &number_of_intermediate_values,
357 	    &intermediate_values, &error_occurred), 0);
358 	assert_int_equal(error_occurred, 1);
359 }
360 
361 /* Ensure perform_operation() returns an error when an integer doesn't follow
362  * an operator. */
test_perform_operation_no_integer_after_operator(void ** state)363 static void test_perform_operation_no_integer_after_operator(void **state) {
364 	const OperatorFunction operator_functions[] = {
365 		{"+", binary_operator},
366 	};
367 	const char *args[] = {
368 		"1", "+", "test",
369 	};
370 	int number_of_intermediate_values;
371 	int *intermediate_values;
372 	int error_occurred;
373 
374         (void) state; /* unused */
375 
376 	expect_string(example_test_fprintf, temporary_buffer,
377 	              "Unable to parse integer test of argument 2\n");
378 
379 	assert_int_equal(perform_operation(
380 	    array_length(args), (char **) args, array_length(operator_functions),
381 	    operator_functions, &number_of_intermediate_values,
382 	    &intermediate_values, &error_occurred), 0);
383 	assert_int_equal(error_occurred, 1);
384 }
385 
386 
387 /* Ensure perform_operation() succeeds given valid input parameters. */
test_perform_operation(void ** state)388 static void test_perform_operation(void **state) {
389 	const OperatorFunction operator_functions[] = {
390 		{"+", binary_operator},
391 		{"*", binary_operator},
392 	};
393 	const char *args[] = {
394 		"1", "+", "3", "*", "10",
395 	};
396 	int number_of_intermediate_values;
397 	int *intermediate_values = NULL;
398 	int error_occurred;
399 
400         (void) state; /* unused */
401 
402 	/* Setup return values of mock operator functions. */
403 	/* Addition. */
404 	expect_value(binary_operator, a, 1);
405 	expect_value(binary_operator, b, 3);
406 	will_return(binary_operator, 4);
407 
408 	/* Multiplication. */
409 	expect_value(binary_operator, a, 4);
410 	expect_value(binary_operator, b, 10);
411 	will_return(binary_operator, 40);
412 
413 	assert_int_equal(perform_operation(
414 	    array_length(args), (char **) args, array_length(operator_functions),
415 	    operator_functions, &number_of_intermediate_values,
416 	    &intermediate_values, &error_occurred), 40);
417 	assert_int_equal(error_occurred, 0);
418 
419 	assert_non_null(intermediate_values);
420 	assert_int_equal(intermediate_values[0], 4);
421 	assert_int_equal(intermediate_values[1], 40);
422 	test_free(intermediate_values);
423 }
424 
425 
426 /* Ensure main() in example.c succeeds given no arguments. */
test_example_main_no_args(void ** state)427 static void test_example_main_no_args(void **state) {
428 	const char *args[] = {
429 		"example",
430 	};
431 
432         (void) state; /* unused */
433 
434 	assert_int_equal(example_main(array_length(args), (char **) args), 0);
435 }
436 
437 
438 
439 /* Ensure main() in example.c succeeds given valid input arguments. */
test_example_main(void ** state)440 static void test_example_main(void **state) {
441 	const char *args[] = {
442 		"example", "1", "+", "3", "*", "10",
443 	};
444 
445         (void) state; /* unused */
446 
447 	expect_string(example_test_printf, temporary_buffer, "1\n");
448 	expect_string(example_test_printf, temporary_buffer, "  + 3 = 4\n");
449 	expect_string(example_test_printf, temporary_buffer, "  * 10 = 40\n");
450 	expect_string(example_test_printf, temporary_buffer, "= 40\n");
451 
452 	assert_int_equal(example_main(array_length(args), (char **) args), 0);
453 }
454 
455 
main(void)456 int main(void) {
457 	const struct CMUnitTest tests[] = {
458 		cmocka_unit_test(test_add),
459 		cmocka_unit_test(test_subtract),
460 		cmocka_unit_test(test_multiply),
461 		cmocka_unit_test(test_divide),
462 		cmocka_unit_test(test_divide_by_zero),
463 		cmocka_unit_test(test_find_operator_function_by_string_null_functions),
464 		cmocka_unit_test(test_find_operator_function_by_string_null_string),
465 		cmocka_unit_test(test_find_operator_function_by_string_valid_null_functions),
466 		cmocka_unit_test(test_find_operator_function_by_string_not_found),
467 		cmocka_unit_test(test_find_operator_function_by_string_found),
468 		cmocka_unit_test(test_perform_operation_null_args),
469 		cmocka_unit_test(test_perform_operation_null_operator_functions),
470 		cmocka_unit_test(test_perform_operation_null_number_of_intermediate_values),
471 		cmocka_unit_test(test_perform_operation_null_intermediate_values),
472 		cmocka_unit_test(test_perform_operation_no_arguments),
473 		cmocka_unit_test(test_perform_operation_first_arg_not_integer),
474 		cmocka_unit_test(test_perform_operation_unknown_operator),
475 		cmocka_unit_test(test_perform_operation_missing_argument),
476 		cmocka_unit_test(test_perform_operation_no_integer_after_operator),
477 		cmocka_unit_test(test_perform_operation),
478 		cmocka_unit_test(test_example_main_no_args),
479 		cmocka_unit_test(test_example_main),
480 	};
481 	return cmocka_run_group_tests(tests, NULL, NULL);
482 }
483