-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
ipc.c
322 lines (253 loc) · 7.25 KB
/
ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stic.h>
#include <stddef.h> /* NULL */
#include <stdlib.h> /* free() malloc() */
#include <string.h> /* memset() strcmp() strdup() */
#include <test-utils.h>
#include "../../src/utils/str.h"
#include "../../src/utils/string_array.h"
#include "../../src/background.h"
#include "../../src/ipc.h"
static void test_ipc_args(char *args[]);
static void test_ipc_args2(char *args[]);
static void recursive_ipc_args(char *args[]);
static char * test_ipc_eval(const char expr[]);
static char * test_ipc_eval_error(const char expr[]);
static void other_instance(bg_op_t *bg_op, void *arg);
static int enabled_and_not_in_wine(void);
static int enabled_and_not_windows(void);
static const char NAME[] = "vifm-test";
static int nmessages;
static char *message;
static int nmessages2;
static char *message2;
static ipc_t *recursive_ipc;
TEARDOWN()
{
nmessages = 0;
nmessages2 = 0;
update_string(&message, NULL);
update_string(&message2, NULL);
}
TEST(destroy_null, IF(ipc_enabled))
{
ipc_free(NULL);
}
TEST(create_and_destroy, IF(ipc_enabled))
{
ipc_t *const ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
assert_non_null(ipc);
ipc_free(ipc);
}
TEST(name_can_be_null, IF(ipc_enabled))
{
ipc_t *const ipc = ipc_init(NULL, &test_ipc_args, &test_ipc_eval);
assert_non_null(ipc);
ipc_free(ipc);
}
TEST(name_is_taken_into_account, IF(ipc_enabled))
{
ipc_t *const ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
assert_string_starts_with(NAME, ipc_get_name(ipc));
ipc_free(ipc);
}
TEST(names_do_not_repeat, IF(enabled_and_not_in_wine))
{
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
assert_true(strcmp(ipc_get_name(ipc1), ipc_get_name(ipc2)) != 0);
ipc_free(ipc1);
ipc_free(ipc2);
}
TEST(instance_is_listed_when_it_exists, IF(enabled_and_not_in_wine))
{
int len;
ipc_t *const ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
char **const list = ipc_list(&len);
assert_true(is_in_string_array(list, len, ipc_get_name(ipc)));
free_string_array(list, len);
ipc_free(ipc);
}
TEST(instance_is_not_listed_after_it_is_freed, IF(ipc_enabled))
{
int len;
char **list;
ipc_t *const ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
char *const name = strdup(ipc_get_name(ipc));
ipc_free(ipc);
list = ipc_list(&len);
assert_false(is_in_string_array(list, len, name));
free_string_array(list, len);
free(name);
}
TEST(message_is_delivered, IF(enabled_and_not_in_wine))
{
char msg[] = "test message";
char *data[] = { msg, NULL };
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &test_ipc_args2, &test_ipc_eval);
assert_success(ipc_send(ipc1, ipc_get_name(ipc2), data));
assert_false(ipc_check(ipc1));
assert_true(ipc_check(ipc2));
assert_false(ipc_check(ipc2));
ipc_free(ipc1);
ipc_free(ipc2);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
assert_int_equal(2, nmessages2);
assert_string_equal(msg, message2);
}
TEST(large_message_is_delivered, IF(enabled_and_not_in_wine))
{
enum { LEN = 64*1024 };
char *msg = malloc(LEN + 1);
memset(msg, 'x', LEN - 1);
msg[LEN - 1] = '\0';
char *data[] = { msg, NULL };
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &test_ipc_args2, &test_ipc_eval);
assert_success(bg_execute("", "", 0, 1, &other_instance, ipc2));
assert_success(ipc_send(ipc1, ipc_get_name(ipc2), data));
assert_false(ipc_check(ipc1));
wait_for_bg();
ipc_free(ipc1);
ipc_free(ipc2);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
assert_int_equal(2, nmessages2);
assert_string_equal(msg, message2);
free(msg);
}
TEST(expr_is_evaluated, IF(enabled_and_not_in_wine))
{
const char expr[] = "good expression";
char *result;
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &test_ipc_args2, &test_ipc_eval);
assert_success(bg_execute("", "", 0, 1, &other_instance, ipc2));
result = ipc_eval(ipc1, ipc_get_name(ipc2), expr);
assert_false(ipc_check(ipc1));
wait_for_bg();
ipc_free(ipc1);
ipc_free(ipc2);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
assert_int_equal(0, nmessages2);
assert_string_equal(NULL, message2);
assert_string_equal("good result", result);
free(result);
}
TEST(eval_error_is_handled, IF(enabled_and_not_in_wine))
{
const char expr[] = "bad expression";
char *result;
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &test_ipc_args2, &test_ipc_eval_error);
assert_success(bg_execute("", "", 0, 1, &other_instance, ipc2));
result = ipc_eval(ipc1, ipc_get_name(ipc2), expr);
assert_false(ipc_check(ipc1));
wait_for_bg();
ipc_free(ipc1);
ipc_free(ipc2);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
assert_int_equal(0, nmessages2);
assert_string_equal(NULL, message2);
assert_string_equal(NULL, result);
free(result);
}
TEST(checking_ipc_from_ipc_handler_is_noop, IF(enabled_and_not_windows))
{
char msg[] = "test message";
char *data[] = { msg, NULL };
ipc_t *const ipc1 = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
ipc_t *const ipc2 = ipc_init(NAME, &recursive_ipc_args, &test_ipc_eval);
recursive_ipc = ipc2;
assert_success(ipc_send(ipc1, ipc_get_name(ipc2), data));
assert_success(ipc_send(ipc1, ipc_get_name(ipc2), data));
assert_true(ipc_check(ipc2));
assert_true(ipc_check(ipc2));
ipc_free(ipc1);
ipc_free(ipc2);
}
TEST(no_send_to_self, IF(enabled_and_not_in_wine))
{
char msg[] = "test message";
char *data[] = { msg, NULL };
ipc_t *ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
assert_failure(ipc_send(ipc, ipc_get_name(ipc), data));
assert_false(ipc_check(ipc));
ipc_free(ipc);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
}
TEST(no_eval_to_self, IF(enabled_and_not_in_wine))
{
const char expr[] = "good expression";
ipc_t *ipc = ipc_init(NAME, &test_ipc_args, &test_ipc_eval);
assert_string_equal(NULL, ipc_eval(ipc, ipc_get_name(ipc), expr));
assert_false(ipc_check(ipc));
ipc_free(ipc);
assert_int_equal(0, nmessages);
assert_string_equal(NULL, message);
}
static void
test_ipc_args(char *args[])
{
nmessages += count_strings(args);
update_string(&message, args[1]);
}
static void
test_ipc_args2(char *args[])
{
nmessages2 += count_strings(args);
update_string(&message2, args[1]);
}
static void
recursive_ipc_args(char *args[])
{
assert_false(ipc_check(recursive_ipc));
}
static char *
test_ipc_eval(const char expr[])
{
if(strcmp("good expression", expr) == 0)
{
return strdup("good result");
}
return NULL;
}
static char *
test_ipc_eval_error(const char expr[])
{
return NULL;
}
static void
other_instance(bg_op_t *bg_op, void *arg)
{
ipc_t *const ipc = arg;
while(!ipc_check(ipc))
{
/* Do nothing. */
}
}
static int
enabled_and_not_in_wine(void)
{
#ifndef _WIN32
return ipc_enabled();
#else
/* Apparently, WINE doesn't implement some things related to named pipes. */
return (ipc_enabled() && not_wine());
#endif
}
static int
enabled_and_not_windows(void)
{
#ifndef _WIN32
return ipc_enabled();
#else
return 0;
#endif
}
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */