upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / test / src / unit_test_run.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
5  * PROPRIETARY/CONFIDENTIAL
6  *
7  * Contact: JuHak Park <juhaki.park@samsung.com>,
8  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
9  *          SunBong Ha <sunbong.ha@samsung.com>
10  *
11  * This software is the confidential and proprietary information of
12  * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not disclose
13  * such Confidential Information and shall use it only in accordance with the
14  * terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
15  * SAMSUNG make no representations or warranties about the suitability of the
16  * software, either express or implied, including but not limited to the
17  * implied warranties of merchantability, fitness for a particular purpose, or
18  * non-infringement. SAMSUNG shall not be liable for any damages suffered by
19  * licensee as a result of using, modifying or distributing this software or
20  * its derivatives.
21  */
22
23 /*
24  * For any sort of issue you concern as to this software,
25  * you may use following point of contact.
26  * All resources contributed on this software
27  * are orinigally written by S-Core Inc., a member of Samsung Group.
28  *
29  * SeongWon Shim <seongwon.shim@samsung.com>
30  */
31
32 /*
33  * unit_test_run.c
34  *
35  *  Created on: 2011. 3. 29.
36  *      Author: yangjoo.suh
37  */
38
39 #include "unit_test_common.h"
40 #include "unit_test_run.h"
41 #include "unit_test_suites.h"
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45
46
47 /* introduction to unit test using check by yangsuh
48   * SRunner : runner of all test which belong to added suites
49   *     Suite : container of test cases(TCase)
50   *             TCase : container of tests
51   *                     test : container of many asserts
52   *
53   * SRunner has fork_status option. I will set this option to CK_FORK which means
54   * not stopping until all test finished.
55   * (Of course, when error like SIGSEGV occurs or test failed, current test will be stopped
56   * and goes to next test)
57   */
58
59 typedef enum fork_status fork_status_t;
60 int unit_test_main(fork_status_t fork_status)
61 {
62         SRunner *sr = NULL;
63
64         /* srunner build up by defined test suites */
65         int suite_count = sizeof(suiteFunctions)/sizeof(SUITE_FUNCTION);
66         fprintf(stderr, "total test suites number = %d\n", suite_count);
67
68         if (suite_count == 0) {
69                 return 0;       /* nothing to do */
70         } else {        /* suite_count > 0 */
71                 SUITE_FUNCTION suite_func = NULL;
72
73                 int i = 0;
74                 for (i = 0; i < suite_count; i++) {
75                         suite_func = suiteFunctions[i];
76                         Suite *s = suite_func();
77                         if (s != NULL) {
78                                 if (i == 0) {
79                                         sr = srunner_create(s);
80                                 } else if (i > 0 && i < suite_count) {
81                                         srunner_add_suite(sr, s);
82                                 }
83                         } else {
84                                 fprintf(stderr, "invalid suite function\n");
85                         }
86                 }
87         }
88
89         /* srunner setting */
90         srunner_set_log(sr, "/tmp/test.log");           /* set log file */
91         srunner_set_fork_status(sr, fork_status);        /* set fork status of Runner */
92
93         srunner_run_all(sr, CK_VERBOSE);                        /* set print mode to verbose */
94         srunner_free(sr);
95
96         return 0;
97 }
98
99 /* TODO : return handling */
100 static inline int unit_test_run_fork_mode()
101 {
102         pid_t pid_w;
103         pid_t pid;
104         int status = 0;
105
106         pid = fork();
107         if (pid == -1)
108                 fprintf(stderr, "Error in call to fork\n");
109         if (pid == 0) {
110                 /* child process : run unit_test_main */
111                 unit_test_main(CK_FORK);
112                 exit(EXIT_SUCCESS);
113         } else {
114                 /* parent process */
115                 fprintf(stderr, "test process pid = %d", pid);
116                 pid_w = waitpid(pid, &status, 0);
117
118                 if (pid_w == pid) {
119                         fprintf(stderr, "test finished successfully\n");
120                         return 1;       /* test finished */
121                 } else {
122                         fprintf(stderr, "test failed\n");
123                         return 0;       /* test error */
124                 }
125         }
126
127         return status;
128 }
129
130 /* running as main function will be need during debugging */
131 /* TODO : return handling */
132 static inline int unit_test_run_function_mode()
133 {
134         return unit_test_main(CK_NOFORK);
135 }
136
137 int unit_test_run(run_unit_test_mode_t mode)
138 {
139         int success = 1;                /* success */
140
141         switch (mode) {
142         case FORK_MODE:
143                 success = unit_test_run_fork_mode();
144                 break;
145         case FUNCTION_MODE:
146                 success = unit_test_run_function_mode();
147                 break;
148         default:
149                 break;
150         }
151
152         return success;
153 }