tizen 2.4 release
[framework/web/wrt-commons.git] / tests / utils / wrt_utility.cpp
1 /*
2  * Copyright (c) 2011 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    wrt_utility.cpp
18  * @author  Janusz Majnert (j.majnert@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for test cases for wrt_utility functions
21  */
22 #include <string>
23 #include <fstream>
24 #include <errno.h>
25 #include <pwd.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/log/wrt_log.h>
32
33 RUNNER_TEST_GROUP_INIT(DPL_WRT_UTILITY)
34
35 /*
36 Name: wrt_utility_WrtUtilJoinPaths
37 Description: join paths test
38 Expected: correctly used separator
39 */
40 RUNNER_TEST(wrt_utility_WrtUtilJoinPaths)
41 {
42     std::string result;
43
44     WrtUtilJoinPaths(result, "a/b/c/", "e/f/g.asd");
45     RUNNER_ASSERT(result == "a/b/c/e/f/g.asd");
46
47     WrtUtilJoinPaths(result, "/a/b/c", "/e/f/g/");
48     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
49
50     WrtUtilJoinPaths(result, "/a/b/c/", "/e/f/g/");
51     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
52
53     WrtUtilJoinPaths(result, "/a/b/c", "e/f/g/");
54     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
55 }
56
57 /**
58  * Create recursive path with specified permissions.
59  * Check if folders exist.
60  * Check if permissions are set.
61  */
62 RUNNER_TEST(wrt_utility_WrtUtilMakeDir)
63 {
64     struct stat st;
65     //First delete the dir if it exists
66     WrtUtilRemove("/tmp/test");
67     WrtUtilMakeDir("/tmp/test/1/2/3/4/5/6/7/8/9", 0755);
68     if (stat("/tmp/test/1/2/3/4/5/6/7/8/9", &st) == 0) {
69         RUNNER_ASSERT_MSG(st.st_mode & S_IRWXU,
70                           "read, write, execute/search by owner");
71         RUNNER_ASSERT_MSG(st.st_mode & S_IXGRP,
72                           "execute/search permission, group");
73         RUNNER_ASSERT_MSG(st.st_mode & S_IRGRP, "read permission, group");
74         RUNNER_ASSERT_MSG(!(st.st_mode & S_IWGRP),
75                           "NO write permission, group ");
76         RUNNER_ASSERT_MSG(st.st_mode & S_IXOTH,
77                           "execute/search permission, others");
78         RUNNER_ASSERT_MSG(st.st_mode & S_IROTH, "read permission, others");
79         RUNNER_ASSERT_MSG(!(st.st_mode & S_IWOTH),
80                           "NO write permission, others ");
81     } else {
82         RUNNER_ASSERT_MSG(false, "Cannot stat folder");
83     }
84 }
85
86 /**
87  * Create directory without permission to write.
88  */
89 RUNNER_TEST(wrt_utility_WrtUtilMakeDir_PermissionError)
90 {
91     if (0 == getuid()) {
92         int bufsize;
93         if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
94             RUNNER_ASSERT_MSG(false,
95                     "Getting an initial value suggested for the size of buffer failed.");
96
97         //Change UID to execute the test correctly
98         errno = 0;
99         char *buffer = new char[bufsize];
100         struct passwd p;
101         struct passwd *result = NULL;
102         int return_value = getpwnam_r("app", &p, buffer, bufsize, &result);
103         delete[] buffer;
104
105         if (return_value != 0 || !result) {
106             int error = errno;
107             RUNNER_ASSERT_MSG(false, "Getting app user UID failed: "
108                               << (error ==
109                                   0 ? "No error detected" : strerror(error)));
110         }
111         if (setuid(p.pw_uid) != 0) {
112             int error = errno;
113             RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
114                               << (error ==
115                                   0 ? "No error detected" : strerror(error)));
116         }
117     }
118     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test2/1",
119                                      0055) == false,
120                       "Creating directory '1' in /temp/test2/ should have failed");
121     //Going back to root UID
122     if (setuid(0) != 0) {
123         int error = errno;
124         WrtLogW("Changing back to root UID failed: %s",
125                    (error == 0 ? "No error detected" : strerror(error)));
126     }
127 }
128
129 /**
130  * Create directory with file inside.
131  * Check if file was removed with directory.
132  */
133 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
134     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
135                       "Could not set up directory for test");
136
137     std::ofstream file;
138     file.open("/tmp/test3/example.txt");
139     file.close();
140     struct stat tmp;
141     RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt", &tmp) == 0,
142                       "Couldn't create the test file");
143
144     WrtUtilRemove("/tmp/test3");
145     if (stat("/tmp/test3", &tmp) != 0) {
146         int error = errno;
147         RUNNER_ASSERT(error == ENOENT);
148         return;
149     }
150     RUNNER_ASSERT(false);
151 }
152
153 /**
154  * Try to remove not existing folder.
155  */
156 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
157 {
158     //First making sure the test dir doesn't exist
159     WrtUtilRemove("/tmp/NOT_EXISTING");
160
161     RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
162                       "Removing non existing directory returned success");
163 }
164
165 /*
166 Name: wrt_utility_WrtUtilFileExists
167 Description: tests file existence
168 Expected: existing file should be reported as existing
169 */
170 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
171 {
172     std::ofstream file;
173     file.open("/tmp/test_file1");
174     file.close();
175     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
176
177     WrtUtilRemove("/tmp/test_file1");
178     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
179 }
180
181 /*
182 Name: wrt_utility_WrtUtilDirExists
183 Description: tests directory existence
184 Expected: existing directory should be reported as existing
185 */
186 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
187 {
188     RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
189     RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);
190 }