Svace: Fix NO_RETURN_VALUE
[platform/core/security/privilege-info.git] / test / tc_privilege_info.c
1 #include <stdio.h>
2 #include <privilege_information.h>
3
4 #define BRIGHTNESS 0
5 #define RED 31
6 #define GREEN 32
7 #define YELLOW 33
8 #define BG_BLACK 40
9
10 static int fail_cnt = 0;
11 static int success_cnt = 0;
12
13 static void __change_color_to_red()
14 {
15         printf("%c[%d;%dm", 0x1B, BRIGHTNESS, RED);
16 }
17
18 static void __change_color_to_green()
19 {
20         printf("%c[%d;%dm", 0x1B, BRIGHTNESS, GREEN);
21 }
22
23 static void __change_color_to_yellow()
24 {
25         printf("%c[%d;%dm", 0x1B, BRIGHTNESS, YELLOW);
26 }
27
28 static void __change_color_to_origin()
29 {
30         printf("%c[%dm", 0x1B, 0);
31 }
32
33 static const char* __get_result_string(privilege_info_error_e ret)
34 {
35         if (ret == PRVINFO_ERROR_NONE)
36                 return "PRVINFO_ERROR_NONE";
37         else if (ret == PRVINFO_ERROR_INVALID_PARAMETER)
38                 return "PRVINFO_ERROR_INVALID_PARAMETER";
39         else if (ret == PRVINFO_ERROR_OUT_OF_MEMORY)
40                 return "PRVINFO_ERROR_OUT_OF_MEMORY";
41         else if (ret == PRVINFO_ERROR_INTERNAL_ERROR)
42                 return "PRVINFO_ERROR_INTERNAL_ERROR";
43
44         return "PRVINFO_ERROR_NO_MATCHED_ERROR_MESSAGE";
45 }
46
47 static void __check_get_privilege_display_name_result(privilege_info_error_e expected_result, privilege_info_error_e result, const char* display_name)
48 {
49         printf("expected result = %s, result = %s\n", __get_result_string(expected_result), __get_result_string(result));
50
51         if (expected_result != result) {
52                 printf("not matched\n");
53                 __change_color_to_red();
54                 printf("test fail\n");
55                 fail_cnt++;
56         } else {
57                 printf("matched\n");
58                 if (result == PRVINFO_ERROR_NONE) {
59                         if (display_name == NULL) {
60                                 printf("display_name must not be NULL\n");
61                                 __change_color_to_red();
62                                 printf("test fail\n");
63                                 fail_cnt++;
64                                 __change_color_to_origin();
65                                 return;
66                         } else {
67                                 printf("display_name = %s\n", display_name);
68                         }
69                 } else {
70                         if (display_name != NULL) {
71                                 printf("display_name = %s\n", display_name);
72                                 printf("display_name must be NULL\n");
73                                 __change_color_to_red();
74                                 printf("test fail\n");
75                                 fail_cnt++;
76                                 __change_color_to_origin();
77                                 return;
78                         }
79                 }
80                 __change_color_to_green();
81                 printf("test success\n");
82                 success_cnt++;
83         }
84         __change_color_to_origin();
85 }
86
87
88 static void __check_get_privilege_description_result(privilege_info_error_e expected_result, privilege_info_error_e result, const char* description)
89 {
90         printf("expected result = %s, result = %s\n", __get_result_string(expected_result), __get_result_string(result));
91
92         if (expected_result != result) {
93                 printf("not matched\n");
94                 __change_color_to_red();
95                 printf("test fail\n");
96                 fail_cnt++;
97         } else {
98            printf("matched\n");
99            if (result == PRVINFO_ERROR_NONE) {
100                         if (description == NULL) {
101                                 printf("description must not be NULL\n");
102                                 __change_color_to_red();
103                                 printf("test fail\n");
104                                 fail_cnt++;
105                                 __change_color_to_origin();
106                                 return;
107                         } else {
108                                 printf("description = %s\n", description);
109                         }
110                 } else {
111                         if (description != NULL) {
112                                 printf("description = %s\n", description);
113                                 printf("description must be NULL\n");
114                                 __change_color_to_red();
115                                 printf("test fail\n");
116                                 fail_cnt++;
117                                 __change_color_to_origin();
118                                 return;
119                         }
120                 }
121
122                 __change_color_to_green();
123                 printf("test success\n");
124                 success_cnt++;
125         }
126         __change_color_to_origin();
127 }
128
129 static void __test_privilege_info_get_display_name()
130 {
131         int ret;
132         char* display_name = NULL;
133
134         printf("-----------------------------------------------------------\n");
135         printf("api_version : 2.3\n");
136         printf("privilege : http://tizen.org/privilege/window.priority.set\n");
137         printf("expected result : PRVINFO_ERROR_NONE\n");
138         ret = privilege_info_get_display_name("2.3", "http://tizen.org/privilege/window.priority.set", &display_name);
139         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
140
141         printf("-----------------------------------------------------------\n");
142         printf("api_version : 2.3\n");
143         printf("privilege : http://tizen.org/privilege/mediacapture\n");
144         printf("expected result : PRVINFO_ERROR_NONE\n");
145         if (display_name != NULL) {
146                 free(display_name);
147                 display_name = NULL;
148         }
149         ret = privilege_info_get_display_name("2.3", "http://tizen.org/privilege/mediacapture", &display_name);
150         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
151
152         printf("-----------------------------------------------------------\n");
153         printf("Not existing privilege\n");
154         printf("api_version : 2.3\n");
155         printf("privilege : http://tizen.org/privilege/RRRRRRRRRR\n");
156         printf("expected result : PRVINFO_ERROR_NONE\n");
157         if (display_name != NULL) {
158                 free(display_name);
159                 display_name = NULL;
160         }
161         ret = privilege_info_get_display_name("2.3", "http://tizen.org/privilege/RRRRRRRRRR", &display_name);
162         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
163
164         printf("-----------------------------------------------------------\n");
165         printf("Invalid parameter\n");
166         printf("api_version : NULL\n");
167         printf("privilege : http://tizen.org/privilege/window.priority.set\n");
168         printf("expected result : PRVINFO_ERROR_NONE\n");
169         if (display_name != NULL) {
170                 free(display_name);
171                 display_name = NULL;
172         }
173         ret = privilege_info_get_display_name(NULL, "http://tizen.org/privilege/window, priority.set", &display_name);
174         __check_get_privilege_display_name_result(PRVINFO_ERROR_INVALID_PARAMETER, ret, display_name);
175
176         free(display_name);
177 }
178 static void __test_privilege_info_get_display_name_by_pkgtype()
179 {
180         int ret;
181         char* display_name = NULL;
182
183         printf("-----------------------------------------------------------\n");
184         printf("pkgtype : PRVINFO_PACKAGE_TYPE_NATIVE\n");
185         printf("api_version : 2.3\n");
186         printf("privilege : http://tizen.org/privilege/window.priority.set\n");
187         printf("expected result : PRVINFO_ERROR_NONE\n");
188         ret = privilege_info_get_display_name_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", "http://tizen.org/privilege/window.priority.set", &display_name);
189         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
190
191         printf("-----------------------------------------------------------\n");
192         printf("pkgtype : PRVINFO_PACKAGE_TYPE_WEB\n");
193         printf("api_version : 2.3\n");
194         printf("privilege : http://tizen.org/privilege/mediacapture\n");
195         printf("expected result : PRVINFO_ERROR_NONE\n");
196         if (display_name != NULL) {
197                 free(display_name);
198                 display_name = NULL;
199         }
200         ret = privilege_info_get_display_name_by_pkgtype("PRVINFO_PACKAGE_TYPE_WEB", "2.3", "http://tizen.org/privilege/mediacapture", &display_name);
201         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
202
203         printf("-----------------------------------------------------------\n");
204         printf("Mismatched package type: write WEB as NATIVE\n");
205         printf("pkgtype : PRVINFO_PACKAGE_TYPE_NATIVE\n");
206         printf("api_version : 2.3\n");
207         printf("privilege : http://tizen.org/privilege/mediacapture\n");
208         printf("expected result : PRVINFO_ERROR_NONE\n");
209         if (display_name != NULL) {
210                 free(display_name);
211                 display_name = NULL;
212         }
213         ret = privilege_info_get_display_name_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", "http://tizen.org/privilege/mediacapture", &display_name);
214         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
215
216
217         printf("-----------------------------------------------------------\n");
218         printf("Not existing privilege\n");
219         printf("api_version : 2.3\n");
220         printf("privilege : http://tizen.org/privilege/RRRRRRRRRR\n");
221         printf("expected result : PRVINFO_ERROR_NONE\n");
222         if (display_name != NULL) {
223                 free(display_name);
224                 display_name = NULL;
225         }
226         ret = privilege_info_get_display_name_by_pkgtype("PRVINFO_PACKAGE_TYPE_WEB", "2.3", "http://tizen.org/privilege/RRRRRRRRRR", &display_name);
227         __check_get_privilege_display_name_result(PRVINFO_ERROR_NONE, ret, display_name);
228
229         printf("-----------------------------------------------------------\n");
230         printf("Invalid parameter\n");
231         printf("api_version : 2.3\n");
232         printf("privilege : http://tizen.org/privilege/mediacapture\n");
233         printf("expected result : PRVINFO_ERROR_NONE\n");
234         if (display_name != NULL) {
235                 free(display_name);
236                 display_name = NULL;
237         }
238         ret = privilege_info_get_display_name_by_pkgtype(NULL, "2.3", "http://tizen.org/privilege/mediacapture", &display_name);
239         __check_get_privilege_display_name_result(PRVINFO_ERROR_INVALID_PARAMETER, ret, display_name);
240
241         free(display_name);
242 }
243
244 static void __test_privilege_info_get_description()
245 {
246         int ret;
247         char* description = NULL;
248
249         printf("-----------------------------------------------------------\n");
250         printf("api_version : 2.3\n");
251         printf("privilege : http://tizen.org/privilege/window.priority.set\n");
252         printf("expected result : PRVINFO_ERROR_NONE\n");
253         ret = privilege_info_get_description("2.3", "http://tizen.org/privilege/window.priority.set", &description);
254         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
255
256         printf("-----------------------------------------------------------\n");
257         printf("api_version : 2.3\n");
258         printf("privilege : http://tizen.org/privilege/mediacapture\n");
259         printf("expected result : PRVINFO_ERROR_NONE\n");
260         if (description != NULL) {
261                 free(description);
262                 description = NULL;
263         }
264         ret = privilege_info_get_description("2.3", "http://tizen.org/privilege/mediacapture", &description);
265         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
266
267         printf("-----------------------------------------------------------\n");
268         printf("Not existing privilege\n");
269         printf("api_version : 2.3\n");
270         printf("privilege : http://tizen.org/privilege/RRRRRRRRRR\n");
271         printf("expected result : PRVINFO_ERROR_NONE\n");
272         if (description != NULL) {
273                 free(description);
274                 description = NULL;
275         }
276         ret = privilege_info_get_description("2.3", "http://tizen.org/privilege/RRRRRRRRRR", &description);
277         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
278
279         printf("-----------------------------------------------------------\n");
280         printf("Invalid parameter\n");
281         printf("api_version : 2.3\n");
282         printf("privilege : http://tizen.org/privilege/mediacapture\n");
283         printf("expected result : PRVINFO_ERROR_NONE\n");
284         if (description != NULL) {
285                 free(description);
286                 description = NULL;
287         }
288         ret = privilege_info_get_description(NULL, "http://tizen.org/privilege/mediacapture", &description);
289         __check_get_privilege_description_result(PRVINFO_ERROR_INVALID_PARAMETER, ret, description);
290
291         free(description);
292 }
293
294 static void __test_privilege_info_get_description_by_pkgtype()
295 {
296         int ret;
297         char* description = NULL;
298
299         printf("-----------------------------------------------------------\n");
300         printf("api_version : 2.3\n");
301         printf("privilege : http://tizen.org/privilege/window.priority.set\n");
302         printf("expected result : PRVINFO_ERROR_NONE\n");
303         ret = privilege_info_get_description_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", "http://tizen.org/privilege/window.priority.set", &description);
304         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
305
306         printf("-----------------------------------------------------------\n");
307         printf("api_version : 2.3\n");
308         printf("privilege : http://tizen.org/privilege/mediacapture\n");
309         printf("expected result : PRVINFO_ERROR_NONE\n");
310         if (description != NULL) {
311                 free(description);
312                 description = NULL;
313         }
314         ret = privilege_info_get_description_by_pkgtype("PRVINFO_PACKAGE_TYPE_WEB", "2.3", "http://tizen.org/privilege/mediacapture", &description);
315         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
316
317         printf("-----------------------------------------------------------\n");
318         printf("Mismatched package type: write WEB as NATIVE\n");
319         printf("pkgtype : PRVINFO_PACKAGE_TYPE_NATIVE\n");
320         printf("api_version : 2.3\n");
321         printf("privilege : http://tizen.org/privilege/mediacapture\n");
322         printf("expected result : PRVINFO_ERROR_NONE\n");
323         if (description != NULL) {
324                 free(description);
325                 description = NULL;
326         }
327         ret = privilege_info_get_description_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", "http://tizen.org/privilege/mediacapture", &description);
328         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
329
330         printf("-----------------------------------------------------------\n");
331         printf("Not existing privilege\n");
332         printf("api_version : 2.3\n");
333         printf("privilege : http://tizen.org/privilege/RRRRRRRRRR\n");
334         printf("expected result : PRVINFO_ERROR_NONE\n");
335         if (description != NULL) {
336                 free(description);
337                 description = NULL;
338         }
339         ret = privilege_info_get_description_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", "http://tizen.org/privilege/RRRRRRRRRR", &description);
340         __check_get_privilege_description_result(PRVINFO_ERROR_NONE, ret, description);
341
342         printf("-----------------------------------------------------------\n");
343         printf("Invalid parameter\n");
344         printf("api_version : 2.3\n");
345         printf("privilege : NULL\n");
346         printf("expected result : PRVINFO_ERROR_INVALID_PARAMETER\n");
347         if (description != NULL) {
348                 free(description);
349                 description = NULL;
350         }
351         ret = privilege_info_get_description_by_pkgtype("PRVINFO_PACKAGE_TYPE_NATIVE", "2.3", NULL, &description);
352         __check_get_privilege_description_result(PRVINFO_ERROR_INVALID_PARAMETER, ret, description);
353
354         free(description);
355 }
356
357 int main()
358 {
359         __change_color_to_yellow();
360         printf("Test function : privilege_info_get_display_name\n");
361         __change_color_to_origin();
362         __test_privilege_info_get_display_name();
363
364         __change_color_to_yellow();
365         printf("Test function : privilege_info_get_display_name_by_pkgtype\n");
366         __change_color_to_origin();
367         __test_privilege_info_get_display_name_by_pkgtype();
368
369         __change_color_to_yellow();
370         printf("Test function : privilege_info_get_description\n");
371         __change_color_to_origin();
372         __test_privilege_info_get_description();
373
374         __change_color_to_yellow();
375         printf("Test function : privilege_info_get_description_by_pkgtype\n");
376         __change_color_to_origin();
377         __test_privilege_info_get_description_by_pkgtype();
378
379         __change_color_to_green();
380         printf("Test Complete\n");
381         printf("success : %d, ", success_cnt);
382         __change_color_to_red();
383         printf("fail : %d\n", fail_cnt);
384         __change_color_to_origin();
385
386 }