• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

proftpd / proftpd / 29116929668

10 Jul 2026 07:06PM UTC coverage: 92.419% (-0.6%) from 93.032%
29116929668

push

github

web-flow
AIX: Avoid `ip_len` macro collision with external headers such as libsodium

48763 of 52763 relevant lines covered (92.42%)

236.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.72
/tests/api/ctrls.c
1
/*
2
 * ProFTPD - FTP server testsuite
3
 * Copyright (c) 2020-2026 The ProFTPD Project team
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
17
 *
18
 * As a special exemption, The ProFTPD Project team and other respective
19
 * copyright holders give permission to link this program with OpenSSL, and
20
 * distribute the resulting executable, without including the source code for
21
 * OpenSSL in the source distribution.
22
 */
23

24
/* Controls API tests */
25

26
#include "tests.h"
27
#include "mod_ctrls.h"
28

29
#if defined(PR_USE_CTRLS)
30
static pool *p = NULL;
31

32
static const char *tmpfile_path = "/tmp/prt-ctrls.dat";
33

34
static void set_up(void) {
39✔
35
  (void) unlink(tmpfile_path);
39✔
36

37
  if (p == NULL) {
39✔
38
    p = permanent_pool = make_sub_pool(NULL);
39✔
39
  }
40

41
  if (getenv("TEST_VERBOSE") != NULL) {
39✔
42
    pr_trace_set_levels("ctrls", 1, 20);
39✔
43
    pr_trace_set_levels("json", 1, 20);
39✔
44
  }
45

46
  init_ctrls2("/tmp/test.sock");
39✔
47
}
39✔
48

49
static void tear_down(void) {
39✔
50
  if (getenv("TEST_VERBOSE") != NULL) {
39✔
51
    pr_trace_set_levels("ctrls", 0, 0);
39✔
52
    pr_trace_set_levels("json", 0, 0);
39✔
53
  }
54

55
  if (p != NULL) {
39✔
56
    destroy_pool(p);
39✔
57
    p = permanent_pool = NULL;
39✔
58
  }
59

60
  (void) unlink(tmpfile_path);
39✔
61
}
39✔
62

63
static int devnull_fd(void) {
6✔
64
  int fd;
65

66
  fd = open("/dev/null", O_RDWR);
6✔
67
  if (fd < 0) {
6✔
68
    fprintf(stderr, "Error opening /dev/null: %s\n", strerror(errno));
×
69
    return -1;
×
70
  }
71

72
  return fd;
73
}
74

75
static int tmpfile_fd(void) {
22✔
76
  int fd;
77

78
  fd = open(tmpfile_path, O_CREAT|O_RDWR, 0600);
44✔
79
  if (fd < 0) {
22✔
80
    fprintf(stderr, "Error opening %s: %s\n", tmpfile_path, strerror(errno));
×
81
    return -1;
×
82
  }
83

84
  (void) unlink(tmpfile_path);
22✔
85
  return fd;
22✔
86
}
87

88
static int reset_fd(int fd) {
89
  (void) close(fd);
14✔
90
  return tmpfile_fd();
14✔
91
}
92

93
static int rewind_fd(int fd) {
94
  if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
20✔
95
    return -1;
96
  }
97

98
  return 0;
99
}
100

101
/* Largely copied from ctrls.c */
102
static int connect_unix(int *fd) {
1✔
103
  int fds[2], res;
104

105
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
1✔
106
    return -1;
107
  }
108

109
  *fd = fds[1];
1✔
110
  return fds[0];
1✔
111
}
112

113
/* Largely copied from mod_ctrls. */
114
static int listen_unix(const char *path) {
1✔
115
  int fd = -1, socklen = 0;
1✔
116
  struct sockaddr_un sock;
117
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
118
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
119
  int opt = 1;
120
  socklen_t optlen = sizeof(opt);
121
#endif /* !LOCAL_CREDS */
122

123
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
1✔
124
  if (fd < 0) {
1✔
125
    fprintf(stderr, "error creating local socket: %s\n", strerror(errno));
×
126
    return -1;
×
127
  }
128

129
  (void) unlink(path);
1✔
130
  memset(&sock, 0, sizeof(sock));
1✔
131
  sock.sun_family = AF_UNIX;
1✔
132
  sstrncpy(sock.sun_path, path, sizeof(sock.sun_path));
1✔
133

134
  socklen = sizeof(sock);
1✔
135
  if (bind(fd, (struct sockaddr *) &sock, socklen) < 0) {
1✔
136
    fprintf(stderr, "error binding local socket to path '%s': %s\n", path,
×
137
      strerror(errno));
×
138
    (void) close(fd);
×
139
    return -1;
×
140
  }
141

142
  if (listen(fd, 5) < 0) {
1✔
143
    fprintf(stderr, "error listening on local socket: %s\n", strerror(errno));
×
144
    (void) close(fd);
×
145
    return -1;
×
146
  }
147

148
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
149
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
150
  /* Set the LOCAL_CREDS socket option. */
151
  if (setsockopt(fd, 0, LOCAL_CREDS, &opt, optlen) < 0) {
152
    fprintf(stderr, "error enabling LOCAL_CREDS: %s\n", strerror(errno));
153
  }
154
#endif /* !LOCAL_CREDS */
155

156
  return fd;
157
}
158

159
/* Tests */
160

161
START_TEST (ctrls_alloc_free_test) {
1✔
162
  int res;
163
  pr_ctrls_t *ctrl, *ctrl2, *ctrl3;
164

165
  mark_point();
1✔
166
  res = pr_ctrls_free(NULL);
1✔
167
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
168
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
169
    strerror(errno), errno);
170

171
  mark_point();
1✔
172
  ctrl = pr_ctrls_alloc();
1✔
173
  ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
2✔
174
  res = pr_ctrls_free(ctrl);
1✔
175
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
176

177
  mark_point();
1✔
178
  ctrl = pr_ctrls_alloc();
1✔
179
  ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
2✔
180
  res = pr_ctrls_free(ctrl);
1✔
181
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
182

183
  /* LIFO order */
184
  mark_point();
1✔
185
  ctrl = pr_ctrls_alloc();
1✔
186
  ctrl2 = pr_ctrls_alloc();
1✔
187
  ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
2✔
188
  ctrl2->ctrls_tmp_pool = make_sub_pool(p);
1✔
189
  ctrl3 = pr_ctrls_alloc();
1✔
190
  ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
2✔
191
  ctrl3->ctrls_tmp_pool = make_sub_pool(p);
1✔
192

193
  res = pr_ctrls_free(ctrl3);
1✔
194
  ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
2✔
195
  res = pr_ctrls_free(ctrl2);
1✔
196
  ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
2✔
197
  res = pr_ctrls_free(ctrl);
1✔
198
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
199

200
  /* FIFO order */
201
  mark_point();
1✔
202
  ctrl = pr_ctrls_alloc();
1✔
203
  ctrl2 = pr_ctrls_alloc();
1✔
204
  ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
2✔
205
  ctrl2->ctrls_tmp_pool = make_sub_pool(p);
1✔
206
  ctrl3 = pr_ctrls_alloc();
1✔
207
  ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
2✔
208
  ctrl3->ctrls_tmp_pool = make_sub_pool(p);
1✔
209

210
  res = pr_ctrls_free(ctrl);
1✔
211
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
212
  res = pr_ctrls_free(ctrl2);
1✔
213
  ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
2✔
214
  res = pr_ctrls_free(ctrl3);
1✔
215
  ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
2✔
216
}
217
END_TEST
1✔
218

219
START_TEST (ctrls_unregister_test) {
1✔
220
  int res;
221

222
  mark_point();
1✔
223
  res = pr_ctrls_unregister(NULL, NULL);
1✔
224
  ck_assert_msg(res < 0, "Failed to handle lack of registered actions");
1✔
225
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2✔
226
    strerror(errno), errno);
227
}
228
END_TEST
1✔
229

230
static int ctrls_test_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
1✔
231
  return 0;
1✔
232
}
233

234
static int ctrls_test2_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
×
235
  return 0;
×
236
}
237

238
START_TEST (ctrls_register_test) {
1✔
239
  int res;
240
  const char *action = NULL, *desc = NULL;
1✔
241
  module m;
242

243
  mark_point();
1✔
244
  res = pr_ctrls_register(NULL, NULL, NULL, NULL);
1✔
245
  ck_assert_msg(res < 0, "Failed to handle null action");
1✔
246
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
247
    strerror(errno), errno);
248

249
  mark_point();
1✔
250
  action = "test";
1✔
251
  res = pr_ctrls_register(NULL, action, NULL, NULL);
1✔
252
  ck_assert_msg(res < 0, "Failed to handle null desc");
2✔
253
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
254
    strerror(errno), errno);
255

256
  mark_point();
1✔
257
  desc = "desc";
1✔
258
  res = pr_ctrls_register(NULL, action, desc, NULL);
1✔
259
  ck_assert_msg(res < 0, "Failed to handle null callback");
2✔
260
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
261
    strerror(errno), errno);
262

263
  mark_point();
1✔
264
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
265
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
266

267
  mark_point();
1✔
268
  res = pr_ctrls_unregister(NULL, action);
1✔
269
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
270
    strerror(errno));
271

272
  mark_point();
1✔
273
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
274
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
275

276
  m.name = "test";
1✔
277
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
278
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
279

280
  mark_point();
1✔
281
  res = pr_ctrls_unregister(NULL, action);
1✔
282
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
283
    strerror(errno));
284
}
285
END_TEST
1✔
286

287
START_TEST (ctrls_add_arg_test) {
1✔
288
  int res;
289
  pr_ctrls_t *ctrl;
290
  char buf[4];
291
  size_t buflen;
292

293
  mark_point();
1✔
294
  res = pr_ctrls_add_arg(NULL, NULL, 0);
1✔
295
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
296
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
297
    strerror(errno), errno);
298

299
  mark_point();
1✔
300
  ctrl = pr_ctrls_alloc();
1✔
301
  res = pr_ctrls_add_arg(ctrl, NULL, 0);
1✔
302
  ck_assert_msg(res < 0, "Failed to handle null arg");
2✔
303
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
304
    strerror(errno), errno);
305

306
  mark_point();
1✔
307
  ctrl = pr_ctrls_alloc();
1✔
308

309
  /* Provide an arg that uses unprintable ASCII. */
310
  buf[0] = 'a';
1✔
311
  buf[1] = 'b';
1✔
312
  buf[2] = -120;
1✔
313
  buflen = 3;
1✔
314

315
  res = pr_ctrls_add_arg(ctrl, buf, buflen);
