2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
5 * @file security_server_mockup.cpp
6 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
8 * @brief All mockups required in security-server tests.
14 #include <sys/types.h>
15 #include <sys/param.h>
20 #include <sys/socket.h>
22 #include <security-server.h>
24 #include <dpl/log/log.h>
26 #define SECURITY_SERVER_TEST_SOCK_PATH "/tmp/.security_server_sock_mockup"
28 /* Create a Unix domain socket and bind */
29 int create_new_socket()
31 int localsockfd = -1, flags;
32 struct sockaddr_un serveraddr;
35 if (-1 == remove(SECURITY_SERVER_TEST_SOCK_PATH)) {
36 LogDebug("Unable to remove " << SECURITY_SERVER_TEST_SOCK_PATH);
39 /* Create Unix domain socket */
40 if ((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
43 LogDebug("Socket creation failed");
47 /* Make socket as non blocking */
48 if ((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
49 fcntl(localsockfd, F_SETFL, flags) < 0)
53 LogDebug("Cannot go to nonblocking mode");
57 bzero (&serveraddr, sizeof(serveraddr));
58 serveraddr.sun_family = AF_UNIX;
59 strncpy(serveraddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH,
60 strlen(SECURITY_SERVER_TEST_SOCK_PATH) + 1);
63 if ((bind(localsockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr))) < 0)
65 LogDebug("Cannot bind");
71 /* Change permission to accept all processes that has different uID/gID */
72 sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
74 /* Flawfinder hits this chmod function as level 5 CRITICAL as race condition flaw *
75 * * Flawfinder recommends to user fchmod insted of chmod
76 * * But, fchmod doesn't work on socket file so there is no other choice at this point */
77 if (chmod(SECURITY_SERVER_TEST_SOCK_PATH, sock_mode) < 0) /* Flawfinder: ignore */
79 LogDebug("chmod() error");
88 int connect_to_testserver()
90 struct sockaddr_un clientaddr;
91 int client_len = 0, localsockfd;
94 if ((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
96 LogDebug("Error on socket. Errno: " << errno);
100 bzero(&clientaddr, sizeof(clientaddr));
101 clientaddr.sun_family = AF_UNIX;
102 strncpy(clientaddr.sun_path, SECURITY_SERVER_TEST_SOCK_PATH, strlen(SECURITY_SERVER_TEST_SOCK_PATH));
103 clientaddr.sun_path[strlen(SECURITY_SERVER_TEST_SOCK_PATH)] = 0;
104 client_len = sizeof(clientaddr);
105 if (connect(localsockfd, (struct sockaddr*)&clientaddr, client_len) < 0)
107 LogDebug("Error on connect. Errno: " << errno);