Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / test / src / unit_test_run.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "unit_test_common.h"
19 #include "unit_test_run.h"
20 #include "unit_test_suites.h"
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24
25 /* introduction to unit test using check by yangsuh
26   * SRunner : runner of all test which belong to added suites
27   *     Suite : container of test cases(TCase)
28   *             TCase : container of tests
29   *                     test : container of many asserts
30   *
31   * SRunner has fork_status option. I will set this option to CK_FORK which means
32   * not stopping until all test finished.
33   * (Of course, when error like SIGSEGV occurs or test failed, current test will be stopped
34   * and goes to next test)
35   */
36
37 typedef enum fork_status fork_status_t;
38 int unit_test_main(fork_status_t fork_status)
39 {
40         SRunner *sr = NULL;
41
42         /* srunner build up by defined test suites */
43         int suite_count = sizeof(suiteFunctions) / sizeof(SUITE_FUNCTION);
44         fprintf(stderr, "total test suites number = %d\n", suite_count);
45
46         if (suite_count == 0) {
47                 return 0;       /* nothing to do */
48         } else {                /* suite_count > 0 */
49                 SUITE_FUNCTION suite_func = NULL;
50
51                 int i = 0;
52                 for (i = 0; i < suite_count; i++) {
53                         suite_func = suiteFunctions[i];
54                         Suite *s = suite_func();
55                         if (s != NULL) {
56                                 if (i == 0) {
57                                         sr = srunner_create(s);
58                                 } else if (i > 0 && i < suite_count) {
59                                         srunner_add_suite(sr, s);
60                                 }
61                         } else {
62                                 fprintf(stderr, "%s\n", "invalid suite function");
63                         }
64                 }
65         }
66
67         /* srunner setting */
68         srunner_set_log(sr, "/tmp/test.log");   /* set log file */
69         srunner_set_fork_status(sr, fork_status);       /* set fork status of Runner */
70
71         srunner_run_all(sr, CK_VERBOSE);        /* set print mode to verbose */
72         srunner_free(sr);
73
74         return 0;
75 }
76
77 /* TODO : return handling */
78 static inline int unit_test_run_fork_mode()
79 {
80         pid_t pid_w;
81         pid_t pid;
82         int status = 0;
83
84         pid = fork();
85         if (pid == -1)
86                 fprintf(stderr, "%s\n", "Error in call to fork");
87         if (pid == 0) {
88                 /* child process : run unit_test_main */
89                 unit_test_main(CK_FORK);
90                 exit(EXIT_SUCCESS);
91         } else {
92                 /* parent process */
93                 fprintf(stderr, "test process pid = %d", pid);
94                 pid_w = waitpid(pid, &status, 0);
95
96                 if (pid_w == pid) {
97                         fprintf(stderr, "%s\n", "test finished successfully");
98                         return 1;       /* test finished */
99                 } else {
100                         fprintf(stderr, "%s\n", "test failed");
101                         return 0;       /* test error */
102                 }
103         }
104
105         return status;
106 }
107
108 /* running as main function will be need during debugging */
109 /* TODO : return handling */
110 static inline int unit_test_run_function_mode()
111 {
112         return unit_test_main(CK_NOFORK);
113 }
114
115 int unit_test_run(run_unit_test_mode_t mode)
116 {
117         int success = 1;        /* success */
118
119         switch (mode) {
120         case FORK_MODE:
121                 success = unit_test_run_fork_mode();
122                 break;
123         case FUNCTION_MODE:
124                 success = unit_test_run_function_mode();
125                 break;
126         default:
127                 break;
128         }
129
130         return success;
131 }