1✔
316
  ck_assert_msg(res < 0, "Failed to handle bad arg");
2✔
317
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
318
    strerror(errno), errno);
319

320
  mark_point();
1✔
321
  buf[0] = 'a';
1✔
322
  buf[1] = 'B';
1✔
323
  buf[2] = '1';
1✔
324
  buflen = 3;
1✔
325

326
  res = pr_ctrls_add_arg(ctrl, buf, buflen);
1✔
327
  ck_assert_msg(res == 0, "Failed to add ctrl arg: %s", strerror(errno));
2✔
328
}
329
END_TEST
1✔
330

331
START_TEST (ctrls_add_response_test) {
1✔
332
  int res;
333
  pr_ctrls_t *ctrl;
334

335
  mark_point();
1✔
336
  res = pr_ctrls_add_response(NULL, NULL);
1✔
337
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
338
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
339
    strerror(errno), errno);
340

341
  mark_point();
1✔
342
  ctrl = pr_ctrls_alloc();
1✔
343
  res = pr_ctrls_add_response(ctrl, NULL);
1✔
344
  ck_assert_msg(res < 0, "Failed to handle null fmt");
2✔
345
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
346
    strerror(errno), errno);
347

348
  mark_point();
1✔
349
  res = pr_ctrls_add_response(ctrl, "%s", "foo");
1✔
350
  ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
2✔
351
}
352
END_TEST
1✔
353

354
START_TEST (ctrls_copy_args_test) {
1✔
355
  int res;
356
  pr_ctrls_t *src_ctrl, *dst_ctrl;
357

358
  mark_point();
1✔
359
  res = pr_ctrls_copy_args(NULL, NULL);
1✔
360
  ck_assert_msg(res < 0, "Failed to handle null src ctrl");
1✔
361
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
362
    strerror(errno), errno);
363

364
  mark_point();
1✔
365
  src_ctrl = pr_ctrls_alloc();
1✔
366
  res = pr_ctrls_copy_args(src_ctrl, NULL);
1✔
367
  ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
2✔
368
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
369
    strerror(errno), errno);
370

371
  mark_point();
1✔
372
  res = pr_ctrls_copy_args(src_ctrl, src_ctrl);
1✔
373
  ck_assert_msg(res < 0, "Failed to handle same src/dst ctrl");
2✔
374
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
375
    strerror(errno), errno);
376

377
  mark_point();
1✔
378
  dst_ctrl = pr_ctrls_alloc();
1✔
379
  res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
1✔
380
  ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
2✔
381

382
  mark_point();
1✔
383
  res = pr_ctrls_add_arg(src_ctrl, "foo", 3);
1✔
384
  ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
2✔
385

386
  res = pr_ctrls_add_arg(src_ctrl, "bar", 3);
1✔
387
  ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
2✔
388

389
  res = pr_ctrls_add_arg(src_ctrl, "baz", 3);
1✔
390
  ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
2✔
391

392
  res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
1✔
393
  ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
2✔
394
}
395
END_TEST
1✔
396

397
START_TEST (ctrls_copy_resps_test) {
1✔
398
  int res;
399
  pr_ctrls_t *src_ctrl, *dst_ctrl;
400

401
  mark_point();
1✔
402
  res = pr_ctrls_copy_resps(NULL, NULL);
1✔
403
  ck_assert_msg(res < 0, "Failed to handle null src ctrl");
1✔
404
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
405
    strerror(errno), errno);
406

407
  mark_point();
1✔
408
  src_ctrl = pr_ctrls_alloc();
1✔
409
  res = pr_ctrls_copy_resps(src_ctrl, NULL);
1✔
410
  ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
2✔
411
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
412
    strerror(errno), errno);
413

414
  mark_point();
1✔
415
  res = pr_ctrls_copy_resps(src_ctrl, src_ctrl);
1✔
416
  ck_assert_msg(res < 0, "Failed to handle same src/dst ctrl");
2✔
417
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
418
    strerror(errno), errno);
419

420
  mark_point();
1✔
421
  dst_ctrl = pr_ctrls_alloc();
1✔
422
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
423
  ck_assert_msg(res < 0, "Failed to handle src ctrl with no responses");
2✔
424
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
425
    strerror(errno), errno);
426

427
  mark_point();
1✔
428
  res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
1✔
429
  ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
2✔
430

431
  res = pr_ctrls_add_response(dst_ctrl, "%s", "bar");
1✔
432
  ck_assert_msg(res == 0, "Failed to add dst ctrl response: %s", strerror(errno));
2✔
433

434
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
435
  ck_assert_msg(res < 0, "Failed to handle dst ctrl with responses");
2✔
436
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
437
    strerror(errno), errno);
438

439
  mark_point();
1✔
440
  src_ctrl = pr_ctrls_alloc();
1✔
441
  res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
1✔
442
  ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
2✔
443

444
  dst_ctrl = pr_ctrls_alloc();
1✔
445
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
446
  ck_assert_msg(res == 0, "Failed to copy ctrl responses: %s", strerror(errno));
2✔
447
}
448
END_TEST
1✔
449

450
START_TEST (ctrls_send_request_test) {
1✔
451
  int fd, res;
452
  char *action = "foo";
1✔
453
  unsigned int msgargc = 1;
1✔
454
  char *msgargv[] = { "bar", NULL };
1✔
455

456
  mark_point();
1✔
457
  res = pr_ctrls_send_request(NULL, -1, NULL, 0, NULL);
1✔
458
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
459
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
460
    strerror(errno), errno);
461

462
  mark_point();
1✔
463
  res = pr_ctrls_send_request(p, -1, NULL, 0, NULL);
1✔
464
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
465
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
466
    strerror(errno), errno);
467

468
  mark_point();
1✔
469
  fd = 7;
1✔
470
  res = pr_ctrls_send_request(p, fd, NULL, 0, NULL);
1✔
471
  ck_assert_msg(res < 0, "Failed to handle null action");
2✔
472
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
473
    strerror(errno), errno);
474

475
  mark_point();
1✔
476
  res = pr_ctrls_send_request(p, fd, action, msgargc, NULL);
1✔
477
  ck_assert_msg(res < 0, "Failed to handle mismatched argc/argv");
2✔
478
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
479
    strerror(errno), errno);
480

481
  mark_point();
1✔
482
  fd = 7777;
1✔
483
  res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
1✔
484
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
485
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
486
    strerror(errno), errno);
487

488
  fd = devnull_fd();
1✔
489
  if (fd < 0) {
1✔
490
    return;
×
491
  }
492

493
  mark_point();
1✔
494
  res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
1✔
495
  ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
1✔
496

497
  (void) close(fd);
1✔
498
}
499
END_TEST
500

501
START_TEST (ctrls_send_response_test) {
1✔
502
  int fd, res, status;
503
  unsigned int msgargc = 1;
1✔
504
  char *msgargv[] = { "foo", NULL };
1✔
505

506
  mark_point();
1✔
507
  res = pr_ctrls_send_response(NULL, -1, 0, 0, NULL);
1✔
508
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
509
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
510
    strerror(errno), errno);
511

512
  mark_point();
1✔
513
  res = pr_ctrls_send_response(p, -1, 0, 0, NULL);
1✔
514
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
515
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
516
    strerror(errno), errno);
517

518
  mark_point();
1✔
519
  fd = devnull_fd();
1✔
520
  if (fd < 0) {
1✔
521
    return;
×
522
  }
523
  res = pr_ctrls_send_response(p, fd, 0, 0, NULL);
1✔
524
  ck_assert_msg(res == 0, "Failed to send zero ctrl messages: %s",
1✔
525
    strerror(errno));
526

527
  mark_point();
1✔
528
  res = pr_ctrls_send_response(p, fd, 0, msgargc, NULL);
1✔
529
  ck_assert_msg(res < 0, "Failed to handle missing argv");
2✔
530
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
531
    strerror(errno), errno);
532
  (void) close(fd);
1✔
533

534
  mark_point();
1✔
535
  fd = 7777;
1✔
536
  status = -24;
1✔
537
  res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
1✔
538
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
539
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
540
    strerror(errno), errno);
541

542
  fd = devnull_fd();
1✔
543
  if (fd < 0) {
1✔
544
    return;
545
  }
546

547
  mark_point();
1✔
548
  status = -24;
1✔
549
  res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
1✔
550
  ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
1✔
551

552
  (void) close(fd);
1✔
553
}
554
END_TEST
555

556
START_TEST (ctrls_flush_response_test) {
1✔
557
  int res;
558
  pr_ctrls_t *ctrl;
559
  pr_ctrls_cl_t *cl;
560

561
  mark_point();
1✔
562
  res = pr_ctrls_flush_response(NULL);
1✔
563
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
564
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
565
    strerror(errno), errno);
566

567
  mark_point();
1✔
568
  ctrl = pr_ctrls_alloc();
1✔
569
  res = pr_ctrls_flush_response(ctrl);
1✔
570
  ck_assert_msg(res == 0, "Failed to flush ctrl with no responses: %s",
2✔
571
    strerror(errno));
572

573
  mark_point();
1✔
574
  res = pr_ctrls_add_response(ctrl, "%s", "foo");
1✔
575
  ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
2✔
576
  res = pr_ctrls_flush_response(ctrl);
1✔
577
  ck_assert_msg(res < 0, "Failed to handle ctrl with no client");
2✔
578
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
579
    strerror(errno), errno);
580

581
  mark_point();
1✔
582
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
583
  cl->cl_fd = devnull_fd();
1✔
584
  if (cl->cl_fd < 0) {
1✔
585
    return;
586
  }
587

588
  ctrl->ctrls_cl = cl;
1✔
589
  res = pr_ctrls_flush_response(ctrl);
1✔
590
  ck_assert_msg(res == 0, "Failed to flush ctrl responses: %s", strerror(errno));
1✔
591

592
  mark_point();
1✔
593
  (void) close(cl->cl_fd);
1✔
594
  res = pr_ctrls_flush_response(ctrl);
1✔
595
  ck_assert_msg(res < 0, "Failed to handle bad fd");
2✔
596
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
597
    strerror(errno), errno);
598
}
599
END_TEST
600

601
START_TEST (ctrls_recv_request_invalid_test) {
1✔
602
  int fd, res;
603
  uint32_t msglen;
604
  char *msg = NULL;
1✔
605
  pr_ctrls_cl_t *cl;
606

607
  mark_point();
1✔
608
  res = pr_ctrls_recv_request(NULL);
1✔
609
  ck_assert_msg(res < 0, "Failed to handle null client");
1✔
610
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
611
    strerror(errno), errno);
