efc603fde26ddbfad54e3be2545ccc6a0ac09b16
[platform/upstream/csr-framework.git] / test / test-resource.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  * @file        test-resource.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       Test resoure name and paths
21  */
22 #include "test-resource.h"
23
24 #include <vector>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28
29 #include "test-common.h"
30
31 namespace {
32
33 std::string s_wgtAppRootCache;
34 std::string s_wgtMalFileCache;
35 std::string s_tpkAppRootCache;
36 std::string s_tpkMalFileCache;
37
38 #ifdef PLATFORM_VERSION_3
39 std::string getUsername(void)
40 {
41         struct passwd pwd;
42         struct passwd *result = nullptr;
43
44         auto bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
45         bufsize = (bufsize == -1) ? 16384 : bufsize;
46
47         std::vector<char> buf(bufsize, 0);
48
49         ::getpwuid_r(::getuid(), &pwd, buf.data(), buf.size(), &result);
50         if (result == nullptr)
51                 throw std::logic_error("Failed to getpwuid_r with errno: " + errno);
52
53         return std::string(pwd.pw_name);
54 }
55 #endif
56
57 } // namespace anonymous
58
59 const std::string &TEST_WGT_APP_ROOT(void)
60 {
61         if (s_wgtAppRootCache.empty())
62 #ifdef PLATFORM_VERSION_3
63                 s_wgtAppRootCache = "/home/" + ::getUsername() + "/apps_rw/" TEST_WGT_PKG_ID;
64 #else
65                 s_wgtAppRootCache = TEST_DIR_APPS "/" TEST_WGT_PKG_ID;
66 #endif
67
68         return s_wgtAppRootCache;
69 }
70
71 const std::string &TEST_WGT_MAL_FILE(void)
72 {
73         if (s_wgtMalFileCache.empty())
74                 s_wgtMalFileCache = TEST_WGT_APP_ROOT() + "/res/wgt/data/malicious.txt";
75
76         return s_wgtMalFileCache;
77 }
78
79 const std::string &TEST_TPK_APP_ROOT(void)
80 {
81         if (s_tpkAppRootCache.empty())
82 #ifdef PLATFORM_VERSION_3
83                 s_tpkAppRootCache = "/home/" + ::getUsername() + "/apps_rw/" TEST_TPK_PKG_ID;
84 #else
85                 s_tpkAppRootCache = TEST_DIR_APPS "/" TEST_TPK_PKG_ID;
86 #endif
87
88         return s_tpkAppRootCache;
89 }
90
91 const std::string &TEST_TPK_MAL_FILE(void)
92 {
93         if (s_tpkMalFileCache.empty())
94                 s_tpkMalFileCache = TEST_TPK_APP_ROOT() + "/shared/res/malicious.txt";
95
96         return s_tpkMalFileCache;
97 }