Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / libprivilege-control-tests / common / duplicates.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15 */
16
17 /*
18  * @file        libprivilege-control_test_duplicates.cpp
19  * @author      Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
20  * @version     1.0
21  * @brief       libprivilege-control private functions duplicates
22  */
23
24 #include <stdlib.h>
25 #include <iri.h>
26 #include <algorithm>
27 #include <string>
28 #include <set>
29 #include <sys/smack.h>
30 #ifndef _XOPEN_SOURCE
31 #define _XOPEN_SOURCE
32 #endif
33 #include <unistd.h>
34 #include "duplicates.h"
35
36 std::string app_type_name(app_type_t app_type)
37 {
38     switch(app_type)
39     {
40         case APP_TYPE_WGT:
41             return "WRT";
42         case APP_TYPE_OSP:
43             return "OSP";
44         case APP_TYPE_EFL:
45             return "EFL";
46         default:
47             return "";
48     }
49 }
50
51 std::string app_type_group_name(app_type_t app_type)
52 {
53     switch (app_type)
54     {
55         case APP_TYPE_WGT:
56             return "WRT";
57         case APP_TYPE_OSP:
58             return "OSP";
59         case APP_TYPE_EFL:
60             return "EFL";
61         default:
62             return "";
63     }
64 }
65
66
67 /*
68  * This function changes permission URI to basename for file name.
69  * For e.g. from http://tizen.org/privilege/contact.read will be
70  * created basename : org.tizen.privilege.contact.read
71  */
72 int base_name_from_perm(const char *perm, std::string& name)
73 {
74     iri_t *iris = iri_parse(perm);
75     if (iris == nullptr || iris->host == nullptr)
76     {
77         iri_destroy(iris);
78         return PC_ERR_INVALID_PARAM;
79     }
80
81     std::string host_dot;
82     std::string host;
83     std::string path;
84     std::string::size_type pos;
85
86     if (iris->path == nullptr)
87     {
88         path = iris->host;
89     }
90     else
91     {
92         path = iris->path;
93         host = iris->host;
94         pos = host.rfind('.');
95         if (pos != std::string::npos)
96         {
97             host_dot = host.substr(pos + 1) + ".";
98             host = host.substr(0, pos);
99         }
100     }
101
102     iri_destroy(iris);
103
104     std::replace(path.begin(), path.end(), '/', '.');
105
106     name = host_dot + host + path;
107
108     return PC_OPERATION_SUCCESS;
109 }