612

613
  mark_point();
1✔
614
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
615
  res = pr_ctrls_recv_request(cl);
1✔
616
  ck_assert_msg(res < 0, "Failed to handle client without ctrls list");
2✔
617
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
618
    strerror(errno), errno);
619

620
  mark_point();
1✔
621
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
622
  cl->cl_fd = -1;
1✔
623
  res = pr_ctrls_recv_request(cl);
1✔
624
  ck_assert_msg(res < 0, "Failed to handle client without fd");
2✔
625
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
626
    strerror(errno), errno);
627

628
  mark_point();
1✔
629
  fd = tmpfile_fd();
1✔
630
  if (fd < 0) {
1✔
631
    return;
×
632
  }
633

634
  cl->cl_fd = fd;
1✔
635
  (void) close(cl->cl_fd);
1✔
636
  res = pr_ctrls_recv_request(cl);
1✔
637
  ck_assert_msg(res < 0, "Failed to handle client with bad fd");
1✔
638
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
639
    strerror(errno), errno);
640

641
  mark_point();
1✔
642
  fd = tmpfile_fd();
1✔
643
  if (fd < 0) {
1✔
644
    return;
645
  }
646

647
  cl->cl_fd = fd;
1✔
648
  (void) write(fd, "a", 1);
1✔
649
  rewind_fd(fd);
1✔
650
  res = pr_ctrls_recv_request(cl);
1✔
651
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
1✔
652
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
653
    strerror(errno), errno);
654

655
  fd = reset_fd(fd);
1✔
656
  if (fd < 0) {
1✔
657
    return;
658
  }
659
  cl->cl_fd = fd;
1✔
660

661
  mark_point();
1✔
662
  msglen = 2;
1✔
663
  (void) write(fd, &msglen, sizeof(msglen));
1✔
664
  (void) write(fd, "a", 1);
1✔
665
  rewind_fd(fd);
1✔
666
  res = pr_ctrls_recv_request(cl);
1✔
667
  ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
1✔
668
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
669
    strerror(errno), errno);
670

671
  fd = reset_fd(fd);
1✔
672
  if (fd < 0) {
1✔
673
    return;
674
  }
675
  cl->cl_fd = fd;
1✔
676

677
  mark_point();
1✔
678
  msglen = 2;
1✔
679
  (void) write(fd, &msglen, sizeof(msglen));
1✔
680
  (void) write(fd, "aa", 2);
1✔
681
  rewind_fd(fd);
1✔
682
  res = pr_ctrls_recv_request(cl);
1✔
683
  ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
1✔
684
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
685
    strerror(errno), errno);
686

687
  fd = reset_fd(fd);
1✔
688
  if (fd < 0) {
1✔
689
    return;
690
  }
691
  cl->cl_fd = fd;
1✔
692

693
  mark_point();
1✔
694
  msg = "{}";
1✔
695
  msglen = strlen(msg);
1✔
696
  (void) write(fd, &msglen, sizeof(msglen));
1✔
697
  (void) write(fd, msg, msglen);
1✔
698
  rewind_fd(fd);
1✔
699
  res = pr_ctrls_recv_request(cl);
1✔
700
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'action')");
1✔
701
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
702
    strerror(errno), errno);
703

704
  fd = reset_fd(fd);
1✔
705
  if (fd < 0) {
1✔
706
    return;
707
  }
708
  cl->cl_fd = fd;
1✔
709

710
  mark_point();
1✔
711
  msg = "{\"action\":\"foo\"}";
1✔
712
  msglen = strlen(msg);
1✔
713
  (void) write(fd, &msglen, sizeof(msglen));
1✔
714
  (void) write(fd, msg, msglen);
1✔
715
  rewind_fd(fd);
1✔
716
  res = pr_ctrls_recv_request(cl);
1✔
717
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'args')");
1✔
718
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
719
    strerror(errno), errno);
720

721
  fd = reset_fd(fd);
1✔
722
  if (fd < 0) {
1✔
723
    return;
724
  }
725
  cl->cl_fd = fd;
1✔
726

727
  mark_point();
1✔
728
  msg = "{\"action\":\"test\",\"args\":[1,2,3,4,5,6,7,8,9,10]}";
1✔
729
  msglen = strlen(msg);
1✔
730
  (void) write(fd, &msglen, sizeof(msglen));
1✔
731
  (void) write(fd, msg, msglen);
1✔
732
  rewind_fd(fd);
1✔
733
  res = pr_ctrls_recv_request(cl);
1✔
734
  ck_assert_msg(res < 0, "Failed to handle unknown action");
1✔
735
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
736
    strerror(errno), errno);
737

738
  (void) close(fd);
1✔
739
}
740
END_TEST
741

742
START_TEST (ctrls_recv_request_too_large_test) {
1✔
743
  int fd, res;
744
  uint32_t msglen;
745
  pr_ctrls_cl_t *cl;
746

747
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
748
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
749

750
  mark_point();
1✔
751
  fd = tmpfile_fd();
1✔
752
  if (fd < 0) {
1✔
753
    return;
×
754
  }
755

756
  cl->cl_fd = fd;
1✔
757

758
  mark_point();
1✔
759
  msglen = (uint32_t) -1;
1✔
760
  (void) write(fd, (const void *) &msglen, sizeof(msglen));
1✔
761
  rewind_fd(fd);
1✔
762
  res = pr_ctrls_recv_request(cl);
1✔
763
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too long)");
1✔
764
  ck_assert_msg(errno == E2BIG, "Expected E2BIG (%d), got %s (%d)", E2BIG,
2✔
765
    strerror(errno), errno);
766

767
  (void) close(fd);
1✔
768
}
769
END_TEST
770

771
START_TEST (ctrls_recv_request_valid_test) {
1✔
772
  int fd, res;
773
  uint32_t msglen = 0;
1✔
774
  char *msg = NULL;
1✔
775
  const char *action, *desc;
776
  pr_ctrls_cl_t *cl;
777
  pr_ctrls_t *ctrl;
778
  module m;
779

780
  mark_point();
1✔
781
  m.name = "test";
1✔
782
  action = "test";
1✔
783
  desc = "desc";
1✔
784
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
785
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1✔
786

787
  mark_point();
1✔
788
  fd = tmpfile_fd();
1✔
789
  if (fd < 0) {
1✔
790
    return;
×
791
  }
792

793
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
794
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
795
  cl->cl_fd = fd;
1✔
796

797
  msg = "{\"action\":\"test\",\"args\":[]}";
1✔
798
  msglen = strlen(msg);
1✔
799
  (void) write(fd, &msglen, sizeof(msglen));
1✔
800
  (void) write(fd, msg, msglen);
1✔
801
  rewind_fd(fd);
1✔
802
  res = pr_ctrls_recv_request(cl);
1✔
803
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
804
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
805
    cl->cl_ctrls->nelts);
806
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
807
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
808
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
809
  ck_assert_msg(ctrl->ctrls_cb_args == NULL,
2✔
810
    "Expected no callback args, got %p", ctrl->ctrls_cb_args);
811

812
  mark_point();
1✔
813
  fd = reset_fd(fd);
1✔
814
  if (fd < 0) {
1✔
815
    return;
816
  }
817
  clear_array(cl->cl_ctrls);
1✔
818
  cl->cl_fd = fd;
1✔
819

820
  msg = "{\"action\":\"test\",\"args\":[\"a\"]}";
1✔
821
  msglen = strlen(msg);
1✔
822
  (void) write(fd, &msglen, sizeof(msglen));
1✔
823
  (void) write(fd, msg, msglen);
1✔
824
  rewind_fd(fd);
1✔
825
  res = pr_ctrls_recv_request(cl);
1✔
826
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
827
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
828
    cl->cl_ctrls->nelts);
829
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
830
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
831
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
832
  ck_assert_msg(ctrl->ctrls_cb_args != NULL,
2✔
833
    "Expected callback args, got %p", ctrl->ctrls_cb_args);
834

835
  mark_point();
1✔
836
  fd = reset_fd(fd);
1✔
837
  if (fd < 0) {
1✔
838
    return;
839
  }
840
  clear_array(cl->cl_ctrls);
1✔
841
  cl->cl_fd = fd;
1✔
842

843
  msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
1✔
844
  msglen = strlen(msg);
1✔
845
  (void) write(fd, &msglen, sizeof(msglen));
1✔
846
  (void) write(fd, msg, msglen);
1✔
847
  rewind_fd(fd);
1✔
848
  res = pr_ctrls_recv_request(cl);
1✔
849
  ck_assert_msg(res == 0, "Failed to handle valid request: %s",
1✔
850
    strerror(errno));
851
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
852
    cl->cl_ctrls->nelts);
853
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
854
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
855
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
856
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
857
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
858
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
859

860
  /* next_action present */
861

862
  mark_point();
1✔
863
  res = pr_ctrls_register(&m, action, desc, ctrls_test2_cb);
1✔
864
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
865

866
  mark_point();
1✔
867
  fd = reset_fd(fd);
1✔
868
  if (fd < 0) {
1✔
869
    return;
870
  }
871
  clear_array(cl->cl_ctrls);
1✔
872
  cl->cl_fd = fd;
1✔
873

874
  msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
1✔
875
  msglen = strlen(msg);
1✔
876
  (void) write(fd, &msglen, sizeof(msglen));
1✔
877
  (void) write(fd, msg, msglen);
1✔
878
  rewind_fd(fd);
1✔
879
  res = pr_ctrls_recv_request(cl);
1✔
880
  ck_assert_msg(res == 0, "Failed to handle valid request: %s",
1✔
881
    strerror(errno));
882
  ck_assert_msg(cl->cl_ctrls->nelts == 2, "Expected 2 ctrl, got %d",
2✔
883
    cl->cl_ctrls->nelts);
884

885
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
886
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
887
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
888
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
889
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
890
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
891

892
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[1];
1✔
893
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
894
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
895
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
896
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
897
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
898

899
  (void) pr_ctrls_unregister(&m, action);
1✔
900
  (void) close(fd);
1✔
901
}
902
END_TEST
903

904
START_TEST (ctrls_recv_response_test) {
1✔
905
  int fd, res, status;
906
  uint32_t msglen;
907
  char *msg = NULL;
1✔
908

909
  mark_point();
1✔
910
  res = pr_ctrls_recv_response(NULL, -1, NULL, NULL);
1✔
911
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
912
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
913
    strerror(errno), errno);
914

915
  mark_point();
1✔
916
  res = pr_ctrls_recv_response(p, -1, NULL, NULL);
