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)
35 RUNNER_TEST(wrt_utility_WrtUtilJoinPaths)
39 WrtUtilJoinPaths(result, "a/b/c/", "e/f/g.asd");
40 RUNNER_ASSERT(result == "a/b/c/e/f/g.asd");
42 WrtUtilJoinPaths(result, "/a/b/c", "/e/f/g/");
43 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
45 WrtUtilJoinPaths(result, "/a/b/c/", "/e/f/g/");
46 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
48 WrtUtilJoinPaths(result, "/a/b/c", "e/f/g/");
49 RUNNER_ASSERT(result == "/a/b/c/e/f/g/");
53 * Create recursive path with specified permissions.
54 * Check if folders exist.
55 * Check if permissions are set.
57 RUNNER_TEST(wrt_utility_WrtUtilMakeDir)
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 ");
77 RUNNER_ASSERT_MSG(false, "Cannot stat folder");
82 * Create directory without permission to write.
84 RUNNER_TEST(wrt_utility_WrtUtilMakeDir_PermissionError)
87 //Change UID to execute the test correctly
89 struct passwd *p = getpwnam("app");
92 RUNNER_ASSERT_MSG(false, "Getting app user UID failed: "
93 << (error == 0 ? "No error detected" : strerror(error)));
95 if (setuid(p->pw_uid) != 0) {
97 RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
98 << (error == 0 ? "No error detected" : strerror(error)));
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) {
106 LogWarning("Changing back to root UID failed: "
107 << (error == 0 ? "No error detected" : strerror(error)));
113 * Create directory with file inside.
114 * Check if file was removed with directory.
116 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
117 RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
118 "Could not set up directory for test");
121 file.open("/tmp/test3/example.txt");
124 RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt",&tmp) == 0,
125 "Couldn't create the test file");
127 WrtUtilRemove("/tmp/test3");
128 if (stat("/tmp/test3", &tmp) != 0) {
130 RUNNER_ASSERT(error == ENOENT);
133 RUNNER_ASSERT(false);
137 * Try to remove not existing folder.
139 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
141 //First making sure the test dir doesn't exist
142 WrtUtilRemove("/tmp/NOT_EXISTING");
144 RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
145 "Removing non existing directory returned success");
148 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
151 file.open("/tmp/test_file1");
153 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
155 WrtUtilRemove("/tmp/test_file1");
156 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
159 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
161 RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
162 RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);