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