Rename /tests to /ckm to align with tizen branch
[platform/core/test/security-tests.git] / src / security-server-tests / security_server_mockup.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  */
4 /*
5  * @file    security_server_mockup.cpp
6  * @author  Bartlomiej Grzelewski (b.grzelewski@samsung.com)
7  * @version 1.0
8  * @brief   All mockups required in security-server tests.
9  */
10
11 #include <stdio.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <fcntl.h>
17 #include <sys/un.h>
18 #include <unistd.h>
19 #include <poll.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <security-server.h>
23
24 #include <dpl/log/log.h>
25
26 #define SECURITY_SERVER_TEST_SOCK_PATH "/tmp/.security_server_sock_mockup"
27
28 /* Create a Unix domain socket and bind */
29 int create_new_socket()
30 {
31     int localsockfd = -1, flags;
32     struct sockaddr_un serveraddr;
33     mode_t sock_mode;
34
35     if (-1 == remove(SECURITY_SERVER_TEST_SOCK_PATH)) {
36         LogDebug("Unable to remove " << SECURITY_SERVER_TEST_SOCK_PATH);
37     }
38
39     /* Create Unix domain socket */
40     if ((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
41     {
42         localsockfd = -1;
43         LogDebug("Socket creation failed");
44         goto error;
45     }
46
47     /* Make socket as non blocking */
48     if ((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
49         fcntl(localsockfd, F_SETFL, flags) < 0)
50     {
51         close(localsockfd);
52         localsockfd = -1;
53         LogDebug("Cannot go to nonblocking mode");
54         goto error;
55     }
56
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);
61
62     /* Bind the socket */
63     if ((bind(localsockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr))) < 0)
64     {
65         LogDebug("Cannot bind");
66         close(localsockfd);
67         localsockfd = -1;
68         goto error;
69     }
70
71     /* Change permission to accept all processes that has different uID/gID */
72     sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
73
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 */
78     {
79         LogDebug("chmod() error");
80         close(localsockfd);
81         localsockfd = -1;
82         goto error;
83     }
84 error:
85     return localsockfd;
86 }
87
88 int connect_to_testserver()
89 {
90     struct sockaddr_un clientaddr;
91     int client_len = 0, localsockfd;
92
93     /* Create a socket */
94     if ((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
95     {
96         LogDebug("Error on socket. Errno: " << errno);
97         return -1;
98     }
99
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)
106     {
107         LogDebug("Error on connect. Errno: " << errno);
108         close(localsockfd);
109         return -1;
110     }
111     return localsockfd;
112 }
113