1✔
917
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
918
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
919
    strerror(errno), errno);
920

921
  mark_point();
1✔
922
  fd = tmpfile_fd();
1✔
923
  if (fd < 0) {
1✔
924
    return;
×
925
  }
926

927
  res = pr_ctrls_recv_response(p, fd, NULL, NULL);
1✔
928
  ck_assert_msg(res < 0, "Failed to handle null status");
1✔
929
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
930
    strerror(errno), errno);
931
  (void) close(fd);
1✔
932

933
  mark_point();
1✔
934
  fd = tmpfile_fd();
1✔
935
  if (fd < 0) {
1✔
936
    return;
937
  }
938

939
  (void) write(fd, "a", 1);
1✔
940
  rewind_fd(fd);
1✔
941
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
942
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
1✔
943
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
944
    strerror(errno), errno);
945

946
  fd = reset_fd(fd);
1✔
947
  if (fd < 0) {
1✔
948
    return;
949
  }
950

951
  mark_point();
1✔
952
  msglen = 2;
1✔
953
  (void) write(fd, &msglen, sizeof(msglen));
1✔
954
  (void) write(fd, "a", 1);
1✔
955
  rewind_fd(fd);
1✔
956
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
957
  ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
1✔
958
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
959
    strerror(errno), errno);
960

961
  fd = reset_fd(fd);
1✔
962
  if (fd < 0) {
1✔
963
    return;
964
  }
965

966
  mark_point();
1✔
967
  msglen = 2;
1✔
968
  (void) write(fd, &msglen, sizeof(msglen));
1✔
969
  (void) write(fd, "aa", 2);
1✔
970
  rewind_fd(fd);
1✔
971
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
972
  ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
1✔
973
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
974
    strerror(errno), errno);
975

976
  fd = reset_fd(fd);
1✔
977
  if (fd < 0) {
1✔
978
    return;
979
  }
980

981
  mark_point();
1✔
982
  msg = "{}";
1✔
983
  msglen = strlen(msg);
1✔
984
  (void) write(fd, &msglen, sizeof(msglen));
1✔
985
  (void) write(fd, msg, msglen);
1✔
986
  rewind_fd(fd);
1✔
987
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
988
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'status')");
1✔
989
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
990
    strerror(errno), errno);
991

992
  fd = reset_fd(fd);
1✔
993
  if (fd < 0) {
1✔
994
    return;
995
  }
996

997
  mark_point();
1✔
998
  msg = "{\"status\":0}";
1✔
999
  msglen = strlen(msg);
1✔
1000
  (void) write(fd, &msglen, sizeof(msglen));
1✔
1001
  (void) write(fd, msg, msglen);
1✔
1002
  rewind_fd(fd);
1✔
1003
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
1004
  ck_assert_msg(res < 0,
1✔
1005
    "Failed to handle invalid message (missing 'responses')");
1006
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1007
    strerror(errno), errno);
1008

1009
  fd = reset_fd(fd);
1✔
1010
  if (fd < 0) {
1✔
1011
    return;
1012
  }
1013

1014
  mark_point();
1✔
1015
  msg = "{\"status\":0,\"responses\":[1]}";
1✔
1016
  msglen = strlen(msg);
1✔
1017
  (void) write(fd, &msglen, sizeof(msglen));
1✔
1018
  (void) write(fd, msg, msglen);
1✔
1019
  rewind_fd(fd);
1✔
1020
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
1021
  ck_assert_msg(res < 0,
1✔
1022
    "Failed to handle invalid message (non-text response)");
1023
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1024
    strerror(errno), errno);
1025

1026
  fd = reset_fd(fd);
1✔
1027
  if (fd < 0) {
1✔
1028
    return;
1029
  }
1030

1031
  mark_point();
1✔
1032
  msg = "{\"status\":0,\"responses\":[\"ok\"]}";
1✔
1033
  msglen = strlen(msg);
1✔
1034
  (void) write(fd, &msglen, sizeof(msglen));
1✔
1035
  (void) write(fd, msg, msglen);
1✔
1036
  rewind_fd(fd);
1✔
1037
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
1038
  ck_assert_msg(res == 1, "Failed to handle valid response: %s",
1✔
1039
    strerror(errno));
1040

1041
  (void) close(fd);
1✔
1042
}
1043
END_TEST
1044

1045
START_TEST (ctrls_recv_response_too_large_test) {
1✔
1046
  int fd, res, status;
1047
  uint32_t msglen;
1048

1049
  mark_point();
1✔
1050
  fd = tmpfile_fd();
1✔
1051
  if (fd < 0) {
1✔
1052
    return;
×
1053
  }
1054

1055
  mark_point();
1✔
1056
  msglen = (uint32_t) -1;
1✔
1057
  (void) write(fd, (const void *) &msglen, sizeof(msglen));
1✔
1058
  rewind_fd(fd);
1✔
1059
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
1060
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too long)");
1✔
1061
  ck_assert_msg(errno == E2BIG, "Expected E2BIG (%d), got %s (%d)", E2BIG,
2✔
1062
    strerror(errno), errno);
1063

1064
  (void) close(fd);
1✔
1065
}
1066
END_TEST
1067

1068
START_TEST (ctrls_issock_unix_test) {
1✔
1069
  int res;
1070
  mode_t mode;
1071

1072
  mark_point();
1✔
1073
  mode = 0;
1✔
1074
  res = pr_ctrls_issock_unix(mode);
1✔
1075
  ck_assert_msg(res < 0, "Failed to handle invalid mode");
1✔
1076
  ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2✔
1077
    strerror(errno), errno);
1078

1079
#if defined(S_ISFIFO)
1080
  mark_point();
1✔
1081
  mode = 0;
1✔
1082
  mode |= S_IFIFO;
1✔
1083
  res = pr_ctrls_issock_unix(mode);
1✔
1084
  if (res < 0) {
1✔
1085
    ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
1✔
1086
  }
1087
#endif /* S_ISFIFO */
1088

1089
#if defined(S_ISSOCK)
1090
  mark_point();
1✔
1091
  mode = 0;
1✔
1092
  mode |= S_IFSOCK;
1✔
1093
  res = pr_ctrls_issock_unix(mode);
1✔
1094
  if (res < 0) {
1✔
1095
    ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
×
1096
  }
1097
#endif /* S_ISSOCK */
1098
}
1099
END_TEST
1✔
1100

1101
START_TEST (ctrls_get_registered_actions_test) {
1✔
1102
  int res;
1103
  pr_ctrls_t *ctrl;
1104
  const char *action, *desc;
1105
  module m;
1106

1107
  mark_point();
1✔
1108
  res = pr_get_registered_actions(NULL, 0);
1✔
1109
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
1110
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1111
    strerror(errno), errno);
1112

1113
  mark_point();
1✔
1114
  ctrl = pr_ctrls_alloc();
1✔
1115
  res = pr_get_registered_actions(ctrl, 0);
1✔
1116
  ck_assert_msg(res == 0, "Failed to handle lack of registered actions: %s",
2✔
1117
    strerror(errno));
1118

1119
  mark_point();
1✔
1120
  pr_block_ctrls();
1✔
1121
  res = pr_get_registered_actions(ctrl, 0);
1✔
1122
  ck_assert_msg(res < 0, "Failed to handle blocked actions");
2✔
1123
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1124
    strerror(errno), errno);
1125
  pr_unblock_ctrls();
1✔
1126

1127
  mark_point();
1✔
1128
  action = "test";
1✔
1129
  desc = "desc";
1✔
1130
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1131
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1132

1133
  m.name = "test";
1✔
1134
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
1135
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1136

1137
  mark_point();
1✔
1138
  res = pr_get_registered_actions(ctrl, 0);
1✔
1139
  ck_assert_msg(res == 0, "Failed to handle invalid flags: %s", strerror(errno));
2✔
1140

1141
  mark_point();
1✔
1142
  res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ALL);
1✔
1143
  ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ALL flag: %s",
2✔
1144
    strerror(errno));
1145

1146
  mark_point();
1✔
1147
  res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ENABLED);
1✔
1148
  ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ENABLED flag: %s",
2✔
1149
    strerror(errno));
1150

1151
  mark_point();
1✔
1152
  res = pr_get_registered_actions(ctrl, CTRLS_GET_DESC);
1✔
1153
  ck_assert_msg(res == 2, "Failed to handle GET_DESC flag: %s", strerror(errno));
2✔
1154

1155
  mark_point();
1✔
1156
  res = pr_ctrls_unregister(NULL, action);
1✔
1157
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1158
    strerror(errno));
1159
}
1160
END_TEST
1✔
1161

1162
START_TEST (ctrls_set_registered_actions_test) {
1✔
1163
  int res;
1164
  const char *action, *desc;
1165

1166
  mark_point();
1✔
1167
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1168
  ck_assert_msg(res < 0, "Failed to handle no registered actions");
1✔
1169
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2✔
1170
    strerror(errno), errno);
1171

1172
  mark_point();
1✔
1173
  res = pr_set_registered_actions(NULL, NULL, FALSE, 24);
1✔
1174
  ck_assert_msg(res < 0, "Failed to handle invalid flag");
2✔
1175
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1176
    strerror(errno), errno);
1177

1178
  mark_point();
1✔
1179
  action = "test";
1✔
1180
  desc = "desc";
1✔
1181
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1182
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1183

1184
  mark_point();
1✔
1185
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1186
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
2✔
1187
    strerror(errno));
1188

1189
  mark_point();
1✔
1190
  pr_block_ctrls();
1✔
1191
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1192
  ck_assert_msg(res < 0, "Failed to handle blocked actions");
2✔
1193
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1194
    strerror(errno), errno);
1195
  pr_unblock_ctrls();
1✔
1196

1197
  mark_point();
1✔
1198
  res = pr_set_registered_actions(NULL, action, FALSE, 0);
1✔
1199
  ck_assert_msg(res == 0, "Failed to handle action '%s': %s", action,
2✔
1200
    strerror(errno));
1201

1202
  mark_point();
1✔
1203
  res = pr_set_registered_actions(NULL, "all", FALSE, 0);
1✔
1204
  ck_assert_msg(res == 0, "Failed to handle action 'all': %s", strerror(errno));
2✔
1205

1206
  mark_point();
1✔
1207
  res = pr_ctrls_unregister(NULL, action);
1✔
1208
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1209
    strerror(errno));
1210
}
1211
END_TEST
1✔
1212

1213
START_TEST (ctrls_check_actions_test) {
1✔
1214
  int res;
1215
  const char *action, *desc;
1216

1217
  mark_point();
1✔
1218
  res = pr_ctrls_check_actions();
1✔
1219
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
1✔
1220
    strerror(errno));
