2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file wrt_utility.cpp
18 * @author Janusz Majnert (j.majnert@samsung.com)
20 * @brief Implementation file for test cases for wrt_utility functions
27 #include <sys/types.h>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <dpl/log/log.h>
33 RUNNER_TEST_GROUP_INIT(DPL_WRT_UTILITY)
36 Name: wrt_utility_WrtUtilJoinPaths
37 Description: join paths test
38 Expected: correctly used separator
40 RUNNER_TEST(wrt_utility_WrtUtilJoinPaths)
44 WrtUtilJoinPaths(result, "a/b/c/", "e/f/g.asd");
45 RUNNER_ASSERT(result == "a/b/c/e/f/g.asd");
47 WrtUtilJoinPaths(result, "/a/b/c", "/e/f/g/");
48 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
50 WrtUtilJoinPaths(result, "/a/b/c/", "/e/f/g/");
51 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
53 WrtUtilJoinPaths(result, "/a/b/c", "e/f/g/");
54 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
58 * Create recursive path with specified permissions.
59 * Check if folders exist.
60 * Check if permissions are set.
62 RUNNER_TEST(wrt_utility_WrtUtilMakeDir)
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 ");
82 RUNNER_ASSERT_MSG(false, "Cannot stat folder");
87 * Create directory without permission to write.
89 RUNNER_TEST(wrt_utility_WrtUtilMakeDir_PermissionError)
92 //Change UID to execute the test correctly
94 struct passwd *p = getpwnam("app");
97 RUNNER_ASSERT_MSG(false, "Getting app user UID failed: "
99 0 ? "No error detected" : strerror(error)));
101 if (setuid(p->pw_uid) != 0) {
103 RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
105 0 ? "No error detected" : strerror(error)));
108 RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test2/1",
110 "Creating directory '1' in /temp/test2/ should have failed");
111 //Going back to root UID
112 if (setuid(0) != 0) {
114 LogWarning("Changing back to root UID failed: "
115 << (error == 0 ? "No error detected" : strerror(error)));
120 * Create directory with file inside.
121 * Check if file was removed with directory.
123 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
124 RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
125 "Could not set up directory for test");
128 file.open("/tmp/test3/example.txt");
131 RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt", &tmp) == 0,
132 "Couldn't create the test file");
134 WrtUtilRemove("/tmp/test3");
135 if (stat("/tmp/test3", &tmp) != 0) {
137 RUNNER_ASSERT(error == ENOENT);
140 RUNNER_ASSERT(false);
144 * Try to remove not existing folder.
146 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
148 //First making sure the test dir doesn't exist
149 WrtUtilRemove("/tmp/NOT_EXISTING");
151 RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
152 "Removing non existing directory returned success");
156 Name: wrt_utility_WrtUtilFileExists
157 Description: tests file existence
158 Expected: existing file should be reported as existing
160 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
163 file.open("/tmp/test_file1");
165 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
167 WrtUtilRemove("/tmp/test_file1");
168 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
172 Name: wrt_utility_WrtUtilDirExists
173 Description: tests directory existence
174 Expected: existing directory should be reported as existing
176 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
178 RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
179 RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);