Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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 RUNNER_TEST(wrt_utility_WrtUtilJoinPaths)
36 {
37     std::string result;
38
39     WrtUtilJoinPaths(result, "a/b/c/", "e/f/g.asd");
40     RUNNER_ASSERT(result == "a/b/c/e/f/g.asd");
41
42     WrtUtilJoinPaths(result, "/a/b/c", "/e/f/g/");
43     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
44
45     WrtUtilJoinPaths(result, "/a/b/c/", "/e/f/g/");
46     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
47
48     WrtUtilJoinPaths(result, "/a/b/c", "e/f/g/");
49     RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
50 }
51
52 /**
53  * Create recursive path with specified permissions.
54  * Check if folders exist.
55  * Check if permissions are set.
56  */
57 RUNNER_TEST(wrt_utility_WrtUtilMakeDir)
58 {
59     struct stat st;
60     //First delete the dir if it exists
61     WrtUtilRemove("/tmp/test");
62     WrtUtilMakeDir("/tmp/test/1/2/3/4/5/6/7/8/9", 0755);
63     if (stat("/tmp/test/1/2/3/4/5/6/7/8/9", &st) == 0) {
64         RUNNER_ASSERT_MSG(st.st_mode & S_IRWXU,
65                 "read, write, execute/search by owner");
66         RUNNER_ASSERT_MSG(st.st_mode & S_IXGRP,
67                 "execute/search permission, group");
68         RUNNER_ASSERT_MSG(st.st_mode & S_IRGRP, "read permission, group");
69         RUNNER_ASSERT_MSG(!(st.st_mode & S_IWGRP),
70                 "NO write permission, group ");
71         RUNNER_ASSERT_MSG(st.st_mode & S_IXOTH,
72                 "execute/search permission, others");
73         RUNNER_ASSERT_MSG(st.st_mode & S_IROTH, "read permission, others");
74         RUNNER_ASSERT_MSG(!(st.st_mode & S_IWOTH),
75                 "NO write permission, others ");
76     } else {
77         RUNNER_ASSERT_MSG(false, "Cannot stat folder");
78     }
79 }
80
81 /**
82  * Create directory without permission to write.
83  */
84 RUNNER_TEST(wrt_utility_WrtUtilMakeDir_PermissionError)
85 {
86     if (0 == getuid()) {
87         //Change UID to execute the test correctly
88         errno = 0;
89         struct passwd *p = getpwnam("app");
90         if (p == NULL) {
91             int error = errno;
92             RUNNER_ASSERT_MSG(false, "Getting app user UID failed: "
93                     << (error == 0 ? "No error detected" : strerror(error)));
94         }
95         if (setuid(p->pw_uid) != 0) {
96             int error = errno;
97             RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
98                     << (error == 0 ? "No error detected" : strerror(error)));
99         }
100     }
101     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test2/1", 0055) == false,
102             "Creating directory '1' in /temp/test2/ should have failed");
103     //Going back to root UID
104     if (setuid(0) != 0) {
105         int error = errno;
106         LogWarning("Changing back to root UID failed: "
107                 << (error == 0 ? "No error detected" : strerror(error)));
108     }
109
110 }
111
112 /**
113  * Create directory with file inside.
114  * Check if file was removed with directory.
115  */
116 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
117     RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
118             "Could not set up directory for test");
119
120     std::ofstream file;
121     file.open("/tmp/test3/example.txt");
122     file.close();
123     struct stat tmp;
124     RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt",&tmp) == 0,
125             "Couldn't create the test file");
126
127     WrtUtilRemove("/tmp/test3");
128     if (stat("/tmp/test3", &tmp) != 0) {
129         int error=errno;
130         RUNNER_ASSERT(error == ENOENT);
131         return;
132     }
133     RUNNER_ASSERT(false);
134 }
135
136 /**
137  * Try to remove not existing folder.
138  */
139 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
140 {
141     //First making sure the test dir doesn't exist
142     WrtUtilRemove("/tmp/NOT_EXISTING");
143
144     RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
145             "Removing non existing directory returned success");
146 }
147
148 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
149 {
150     std::ofstream file;
151     file.open("/tmp/test_file1");
152     file.close();
153     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
154
155     WrtUtilRemove("/tmp/test_file1");
156     RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
157 }
158
159 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
160 {
161     RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
162     RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);
163 }