1221

1222
  mark_point();
1✔
1223
  action = "test";
1✔
1224
  desc = "desc";
1✔
1225
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1226
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1227

1228
  mark_point();
1✔
1229
  res = pr_ctrls_check_actions();
1✔
1230
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
2✔
1231
    strerror(errno));
1232

1233
  /* Register a duplicate action name, then check. */
1234

1235
  mark_point();
1✔
1236
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1237
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1238

1239
  mark_point();
1✔
1240
  res = pr_ctrls_check_actions();
1✔
1241
  ck_assert_msg(res == 0, "Failed to handle registered actions: %s",
2✔
1242
    strerror(errno));
1243

1244
  mark_point();
1✔
1245
  res = pr_set_registered_actions(NULL, action, FALSE, PR_CTRLS_ACT_SOLITARY);
1✔
1246
  ck_assert_msg(res == 0, "Failed to set SOLITARY action flag: %s",
2✔
1247
    strerror(errno));
1248

1249
  mark_point();
1✔
1250
  res = pr_ctrls_check_actions();
1✔
1251
  ck_assert_msg(res < 0, "Failed to handle duplicate SOLITARY actions");
2✔
1252
  ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
2✔
1253
    strerror(errno), errno);
1254

1255
  mark_point();
1✔
1256
  res = pr_ctrls_unregister(NULL, action);
1✔
1257
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1258
    strerror(errno));
1259
}
1260
END_TEST
1✔
1261

1262
START_TEST (ctrls_run_ctrls_test) {
1✔
1263
  int fd, res;
1264
  uint32_t msglen = 0;
1✔
1265
  char *msg = NULL;
1✔
1266
  const char *action, *desc;
1267
  pr_ctrls_cl_t *cl;
1268
  pr_ctrls_t *ctrl;
1269
  module m, m2;
1270
  time_t now;
1271

1272
  mark_point();
1✔
1273
  res = pr_run_ctrls(NULL, NULL);
1✔
1274
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1✔
1275

1276
  mark_point();
1✔
1277
  pr_block_ctrls();
1✔
1278
  res = pr_run_ctrls(NULL, NULL);
1✔
1279
  ck_assert_msg(res < 0, "Failed to handle blocked ctrls");
2✔
1280
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1281
    strerror(errno), errno);
1282
  pr_unblock_ctrls();
1✔
1283

1284
  mark_point();
1✔
1285
  action = "test";
1✔
1286
  desc = "desc";
1✔
1287
  m.name = "test";
1✔
1288
  m2.name = "test2";
1✔
1289
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
1290
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1291

1292
  mark_point();
1✔
1293
  res = pr_run_ctrls(NULL, NULL);
1✔
1294
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1295

1296
  mark_point();
1✔
1297
  res = pr_run_ctrls(&m2, NULL);
1✔
1298
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1299

1300
  mark_point();
1✔
1301
  res = pr_run_ctrls(&m, NULL);
1✔
1302
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1303

1304
  /* XXX TODO More test cases to fill in here. */
1305
  /* Note that pr_run_ctrls() makes a lot of assumptions about recv_response,
1306
   * recv_request having been called previously.  Not great.  How to deal
1307
   * with that?
1308
   */
1309

1310
  mark_point();
1✔
1311
  fd = tmpfile_fd();
1✔
1312
  if (fd < 0) {
1✔
1313
    return;
×
1314
  }
1315

1316
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
1317
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
1318
  cl->cl_fd = fd;
1✔
1319

1320
  msg = "{\"action\":\"test\",\"args\":[\"FOO\"]}";
1✔
1321
  msglen = strlen(msg);
1✔
1322
  (void) write(fd, &msglen, sizeof(msglen));
1✔
1323
  (void) write(fd, msg, msglen);
1✔
1324
  rewind_fd(fd);
1✔
1325
  res = pr_ctrls_recv_request(cl);
1✔
1326
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
1327
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
1328
    cl->cl_ctrls->nelts);
1329
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
1330

1331
  mark_point();
1✔
1332
  res = pr_run_ctrls(&m2, NULL);
1✔
1333
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1334

1335
  mark_point();
1✔
1336
  res = pr_run_ctrls(&m, NULL);
1✔
1337
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1338

1339
  mark_point();
1✔
1340
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1341
  ctrl->ctrls_flags |= PR_CTRLS_ACT_DISABLED;
1✔
1342
  res = pr_run_ctrls(&m, NULL);
1✔
1343
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1344

1345
  mark_point();
1✔
1346
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1347
  ctrl->ctrls_flags &= ~PR_CTRLS_ACT_DISABLED;
1✔
1348
  ctrl->ctrls_flags &= ~PR_CTRLS_FL_REQUESTED;
1✔
1349
  res = pr_run_ctrls(&m, NULL);
1✔
1350
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1351

1352
  mark_point();
1✔
1353
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1354
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1355
  now = time(NULL);
1✔
1356
  ctrl->ctrls_when = now + 10;
1✔
1357
  res = pr_run_ctrls(&m, NULL);
1✔
1358
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1359

1360
  mark_point();
1✔
1361
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1362
  ctrl->ctrls_flags &= PR_CTRLS_FL_PENDING;
1✔
1363
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1364
  ctrl->ctrls_when = now - 10;
1✔
1365
  res = pr_run_ctrls(&m, "test2");
1✔
1366
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1367

1368
  mark_point();
1✔
1369
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1370
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1371
  res = pr_run_ctrls(&m, "test");
1✔
1372
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1373

1374
  mark_point();
1✔
1375
  res = pr_ctrls_unregister(NULL, action);
1✔
1376
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1377
    strerror(errno));
1378

1379
  (void) close(fd);
1✔
1380
}
1381
END_TEST
1382

1383
START_TEST (ctrls_reset_ctrls_test) {
1✔
1384
  int res;
1385
  const char *action, *desc;
1386

1387
  mark_point();
1✔
1388
  res = pr_ctrls_reset();
1✔
1389
  ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
1✔
1390

1391
  mark_point();
1✔
1392
  action = "test";
1✔
1393
  desc = "desc";
1✔
1394
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1395
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1396

1397
  mark_point();
1✔
1398
  res = pr_ctrls_reset();
1✔
1399
  ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
2✔
1400

1401
  mark_point();
1✔
1402
  res = pr_ctrls_unregister(NULL, action);
1✔
1403
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1404
    strerror(errno));
1405
}
1406
END_TEST
1✔
1407

1408
START_TEST (ctrls_accept_test) {
1✔
1409
  int fd, res;
1410

1411
  mark_point();
1✔
1412
  res = pr_ctrls_accept(-1, NULL, NULL, NULL, 0);
1✔
1413
  ck_assert_msg(res < 0, "Failed to handle bad fd");
1✔
1414
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
1415
    strerror(errno), errno);
1416

1417
  mark_point();
1✔
1418
  fd = devnull_fd();
1✔
1419
  if (fd < 0) {
1✔
1420
    return;
1421
  }
1422

1423
  res = pr_ctrls_accept(fd, NULL, NULL, NULL, 5);
1✔
1424
  ck_assert_msg(res < 0, "Failed to handle no clients");
1✔
1425
  ck_assert_msg(errno = ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1✔
1426
    strerror(errno), errno);
1427

1428
  (void) close(fd);
1✔
1429
}
1430
END_TEST
1431

1432
START_TEST (ctrls_accept_einval_path_test) {
1✔
1433
  int fd = -1, other_fd = -1, res;
1✔
1434

1435
  mark_point();
1✔
1436
  fd = connect_unix(&other_fd);
1✔
1437
  if (fd < 0) {
1✔
1438
    return;
×
1439
  }
1440

1441
  mark_point();
1✔
1442
  res = pr_ctrls_accept(fd, NULL, NULL, NULL, 5);
1✔
1443
  ck_assert_msg(res < 0, "Failed to handle invalid path");
1✔
1444
  ck_assert_msg(errno = EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1445
    strerror(errno), errno);
1446

1447
  (void) close(fd);
1✔
1448
  (void) close(other_fd);
1✔
1449
}
1450
END_TEST
1451

1452
START_TEST (ctrls_connect_test) {
1✔
1453
  int fd, res;
1454
  const char *socket_path;
1455

1456
  mark_point();
1✔
1457
  res = pr_ctrls_connect(NULL);
1✔
1458
  ck_assert_msg(res < 0, "Failed to handle null path");
1✔
1459
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1460
    strerror(errno), errno);
1461

1462
  mark_point();
1✔
1463
  socket_path = "/tmp/foo.sock";
1✔
1464
  res = pr_ctrls_connect(socket_path);
1✔
1465
  ck_assert_msg(res < 0, "Failed to handle nonexistent socket path");
2✔
1466
  ck_assert_msg(errno == ECONNREFUSED || errno == ENOENT,
2✔
1467
    "Expected ECONNREFUSED (%d) or ENOENT (%d), got %s (%d)", ECONNREFUSED,
1468
    ENOENT, strerror(errno), errno);
1469

1470
  mark_point();
1✔
1471
  fd = listen_unix(socket_path);
1✔
1472
  if (fd < 0) {
1✔
1473
    return;
1474
  }
1475

1476
  res = pr_ctrls_connect(socket_path);
1✔
1477
  ck_assert_msg(res >= 0, "Failed to connect to local socket: %s",
1✔
1478
    strerror(errno));
1479

1480
  (void) close(res);
1✔
1481
  (void) close(fd);
1✔
1482
  (void) unlink(socket_path);
1✔
1483
}
1484
END_TEST
1485

1486
START_TEST (ctrls_connect_eexist_directory_test) {
1✔
1487
  int res;
1488
  const char socket_path[1024];
1489

1490
  mark_point();
1✔
1491
  memset(socket_path, '\0', sizeof(socket_path));
1✔
1492
  snprintf(socket_path, sizeof(socket_path)-1, "/tmp/ftp.cl%05u",
2✔
1493
    (unsigned int) getpid());
1✔
1494
  res = pr_ctrls_connect(socket_path);
1✔
1495
  ck_assert_msg(res < 0, "Failed to handle nonexistent socket path");
1✔
1496
  ck_assert_msg(errno == ECONNREFUSED || errno == ENOENT,
2✔
1497
    "Expected ECONNREFUSED (%d) or ENOENT (%d), got %s (%d)", ECONNREFUSED,
1498
    ENOENT, strerror(errno), errno);
1499

1500
  mark_point();
1✔
1501
  if (mkdir(socket_path, 0777) < 0) {
1✔
1502
    return;
×
1503
  }
1504

1505
  res = pr_ctrls_connect(socket_path);
1✔
1506
  ck_assert_msg(res < 0, "Failed to handle directory socket path");
1✔
1507
  ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)",
