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: "
94 0 ? "No error detected" : strerror(error)));
96 if (setuid(p->pw_uid) != 0) {
98 RUNNER_ASSERT_MSG(false, "Changing to app user's UID failed: "
100 0 ? "No error detected" : strerror(error)));
103 RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test2/1",
105 "Creating directory '1' in /temp/test2/ should have failed");
106 //Going back to root UID
107 if (setuid(0) != 0) {
109 LogWarning("Changing back to root UID failed: "
110 << (error == 0 ? "No error detected" : strerror(error)));
115 * Create directory with file inside.
116 * Check if file was removed with directory.
118 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir) {
119 RUNNER_ASSERT_MSG(WrtUtilMakeDir("/tmp/test3/", 0755) == true,
120 "Could not set up directory for test");
123 file.open("/tmp/test3/example.txt");
126 RUNNER_ASSERT_MSG(stat("/tmp/test3/example.txt", &tmp) == 0,
127 "Couldn't create the test file");
129 WrtUtilRemove("/tmp/test3");
130 if (stat("/tmp/test3", &tmp) != 0) {
132 RUNNER_ASSERT(error == ENOENT);
135 RUNNER_ASSERT(false);
139 * Try to remove not existing folder.
141 RUNNER_TEST(wrt_utility_WrtUtilRemoveDir_NoDirError)
143 //First making sure the test dir doesn't exist
144 WrtUtilRemove("/tmp/NOT_EXISTING");
146 RUNNER_ASSERT_MSG(WrtUtilRemove("/tmp/NOT_EXISTING") == false,
147 "Removing non existing directory returned success");
150 RUNNER_TEST(wrt_utility_WrtUtilFileExists)
153 file.open("/tmp/test_file1");
155 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1"));
157 WrtUtilRemove("/tmp/test_file1");
158 RUNNER_ASSERT(WrtUtilFileExists("/tmp/test_file1") == false);
161 RUNNER_TEST(wrt_utility_WrtUtilDirExists)
163 RUNNER_ASSERT(WrtUtilDirExists("/tmp"));
164 RUNNER_ASSERT(WrtUtilDirExists("/UNAVAILABLE_DIR") == false);