Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #include <stddef.h>
32 : : #include <stdarg.h>
33 : : #include <string.h>
34 : : #include <setjmp.h>
35 : : #include <cmocka.h>
36 : : #include <errno.h>
37 : :
38 : : #include <sys/un.h>
39 : : #include <sys/socket.h>
40 : :
41 : : #include "util.h"
42 : :
43 : : struct mock_ipc_params
44 : : {
45 : : struct mock_select_params write_select_params;
46 : : tapdisk_message_t write_message;
47 : : int read_message_count;
48 : : struct mock_select_params *read_select_params;
49 : : tapdisk_message_t *read_message;
50 : : struct mock_read_params *read_params;
51 : : struct sockaddr_un saddr;
52 : : };
53 : :
54 : 12 : void initialise_select_params(struct mock_select_params *params)
55 : : {
56 : 27 : FD_ZERO(¶ms->readfds);
57 : 27 : FD_ZERO(¶ms->writefds);
58 : 27 : FD_ZERO(¶ms->exceptfds);
59 : 12 : }
60 : :
61 : 6 : struct mock_ipc_params *setup_ipc(char *ipc_socket_name, int ipc_socket_fd,
62 : : tapdisk_message_t *write_message,
63 : : tapdisk_message_t *read_message,
64 : : int read_message_count)
65 : : {
66 : : struct mock_ipc_params *ipc_params;
67 : : int id;
68 : :
69 : 6 : ipc_params = test_malloc(sizeof(struct mock_ipc_params));
70 : 6 : ipc_params->read_message_count = read_message_count;
71 : 6 : ipc_params->read_select_params = test_malloc(read_message_count * sizeof(struct mock_select_params));
72 : 6 : ipc_params->read_message = test_malloc(read_message_count * sizeof(tapdisk_message_t));
73 : 6 : ipc_params->read_params = test_malloc(read_message_count * sizeof(struct mock_read_params));
74 : :
75 : 6 : initialise_select_params(&(ipc_params->write_select_params));
76 : :
77 : 6 : memcpy(&(ipc_params->write_message), write_message, sizeof(tapdisk_message_t));
78 : :
79 : 6 : memset(&(ipc_params->saddr), 0, sizeof(ipc_params->saddr));
80 : 6 : ipc_params->saddr.sun_family = AF_UNIX;
81 : 6 : strcpy(ipc_params->saddr.sun_path, ipc_socket_name);
82 : :
83 : 6 : expect_value(__wrap_socket, domain, AF_UNIX);
84 : 6 : expect_value(__wrap_socket, type, SOCK_STREAM);
85 : 6 : expect_any(__wrap_socket, protocol);
86 : 6 : will_return(__wrap_socket, ipc_socket_fd);
87 : :
88 : 6 : expect_value(__wrap_connect, sockfd, ipc_socket_fd);
89 : 6 : expect_memory(__wrap_connect, addr,
90 : : &(ipc_params->saddr), sizeof(ipc_params->saddr));
91 : 6 : will_return(__wrap_connect, 0);
92 : :
93 : 6 : ipc_params->write_select_params.result = 1;
94 [ - + ][ # # ]: 6 : FD_SET(ipc_socket_fd, &(ipc_params->write_select_params.writefds));
95 : 6 : expect_any(__wrap_select, timeout);
96 : 6 : will_return(__wrap_select, &(ipc_params->write_select_params));
97 : :
98 : 6 : expect_value(__wrap_write, fd, ipc_socket_fd);
99 : 6 : expect_memory(__wrap_write, buf,
100 : : &(ipc_params->write_message), sizeof(tapdisk_message_t));
101 : 6 : will_return(__wrap_write, 1024);
102 : :
103 : : /* Add Read responses */
104 [ + + ]: 15 : for (id = 0; id < read_message_count; id++) {
105 : 9 : initialise_select_params(&(ipc_params->read_select_params[id]));
106 : 9 : ipc_params->read_select_params[id].result = 1;
107 [ - + ][ # # ]: 9 : FD_SET(ipc_socket_fd, &(ipc_params->read_select_params[id].readfds));
108 : 9 : expect_any(__wrap_select, timeout);
109 : 9 : will_return(__wrap_select, &(ipc_params->read_select_params[id]));
110 : :
111 : 9 : memcpy(&(ipc_params->read_message[id]),
112 : 9 : &(read_message[id]),
113 : : sizeof(tapdisk_message_t));
114 : :
115 : 9 : ipc_params->read_params[id].result = sizeof(*read_message);
116 : 9 : ipc_params->read_params[id].data = &(ipc_params->read_message[id]);
117 : 9 : expect_value(__wrap_read, fd, ipc_socket_fd);
118 : 9 : will_return(__wrap_read, &(ipc_params->read_params[id]));
119 : : }
120 : :
121 : 6 : expect_value(__wrap_close, fd, ipc_socket_fd);
122 : 6 : will_return(__wrap_close, 0);
123 : :
124 : 6 : return ipc_params;
125 : : }
126 : :
127 : 6 : void free_ipc_params(struct mock_ipc_params *params)
128 : : {
129 : 6 : test_free(params->read_message);
130 : 6 : test_free(params->read_select_params);
131 : 6 : test_free(params->read_params);
132 : 6 : test_free(params);
133 : 6 : }
134 : :
|