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