Add PasswdAccess 45/32445/5
authorAleksander Zdyb <a.zdyb@samsung.com>
Tue, 18 Nov 2014 10:07:05 +0000 (11:07 +0100)
committerAleksander Zdyb <a.zdyb@samsung.com>
Wed, 11 Mar 2015 11:06:55 +0000 (12:06 +0100)
A wrapper on getpwnam() and getgrnam().

Change-Id: Ib833b6c1922ad45de3f713f29a904283d094600f

src/common/CMakeLists.txt
src/common/passwd_access.cpp [new file with mode: 0644]
src/common/passwd_access.h [new file with mode: 0644]

index 49c2d9f..8c35483 100644 (file)
@@ -21,6 +21,7 @@ SET(COMMON_TARGET_TEST_SOURCES
     ${PROJECT_SOURCE_DIR}/src/common/memory.cpp
     ${PROJECT_SOURCE_DIR}/src/common/db_sqlite.cpp
     ${PROJECT_SOURCE_DIR}/src/common/fs_label_manager.cpp
+    ${PROJECT_SOURCE_DIR}/src/common/passwd_access.cpp
     )
 
 #system and local includes
diff --git a/src/common/passwd_access.cpp b/src/common/passwd_access.cpp
new file mode 100644 (file)
index 0000000..6f55549
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/**
+ * @file        passwd_access.cpp
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Provides access to UID and GID
+ */
+
+#include <cerrno>
+#include <grp.h>
+#include <pwd.h>
+
+#include <dpl/test/test_runner.h>
+
+#include "passwd_access.h"
+
+namespace PasswdAccess {
+    uid_t uid(const std::string &username) {
+        struct passwd *passwd = nullptr;
+        do {
+            errno = 0;
+            passwd = getpwnam(username.c_str());
+        } while (passwd == nullptr && errno == EINTR);
+        RUNNER_ASSERT_ERRNO_MSG(passwd != nullptr, "Error in getpwnam(). Username: " << username);
+        return passwd->pw_uid;
+    }
+
+    gid_t gid(const std::string &groupname) {
+        struct group *group = nullptr;
+        do {
+            errno = 0;
+            group = getgrnam(groupname.c_str());
+        } while (group == nullptr && errno == EINTR);
+        RUNNER_ASSERT_ERRNO_MSG(group != nullptr, "Error in getgrnam(). Groupname: " << groupname);
+        return group->gr_gid;
+    }
+} // namespace PasswdAccess
diff --git a/src/common/passwd_access.h b/src/common/passwd_access.h
new file mode 100644 (file)
index 0000000..3638828
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/**
+ * @file        passwd_access.h
+ * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version     1.0
+ * @brief       Provides access to UID and GID
+ */
+
+#ifndef TESTS_COMMON_PASSWD_ACCESS_H_
+#define TESTS_COMMON_PASSWD_ACCESS_H_
+
+#include <string>
+#include <sys/types.h>
+
+namespace PasswdAccess {
+    uid_t uid(const std::string &username);
+    gid_t gid(const std::string &groupname);
+} // namespace PasswdAccess
+
+#endif // TESTS_COMMON_PASSWD_ACCESS_H_