Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / tzplatform.cpp
1 /*
2  * Copyright (c) 2016-2017 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 #include <pwd.h>
18 #include <sys/types.h>
19
20 #include <dpl/test/test_runner.h>
21 #include <memory.h>
22 #include <temp_test_user.h>
23 #include <tzplatform.h>
24
25 namespace TzPlatformConfig {
26
27 DEFINE_SMARTPTR(tzplatform_context_destroy, tzplatform_context, TzPlatformContextPtr);
28
29 std::string getEnv(enum tzplatform_variable id)
30 {
31     auto env = tzplatform_getenv(id);
32     RUNNER_ASSERT_MSG(env != nullptr, "Tzplatform-config returned null for " << id);
33     return env;
34 }
35
36 uid_t getGlobalUserId(void)
37 {
38     return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
39 }
40
41 uid_t getGlobalGroupId(void)
42 {
43     gid_t global_uid = getGlobalUserId();
44     errno = 0;
45     passwd* pw = getpwuid(global_uid);
46     RUNNER_ASSERT_ERRNO_MSG(pw, "getpwuid() failed.");
47     return pw->pw_gid;
48 }
49
50 const std::string appDirPath(const TemporaryTestUser &user, const std::string &appId,
51                              const std::string &pkgId)
52 {
53     return appDirPath(user.getUid()) + pkgId + "/" + appId;
54 }
55
56 const std::string appDirPath(uid_t uid)
57 {
58     struct tzplatform_context *tzCtxPtr = nullptr;
59
60     RUNNER_ASSERT(0 == tzplatform_context_create(&tzCtxPtr));
61     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
62
63     RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, uid),
64                       "Unable to set user with uid <" << uid << "> for tzplatform context");
65
66     const char *appDir = tzplatform_context_getenv(tzCtxPtr,
67                                 getGlobalUserId() == uid ? TZ_SYS_RW_APP : TZ_USER_APP);
68     RUNNER_ASSERT_MSG(nullptr != appDir,
69                       "tzplatform_context_getenv failed"
70                           << "for getting sys rw app of user with uid<" << uid << ">");
71
72     return std::string(appDir) + "/";
73 }
74
75 const std::string globalAppDir()
76 {
77     struct tzplatform_context *tzCtxPtr = nullptr;
78
79     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
80     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
81
82     const char *appDir = tzplatform_context_getenv(tzCtxPtr, TZ_SYS_RW_APP);
83     RUNNER_ASSERT_MSG(nullptr != appDir,
84                       "tzplatform_context_getenv failed for getting sys rw app");
85     return appDir;
86 }
87
88 const std::string extendedSdDir()
89 {
90     struct tzplatform_context *tzCtxPtr = nullptr;
91
92     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
93     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
94
95     const char *appDir = tzplatform_context_getenv(tzCtxPtr, TZ_SYS_EXTENDEDSD_APP);
96     RUNNER_ASSERT_MSG(nullptr != appDir,
97                       "tzplatform_context_getenv failed for getting sys extended sd");
98     return appDir;
99 }
100
101 const std::string extendedSdUserDir(uid_t uid)
102 {
103     struct tzplatform_context *tzCtxPtr = nullptr;
104
105     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
106     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
107
108     RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, uid),
109                       "Unable to set user with uid <" << uid << "> for tzplatform context");
110
111     const char *appDir = tzplatform_context_getenv(tzCtxPtr, TZ_USER_EXTENDEDSD_APP);
112     RUNNER_ASSERT_MSG(nullptr != appDir,
113                       "tzplatform_context_getenv failed for getting user extended sd");
114     return appDir;
115 }
116
117 const std::string getPath(enum tzplatform_variable id) {
118     struct tzplatform_context *tzCtxPtr = nullptr;
119
120     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
121     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
122
123     const char *path = tzplatform_context_getenv(tzCtxPtr, id);
124     RUNNER_ASSERT_MSG(nullptr != path,
125                           "tzplatform_context_getenv failed for getting " << static_cast<int>(id));
126     return path;
127 }
128
129 }
130