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