8484cb92a39316c4dc1427243f6f06c88af5b259
[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 #include <tzplatform_config.h>
20
21 #include <dpl/test/test_runner.h>
22 #include <memory.h>
23 #include <temp_test_user.h>
24 #include <tzplatform.h>
25
26 namespace TzPlatformConfig {
27
28 DEFINE_SMARTPTR(tzplatform_context_destroy, tzplatform_context, TzPlatformContextPtr);
29
30 uid_t getGlobalUserId(void)
31 {
32     return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
33 }
34
35 uid_t getGlobalGroupId(void)
36 {
37     gid_t global_uid = getGlobalUserId();
38     errno = 0;
39     passwd* pw = getpwuid(global_uid);
40     RUNNER_ASSERT_ERRNO_MSG(pw, "getpwuid() failed.");
41     return pw->pw_gid;
42 }
43
44 const std::string appDirPath(const TemporaryTestUser &user, const std::string &appId,
45                              const std::string &pkgId)
46 {
47     struct tzplatform_context *tzCtxPtr = nullptr;
48
49     RUNNER_ASSERT(0 == tzplatform_context_create(&tzCtxPtr));
50     TzPlatformContextPtr tzCtxPtrSmart(tzCtxPtr);
51
52     RUNNER_ASSERT_MSG(0 == tzplatform_context_set_user(tzCtxPtr, user.getUid()),
53                       "Unable to set user <" << user.getUserName() << "> for tzplatform context");
54
55     const char *appDir = tzplatform_context_getenv(tzCtxPtr,
56                                 getGlobalUserId() == user.getUid() ? TZ_SYS_RW_APP : TZ_USER_APP);
57     RUNNER_ASSERT_MSG(nullptr != appDir,
58                       "tzplatform_context_getenv failed"
59                           << "for getting sys rw app of user <" << user.getUserName() << ">");
60
61     return std::string(appDir) + "/" + pkgId + "/" + appId;
62 }
63
64 }
65