Move label generation to global commons
[platform/core/test/security-tests.git] / src / security-manager-tests / common / tzplatform.cpp
1 /*
2  * Copyright (c) 2016 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 uid_t getGlobalUserId(void)
30 {
31     return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
32 }
33
34 uid_t getGlobalGroupId(void)
35 {
36     gid_t global_uid = getGlobalUserId();
37     errno = 0;
38     passwd* pw = getpwuid(global_uid);
39     RUNNER_ASSERT_ERRNO_MSG(pw, "getpwuid() failed.");
40     return pw->pw_gid;
41 }
42
43 const std::string appDirPath(const TemporaryTestUser &user, const std::string &appId,
44                              const std::string &pkgId)
45 {
46     return appDirPath(user.getUid()) + pkgId + "/" + appId;
47 }
48
49 const std::string appDirPath(uid_t uid)
50 {
51     struct tzplatform_context *tzCtxPtr = nullptr;
52
53     RUNNER_ASSERT(0 == tzplatform_context_create(&tzCtxPtr));
54     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
55
56     RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, uid),
57                       "Unable to set user with uid <" << uid << "> for tzplatform context");
58
59     const char *appDir = tzplatform_context_getenv(tzCtxPtr,
60                                 getGlobalUserId() == uid ? TZ_SYS_RW_APP : TZ_USER_APP);
61     RUNNER_ASSERT_MSG(nullptr != appDir,
62                       "tzplatform_context_getenv failed"
63                           << "for getting sys rw app of user with uid<" << uid << ">");
64
65     return std::string(appDir) + "/";
66 }
67
68 const std::string globalAppDir()
69 {
70     struct tzplatform_context *tzCtxPtr = nullptr;
71
72     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
73     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
74
75     const char *appDir = tzplatform_context_getenv(tzCtxPtr, TZ_SYS_RW_APP);
76     RUNNER_ASSERT_MSG(nullptr != appDir,
77                       "tzplatform_context_getenv failed for getting sys rw app");
78     return appDir;
79 }
80
81 const std::string getPath(enum tzplatform_variable id) {
82     struct tzplatform_context *tzCtxPtr = nullptr;
83
84     RUNNER_ASSERT_MSG(0 == tzplatform_context_create(&tzCtxPtr), "Couldn't create tzplatform context");
85     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
86
87     const char *path = tzplatform_context_getenv(tzCtxPtr, id);
88     RUNNER_ASSERT_MSG(nullptr != path,
89                           "tzplatform_context_getenv failed for getting " << static_cast<int>(id));
90     return path;
91 }
92
93 }
94