2✔
1508
    EEXIST, strerror(errno), errno);
1509

1510
  (void) rmdir(socket_path);
1✔
1511
}
1512
END_TEST
1513

1514
START_TEST (ctrls_connect_eexist_symlink_test) {
1✔
1515
  int res;
1516
  const char socket_path[1024];
1517

1518
  mark_point();
1✔
1519
  memset(socket_path, '\0', sizeof(socket_path));
1✔
1520
  snprintf(socket_path, sizeof(socket_path)-1, "/tmp/ftp.cl%05u",
2✔
1521
    (unsigned int) getpid());
1✔
1522
  res = pr_ctrls_connect(socket_path);
1✔
1523
  ck_assert_msg(res < 0, "Failed to handle nonexistent socket path");
1✔
1524
  ck_assert_msg(errno == ECONNREFUSED || errno == ENOENT,
2✔
1525
    "Expected ECONNREFUSED (%d) or ENOENT (%d), got %s (%d)", ECONNREFUSED,
1526
    ENOENT, strerror(errno), errno);
1527

1528
  mark_point();
1✔
1529
  if (symlink(socket_path, "/foo/bar/baz") < 0) {
1✔
1530
    return;
1✔
1531
  }
1532

1533
  res = pr_ctrls_connect(socket_path);
×
1534
  ck_assert_msg(res < 0, "Failed to handle symlink socket path");
×
1535
  ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)",
×
1536
    EEXIST, strerror(errno), errno);
1537

1538
  (void) unlink(socket_path);
×
1539
}
1540
END_TEST
1541

1542
START_TEST (ctrls_check_group_acl_test) {
1✔
1543
  int res;
1544
  gid_t gid;
1545
  ctrls_group_acl_t *group_acl;
1546

1547
  mark_point();
1✔
1548
  res = pr_ctrls_check_group_acl(0, NULL);
1✔
1549
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1550
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1551
    strerror(errno), errno);
1552

1553
  mark_point();
1✔
1554
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1555
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1556
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1557

1558
  mark_point();
1✔
1559
  group_acl->allow = TRUE;
1✔
1560
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1561
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1562

1563
  mark_point();
1✔
1564
  group_acl->ngids = 1;
1✔
1565
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1566
  ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
2✔
1567
    group_acl->allow, res);
1568

1569
  group_acl->allow = FALSE;
1✔
1570
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1571
  ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
2✔
1572
    group_acl->allow, res);
1573

1574
  mark_point();
1✔
1575
  gid = 1;
1✔
1576
  group_acl->allow = TRUE;
1✔
1577
  group_acl->gids = palloc(p, sizeof(gid_t) * 2);
1✔
1578
  ((gid_t *) group_acl->gids)[0] = gid;
1✔
1579
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1580
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1581

1582
  res = pr_ctrls_check_group_acl(gid, group_acl);
1✔
1583
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1584
}
1585
END_TEST
1✔
1586

1587
START_TEST (ctrls_check_user_acl_test) {
1✔
1588
  int res;
1589
  uid_t uid;
1590
  ctrls_user_acl_t *user_acl;
1591

1592
  mark_point();
1✔
1593
  res = pr_ctrls_check_user_acl(0, NULL);
1✔
1594
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1595
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1596
    strerror(errno), errno);
1597

1598
  mark_point();
1✔
1599
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1600
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1601
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1602

1603
  mark_point();
1✔
1604
  user_acl->allow = TRUE;
1✔
1605
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1606
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1607

1608
  mark_point();
1✔
1609
  user_acl->nuids = 1;
1✔
1610
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1611
  ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
2✔
1612
    user_acl->allow, res);
1613

1614
  user_acl->allow = FALSE;
1✔
1615
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1616
  ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
2✔
1617
    user_acl->allow, res);
1618

1619
  mark_point();
1✔
1620
  uid = 1;
1✔
1621
  user_acl->allow = TRUE;
1✔
1622
  user_acl->uids = palloc(p, sizeof(uid_t) * 2);
1✔
1623
  ((uid_t *) user_acl->uids)[0] = uid;
1✔
1624
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1625
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1626

1627
  res = pr_ctrls_check_user_acl(uid, user_acl);
1✔
1628
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1629
}
1630
END_TEST
1✔
1631

1632
START_TEST (ctrls_init_acl_test) {
1✔
1633
  int res;
1634
  ctrls_acl_t *acl;
1635

1636
  mark_point();
1✔
1637
  res = pr_ctrls_init_acl(NULL);
1✔
1638
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1639
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1640
    strerror(errno), errno);
1641

1642
  mark_point();
1✔
1643
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1644
  res = pr_ctrls_init_acl(acl);
1✔
1645
  ck_assert_msg(res == 0, "Failed to init acl: %s", strerror(errno));
2✔
1646
}
1647
END_TEST
1✔
1648

1649
static int test_action_cb(pr_ctrls_t *ctl, int reqargc, char **reqargv) {
×
1650
  return 0;
×
1651
}
1652

1653
START_TEST (ctrls_check_acl_test) {
1✔
1654
  int res;
1655
  pr_ctrls_t *ctrl;
1656
  pr_ctrls_cl_t *cl;
1657
  ctrls_acl_t *acl;
1658
  const char *action;
1659
  ctrls_acttab_t acttab[] = {
1✔
1660
    { "test", "desc", NULL, test_action_cb },
1661
    { NULL, NULL, NULL, NULL }
1662
  };
1663

1664
  mark_point();
1✔
1665
  res = pr_ctrls_check_acl(NULL, NULL, NULL);
1✔
1666
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
1667
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1668
    strerror(errno), errno);
1669

1670
  mark_point();
1✔
1671
  ctrl = pr_ctrls_alloc();
1✔
1672
  res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1✔
1673
  ck_assert_msg(res < 0, "Failed to handle ctrl without client");
2✔
1674
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1675
    strerror(errno), errno);
1676

1677
  mark_point();
1✔
1678
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
1679
  cl->cl_uid = cl->cl_gid = 1;
1✔
1680
  ctrl->ctrls_cl = cl;
1✔
1681
  res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1✔
1682
  ck_assert_msg(res < 0, "Failed to handle null acttab");
2✔
1683
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1684
    strerror(errno), errno);
1685

1686
  mark_point();
1✔
1687
  res = pr_ctrls_check_acl(ctrl, acttab, NULL);
1✔
1688
  ck_assert_msg(res < 0, "Failed to handle null action");
2✔
1689
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1690
    strerror(errno), errno);
1691

1692
  mark_point();
1✔
1693
  action = "foobar";
1✔
1694
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1695
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1696

1697
  mark_point();
1✔
1698
  action = "test";
1✔
1699
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1700
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1701

1702
  mark_point();
1✔
1703
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1704
  res = pr_ctrls_init_acl(acl);
1✔
1705
  acttab[0].act_acl = acl;
1✔
1706
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1707
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1708

1709
  mark_point();
1✔
1710
  acl->acl_groups.ngids = 1;
1✔
1711
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1712
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1713

1714
  mark_point();
1✔
1715
  acl->acl_users.nuids = 1;
1✔
1716
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1717
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1718
}
1719
END_TEST
1✔
1720

1721
START_TEST (ctrls_parse_acl_test) {
1✔
1722
  char **res;
1723
  const char *names;
1724

1725
  mark_point();
1✔
1726
  res = pr_ctrls_parse_acl(NULL, NULL);
1✔
1727
  ck_assert_msg(res == NULL, "Failed to handle null pool");
1✔
1728
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1729
    strerror(errno), errno);
1730

1731
  mark_point();
1✔
1732
  res = pr_ctrls_parse_acl(p, NULL);
1✔
1733
  ck_assert_msg(res == NULL, "Failed to handle null ACL text");
2✔
1734
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1735
    strerror(errno), errno);
1736

1737
  mark_point();
1✔
1738
  names = "foo";
1✔
1739
  res = pr_ctrls_parse_acl(p, names);
1✔
1740
  ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
2✔
1741
    strerror(errno));
1742
  ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
2✔
1743
  ck_assert_msg(res[1] == NULL, "Expected NULL, got %p", res[1]);
2✔
1744

1745
  mark_point();
1✔
1746
  names = "foo,'Bar',BAZ";
1✔
1747
  res = pr_ctrls_parse_acl(p, names);
1✔
1748
  ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
2✔
1749
    strerror(errno));
1750
  ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
2✔
1751
  ck_assert_msg(strcmp(res[1], "'Bar'") == 0, "Expected 'Bar', got '%s'", res[1]);
2✔
1752
  ck_assert_msg(strcmp(res[2], "BAZ") == 0, "Expected 'BAZ', got '%s'", res[2]);
2✔
1753
  ck_assert_msg(res[3] == NULL, "Expected NULL, got %p", res[3]);
2✔
1754
}
1755
END_TEST
1✔
1756

1757
START_TEST (ctrls_set_group_acl_test) {
1✔
1758
  int res;
1759
  ctrls_group_acl_t *group_acl;
1760
  const char *allow;
1761
  char *grouplist;
1762

1763
  mark_point();
1✔
1764
  res = pr_ctrls_set_group_acl(NULL, NULL, NULL, NULL);
1✔
1765
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
1766
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1767
    strerror(errno), errno);
1768

1769
  mark_point();
1✔
1770
  res = pr_ctrls_set_group_acl(p, NULL, NULL, NULL);
1✔
1771
  ck_assert_msg(res < 0, "Failed to handle null group_acl");
2✔
1772
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1773
    strerror(errno), errno);
1774

1775
  mark_point();
1✔
1776
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1777
  res = pr_ctrls_set_group_acl(p, group_acl, NULL, NULL);
1✔
1778
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1779
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1780
    strerror(errno), errno);
1781

1782
  mark_point();
1✔
1783
  allow = "allow";
1✔
1784
  res = pr_ctrls_set_group_acl(p, group_acl, allow, NULL);
1✔
1785
  ck_assert_msg(res < 0, "Failed to handle null grouplist");
2✔
1786
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1787
    strerror(errno), errno);
1788

1789
  mark_point();
1✔
1790
  grouplist = "foo,bar,baz,wheel";
1✔
1791
  res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1✔
1792
  ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
2✔
1793
  ck_assert_msg(group_acl->allow == TRUE, "Expected TRUE, got %d",
2✔
1794
    group_acl->allow);
