+/*
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ */
+/*
+ * @file security_server_mockup.cpp
+ * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
+ * @version 1.0
+ * @brief All mockups required in security-server tests.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <fcntl.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <poll.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <security-server.h>
+
+#include <dpl/log/log.h>
+
+#define SECURITY_SERVER_TEST_SOCK_PATH "/tmp/.security_server_sock_mockup"
+
+/* Create a Unix domain socket and bind */
+int create_new_socket()
+{
+ int localsockfd = -1, flags;
+ struct sockaddr_un serveraddr;
+ mode_t sock_mode;
+
+ if (-1 == remove(SECURITY_SERVER_TEST_SOCK_PATH)) {
+ LogDebug("Unable to remove " << SECURITY_SERVER_TEST_SOCK_PATH);
+ }
+
+ /* Create Unix domain socket */
+ if((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
+ {
+ localsockfd = -1;
+ LogDebug("Socket creation failed");
+ goto error;
+ }
+
+ /* Make socket as non blocking */
+ if((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
+ fcntl(localsockfd, F_SETFL, flags) < 0)
+ {
+ close(localsockfd);
+ localsockfd = -1;
+ LogDebug("Cannot go to nonblocking mode");
+ goto error;
+ }
+
+ bzero (&serveraddr, sizeof(serveraddr));
+ serveraddr.sun_family = AF_UNIX;
+ strncpy(serveraddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH,
+ strlen(SECURITY_SERVER_TEST_SOCK_PATH) + 1);
+
+ /* Bind the socket */
+ if((bind(localsockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
+ {
+ LogDebug("Cannot bind");
+ close(localsockfd);
+ localsockfd = -1;
+ goto error;
+ }
+
+ /* Change permission to accept all processes that has different uID/gID */
+ sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
+
+ /* Flawfinder hits this chmod function as level 5 CRITICAL as race condition flaw *
+ * * Flawfinder recommends to user fchmod insted of chmod
+ * * But, fchmod doesn't work on socket file so there is no other choice at this point */
+ if(chmod(SECURITY_SERVER_TEST_SOCK_PATH, sock_mode) < 0) /* Flawfinder: ignore */
+ {
+ LogDebug("chmod() error");
+ close(localsockfd);
+ localsockfd = -1;
+ goto error;
+ }
+error:
+ return localsockfd;
+}
+
+int connect_to_testserver()
+{
+ struct sockaddr_un clientaddr;
+ int client_len = 0, localsockfd;
+
+ /* Create a socket */
+ if((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+ {
+ LogDebug("Error on socket. Errno: " << errno);
+ return -1;
+ }
+
+ bzero(&clientaddr, sizeof(clientaddr));
+ clientaddr.sun_family = AF_UNIX;
+ strncpy(clientaddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH, strlen(SECURITY_SERVER_TEST_SOCK_PATH));
+ clientaddr.sun_path[strlen(SECURITY_SERVER_TEST_SOCK_PATH)] = 0;
+ client_len = sizeof(clientaddr);
+ if(connect(localsockfd, (struct sockaddr*)&clientaddr, client_len) < 0)
+ {
+ LogDebug("Error on connect. Errno: " << errno);
+ close(localsockfd);
+ return -1;
+ }
+ return localsockfd;
+}
+