--- /dev/null
+/*
+ * 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 uds.cpp
+ * @author Aleksander Zdyb <a.zdyb@samsung.com>
+ * @version 1.0
+ * @brief Helpers for Unix Domain Sockets
+ */
+
+#include <cstring>
+#include <poll.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <dpl/test/test_runner.h>
+#include <memory.h>
+
+#include "uds.h"
+
+namespace UDSHelpers {
+
+int createServer(const struct sockaddr_un *sockaddr) {
+ int sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed");
+
+ SockUniquePtr sockPtr(&sock);
+
+ int bindResult = bind(sock, (const struct sockaddr*) sockaddr, sizeof(struct sockaddr_un));
+ RUNNER_ASSERT_ERRNO_MSG(bindResult == 0, "bind failed");
+
+ int listenResult = listen(sock, 1);
+ RUNNER_ASSERT_ERRNO_MSG(listenResult == 0, "listen failed");
+
+ sockPtr.release();
+ return sock;
+}
+
+int createClient(const struct sockaddr_un *sockaddr) {
+ int sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ RUNNER_ASSERT_ERRNO_MSG(sock >= 0, "socket failed");
+
+ SockUniquePtr sockPtr(&sock);
+
+ int connectResult = TEMP_FAILURE_RETRY(
+ connect(sock, (const struct sockaddr*) sockaddr, sizeof(struct sockaddr_un)));
+ RUNNER_ASSERT_ERRNO_MSG(connectResult == 0, "connect failed");
+
+ sockPtr.release();
+ return sock;
+}
+
+int acceptClient(int sock) {
+ int clientSock = TEMP_FAILURE_RETRY(accept(sock, NULL, NULL));
+ RUNNER_ASSERT_ERRNO_MSG(clientSock >= 0, "accept failed");
+ return clientSock;
+}
+
+void waitForDisconnect(int sock) {
+ const nfds_t fdCount = 1;
+ const int timeout = -1; // no timeout
+
+ struct pollfd pfd { sock, POLLRDHUP, 0 };
+ int ret = TEMP_FAILURE_RETRY(poll(&pfd, fdCount, timeout));
+ RUNNER_ASSERT_ERRNO_MSG(ret >= 0, "poll failed");
+}
+
+struct sockaddr_un makeAbstractAddress(const std::string &path) {
+ struct sockaddr_un sockaddr;
+ RUNNER_ASSERT_MSG(path.size() <= sizeof(sockaddr.sun_path) - 1, "Socket path too long");
+ memset(&sockaddr, 0, sizeof(struct sockaddr_un));
+ sockaddr.sun_family = AF_UNIX;
+ // Leave '\0' as a first character of path
+ memcpy(sockaddr.sun_path + 1, path.c_str(), path.size());
+ return sockaddr;
+}
+
+} // namespace UDSHelpers
--- /dev/null
+/*
+ * 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 uds.h
+ * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @version 1.0
+ * @brief Helpers for Unix Domain Sockets
+ */
+
+#ifndef TESTS_COMMON_UDS_H_
+#define TESTS_COMMON_UDS_H_
+
+#include <string>
+#include <sys/un.h>
+
+namespace UDSHelpers {
+ int createServer(const struct sockaddr_un *sockaddr);
+ int createClient(const struct sockaddr_un *sockaddr);
+ int acceptClient(int sock);
+ void waitForDisconnect(int sock);
+ struct sockaddr_un makeAbstractAddress(const std::string &path);
+};
+
+#endif // TESTS_COMMON_UDS_H_