1795
  /* Note that we expect zero here, because of name/GID lookup failures. */
1796
  ck_assert_msg(group_acl->ngids == 0, "Expected 0, got %d",
2✔
1797
    group_acl->ngids);
1798
  ck_assert_msg(group_acl->gids != NULL, "Got NULL unexpectedly");
2✔
1799

1800
  mark_point();
1✔
1801
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1802
  allow = "deny";
1✔
1803
  grouplist = "foo,*";
1✔
1804
  res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1✔
1805
  ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
2✔
1806
  ck_assert_msg(group_acl->allow == FALSE, "Expected FALSE, got %d",
2✔
1807
    group_acl->allow);
1808
  ck_assert_msg(group_acl->ngids == 1, "Expected 1, got %d",
2✔
1809
    group_acl->ngids);
1810
  ck_assert_msg(group_acl->gids == NULL, "Expected NULL, got %p",
2✔
1811
    group_acl->gids);
1812
}
1813
END_TEST
1✔
1814

1815
START_TEST (ctrls_set_user_acl_test) {
1✔
1816
  int res;
1817
  ctrls_user_acl_t *user_acl;
1818
  const char *allow;
1819
  char *userlist;
1820

1821
  mark_point();
1✔
1822
  res = pr_ctrls_set_user_acl(NULL, NULL, NULL, NULL);
1✔
1823
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
1824
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1825
    strerror(errno), errno);
1826

1827
  mark_point();
1✔
1828
  res = pr_ctrls_set_user_acl(p, NULL, NULL, NULL);
1✔
1829
  ck_assert_msg(res < 0, "Failed to handle null user_acl");
2✔
1830
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1831
    strerror(errno), errno);
1832

1833
  mark_point();
1✔
1834
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1835
  res = pr_ctrls_set_user_acl(p, user_acl, NULL, NULL);
1✔
1836
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1837
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1838
    strerror(errno), errno);
1839

1840
  mark_point();
1✔
1841
  allow = "allow";
1✔
1842
  res = pr_ctrls_set_user_acl(p, user_acl, allow, NULL);
1✔
1843
  ck_assert_msg(res < 0, "Failed to handle null userlist");
2✔
1844
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1845
    strerror(errno), errno);
1846

1847
  mark_point();
1✔
1848
  userlist = "foo,bar,baz,root";
1✔
1849
  res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1✔
1850
  ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
2✔
1851
  ck_assert_msg(user_acl->allow == TRUE, "Expected TRUE, got %d",
2✔
1852
    user_acl->allow);
1853
  /* Note that we expect zero here, because of name/UID lookup failures. */
1854
  ck_assert_msg(user_acl->nuids == 0, "Expected 0, got %d", user_acl->nuids);
2✔
1855
  ck_assert_msg(user_acl->uids != NULL, "Got NULL unexpectedly");
2✔
1856

1857
  mark_point();
1✔
1858
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1859
  allow = "deny";
1✔
1860
  userlist = "foo,*";
1✔
1861
  res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1✔
1862
  ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
2✔
1863
  ck_assert_msg(user_acl->allow == FALSE, "Expected FALSE, got %d",
2✔
1864
    user_acl->allow);
1865
  ck_assert_msg(user_acl->nuids == 1, "Expected 1, got %d", user_acl->nuids);
2✔
1866
  ck_assert_msg(user_acl->uids == NULL, "Expected NULL, got %p",
2✔
1867
    user_acl->uids);
1868
}
1869
END_TEST
1✔
1870

1871
START_TEST (ctrls_set_module_acls_test) {
1✔
1872
  char *res, *list;
1873
  const char *allow, *type;
1874
  ctrls_acl_t *acl;
1875
  char *good_actions[] = { "test", NULL };
1✔
1876
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1877
  char *all_actions[] = { "all", NULL };
1✔
1878
  ctrls_acttab_t acttab[] = {
1✔
1879
    { "test", "desc", NULL, test_action_cb },
1880
    { NULL, NULL, NULL, NULL }
1881
  };
1882

1883
  mark_point();
1✔
1884
  res = pr_ctrls_set_module_acls(NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1885
  ck_assert_msg(res == NULL, "Failed to handle null acttab");
1✔
1886
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1887
    strerror(errno), errno);
1888

1889
  mark_point();
1✔
1890
  res = pr_ctrls_set_module_acls(acttab, NULL, NULL, NULL, NULL, NULL);
1✔
1891
  ck_assert_msg(res == NULL, "Failed to handle null acl_pool");
2✔
1892
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1893
    strerror(errno), errno);
1894

1895
  mark_point();
1✔
1896
  res = pr_ctrls_set_module_acls(acttab, p, NULL, NULL, NULL, NULL);
1✔
1897
  ck_assert_msg(res == NULL, "Failed to handle null actions");
2✔
1898
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1899
    strerror(errno), errno);
1900

1901
  mark_point();
1✔
1902
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, NULL, NULL, NULL);
1✔
1903
  ck_assert_msg(res == NULL, "Failed to handle null allow");
2✔
1904
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1905
    strerror(errno), errno);
1906

1907
  mark_point();
1✔
1908
  allow = "allow";
1✔
1909
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, NULL, NULL);
1✔
1910
  ck_assert_msg(res == NULL, "Failed to handle null type");
2✔
1911
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1912
    strerror(errno), errno);
1913

1914
  mark_point();
1✔
1915
  type = "test";
1✔
1916
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, NULL);
1✔
1917
  ck_assert_msg(res == NULL, "Failed to handle null list");
2✔
1918
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1919
    strerror(errno), errno);
1920

1921
  mark_point();
1✔
1922
  list = "foo,bar,baz";
1✔
1923
  res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1✔
1924
  ck_assert_msg(res == NULL, "Failed to handle invalid type '%s'", type);
2✔
1925
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1926
    strerror(errno), errno);
1927

1928
  mark_point();
1✔
1929
  type = "user";
1✔
1930
  res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1✔
1931
  ck_assert_msg(res != NULL, "Failed to handle invalid action");
2✔
1932
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1933
    strerror(errno), errno);
1934
  ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
2✔
1935

1936
  mark_point();
1✔
1937
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1938
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
1939
    "Failed to initialize acl: %s", strerror(errno));
1940

1941
  acttab[0].act_acl = acl;
1✔
1942
  type = "group";
1✔
1943
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1✔
1944
  ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
2✔
1945

1946
  mark_point();
1✔
1947
  type = "user";
1✔
1948
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1✔
1949
  ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
2✔
1950

1951
  mark_point();
1✔
1952
  res = pr_ctrls_set_module_acls(acttab, p, all_actions, allow, type, list);
1✔
1953
  ck_assert_msg(res == NULL, "Failed to handle all actions: %s", strerror(errno));
2✔
1954
}
1955
END_TEST
1✔
1956

1957
START_TEST (ctrls_set_module_acls2_test) {
1✔
1958
  int res;
1959
  char *list;
1960
  const char *allow, *type, *bad_action = NULL;
1✔
1961
  ctrls_acl_t *acl;
1962
  char *good_actions[] = { "test", NULL };
1✔
1963
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1964
  char *all_actions[] = { "all", NULL };
1✔
1965
  ctrls_acttab_t acttab[] = {
1✔
1966
    { "test", "desc", NULL, test_action_cb },
1967
    { NULL, NULL, NULL, NULL }
1968
  };
1969

1970
  mark_point();
1✔
1971
  res = pr_ctrls_set_module_acls2(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1972
  ck_assert_msg(res < 0, "Failed to handle null acttab");
1✔
1973
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1974
    strerror(errno), errno);
1975

1976
  mark_point();
1✔
1977
  res = pr_ctrls_set_module_acls2(acttab, NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1978
  ck_assert_msg(res < 0, "Failed to handle null acl_pool");
2✔
1979
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1980
    strerror(errno), errno);
1981

1982
  mark_point();
1✔
1983
  res = pr_ctrls_set_module_acls2(acttab, p, NULL, NULL, NULL, NULL, NULL);
1✔
1984
  ck_assert_msg(res < 0, "Failed to handle null actions");
2✔
1985
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1986
    strerror(errno), errno);
1987

1988
  mark_point();
1✔
1989
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, NULL, NULL, NULL,
1✔
1990
    NULL);
1991
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1992
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1993
    strerror(errno), errno);
1994

1995
  mark_point();
1✔
1996
  allow = "allow";
1✔
1997
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, NULL, NULL,
1✔
1998
    NULL);
1999
  ck_assert_msg(res < 0, "Failed to handle null type");
2✔
2000
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2001
    strerror(errno), errno);
2002

2003
  mark_point();
1✔
2004
  type = "test";
1✔
2005
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, NULL,
1✔
2006
    NULL);
2007
  ck_assert_msg(res < 0, "Failed to handle null list");
2✔
2008
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2009
    strerror(errno), errno);
2010

2011
  mark_point();
1✔
2012
  list = "foo,bar,baz";
1✔
2013
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
2014
    NULL);
2015
  ck_assert_msg(res < 0, "Failed to handle null bad_action");
2✔
2016
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2017
    strerror(errno), errno);
2018

2019
  mark_point();
1✔
2020
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
2021
    &bad_action);
2022
  ck_assert_msg(res < 0, "Failed to handle invalid type '%s'", type);
2✔
2023
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2024
    strerror(errno), errno);
2025

2026
  mark_point();
1✔
2027
  type = "user";
1✔
2028
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
2029
    &bad_action);
2030
  ck_assert_msg(res < 0, "Failed to handle invalid action");
2✔
2031
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
2032
    strerror(errno), errno);
2033
  ck_assert_msg(strcmp(bad_action, "bar") == 0,
2✔
2034
    "Expected 'bar', got '%s'", bad_action);
2035

2036
  mark_point();
1✔
2037
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
2038
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
2039
    "Failed to initialize acl: %s", strerror(errno));
2040

2041
  acttab[0].act_acl = acl;
1✔
2042
  type = "group";
1✔
2043
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1✔
2044
    &bad_action);
2045
  ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
2✔
2046

2047
  mark_point();
1✔
2048
  type = "user";
1✔
2049
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1✔
2050
    &bad_action);
2051
  ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
2✔
2052

2053
  mark_point();
1✔
2054
  res = pr_ctrls_set_module_acls2(acttab, p, all_actions, allow, type, list,
1✔
2055
    &bad_action);
2056
  ck_assert_msg(res == 0, "Failed to handle all actions: %s", strerror(errno));
2✔
2057
}
2058
END_TEST
1✔
2059

