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