Fix test-resource and permissions on platform v3.0
[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_testDirMedia;
34 std::string s_testDirApps;
35
36 std::string s_testFileMedia;
37 std::string s_testFileInDirMalwares;
38
39 std::string s_wgtAppRoot;
40 std::string s_wgtMalFile;
41 std::string s_tpkAppRoot;
42 std::string s_tpkMalFile;
43 std::string s_fakeAppRoot;
44 std::string s_fakeAppFile;
45
46 #ifdef PLATFORM_VERSION_3
47 std::string getUsername(void)
48 {
49         struct passwd pwd;
50         struct passwd *result = nullptr;
51
52         auto bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
53         bufsize = (bufsize == -1) ? 16384 : bufsize;
54
55         std::vector<char> buf(bufsize, 0);
56
57         ::getpwuid_r(::getuid(), &pwd, buf.data(), buf.size(), &result);
58         if (result == nullptr)
59                 throw std::logic_error("Failed to getpwuid_r with errno: " + errno);
60
61         return std::string(pwd.pw_name);
62 }
63 #endif
64
65 } // namespace anonymous
66
67 const char *TEST_DIR_MEDIA(void)
68 {
69         if (s_testDirMedia.empty())
70 #ifdef PLATFORM_VERSION_3
71                 s_testDirMedia = "/home/" + ::getUsername() + "/content";
72 #else
73                 s_testDirMedia = "/opt/usr/media";
74 #endif
75
76         return s_testDirMedia.c_str();
77 }
78
79 const char *TEST_DIR_APPS(void)
80 {
81         if (s_testDirApps.empty())
82 #ifdef PLATFORM_VERSION_3
83                 s_testDirApps = "/home/" + ::getUsername() + "/apps_rw";
84 #else
85                 s_testDirApps = "/opt/usr/apps";
86 #endif
87
88         return s_testDirApps.c_str();
89 }
90
91 const char *TEST_FILE_MEDIA(void)
92 {
93         if (s_testFileMedia.empty())
94                 s_testFileMedia = std::string(TEST_DIR_MEDIA()) + "/test_malware_file";
95
96         return s_testFileMedia.c_str();
97 }
98
99 const char *TEST_WGT_APP_ROOT(void)
100 {
101         if (s_wgtAppRoot.empty())
102                 s_wgtAppRoot = std::string(TEST_DIR_APPS()) + "/" TEST_WGT_PKG_ID;
103
104         return s_wgtAppRoot.c_str();
105 }
106
107 const char *TEST_WGT_MAL_FILE(void)
108 {
109         if (s_wgtMalFile.empty())
110                 s_wgtMalFile = std::string(TEST_WGT_APP_ROOT()) + "/res/wgt/data/eicar.txt";
111
112         return s_wgtMalFile.c_str();
113 }
114
115 const char *TEST_TPK_APP_ROOT(void)
116 {
117         if (s_tpkAppRoot.empty())
118                 s_tpkAppRoot = std::string(TEST_DIR_APPS()) + "/" TEST_TPK_PKG_ID;
119
120         return s_tpkAppRoot.c_str();
121 }
122
123 const char *TEST_TPK_MAL_FILE(void)
124 {
125         if (s_tpkMalFile.empty())
126                 s_tpkMalFile = std::string(TEST_TPK_APP_ROOT()) + "/shared/res/eicar.txt";
127
128         return s_tpkMalFile.c_str();
129 }
130
131 const char *TEST_FAKE_APP_ROOT(void)
132 {
133         if (s_fakeAppRoot.empty())
134                 s_fakeAppRoot = std::string(TEST_DIR_APPS()) + "/fake_app";
135
136         return s_fakeAppRoot.c_str();
137 }
138
139 const char *TEST_FAKE_APP_FILE(void)
140 {
141         if (s_fakeAppFile.empty())
142                 s_fakeAppFile = std::string(TEST_FAKE_APP_ROOT()) + "/malicious.txt";
143
144         return s_fakeAppFile.c_str();
145 }