Fixed buildrequires dukgenerator to dukgenerator-devel
[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/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         //Change UID to execute the test correctly
93         errno = 0;
94         struct passwd *p = getpwnam("app");
95         if (p == NULL) {
96             int error = errno;
97             RUNNER_ASSERT_MSG(false, "Getting app user UID failed: "
98                               << (error ==
99                                   0 ? "No error detected" : strerror(error)));
100         }
101         if (setuid(p->pw_uid) != 0) {
102             int error = errno;
103             RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
104                               << (error ==
105                                   0 ? "No error detected" : strerror(error)));
106         }
107     }
108     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test2/1",
109                                      0055) == false,
110                       "Creating directory '1' in /temp/test2/ should have failed");
111     //Going back to root UID
112     if (setuid(0) != 0) {
113         int error = errno;
114         LogWarning("Changing back to root UID failed: "
115                    << (error == 0 ? "No error detected" : strerror(error)));
116     }
117 }
118
119 /**
120  * Create directory with file inside.
121  * Check if file was removed with directory.
122  */
123 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
124     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
125                       "Could not set up directory for test");
126
127     std::ofstream file;
128     file.open("/tmp/test3/example.txt");
129     file.close();
130     struct stat tmp;
131     RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt", &tmp) == 0,
132                       "Couldn't create the test file");
133
134     WrtUtilRemove("/tmp/test3");
135     if (stat("/tmp/test3", &tmp) != 0) {
136         int error = errno;
137         RUNNER_ASSERT(error == ENOENT);
138         return;
139     }
140     RUNNER_ASSERT(false);
141 }
142
143 /**
144  * Try to remove not existing folder.
145  */
146 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
147 {
148     //First making sure the test dir doesn't exist
149     WrtUtilRemove("/tmp/NOT_EXISTING");
150
151     RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
152                       "Removing non existing directory returned success");
153 }
154
155 /*
156 Name: wrt_utility_WrtUtilFileExists
157 Description: tests file existence
158 Expected: existing file should be reported as existing
159 */
160 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
161 {
162     std::ofstream file;
163     file.open("/tmp/test_file1");
164     file.close();
165     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
166
167     WrtUtilRemove("/tmp/test_file1");
168     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
169 }
170
171 /*
172 Name: wrt_utility_WrtUtilDirExists
173 Description: tests directory existence
174 Expected: existing directory should be reported as existing
175 */
176 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
177 {
178     RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
179     RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);
180 }