2060
START_TEST (ctrls_unregister_module_actions_test) {
1✔
2061
  char *res;
2062
  char *good_actions[] = { "test", NULL };
1✔
2063
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
2064
  ctrls_acl_t *acl;
2065
  ctrls_acttab_t acttab[] = {
1✔
2066
    { "test", "desc", NULL, test_action_cb },
2067
    { NULL, NULL, NULL, NULL }
2068
  };
2069
  module m;
2070

2071
  mark_point();
1✔
2072
  res = pr_ctrls_unregister_module_actions(NULL, NULL, NULL);
1✔
2073
  ck_assert_msg(res == NULL, "Failed to handle null acttab");
1✔
2074
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2075
    strerror(errno), errno);
2076

2077
  mark_point();
1✔
2078
  res = pr_ctrls_unregister_module_actions(acttab, NULL, NULL);
1✔
2079
  ck_assert_msg(res == NULL, "Failed to handle null actions");
2✔
2080
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2081
    strerror(errno), errno);
2082

2083
  mark_point();
1✔
2084
  res = pr_ctrls_unregister_module_actions(acttab, good_actions, NULL);
1✔
2085
  ck_assert_msg(res == NULL, "Failed to handle null module");
2✔
2086
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2087
    strerror(errno), errno);
2088

2089
  mark_point();
1✔
2090
  m.name = "test";
1✔
2091
  res = pr_ctrls_unregister_module_actions(acttab, bad_actions, &m);
1✔
2092
  ck_assert_msg(res != NULL, "Failed to handle invalid action");
2✔
2093
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
2094
    strerror(errno), errno);
2095
  ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
2✔
2096

2097
  mark_point();
1✔
2098
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
2099
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
2100
    "Failed to initialize acl: %s", strerror(errno));
2101
  acttab[0].act_acl = acl;
1✔
2102
  res = pr_ctrls_unregister_module_actions(acttab, good_actions, &m);
1✔
2103
  ck_assert_msg(res == NULL, "Failed to handle valid action: %s",
2✔
2104
    strerror(errno));
2105
}
2106
END_TEST
1✔
2107

2108
START_TEST (ctrls_unregister_module_actions2_test) {
1✔
2109
  int res;
2110
  const char *bad_action = NULL;
1✔
2111
  char *good_actions[] = { "test", NULL };
1✔
2112
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
2113
  ctrls_acl_t *acl;
2114
  ctrls_acttab_t acttab[] = {
1✔
2115
    { "test", "desc", NULL, test_action_cb },
2116
    { NULL, NULL, NULL, NULL }
2117
  };
2118
  module m;
2119

2120
  mark_point();
1✔
2121
  res = pr_ctrls_unregister_module_actions2(NULL, NULL, NULL, NULL);
1✔
2122
  ck_assert_msg(res < 0, "Failed to handle null acttab");
1✔
2123
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2124
    strerror(errno), errno);
2125

2126
  mark_point();
1✔
2127
  res = pr_ctrls_unregister_module_actions2(acttab, NULL, NULL, NULL);
1✔
2128
  ck_assert_msg(res < 0, "Failed to handle null actions");
2✔
2129
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2130
    strerror(errno), errno);
2131

2132
  mark_point();
1✔
2133
  res = pr_ctrls_unregister_module_actions2(acttab, good_actions, NULL, NULL);
1✔
2134
  ck_assert_msg(res < 0, "Failed to handle null module");
2✔
2135
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2136
    strerror(errno), errno);
2137

2138
  mark_point();
1✔
2139
  m.name = "test";
1✔
2140
  res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m, NULL);
1✔
2141
  ck_assert_msg(res < 0, "Failed to handle null bad_action");
2✔
2142
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2143
    strerror(errno), errno);
2144

2145
  mark_point();
1✔
2146
  res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m,
1✔
2147
    &bad_action);
2148
  ck_assert_msg(res < 0, "Failed to handle invalid action");
2✔
2149
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
2150
    strerror(errno), errno);
2151
  ck_assert_msg(bad_action != NULL, "Expected bad_action, got NULL");
2✔
2152
  ck_assert_msg(strcmp(bad_action, "bar") == 0,
2✔
2153
    "Expected 'bar', got '%s'", bad_action);
2154

2155
  mark_point();
1✔
2156
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
2157
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
2158
    "Failed to initialize acl: %s", strerror(errno));
2159
  acttab[0].act_acl = acl;
1✔
2160
  res = pr_ctrls_unregister_module_actions2(acttab, good_actions, &m,
1✔
2161
    &bad_action);
2162
  ck_assert_msg(res == 0, "Failed to handle valid action: %s", strerror(errno));
2✔
2163
}
2164
END_TEST
1✔
2165

2166
START_TEST (ctrls_set_logfd_test) {
1✔
2167
  int fd, res;
2168

2169
  mark_point();
1✔
2170
  fd = 0;
1✔
2171
  res = pr_ctrls_set_logfd(fd);
1✔
2172
  ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
1✔
2173
     strerror(errno));
2174

2175
  mark_point();
1✔
2176
  fd = -1;
1✔
2177
  res = pr_ctrls_set_logfd(fd);
1✔
2178
  ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
2✔
2179
     strerror(errno));
2180
}
2181
END_TEST
1✔
2182

2183
START_TEST (ctrls_log_test) {
1✔
2184
  int fd, res;
2185

2186
  mark_point();
1✔
2187
  fd = -1;
1✔
2188
  pr_ctrls_set_logfd(fd);
1✔
2189
  res = pr_ctrls_log(NULL, NULL);
1✔
2190
  ck_assert_msg(res == 0, "Failed to handle bad logfd: %s", strerror(errno));
1✔
2191

2192
  mark_point();
1✔
2193
  fd = devnull_fd();
1✔
2194
  if (fd < 0) {
1✔
2195
    return;
2196
  }
2197

2198
  pr_ctrls_set_logfd(fd);
1✔
2199
  res = pr_ctrls_log(NULL, NULL);
1✔
2200
  ck_assert_msg(res == 0, "Failed to handle bad module_version: %s",
1✔
2201
    strerror(errno));
2202

2203
  mark_point();
1✔
2204
  res = pr_ctrls_log("test", NULL);
1✔
2205
  ck_assert_msg(res == 0, "Failed to handle null fmt: %s", strerror(errno));
2✔
2206

2207
  mark_point();
1✔
2208
  res = pr_ctrls_log("test", "%s", "foo bar baz");
1✔
2209
  ck_assert_msg(res == 0, "Failed to handle valid fmt: %s", strerror(errno));
2✔
2210

2211
  (void) close(fd);
1✔
2212
}
2213
END_TEST
2214

2215
#endif /* PR_USE_CTRLS */
2216

2217
Suite *tests_get_ctrls_suite(void) {
920✔
2218
  Suite *suite;
2219
  TCase *testcase;
2220

2221
  suite = suite_create("ctrls");
920✔
2222
  testcase = tcase_create("base");
920✔
2223

2224
#if defined(PR_USE_CTRLS)
2225
  tcase_add_checked_fixture(testcase, set_up, tear_down);
920✔
2226

2227
  tcase_add_test(testcase, ctrls_alloc_free_test);
920✔
2228
  tcase_add_test(testcase, ctrls_unregister_test);
920✔
2229
  tcase_add_test(testcase, ctrls_register_test);
920✔
2230
  tcase_add_test(testcase, ctrls_add_arg_test);
920✔
2231
  tcase_add_test(testcase, ctrls_add_response_test);
920✔
2232
  tcase_add_test(testcase, ctrls_copy_args_test);
920✔
2233
  tcase_add_test(testcase, ctrls_copy_resps_test);
920✔
2234
  tcase_add_test(testcase, ctrls_send_request_test);
920✔
2235
  tcase_add_test(testcase, ctrls_send_response_test);
920✔
2236
  tcase_add_test(testcase, ctrls_flush_response_test);
920✔
2237
  tcase_add_test(testcase, ctrls_recv_request_invalid_test);
920✔
2238
  tcase_add_test(testcase, ctrls_recv_request_too_large_test);
920✔
2239
  tcase_add_test(testcase, ctrls_recv_request_valid_test);
920✔
2240
  tcase_add_test(testcase, ctrls_recv_response_test);
920✔
2241
  tcase_add_test(testcase, ctrls_recv_response_too_large_test);
920✔
2242

2243
  tcase_add_test(testcase, ctrls_issock_unix_test);
920✔
2244
  tcase_add_test(testcase, ctrls_get_registered_actions_test);
920✔
2245
  tcase_add_test(testcase, ctrls_set_registered_actions_test);
920✔
2246
  tcase_add_test(testcase, ctrls_check_actions_test);
920✔
2247
  tcase_add_test(testcase, ctrls_run_ctrls_test);
920✔
2248
  tcase_add_test(testcase, ctrls_reset_ctrls_test);
920✔
2249
  tcase_add_test(testcase, ctrls_accept_test);
920✔
2250
  tcase_add_test(testcase, ctrls_accept_einval_path_test);
920✔
2251
  tcase_add_test(testcase, ctrls_connect_test);
920✔
2252
  tcase_add_test(testcase, ctrls_connect_eexist_directory_test);
920✔
2253
  tcase_add_test(testcase, ctrls_connect_eexist_symlink_test);
920✔
2254

2255
  /* mod_ctrls */
2256
  tcase_add_test(testcase, ctrls_check_group_acl_test);
920✔
2257
  tcase_add_test(testcase, ctrls_check_user_acl_test);
920✔
2258
  tcase_add_test(testcase, ctrls_init_acl_test);
920✔
2259
  tcase_add_test(testcase, ctrls_check_acl_test);
920✔
2260
  tcase_add_test(testcase, ctrls_parse_acl_test);
920✔
2261
  tcase_add_test(testcase, ctrls_set_group_acl_test);
920✔
2262
  tcase_add_test(testcase, ctrls_set_user_acl_test);
920✔
2263
  tcase_add_test(testcase, ctrls_set_module_acls_test);
920✔
2264
  tcase_add_test(testcase, ctrls_set_module_acls2_test);
920✔
2265
  tcase_add_test(testcase, ctrls_unregister_module_actions_test);
920✔
2266
  tcase_add_test(testcase, ctrls_unregister_module_actions2_test);
920✔
2267
  tcase_add_test(testcase, ctrls_set_logfd_test);
920✔
2268
  tcase_add_test(testcase, ctrls_log_test);
920✔
2269
#endif /* PR_USE_CTRLS */
2270

2271
  suite_add_tcase(suite, testcase);
920✔
2272
  return suite;
920✔
2273
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc