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

proftpd / proftpd / 25508939015

07 May 2026 04:34PM UTC coverage: 92.632% (-0.4%) from 93.024%
25508939015

push

github

web-flow
Issue #2052: It is possible that some environment values come from user-supplied text, so we should always escape `%{env:...}` variables, too.

47243 of 51001 relevant lines covered (92.63%)

241.97 hits per line

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

98.18
/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) {
34✔
35
  (void) unlink(tmpfile_path);
34✔
36

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

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

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

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

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

60
  (void) unlink(tmpfile_path);
34✔
61
}
34✔
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) {
20✔
76
  int fd;
77

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

84
  (void) unlink(tmpfile_path);
20✔
85
  return fd;
20✔
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) {
18✔
95
    return -1;
96
  }
97

98
  return 0;
99
}
100

101
/* Largely copied from mod_ctrls. */
102
static int listen_unix(const char *path) {
1✔
103
  int fd = -1, socklen = 0;
1✔
104
  struct sockaddr_un sock;
105
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
106
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
107
  int opt = 1;
108
  socklen_t optlen = sizeof(opt);
109
#endif /* !LOCAL_CREDS */
110

111
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
1✔
112
  if (fd < 0) {
1✔
113
    fprintf(stderr, "error creating local socket: %s\n", strerror(errno));
×
114
    return -1;
×
115
  }
116

117
  (void) unlink(path);
1✔
118
  memset(&sock, 0, sizeof(sock));
1✔
119
  sock.sun_family = AF_UNIX;
1✔
120
  sstrncpy(sock.sun_path, path, sizeof(sock.sun_path));
1✔
121

122
  socklen = sizeof(sock);
1✔
123
  if (bind(fd, (struct sockaddr *) &sock, socklen) < 0) {
1✔
124
    fprintf(stderr, "error binding local socket to path '%s': %s\n", path,
×
125
      strerror(errno));
×
126
    (void) close(fd);
×
127
    return -1;
×
128
  }
129

130
  if (listen(fd, 5) < 0) {
1✔
131
    fprintf(stderr, "error listening on local socket: %s\n", strerror(errno));
×
132
    (void) close(fd);
×
133
    return -1;
×
134
  }
135

136
#if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
137
    !defined(HAVE_GETPEERUCRED) && defined(LOCAL_CREDS)
138
  /* Set the LOCAL_CREDS socket option. */
139
  if (setsockopt(fd, 0, LOCAL_CREDS, &opt, optlen) < 0) {
140
    fprintf(stderr, "error enabling LOCAL_CREDS: %s\n", strerror(errno));
141
  }
142
#endif /* !LOCAL_CREDS */
143

144
  return fd;
145
}
146

147
/* Tests */
148

149
START_TEST (ctrls_alloc_free_test) {
1✔
150
  int res;
151
  pr_ctrls_t *ctrl, *ctrl2, *ctrl3;
152

153
  mark_point();
1✔
154
  res = pr_ctrls_free(NULL);
1✔
155
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
156
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
157
    strerror(errno), errno);
158

159
  mark_point();
1✔
160
  ctrl = pr_ctrls_alloc();
1✔
161
  ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
2✔
162
  res = pr_ctrls_free(ctrl);
1✔
163
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
164

165
  mark_point();
1✔
166
  ctrl = pr_ctrls_alloc();
1✔
167
  ck_assert_msg(ctrl != NULL, "Failed to allocate ctrl: %s", strerror(errno));
2✔
168
  res = pr_ctrls_free(ctrl);
1✔
169
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
170

171
  /* LIFO order */
172
  mark_point();
1✔
173
  ctrl = pr_ctrls_alloc();
1✔
174
  ctrl2 = pr_ctrls_alloc();
1✔
175
  ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
2✔
176
  ctrl2->ctrls_tmp_pool = make_sub_pool(p);
1✔
177
  ctrl3 = pr_ctrls_alloc();
1✔
178
  ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
2✔
179
  ctrl3->ctrls_tmp_pool = make_sub_pool(p);
1✔
180

181
  res = pr_ctrls_free(ctrl3);
1✔
182
  ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
2✔
183
  res = pr_ctrls_free(ctrl2);
1✔
184
  ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
2✔
185
  res = pr_ctrls_free(ctrl);
1✔
186
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
187

188
  /* FIFO order */
189
  mark_point();
1✔
190
  ctrl = pr_ctrls_alloc();
1✔
191
  ctrl2 = pr_ctrls_alloc();
1✔
192
  ck_assert_msg(ctrl2 != NULL, "Failed to allocate ctrl2: %s", strerror(errno));
2✔
193
  ctrl2->ctrls_tmp_pool = make_sub_pool(p);
1✔
194
  ctrl3 = pr_ctrls_alloc();
1✔
195
  ck_assert_msg(ctrl3 != NULL, "Failed to allocate ctrl3: %s", strerror(errno));
2✔
196
  ctrl3->ctrls_tmp_pool = make_sub_pool(p);
1✔
197

198
  res = pr_ctrls_free(ctrl);
1✔
199
  ck_assert_msg(res == 0, "Failed to free ctrl: %s", strerror(errno));
2✔
200
  res = pr_ctrls_free(ctrl2);
1✔
201
  ck_assert_msg(res == 0, "Failed to free ctrl2: %s", strerror(errno));
2✔
202
  res = pr_ctrls_free(ctrl3);
1✔
203
  ck_assert_msg(res == 0, "Failed to free ctrl3 %s", strerror(errno));
2✔
204
}
205
END_TEST
1✔
206

207
START_TEST (ctrls_unregister_test) {
1✔
208
  int res;
209

210
  mark_point();
1✔
211
  res = pr_ctrls_unregister(NULL, NULL);
1✔
212
  ck_assert_msg(res < 0, "Failed to handle lack of registered actions");
1✔
213
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2✔
214
    strerror(errno), errno);
215
}
216
END_TEST
1✔
217

218
static int ctrls_test_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
1✔
219
  return 0;
1✔
220
}
221

222
static int ctrls_test2_cb(pr_ctrls_t *ctrl, int reqargc, char **reqargv) {
×
223
  return 0;
×
224
}
225

226
START_TEST (ctrls_register_test) {
1✔
227
  int res;
228
  const char *action = NULL, *desc = NULL;
1✔
229
  module m;
230

231
  mark_point();
1✔
232
  res = pr_ctrls_register(NULL, NULL, NULL, NULL);
1✔
233
  ck_assert_msg(res < 0, "Failed to handle null action");
1✔
234
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
235
    strerror(errno), errno);
236

237
  mark_point();
1✔
238
  action = "test";
1✔
239
  res = pr_ctrls_register(NULL, action, NULL, NULL);
1✔
240
  ck_assert_msg(res < 0, "Failed to handle null desc");
2✔
241
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
242
    strerror(errno), errno);
243

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

251
  mark_point();
1✔
252
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
253
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
254

255
  mark_point();
1✔
256
  res = pr_ctrls_unregister(NULL, action);
1✔
257
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
258
    strerror(errno));
259

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

264
  m.name = "test";
1✔
265
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
266
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
267

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

275
START_TEST (ctrls_add_arg_test) {
1✔
276
  int res;
277
  pr_ctrls_t *ctrl;
278
  char buf[4];
279
  size_t buflen;
280

281
  mark_point();
1✔
282
  res = pr_ctrls_add_arg(NULL, NULL, 0);
1✔
283
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
284
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
285
    strerror(errno), errno);
286

287
  mark_point();
1✔
288
  ctrl = pr_ctrls_alloc();
1✔
289
  res = pr_ctrls_add_arg(ctrl, NULL, 0);
1✔
290
  ck_assert_msg(res < 0, "Failed to handle null arg");
2✔
291
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
292
    strerror(errno), errno);
293

294
  mark_point();
1✔
295
  ctrl = pr_ctrls_alloc();
1✔
296

297
  /* Provide an arg that uses unprintable ASCII. */
298
  buf[0] = 'a';
1✔
299
  buf[1] = 'b';
1✔
300
  buf[2] = -120;
1✔
301
  buflen = 3;
1✔
302

303
  res = pr_ctrls_add_arg(ctrl, buf, buflen);
1✔
304
  ck_assert_msg(res < 0, "Failed to handle bad arg");
2✔
305
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
306
    strerror(errno), errno);
307

308
  mark_point();
1✔
309
  buf[0] = 'a';
1✔
310
  buf[1] = 'B';
1✔
311
  buf[2] = '1';
1✔
312
  buflen = 3;
1✔
313

314
  res = pr_ctrls_add_arg(ctrl, buf, buflen);
1✔
315
  ck_assert_msg(res == 0, "Failed to add ctrl arg: %s", strerror(errno));
2✔
316
}
317
END_TEST
1✔
318

319
START_TEST (ctrls_add_response_test) {
1✔
320
  int res;
321
  pr_ctrls_t *ctrl;
322

323
  mark_point();
1✔
324
  res = pr_ctrls_add_response(NULL, NULL);
1✔
325
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
326
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
327
    strerror(errno), errno);
328

329
  mark_point();
1✔
330
  ctrl = pr_ctrls_alloc();
1✔
331
  res = pr_ctrls_add_response(ctrl, NULL);
1✔
332
  ck_assert_msg(res < 0, "Failed to handle null fmt");
2✔
333
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
334
    strerror(errno), errno);
335

336
  mark_point();
1✔
337
  res = pr_ctrls_add_response(ctrl, "%s", "foo");
1✔
338
  ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
2✔
339
}
340
END_TEST
1✔
341

342
START_TEST (ctrls_copy_args_test) {
1✔
343
  int res;
344
  pr_ctrls_t *src_ctrl, *dst_ctrl;
345

346
  mark_point();
1✔
347
  res = pr_ctrls_copy_args(NULL, NULL);
1✔
348
  ck_assert_msg(res < 0, "Failed to handle null src ctrl");
1✔
349
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
350
    strerror(errno), errno);
351

352
  mark_point();
1✔
353
  src_ctrl = pr_ctrls_alloc();
1✔
354
  res = pr_ctrls_copy_args(src_ctrl, NULL);
1✔
355
  ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
2✔
356
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
357
    strerror(errno), errno);
358

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

365
  mark_point();
1✔
366
  dst_ctrl = pr_ctrls_alloc();
1✔
367
  res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
1✔
368
  ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
2✔
369

370
  mark_point();
1✔
371
  res = pr_ctrls_add_arg(src_ctrl, "foo", 3);
1✔
372
  ck_assert_msg(res == 0, "Failed to add src ctrl arg: %s", strerror(errno));
2✔
373

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

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

380
  res = pr_ctrls_copy_args(src_ctrl, dst_ctrl);
1✔
381
  ck_assert_msg(res == 0, "Failed to copy ctrl args: %s", strerror(errno));
2✔
382
}
383
END_TEST
1✔
384

385
START_TEST (ctrls_copy_resps_test) {
1✔
386
  int res;
387
  pr_ctrls_t *src_ctrl, *dst_ctrl;
388

389
  mark_point();
1✔
390
  res = pr_ctrls_copy_resps(NULL, NULL);
1✔
391
  ck_assert_msg(res < 0, "Failed to handle null src ctrl");
1✔
392
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
393
    strerror(errno), errno);
394

395
  mark_point();
1✔
396
  src_ctrl = pr_ctrls_alloc();
1✔
397
  res = pr_ctrls_copy_resps(src_ctrl, NULL);
1✔
398
  ck_assert_msg(res < 0, "Failed to handle null dst ctrl");
2✔
399
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
400
    strerror(errno), errno);
401

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

408
  mark_point();
1✔
409
  dst_ctrl = pr_ctrls_alloc();
1✔
410
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
411
  ck_assert_msg(res < 0, "Failed to handle src ctrl with no responses");
2✔
412
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
413
    strerror(errno), errno);
414

415
  mark_point();
1✔
416
  res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
1✔
417
  ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
2✔
418

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

422
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
423
  ck_assert_msg(res < 0, "Failed to handle dst ctrl with 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
  src_ctrl = pr_ctrls_alloc();
1✔
429
  res = pr_ctrls_add_response(src_ctrl, "%s", "foo");
1✔
430
  ck_assert_msg(res == 0, "Failed to add src ctrl response: %s", strerror(errno));
2✔
431

432
  dst_ctrl = pr_ctrls_alloc();
1✔
433
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
434
  ck_assert_msg(res == 0, "Failed to copy ctrl responses: %s", strerror(errno));
2✔
435
}
436
END_TEST
1✔
437

438
START_TEST (ctrls_send_request_test) {
1✔
439
  int fd, res;
440
  char *action = "foo";
1✔
441
  unsigned int msgargc = 1;
1✔
442
  char *msgargv[] = { "bar", NULL };
1✔
443

444
  mark_point();
1✔
445
  res = pr_ctrls_send_request(NULL, -1, NULL, 0, NULL);
1✔
446
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
447
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
448
    strerror(errno), errno);
449

450
  mark_point();
1✔
451
  res = pr_ctrls_send_request(p, -1, NULL, 0, NULL);
1✔
452
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
453
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
454
    strerror(errno), errno);
455

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

463
  mark_point();
1✔
464
  res = pr_ctrls_send_request(p, fd, action, msgargc, NULL);
1✔
465
  ck_assert_msg(res < 0, "Failed to handle mismatched argc/argv");
2✔
466
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
467
    strerror(errno), errno);
468

469
  mark_point();
1✔
470
  fd = 7777;
1✔
471
  res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
1✔
472
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
473
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
474
    strerror(errno), errno);
475

476
  fd = devnull_fd();
1✔
477
  if (fd < 0) {
1✔
478
    return;
×
479
  }
480

481
  mark_point();
1✔
482
  res = pr_ctrls_send_request(p, fd, action, msgargc, msgargv);
1✔
483
  ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
1✔
484

485
  (void) close(fd);
1✔
486
}
487
END_TEST
488

489
START_TEST (ctrls_send_response_test) {
1✔
490
  int fd, res, status;
491
  unsigned int msgargc = 1;
1✔
492
  char *msgargv[] = { "foo", NULL };
1✔
493

494
  mark_point();
1✔
495
  res = pr_ctrls_send_response(NULL, -1, 0, 0, NULL);
1✔
496
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
497
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
498
    strerror(errno), errno);
499

500
  mark_point();
1✔
501
  res = pr_ctrls_send_response(p, -1, 0, 0, NULL);
1✔
502
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
503
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
504
    strerror(errno), errno);
505

506
  mark_point();
1✔
507
  fd = devnull_fd();
1✔
508
  if (fd < 0) {
1✔
509
    return;
×
510
  }
511
  res = pr_ctrls_send_response(p, fd, 0, 0, NULL);
1✔
512
  ck_assert_msg(res == 0, "Failed to send zero ctrl messages: %s",
1✔
513
    strerror(errno));
514

515
  mark_point();
1✔
516
  res = pr_ctrls_send_response(p, fd, 0, msgargc, NULL);
1✔
517
  ck_assert_msg(res < 0, "Failed to handle missing argv");
2✔
518
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
519
    strerror(errno), errno);
520
  (void) close(fd);
1✔
521

522
  mark_point();
1✔
523
  fd = 7777;
1✔
524
  status = -24;
1✔
525
  res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
1✔
526
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
527
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
528
    strerror(errno), errno);
529

530
  fd = devnull_fd();
1✔
531
  if (fd < 0) {
1✔
532
    return;
533
  }
534

535
  mark_point();
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 send ctrl message: %s", strerror(errno));
1✔
539

540
  (void) close(fd);
1✔
541
}
542
END_TEST
543

544
START_TEST (ctrls_flush_response_test) {
1✔
545
  int res;
546
  pr_ctrls_t *ctrl;
547
  pr_ctrls_cl_t *cl;
548

549
  mark_point();
1✔
550
  res = pr_ctrls_flush_response(NULL);
1✔
551
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
552
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
553
    strerror(errno), errno);
554

555
  mark_point();
1✔
556
  ctrl = pr_ctrls_alloc();
1✔
557
  res = pr_ctrls_flush_response(ctrl);
1✔
558
  ck_assert_msg(res == 0, "Failed to flush ctrl with no responses: %s",
2✔
559
    strerror(errno));
560

561
  mark_point();
1✔
562
  res = pr_ctrls_add_response(ctrl, "%s", "foo");
1✔
563
  ck_assert_msg(res == 0, "Failed to add ctrl response: %s", strerror(errno));
2✔
564
  res = pr_ctrls_flush_response(ctrl);
1✔
565
  ck_assert_msg(res < 0, "Failed to handle ctrl with no client");
2✔
566
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
567
    strerror(errno), errno);
568

569
  mark_point();
1✔
570
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
571
  cl->cl_fd = devnull_fd();
1✔
572
  if (cl->cl_fd < 0) {
1✔
573
    return;
574
  }
575

576
  ctrl->ctrls_cl = cl;
1✔
577
  res = pr_ctrls_flush_response(ctrl);
1✔
578
  ck_assert_msg(res == 0, "Failed to flush ctrl responses: %s", strerror(errno));
1✔
579

580
  mark_point();
1✔
581
  (void) close(cl->cl_fd);
1✔
582
  res = pr_ctrls_flush_response(ctrl);
1✔
583
  ck_assert_msg(res < 0, "Failed to handle bad fd");
2✔
584
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
585
    strerror(errno), errno);
586
}
587
END_TEST
588

589
START_TEST (ctrls_recv_request_invalid_test) {
1✔
590
  int fd, res;
591
  uint32_t msglen;
592
  char *msg = NULL;
1✔
593
  pr_ctrls_cl_t *cl;
594

595
  mark_point();
1✔
596
  res = pr_ctrls_recv_request(NULL);
1✔
597
  ck_assert_msg(res < 0, "Failed to handle null client");
1✔
598
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
599
    strerror(errno), errno);
600

601
  mark_point();
1✔
602
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
603
  res = pr_ctrls_recv_request(cl);
1✔
604
  ck_assert_msg(res < 0, "Failed to handle client without ctrls list");
2✔
605
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
606
    strerror(errno), errno);
607

608
  mark_point();
1✔
609
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
610
  cl->cl_fd = -1;
1✔
611
  res = pr_ctrls_recv_request(cl);
1✔
612
  ck_assert_msg(res < 0, "Failed to handle client without fd");
2✔
613
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
614
    strerror(errno), errno);
615

616
  mark_point();
1✔
617
  fd = tmpfile_fd();
1✔
618
  if (fd < 0) {
1✔
619
    return;
×
620
  }
621

622
  cl->cl_fd = fd;
1✔
623
  (void) close(cl->cl_fd);
1✔
624
  res = pr_ctrls_recv_request(cl);
1✔
625
  ck_assert_msg(res < 0, "Failed to handle client with bad fd");
1✔
626
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
627
    strerror(errno), errno);
628

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

635
  cl->cl_fd = fd;
1✔
636
  (void) write(fd, "a", 1);
1✔
637
  rewind_fd(fd);
1✔
638
  res = pr_ctrls_recv_request(cl);
1✔
639
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
1✔
640
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
641
    strerror(errno), errno);
642

643
  fd = reset_fd(fd);
1✔
644
  if (fd < 0) {
1✔
645
    return;
646
  }
647
  cl->cl_fd = fd;
1✔
648

649
  mark_point();
1✔
650
  msglen = 2;
1✔
651
  (void) write(fd, &msglen, sizeof(msglen));
1✔
652
  (void) write(fd, "a", 1);
1✔
653
  rewind_fd(fd);
1✔
654
  res = pr_ctrls_recv_request(cl);
1✔
655
  ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
1✔
656
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
657
    strerror(errno), errno);
658

659
  fd = reset_fd(fd);
1✔
660
  if (fd < 0) {
1✔
661
    return;
662
  }
663
  cl->cl_fd = fd;
1✔
664

665
  mark_point();
1✔
666
  msglen = 2;
1✔
667
  (void) write(fd, &msglen, sizeof(msglen));
1✔
668
  (void) write(fd, "aa", 2);
1✔
669
  rewind_fd(fd);
1✔
670
  res = pr_ctrls_recv_request(cl);
1✔
671
  ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
1✔
672
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
673
    strerror(errno), errno);
674

675
  fd = reset_fd(fd);
1✔
676
  if (fd < 0) {
1✔
677
    return;
678
  }
679
  cl->cl_fd = fd;
1✔
680

681
  mark_point();
1✔
682
  msg = "{}";
1✔
683
  msglen = strlen(msg);
1✔
684
  (void) write(fd, &msglen, sizeof(msglen));
1✔
685
  (void) write(fd, msg, msglen);
1✔
686
  rewind_fd(fd);
1✔
687
  res = pr_ctrls_recv_request(cl);
1✔
688
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'action')");
1✔
689
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
690
    strerror(errno), errno);
691

692
  fd = reset_fd(fd);
1✔
693
  if (fd < 0) {
1✔
694
    return;
695
  }
696
  cl->cl_fd = fd;
1✔
697

698
  mark_point();
1✔
699
  msg = "{\"action\":\"foo\"}";
1✔
700
  msglen = strlen(msg);
1✔
701
  (void) write(fd, &msglen, sizeof(msglen));
1✔
702
  (void) write(fd, msg, msglen);
1✔
703
  rewind_fd(fd);
1✔
704
  res = pr_ctrls_recv_request(cl);
1✔
705
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'args')");
1✔
706
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
707
    strerror(errno), errno);
708

709
  fd = reset_fd(fd);
1✔
710
  if (fd < 0) {
1✔
711
    return;
712
  }
713
  cl->cl_fd = fd;
1✔
714

715
  mark_point();
1✔
716
  msg = "{\"action\":\"test\",\"args\":[1,2,3,4,5,6,7,8,9,10]}";
1✔
717
  msglen = strlen(msg);
1✔
718
  (void) write(fd, &msglen, sizeof(msglen));
1✔
719
  (void) write(fd, msg, msglen);
1✔
720
  rewind_fd(fd);
1✔
721
  res = pr_ctrls_recv_request(cl);
1✔
722
  ck_assert_msg(res < 0, "Failed to handle unknown action");
1✔
723
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
724
    strerror(errno), errno);
725

726
  (void) close(fd);
1✔
727
}
728
END_TEST
729

730
START_TEST (ctrls_recv_request_valid_test) {
1✔
731
  int fd, res;
732
  uint32_t msglen = 0;
1✔
733
  char *msg = NULL;
1✔
734
  const char *action, *desc;
735
  pr_ctrls_cl_t *cl;
736
  pr_ctrls_t *ctrl;
737
  module m;
738

739
  mark_point();
1✔
740
  m.name = "test";
1✔
741
  action = "test";
1✔
742
  desc = "desc";
1✔
743
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
744
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1✔
745

746
  mark_point();
1✔
747
  fd = tmpfile_fd();
1✔
748
  if (fd < 0) {
1✔
749
    return;
×
750
  }
751

752
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
753
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
754
  cl->cl_fd = fd;
1✔
755

756
  msg = "{\"action\":\"test\",\"args\":[]}";
1✔
757
  msglen = strlen(msg);
1✔
758
  (void) write(fd, &msglen, sizeof(msglen));
1✔
759
  (void) write(fd, msg, msglen);
1✔
760
  rewind_fd(fd);
1✔
761
  res = pr_ctrls_recv_request(cl);
1✔
762
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
763
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
764
    cl->cl_ctrls->nelts);
765
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
766
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
767
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
768
  ck_assert_msg(ctrl->ctrls_cb_args == NULL,
2✔
769
    "Expected no callback args, got %p", ctrl->ctrls_cb_args);
770

771
  mark_point();
1✔
772
  fd = reset_fd(fd);
1✔
773
  if (fd < 0) {
1✔
774
    return;
775
  }
776
  clear_array(cl->cl_ctrls);
1✔
777
  cl->cl_fd = fd;
1✔
778

779
  msg = "{\"action\":\"test\",\"args\":[\"a\"]}";
1✔
780
  msglen = strlen(msg);
1✔
781
  (void) write(fd, &msglen, sizeof(msglen));
1✔
782
  (void) write(fd, msg, msglen);
1✔
783
  rewind_fd(fd);
1✔
784
  res = pr_ctrls_recv_request(cl);
1✔
785
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
786
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
787
    cl->cl_ctrls->nelts);
788
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
789
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
790
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
791
  ck_assert_msg(ctrl->ctrls_cb_args != NULL,
2✔
792
    "Expected callback args, got %p", ctrl->ctrls_cb_args);
793

794
  mark_point();
1✔
795
  fd = reset_fd(fd);
1✔
796
  if (fd < 0) {
1✔
797
    return;
798
  }
799
  clear_array(cl->cl_ctrls);
1✔
800
  cl->cl_fd = fd;
1✔
801

802
  msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
1✔
803
  msglen = strlen(msg);
1✔
804
  (void) write(fd, &msglen, sizeof(msglen));
1✔
805
  (void) write(fd, msg, msglen);
1✔
806
  rewind_fd(fd);
1✔
807
  res = pr_ctrls_recv_request(cl);
1✔
808
  ck_assert_msg(res == 0, "Failed to handle valid request: %s",
1✔
809
    strerror(errno));
810
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
811
    cl->cl_ctrls->nelts);
812
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
813
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
814
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
815
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
816
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
817
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
818

819
  /* next_action present */
820

821
  mark_point();
1✔
822
  res = pr_ctrls_register(&m, action, desc, ctrls_test2_cb);
1✔
823
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
824

825
  mark_point();
1✔
826
  fd = reset_fd(fd);
1✔
827
  if (fd < 0) {
1✔
828
    return;
829
  }
830
  clear_array(cl->cl_ctrls);
1✔
831
  cl->cl_fd = fd;
1✔
832

833
  msg = "{\"action\":\"test\",\"args\":[\"next\"]}";
1✔
834
  msglen = strlen(msg);
1✔
835
  (void) write(fd, &msglen, sizeof(msglen));
1✔
836
  (void) write(fd, msg, msglen);
1✔
837
  rewind_fd(fd);
1✔
838
  res = pr_ctrls_recv_request(cl);
1✔
839
  ck_assert_msg(res == 0, "Failed to handle valid request: %s",
1✔
840
    strerror(errno));
841
  ck_assert_msg(cl->cl_ctrls->nelts == 2, "Expected 2 ctrl, got %d",
2✔
842
    cl->cl_ctrls->nelts);
843

844
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
845
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
846
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
847
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
848
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
849
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
850

851
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[1];
1✔
852
  ck_assert_msg(ctrl->ctrls_flags & PR_CTRLS_FL_REQUESTED,
2✔
853
    "Expected PR_CTRLS_FL_REQUESTED flag, got %lu", ctrl->ctrls_flags);
854
  ck_assert_msg(ctrl->ctrls_cb_args != NULL, "Expected callback args, got NULL");
2✔
855
  ck_assert_msg(ctrl->ctrls_cb_args->nelts == 1,
2✔
856
    "Expected 1 callback arg, got %d", ctrl->ctrls_cb_args->nelts);
857

858
  (void) pr_ctrls_unregister(&m, action);
1✔
859
  (void) close(fd);
1✔
860
}
861
END_TEST
862

863
START_TEST (ctrls_recv_response_test) {
1✔
864
  int fd, res, status;
865
  uint32_t msglen;
866
  char *msg = NULL;
1✔
867

868
  mark_point();
1✔
869
  res = pr_ctrls_recv_response(NULL, -1, NULL, NULL);
1✔
870
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
871
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
872
    strerror(errno), errno);
873

874
  mark_point();
1✔
875
  res = pr_ctrls_recv_response(p, -1, NULL, NULL);
1✔
876
  ck_assert_msg(res < 0, "Failed to handle invalid fd");
2✔
877
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
878
    strerror(errno), errno);
879

880
  mark_point();
1✔
881
  fd = tmpfile_fd();
1✔
882
  if (fd < 0) {
1✔
883
    return;
×
884
  }
885

886
  res = pr_ctrls_recv_response(p, fd, NULL, NULL);
1✔
887
  ck_assert_msg(res < 0, "Failed to handle null status");
1✔
888
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
889
    strerror(errno), errno);
890
  (void) close(fd);
1✔
891

892
  mark_point();
1✔
893
  fd = tmpfile_fd();
1✔
894
  if (fd < 0) {
1✔
895
    return;
896
  }
897

898
  (void) write(fd, "a", 1);
1✔
899
  rewind_fd(fd);
1✔
900
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
901
  ck_assert_msg(res < 0, "Failed to handle invalid msglen (too short)");
1✔
902
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
903
    strerror(errno), errno);
904

905
  fd = reset_fd(fd);
1✔
906
  if (fd < 0) {
1✔
907
    return;
908
  }
909

910
  mark_point();
1✔
911
  msglen = 2;
1✔
912
  (void) write(fd, &msglen, sizeof(msglen));
1✔
913
  (void) write(fd, "a", 1);
1✔
914
  rewind_fd(fd);
1✔
915
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
916
  ck_assert_msg(res < 0, "Failed to handle invalid message (too short)");
1✔
917
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
918
    strerror(errno), errno);
919

920
  fd = reset_fd(fd);
1✔
921
  if (fd < 0) {
1✔
922
    return;
923
  }
924

925
  mark_point();
1✔
926
  msglen = 2;
1✔
927
  (void) write(fd, &msglen, sizeof(msglen));
1✔
928
  (void) write(fd, "aa", 2);
1✔
929
  rewind_fd(fd);
1✔
930
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
931
  ck_assert_msg(res < 0, "Failed to handle invalid message (wrong format)");
1✔
932
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
933
    strerror(errno), errno);
934

935
  fd = reset_fd(fd);
1✔
936
  if (fd < 0) {
1✔
937
    return;
938
  }
939

940
  mark_point();
1✔
941
  msg = "{}";
1✔
942
  msglen = strlen(msg);
1✔
943
  (void) write(fd, &msglen, sizeof(msglen));
1✔
944
  (void) write(fd, msg, msglen);
1✔
945
  rewind_fd(fd);
1✔
946
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
947
  ck_assert_msg(res < 0, "Failed to handle invalid message (missing 'status')");
1✔
948
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
949
    strerror(errno), errno);
950

951
  fd = reset_fd(fd);
1✔
952
  if (fd < 0) {
1✔
953
    return;
954
  }
955

956
  mark_point();
1✔
957
  msg = "{\"status\":0}";
1✔
958
  msglen = strlen(msg);
1✔
959
  (void) write(fd, &msglen, sizeof(msglen));
1✔
960
  (void) write(fd, msg, msglen);
1✔
961
  rewind_fd(fd);
1✔
962
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
963
  ck_assert_msg(res < 0,
1✔
964
    "Failed to handle invalid message (missing 'responses')");
965
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
966
    strerror(errno), errno);
967

968
  fd = reset_fd(fd);
1✔
969
  if (fd < 0) {
1✔
970
    return;
971
  }
972

973
  mark_point();
1✔
974
  msg = "{\"status\":0,\"responses\":[1]}";
1✔
975
  msglen = strlen(msg);
1✔
976
  (void) write(fd, &msglen, sizeof(msglen));
1✔
977
  (void) write(fd, msg, msglen);
1✔
978
  rewind_fd(fd);
1✔
979
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
980
  ck_assert_msg(res < 0,
1✔
981
    "Failed to handle invalid message (non-text response)");
982
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
983
    strerror(errno), errno);
984

985
  fd = reset_fd(fd);
1✔
986
  if (fd < 0) {
1✔
987
    return;
988
  }
989

990
  mark_point();
1✔
991
  msg = "{\"status\":0,\"responses\":[\"ok\"]}";
1✔
992
  msglen = strlen(msg);
1✔
993
  (void) write(fd, &msglen, sizeof(msglen));
1✔
994
  (void) write(fd, msg, msglen);
1✔
995
  rewind_fd(fd);
1✔
996
  res = pr_ctrls_recv_response(p, fd, &status, NULL);
1✔
997
  ck_assert_msg(res == 1, "Failed to handle valid response: %s",
1✔
998
    strerror(errno));
999

1000
  (void) close(fd);
1✔
1001
}
1002
END_TEST
1003

1004
START_TEST (ctrls_issock_unix_test) {
1✔
1005
  int res;
1006
  mode_t mode;
1007

1008
  mark_point();
1✔
1009
  mode = 0;
1✔
1010
  res = pr_ctrls_issock_unix(mode);
1✔
1011
  ck_assert_msg(res < 0, "Failed to handle invalid mode");
1✔
1012
  ck_assert_msg(errno == ENOSYS, "Expected ENOSYS (%d), got %s (%d)", ENOSYS,
2✔
1013
    strerror(errno), errno);
1014

1015
#if defined(S_ISFIFO)
1016
  mark_point();
1✔
1017
  mode = 0;
1✔
1018
  mode |= S_IFIFO;
1✔
1019
  res = pr_ctrls_issock_unix(mode);
1✔
1020
  if (res < 0) {
1✔
1021
    ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
1✔
1022
  }
1023
#endif /* S_ISFIFO */
1024

1025
#if defined(S_ISSOCK)
1026
  mark_point();
1✔
1027
  mode = 0;
1✔
1028
  mode |= S_IFSOCK;
1✔
1029
  res = pr_ctrls_issock_unix(mode);
1✔
1030
  if (res < 0) {
1✔
1031
    ck_assert_msg(errno == ENOSYS, "Did not expect ENOSYS (%d)", ENOSYS);
×
1032
  }
1033
#endif /* S_ISSOCK */
1034
}
1035
END_TEST
1✔
1036

1037
START_TEST (ctrls_get_registered_actions_test) {
1✔
1038
  int res;
1039
  pr_ctrls_t *ctrl;
1040
  const char *action, *desc;
1041
  module m;
1042

1043
  mark_point();
1✔
1044
  res = pr_get_registered_actions(NULL, 0);
1✔
1045
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
1046
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1047
    strerror(errno), errno);
1048

1049
  mark_point();
1✔
1050
  ctrl = pr_ctrls_alloc();
1✔
1051
  res = pr_get_registered_actions(ctrl, 0);
1✔
1052
  ck_assert_msg(res == 0, "Failed to handle lack of registered actions: %s",
2✔
1053
    strerror(errno));
1054

1055
  mark_point();
1✔
1056
  pr_block_ctrls();
1✔
1057
  res = pr_get_registered_actions(ctrl, 0);
1✔
1058
  ck_assert_msg(res < 0, "Failed to handle blocked actions");
2✔
1059
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1060
    strerror(errno), errno);
1061
  pr_unblock_ctrls();
1✔
1062

1063
  mark_point();
1✔
1064
  action = "test";
1✔
1065
  desc = "desc";
1✔
1066
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1067
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1068

1069
  m.name = "test";
1✔
1070
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
1071
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1072

1073
  mark_point();
1✔
1074
  res = pr_get_registered_actions(ctrl, 0);
1✔
1075
  ck_assert_msg(res == 0, "Failed to handle invalid flags: %s", strerror(errno));
2✔
1076

1077
  mark_point();
1✔
1078
  res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ALL);
1✔
1079
  ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ALL flag: %s",
2✔
1080
    strerror(errno));
1081

1082
  mark_point();
1✔
1083
  res = pr_get_registered_actions(ctrl, CTRLS_GET_ACTION_ENABLED);
1✔
1084
  ck_assert_msg(res == 2, "Failed to handle GET_ACTION_ENABLED flag: %s",
2✔
1085
    strerror(errno));
1086

1087
  mark_point();
1✔
1088
  res = pr_get_registered_actions(ctrl, CTRLS_GET_DESC);
1✔
1089
  ck_assert_msg(res == 2, "Failed to handle GET_DESC flag: %s", strerror(errno));
2✔
1090

1091
  mark_point();
1✔
1092
  res = pr_ctrls_unregister(NULL, action);
1✔
1093
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1094
    strerror(errno));
1095
}
1096
END_TEST
1✔
1097

1098
START_TEST (ctrls_set_registered_actions_test) {
1✔
1099
  int res;
1100
  const char *action, *desc;
1101

1102
  mark_point();
1✔
1103
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1104
  ck_assert_msg(res < 0, "Failed to handle no registered actions");
1✔
1105
  ck_assert_msg(errno == ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
2✔
1106
    strerror(errno), errno);
1107

1108
  mark_point();
1✔
1109
  res = pr_set_registered_actions(NULL, NULL, FALSE, 24);
1✔
1110
  ck_assert_msg(res < 0, "Failed to handle invalid flag");
2✔
1111
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1112
    strerror(errno), errno);
1113

1114
  mark_point();
1✔
1115
  action = "test";
1✔
1116
  desc = "desc";
1✔
1117
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1118
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1119

1120
  mark_point();
1✔
1121
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1122
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
2✔
1123
    strerror(errno));
1124

1125
  mark_point();
1✔
1126
  pr_block_ctrls();
1✔
1127
  res = pr_set_registered_actions(NULL, NULL, FALSE, 0);
1✔
1128
  ck_assert_msg(res < 0, "Failed to handle blocked actions");
2✔
1129
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1130
    strerror(errno), errno);
1131
  pr_unblock_ctrls();
1✔
1132

1133
  mark_point();
1✔
1134
  res = pr_set_registered_actions(NULL, action, FALSE, 0);
1✔
1135
  ck_assert_msg(res == 0, "Failed to handle action '%s': %s", action,
2✔
1136
    strerror(errno));
1137

1138
  mark_point();
1✔
1139
  res = pr_set_registered_actions(NULL, "all", FALSE, 0);
1✔
1140
  ck_assert_msg(res == 0, "Failed to handle action 'all': %s", strerror(errno));
2✔
1141

1142
  mark_point();
1✔
1143
  res = pr_ctrls_unregister(NULL, action);
1✔
1144
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1145
    strerror(errno));
1146
}
1147
END_TEST
1✔
1148

1149
START_TEST (ctrls_check_actions_test) {
1✔
1150
  int res;
1151
  const char *action, *desc;
1152

1153
  mark_point();
1✔
1154
  res = pr_ctrls_check_actions();
1✔
1155
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
1✔
1156
    strerror(errno));
1157

1158
  mark_point();
1✔
1159
  action = "test";
1✔
1160
  desc = "desc";
1✔
1161
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1162
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1163

1164
  mark_point();
1✔
1165
  res = pr_ctrls_check_actions();
1✔
1166
  ck_assert_msg(res == 0, "Failed to handle no registered actions: %s",
2✔
1167
    strerror(errno));
1168

1169
  /* Register a duplicate action name, then check. */
1170

1171
  mark_point();
1✔
1172
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1173
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1174

1175
  mark_point();
1✔
1176
  res = pr_ctrls_check_actions();
1✔
1177
  ck_assert_msg(res == 0, "Failed to handle registered actions: %s",
2✔
1178
    strerror(errno));
1179

1180
  mark_point();
1✔
1181
  res = pr_set_registered_actions(NULL, action, FALSE, PR_CTRLS_ACT_SOLITARY);
1✔
1182
  ck_assert_msg(res == 0, "Failed to set SOLITARY action flag: %s",
2✔
1183
    strerror(errno));
1184

1185
  mark_point();
1✔
1186
  res = pr_ctrls_check_actions();
1✔
1187
  ck_assert_msg(res < 0, "Failed to handle duplicate SOLITARY actions");
2✔
1188
  ck_assert_msg(errno == EEXIST, "Expected EEXIST (%d), got %s (%d)", EEXIST,
2✔
1189
    strerror(errno), errno);
1190

1191
  mark_point();
1✔
1192
  res = pr_ctrls_unregister(NULL, action);
1✔
1193
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1194
    strerror(errno));
1195
}
1196
END_TEST
1✔
1197

1198
START_TEST (ctrls_run_ctrls_test) {
1✔
1199
  int fd, res;
1200
  uint32_t msglen = 0;
1✔
1201
  char *msg = NULL;
1✔
1202
  const char *action, *desc;
1203
  pr_ctrls_cl_t *cl;
1204
  pr_ctrls_t *ctrl;
1205
  module m, m2;
1206
  time_t now;
1207

1208
  mark_point();
1✔
1209
  res = pr_run_ctrls(NULL, NULL);
1✔
1210
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1✔
1211

1212
  mark_point();
1✔
1213
  pr_block_ctrls();
1✔
1214
  res = pr_run_ctrls(NULL, NULL);
1✔
1215
  ck_assert_msg(res < 0, "Failed to handle blocked ctrls");
2✔
1216
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1217
    strerror(errno), errno);
1218
  pr_unblock_ctrls();
1✔
1219

1220
  mark_point();
1✔
1221
  action = "test";
1✔
1222
  desc = "desc";
1✔
1223
  m.name = "test";
1✔
1224
  m2.name = "test2";
1✔
1225
  res = pr_ctrls_register(&m, 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_run_ctrls(NULL, NULL);
1✔
1230
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1231

1232
  mark_point();
1✔
1233
  res = pr_run_ctrls(&m2, NULL);
1✔
1234
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1235

1236
  mark_point();
1✔
1237
  res = pr_run_ctrls(&m, NULL);
1✔
1238
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1239

1240
  /* XXX TODO More test cases to fill in here. */
1241
  /* Note that pr_run_ctrls() makes a lot of assumptions about recv_response,
1242
   * recv_request having been called previously.  Not great.  How to deal
1243
   * with that?
1244
   */
1245

1246
  mark_point();
1✔
1247
  fd = tmpfile_fd();
1✔
1248
  if (fd < 0) {
1✔
1249
    return;
×
1250
  }
1251

1252
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
1253
  cl->cl_ctrls = make_array(p, 0, sizeof(pr_ctrls_t *));
1✔
1254
  cl->cl_fd = fd;
1✔
1255

1256
  msg = "{\"action\":\"test\",\"args\":[\"FOO\"]}";
1✔
1257
  msglen = strlen(msg);
1✔
1258
  (void) write(fd, &msglen, sizeof(msglen));
1✔
1259
  (void) write(fd, msg, msglen);
1✔
1260
  rewind_fd(fd);
1✔
1261
  res = pr_ctrls_recv_request(cl);
1✔
1262
  ck_assert_msg(res == 0, "Failed to handle known action: %s", strerror(errno));
1✔
1263
  ck_assert_msg(cl->cl_ctrls->nelts == 1, "Expected 1 ctrl, got %d",
2✔
1264
    cl->cl_ctrls->nelts);
1265
  ctrl = ((pr_ctrls_t **) cl->cl_ctrls->elts)[0];
1✔
1266

1267
  mark_point();
1✔
1268
  res = pr_run_ctrls(&m2, NULL);
1✔
1269
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1270

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

1275
  mark_point();
1✔
1276
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1277
  ctrl->ctrls_flags |= PR_CTRLS_ACT_DISABLED;
1✔
1278
  res = pr_run_ctrls(&m, NULL);
1✔
1279
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1280

1281
  mark_point();
1✔
1282
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1283
  ctrl->ctrls_flags &= ~PR_CTRLS_ACT_DISABLED;
1✔
1284
  ctrl->ctrls_flags &= ~PR_CTRLS_FL_REQUESTED;
1✔
1285
  res = pr_run_ctrls(&m, NULL);
1✔
1286
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1287

1288
  mark_point();
1✔
1289
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1290
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1291
  now = time(NULL);
1✔
1292
  ctrl->ctrls_when = now + 10;
1✔
1293
  res = pr_run_ctrls(&m, NULL);
1✔
1294
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1295

1296
  mark_point();
1✔
1297
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1298
  ctrl->ctrls_flags &= PR_CTRLS_FL_PENDING;
1✔
1299
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1300
  ctrl->ctrls_when = now - 10;
1✔
1301
  res = pr_run_ctrls(&m, "test2");
1✔
1302
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1303

1304
  mark_point();
1✔
1305
  cl->cl_flags = PR_CTRLS_CL_HAVEREQ;
1✔
1306
  ctrl->ctrls_flags |= PR_CTRLS_FL_REQUESTED;
1✔
1307
  res = pr_run_ctrls(&m, "test");
1✔
1308
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
2✔
1309

1310
  mark_point();
1✔
1311
  res = pr_ctrls_unregister(NULL, action);
1✔
1312
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1313
    strerror(errno));
1314

1315
  (void) close(fd);
1✔
1316
}
1317
END_TEST
1318

1319
START_TEST (ctrls_reset_ctrls_test) {
1✔
1320
  int res;
1321
  const char *action, *desc;
1322

1323
  mark_point();
1✔
1324
  res = pr_ctrls_reset();
1✔
1325
  ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
1✔
1326

1327
  mark_point();
1✔
1328
  action = "test";
1✔
1329
  desc = "desc";
1✔
1330
  res = pr_ctrls_register(NULL, action, desc, ctrls_test_cb);
1✔
1331
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
2✔
1332

1333
  mark_point();
1✔
1334
  res = pr_ctrls_reset();
1✔
1335
  ck_assert_msg(res == 0, "Failed to reset ctrls: %s", strerror(errno));
2✔
1336

1337
  mark_point();
1✔
1338
  res = pr_ctrls_unregister(NULL, action);
1✔
1339
  ck_assert_msg(res == 0, "Failed to unregister ctrls action: %s",
2✔
1340
    strerror(errno));
1341
}
1342
END_TEST
1✔
1343

1344
START_TEST (ctrls_accept_test) {
1✔
1345
  int fd, res;
1346

1347
  mark_point();
1✔
1348
  res = pr_ctrls_accept(-1, NULL, NULL, NULL, 0);
1✔
1349
  ck_assert_msg(res < 0, "Failed to handle bad fd");
1✔
1350
  ck_assert_msg(errno == EBADF, "Expected EBADF (%d), got %s (%d)", EBADF,
2✔
1351
    strerror(errno), errno);
1352

1353
  mark_point();
1✔
1354
  fd = devnull_fd();
1✔
1355
  if (fd < 0) {
1✔
1356
    return;
1357
  }
1358

1359
  res = pr_ctrls_accept(fd, NULL, NULL, NULL, 5);
1✔
1360
  ck_assert_msg(res < 0, "Failed to handle no clients");
1✔
1361
  ck_assert_msg(errno = ENOENT, "Expected ENOENT (%d), got %s (%d)", ENOENT,
1✔
1362
    strerror(errno), errno);
1363

1364
  (void) close(fd);
1✔
1365
}
1366
END_TEST
1367

1368
START_TEST (ctrls_connect_test) {
1✔
1369
  int fd, res;
1370
  const char *socket_path;
1371

1372
  mark_point();
1✔
1373
  res = pr_ctrls_connect(NULL);
1✔
1374
  ck_assert_msg(res < 0, "Failed to handle null path");
1✔
1375
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1376
    strerror(errno), errno);
1377

1378
  mark_point();
1✔
1379
  socket_path = "/tmp/foo.sock";
1✔
1380
  res = pr_ctrls_connect(socket_path);
1✔
1381
  ck_assert_msg(res < 0, "Failed to handle nonexistent socket path");
2✔
1382
  ck_assert_msg(errno == ECONNREFUSED || errno == ENOENT,
2✔
1383
    "Expected ECONNREFUSED (%d) or ENOENT (%d), got %s (%d)", ECONNREFUSED,
1384
    ENOENT, strerror(errno), errno);
1385

1386
  mark_point();
1✔
1387
  fd = listen_unix(socket_path);
1✔
1388
  if (fd < 0) {
1✔
1389
    return;
1390
  }
1391

1392
  res = pr_ctrls_connect(socket_path);
1✔
1393
  ck_assert_msg(res >= 0, "Failed to connect to local socket: %s",
1✔
1394
    strerror(errno));
1395

1396
  (void) close(res);
1✔
1397
  (void) close(fd);
1✔
1398
  (void) unlink(socket_path);
1✔
1399
}
1400
END_TEST
1401

1402
START_TEST (ctrls_check_group_acl_test) {
1✔
1403
  int res;
1404
  gid_t gid;
1405
  ctrls_group_acl_t *group_acl;
1406

1407
  mark_point();
1✔
1408
  res = pr_ctrls_check_group_acl(0, NULL);
1✔
1409
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1410
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1411
    strerror(errno), errno);
1412

1413
  mark_point();
1✔
1414
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1415
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1416
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1417

1418
  mark_point();
1✔
1419
  group_acl->allow = TRUE;
1✔
1420
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1421
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1422

1423
  mark_point();
1✔
1424
  group_acl->ngids = 1;
1✔
1425
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1426
  ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
2✔
1427
    group_acl->allow, res);
1428

1429
  group_acl->allow = FALSE;
1✔
1430
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1431
  ck_assert_msg(res == group_acl->allow, "Expected %d, got %d",
2✔
1432
    group_acl->allow, res);
1433

1434
  mark_point();
1✔
1435
  gid = 1;
1✔
1436
  group_acl->allow = TRUE;
1✔
1437
  group_acl->gids = palloc(p, sizeof(gid_t) * 2);
1✔
1438
  ((gid_t *) group_acl->gids)[0] = gid;
1✔
1439
  res = pr_ctrls_check_group_acl(0, group_acl);
1✔
1440
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1441

1442
  res = pr_ctrls_check_group_acl(gid, group_acl);
1✔
1443
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1444
}
1445
END_TEST
1✔
1446

1447
START_TEST (ctrls_check_user_acl_test) {
1✔
1448
  int res;
1449
  uid_t uid;
1450
  ctrls_user_acl_t *user_acl;
1451

1452
  mark_point();
1✔
1453
  res = pr_ctrls_check_user_acl(0, NULL);
1✔
1454
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1455
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1456
    strerror(errno), errno);
1457

1458
  mark_point();
1✔
1459
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1460
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1461
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1462

1463
  mark_point();
1✔
1464
  user_acl->allow = TRUE;
1✔
1465
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1466
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1467

1468
  mark_point();
1✔
1469
  user_acl->nuids = 1;
1✔
1470
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1471
  ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
2✔
1472
    user_acl->allow, res);
1473

1474
  user_acl->allow = FALSE;
1✔
1475
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1476
  ck_assert_msg(res == user_acl->allow, "Expected %d, got %d",
2✔
1477
    user_acl->allow, res);
1478

1479
  mark_point();
1✔
1480
  uid = 1;
1✔
1481
  user_acl->allow = TRUE;
1✔
1482
  user_acl->uids = palloc(p, sizeof(uid_t) * 2);
1✔
1483
  ((uid_t *) user_acl->uids)[0] = uid;
1✔
1484
  res = pr_ctrls_check_user_acl(0, user_acl);
1✔
1485
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1486

1487
  res = pr_ctrls_check_user_acl(uid, user_acl);
1✔
1488
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1489
}
1490
END_TEST
1✔
1491

1492
START_TEST (ctrls_init_acl_test) {
1✔
1493
  int res;
1494
  ctrls_acl_t *acl;
1495

1496
  mark_point();
1✔
1497
  res = pr_ctrls_init_acl(NULL);
1✔
1498
  ck_assert_msg(res < 0, "Failed to handle null acl");
1✔
1499
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1500
    strerror(errno), errno);
1501

1502
  mark_point();
1✔
1503
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1504
  res = pr_ctrls_init_acl(acl);
1✔
1505
  ck_assert_msg(res == 0, "Failed to init acl: %s", strerror(errno));
2✔
1506
}
1507
END_TEST
1✔
1508

1509
static int test_action_cb(pr_ctrls_t *ctl, int reqargc, char **reqargv) {
×
1510
  return 0;
×
1511
}
1512

1513
START_TEST (ctrls_check_acl_test) {
1✔
1514
  int res;
1515
  pr_ctrls_t *ctrl;
1516
  pr_ctrls_cl_t *cl;
1517
  ctrls_acl_t *acl;
1518
  const char *action;
1519
  ctrls_acttab_t acttab[] = {
1✔
1520
    { "test", "desc", NULL, test_action_cb },
1521
    { NULL, NULL, NULL, NULL }
1522
  };
1523

1524
  mark_point();
1✔
1525
  res = pr_ctrls_check_acl(NULL, NULL, NULL);
1✔
1526
  ck_assert_msg(res < 0, "Failed to handle null ctrl");
1✔
1527
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1528
    strerror(errno), errno);
1529

1530
  mark_point();
1✔
1531
  ctrl = pr_ctrls_alloc();
1✔
1532
  res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1✔
1533
  ck_assert_msg(res < 0, "Failed to handle ctrl without client");
2✔
1534
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1535
    strerror(errno), errno);
1536

1537
  mark_point();
1✔
1538
  cl = pcalloc(p, sizeof(pr_ctrls_cl_t));
1✔
1539
  cl->cl_uid = cl->cl_gid = 1;
1✔
1540
  ctrl->ctrls_cl = cl;
1✔
1541
  res = pr_ctrls_check_acl(ctrl, NULL, NULL);
1✔
1542
  ck_assert_msg(res < 0, "Failed to handle null acttab");
2✔
1543
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1544
    strerror(errno), errno);
1545

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

1552
  mark_point();
1✔
1553
  action = "foobar";
1✔
1554
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1555
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1556

1557
  mark_point();
1✔
1558
  action = "test";
1✔
1559
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1560
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1561

1562
  mark_point();
1✔
1563
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1564
  res = pr_ctrls_init_acl(acl);
1✔
1565
  acttab[0].act_acl = acl;
1✔
1566
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1567
  ck_assert_msg(res == FALSE, "Expected FALSE, got %d", res);
2✔
1568

1569
  mark_point();
1✔
1570
  acl->acl_groups.ngids = 1;
1✔
1571
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1572
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1573

1574
  acl->acl_users.nuids = 1;
1✔
1575
  res = pr_ctrls_check_acl(ctrl, acttab, action);
1✔
1576
  ck_assert_msg(res == TRUE, "Expected TRUE, got %d", res);
2✔
1577
}
1578
END_TEST
1✔
1579

1580
START_TEST (ctrls_parse_acl_test) {
1✔
1581
  char **res;
1582
  const char *names;
1583

1584
  mark_point();
1✔
1585
  res = pr_ctrls_parse_acl(NULL, NULL);
1✔
1586
  ck_assert_msg(res == NULL, "Failed to handle null pool");
1✔
1587
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1588
    strerror(errno), errno);
1589

1590
  mark_point();
1✔
1591
  res = pr_ctrls_parse_acl(p, NULL);
1✔
1592
  ck_assert_msg(res == NULL, "Failed to handle null ACL text");
2✔
1593
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1594
    strerror(errno), errno);
1595

1596
  mark_point();
1✔
1597
  names = "foo";
1✔
1598
  res = pr_ctrls_parse_acl(p, names);
1✔
1599
  ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
2✔
1600
    strerror(errno));
1601
  ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
2✔
1602
  ck_assert_msg(res[1] == NULL, "Expected NULL, got %p", res[1]);
2✔
1603

1604
  mark_point();
1✔
1605
  names = "foo,'Bar',BAZ";
1✔
1606
  res = pr_ctrls_parse_acl(p, names);
1✔
1607
  ck_assert_msg(res != NULL, "Failed to parse ACL '%s': %s", names,
2✔
1608
    strerror(errno));
1609
  ck_assert_msg(strcmp(res[0], "foo") == 0, "Expected 'foo', got '%s'", res[0]);
2✔
1610
  ck_assert_msg(strcmp(res[1], "'Bar'") == 0, "Expected 'Bar', got '%s'", res[1]);
2✔
1611
  ck_assert_msg(strcmp(res[2], "BAZ") == 0, "Expected 'BAZ', got '%s'", res[2]);
2✔
1612
  ck_assert_msg(res[3] == NULL, "Expected NULL, got %p", res[3]);
2✔
1613
}
1614
END_TEST
1✔
1615

1616
START_TEST (ctrls_set_group_acl_test) {
1✔
1617
  int res;
1618
  ctrls_group_acl_t *group_acl;
1619
  const char *allow;
1620
  char *grouplist;
1621

1622
  mark_point();
1✔
1623
  res = pr_ctrls_set_group_acl(NULL, NULL, NULL, NULL);
1✔
1624
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
1625
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1626
    strerror(errno), errno);
1627

1628
  mark_point();
1✔
1629
  res = pr_ctrls_set_group_acl(p, NULL, NULL, NULL);
1✔
1630
  ck_assert_msg(res < 0, "Failed to handle null group_acl");
2✔
1631
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1632
    strerror(errno), errno);
1633

1634
  mark_point();
1✔
1635
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1636
  res = pr_ctrls_set_group_acl(p, group_acl, NULL, NULL);
1✔
1637
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1638
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1639
    strerror(errno), errno);
1640

1641
  mark_point();
1✔
1642
  allow = "allow";
1✔
1643
  res = pr_ctrls_set_group_acl(p, group_acl, allow, NULL);
1✔
1644
  ck_assert_msg(res < 0, "Failed to handle null grouplist");
2✔
1645
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1646
    strerror(errno), errno);
1647

1648
  mark_point();
1✔
1649
  grouplist = "foo,bar,baz,wheel";
1✔
1650
  res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1✔
1651
  ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
2✔
1652
  ck_assert_msg(group_acl->allow == TRUE, "Expected TRUE, got %d",
2✔
1653
    group_acl->allow);
1654
  /* Note that we expect zero here, because of name/GID lookup failures. */
1655
  ck_assert_msg(group_acl->ngids == 0, "Expected 0, got %d",
2✔
1656
    group_acl->ngids);
1657
  ck_assert_msg(group_acl->gids != NULL, "Got NULL unexpectedly");
2✔
1658

1659
  mark_point();
1✔
1660
  group_acl = pcalloc(p, sizeof(ctrls_group_acl_t));
1✔
1661
  allow = "deny";
1✔
1662
  grouplist = "foo,*";
1✔
1663
  res = pr_ctrls_set_group_acl(p, group_acl, allow, grouplist);
1✔
1664
  ck_assert_msg(res == 0, "Failed to set group acl: %s", strerror(errno));
2✔
1665
  ck_assert_msg(group_acl->allow == FALSE, "Expected FALSE, got %d",
2✔
1666
    group_acl->allow);
1667
  ck_assert_msg(group_acl->ngids == 1, "Expected 1, got %d",
2✔
1668
    group_acl->ngids);
1669
  ck_assert_msg(group_acl->gids == NULL, "Expected NULL, got %p",
2✔
1670
    group_acl->gids);
1671
}
1672
END_TEST
1✔
1673

1674
START_TEST (ctrls_set_user_acl_test) {
1✔
1675
  int res;
1676
  ctrls_user_acl_t *user_acl;
1677
  const char *allow;
1678
  char *userlist;
1679

1680
  mark_point();
1✔
1681
  res = pr_ctrls_set_user_acl(NULL, NULL, NULL, NULL);
1✔
1682
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
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_set_user_acl(p, NULL, NULL, NULL);
1✔
1688
  ck_assert_msg(res < 0, "Failed to handle null user_acl");
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
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1694
  res = pr_ctrls_set_user_acl(p, user_acl, NULL, NULL);
1✔
1695
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1696
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1697
    strerror(errno), errno);
1698

1699
  mark_point();
1✔
1700
  allow = "allow";
1✔
1701
  res = pr_ctrls_set_user_acl(p, user_acl, allow, NULL);
1✔
1702
  ck_assert_msg(res < 0, "Failed to handle null userlist");
2✔
1703
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1704
    strerror(errno), errno);
1705

1706
  mark_point();
1✔
1707
  userlist = "foo,bar,baz,root";
1✔
1708
  res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1✔
1709
  ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
2✔
1710
  ck_assert_msg(user_acl->allow == TRUE, "Expected TRUE, got %d",
2✔
1711
    user_acl->allow);
1712
  /* Note that we expect zero here, because of name/UID lookup failures. */
1713
  ck_assert_msg(user_acl->nuids == 0, "Expected 0, got %d", user_acl->nuids);
2✔
1714
  ck_assert_msg(user_acl->uids != NULL, "Got NULL unexpectedly");
2✔
1715

1716
  mark_point();
1✔
1717
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1718
  allow = "deny";
1✔
1719
  userlist = "foo,*";
1✔
1720
  res = pr_ctrls_set_user_acl(p, user_acl, allow, userlist);
1✔
1721
  ck_assert_msg(res == 0, "Failed to set user acl: %s", strerror(errno));
2✔
1722
  ck_assert_msg(user_acl->allow == FALSE, "Expected FALSE, got %d",
2✔
1723
    user_acl->allow);
1724
  ck_assert_msg(user_acl->nuids == 1, "Expected 1, got %d", user_acl->nuids);
2✔
1725
  ck_assert_msg(user_acl->uids == NULL, "Expected NULL, got %p",
2✔
1726
    user_acl->uids);
1727
}
1728
END_TEST
1✔
1729

1730
START_TEST (ctrls_set_module_acls_test) {
1✔
1731
  char *res, *list;
1732
  const char *allow, *type;
1733
  ctrls_acl_t *acl;
1734
  char *good_actions[] = { "test", NULL };
1✔
1735
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1736
  char *all_actions[] = { "all", NULL };
1✔
1737
  ctrls_acttab_t acttab[] = {
1✔
1738
    { "test", "desc", NULL, test_action_cb },
1739
    { NULL, NULL, NULL, NULL }
1740
  };
1741

1742
  mark_point();
1✔
1743
  res = pr_ctrls_set_module_acls(NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1744
  ck_assert_msg(res == NULL, "Failed to handle null acttab");
1✔
1745
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1746
    strerror(errno), errno);
1747

1748
  mark_point();
1✔
1749
  res = pr_ctrls_set_module_acls(acttab, NULL, NULL, NULL, NULL, NULL);
1✔
1750
  ck_assert_msg(res == NULL, "Failed to handle null acl_pool");
2✔
1751
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1752
    strerror(errno), errno);
1753

1754
  mark_point();
1✔
1755
  res = pr_ctrls_set_module_acls(acttab, p, NULL, NULL, NULL, NULL);
1✔
1756
  ck_assert_msg(res == NULL, "Failed to handle null actions");
2✔
1757
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1758
    strerror(errno), errno);
1759

1760
  mark_point();
1✔
1761
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, NULL, NULL, NULL);
1✔
1762
  ck_assert_msg(res == NULL, "Failed to handle null allow");
2✔
1763
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1764
    strerror(errno), errno);
1765

1766
  mark_point();
1✔
1767
  allow = "allow";
1✔
1768
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, NULL, NULL);
1✔
1769
  ck_assert_msg(res == NULL, "Failed to handle null type");
2✔
1770
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1771
    strerror(errno), errno);
1772

1773
  mark_point();
1✔
1774
  type = "test";
1✔
1775
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, NULL);
1✔
1776
  ck_assert_msg(res == NULL, "Failed to handle null list");
2✔
1777
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1778
    strerror(errno), errno);
1779

1780
  mark_point();
1✔
1781
  list = "foo,bar,baz";
1✔
1782
  res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1✔
1783
  ck_assert_msg(res == NULL, "Failed to handle invalid type '%s'", type);
2✔
1784
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1785
    strerror(errno), errno);
1786

1787
  mark_point();
1✔
1788
  type = "user";
1✔
1789
  res = pr_ctrls_set_module_acls(acttab, p, bad_actions, allow, type, list);
1✔
1790
  ck_assert_msg(res != NULL, "Failed to handle invalid action");
2✔
1791
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1792
    strerror(errno), errno);
1793
  ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
2✔
1794

1795
  mark_point();
1✔
1796
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1797
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
1798
    "Failed to initialize acl: %s", strerror(errno));
1799

1800
  acttab[0].act_acl = acl;
1✔
1801
  type = "group";
1✔
1802
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1✔
1803
  ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
2✔
1804

1805
  mark_point();
1✔
1806
  type = "user";
1✔
1807
  res = pr_ctrls_set_module_acls(acttab, p, good_actions, allow, type, list);
1✔
1808
  ck_assert_msg(res == NULL, "Failed to handle good action: %s", strerror(errno));
2✔
1809

1810
  mark_point();
1✔
1811
  res = pr_ctrls_set_module_acls(acttab, p, all_actions, allow, type, list);
1✔
1812
  ck_assert_msg(res == NULL, "Failed to handle all actions: %s", strerror(errno));
2✔
1813
}
1814
END_TEST
1✔
1815

1816
START_TEST (ctrls_set_module_acls2_test) {
1✔
1817
  int res;
1818
  char *list;
1819
  const char *allow, *type, *bad_action = NULL;
1✔
1820
  ctrls_acl_t *acl;
1821
  char *good_actions[] = { "test", NULL };
1✔
1822
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1823
  char *all_actions[] = { "all", NULL };
1✔
1824
  ctrls_acttab_t acttab[] = {
1✔
1825
    { "test", "desc", NULL, test_action_cb },
1826
    { NULL, NULL, NULL, NULL }
1827
  };
1828

1829
  mark_point();
1✔
1830
  res = pr_ctrls_set_module_acls2(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1831
  ck_assert_msg(res < 0, "Failed to handle null acttab");
1✔
1832
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1833
    strerror(errno), errno);
1834

1835
  mark_point();
1✔
1836
  res = pr_ctrls_set_module_acls2(acttab, NULL, NULL, NULL, NULL, NULL, NULL);
1✔
1837
  ck_assert_msg(res < 0, "Failed to handle null acl_pool");
2✔
1838
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1839
    strerror(errno), errno);
1840

1841
  mark_point();
1✔
1842
  res = pr_ctrls_set_module_acls2(acttab, p, NULL, NULL, NULL, NULL, NULL);
1✔
1843
  ck_assert_msg(res < 0, "Failed to handle null actions");
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
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, NULL, NULL, NULL,
1✔
1849
    NULL);
1850
  ck_assert_msg(res < 0, "Failed to handle null allow");
2✔
1851
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1852
    strerror(errno), errno);
1853

1854
  mark_point();
1✔
1855
  allow = "allow";
1✔
1856
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, NULL, NULL,
1✔
1857
    NULL);
1858
  ck_assert_msg(res < 0, "Failed to handle null type");
2✔
1859
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1860
    strerror(errno), errno);
1861

1862
  mark_point();
1✔
1863
  type = "test";
1✔
1864
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, NULL,
1✔
1865
    NULL);
1866
  ck_assert_msg(res < 0, "Failed to handle null list");
2✔
1867
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1868
    strerror(errno), errno);
1869

1870
  mark_point();
1✔
1871
  list = "foo,bar,baz";
1✔
1872
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
1873
    NULL);
1874
  ck_assert_msg(res < 0, "Failed to handle null bad_action");
2✔
1875
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1876
    strerror(errno), errno);
1877

1878
  mark_point();
1✔
1879
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
1880
    &bad_action);
1881
  ck_assert_msg(res < 0, "Failed to handle invalid type '%s'", type);
2✔
1882
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1883
    strerror(errno), errno);
1884

1885
  mark_point();
1✔
1886
  type = "user";
1✔
1887
  res = pr_ctrls_set_module_acls2(acttab, p, bad_actions, allow, type, list,
1✔
1888
    &bad_action);
1889
  ck_assert_msg(res < 0, "Failed to handle invalid action");
2✔
1890
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1891
    strerror(errno), errno);
1892
  ck_assert_msg(strcmp(bad_action, "bar") == 0,
2✔
1893
    "Expected 'bar', got '%s'", bad_action);
1894

1895
  mark_point();
1✔
1896
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1897
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
1898
    "Failed to initialize acl: %s", strerror(errno));
1899

1900
  acttab[0].act_acl = acl;
1✔
1901
  type = "group";
1✔
1902
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1✔
1903
    &bad_action);
1904
  ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
2✔
1905

1906
  mark_point();
1✔
1907
  type = "user";
1✔
1908
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, allow, type, list,
1✔
1909
    &bad_action);
1910
  ck_assert_msg(res == 0, "Failed to handle good action: %s", strerror(errno));
2✔
1911

1912
  mark_point();
1✔
1913
  res = pr_ctrls_set_module_acls2(acttab, p, all_actions, allow, type, list,
1✔
1914
    &bad_action);
1915
  ck_assert_msg(res == 0, "Failed to handle all actions: %s", strerror(errno));
2✔
1916
}
1917
END_TEST
1✔
1918

1919
START_TEST (ctrls_unregister_module_actions_test) {
1✔
1920
  char *res;
1921
  char *good_actions[] = { "test", NULL };
1✔
1922
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1923
  ctrls_acl_t *acl;
1924
  ctrls_acttab_t acttab[] = {
1✔
1925
    { "test", "desc", NULL, test_action_cb },
1926
    { NULL, NULL, NULL, NULL }
1927
  };
1928
  module m;
1929

1930
  mark_point();
1✔
1931
  res = pr_ctrls_unregister_module_actions(NULL, NULL, NULL);
1✔
1932
  ck_assert_msg(res == NULL, "Failed to handle null acttab");
1✔
1933
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1934
    strerror(errno), errno);
1935

1936
  mark_point();
1✔
1937
  res = pr_ctrls_unregister_module_actions(acttab, NULL, NULL);
1✔
1938
  ck_assert_msg(res == NULL, "Failed to handle null actions");
2✔
1939
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1940
    strerror(errno), errno);
1941

1942
  mark_point();
1✔
1943
  res = pr_ctrls_unregister_module_actions(acttab, good_actions, NULL);
1✔
1944
  ck_assert_msg(res == NULL, "Failed to handle null module");
2✔
1945
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1946
    strerror(errno), errno);
1947

1948
  mark_point();
1✔
1949
  m.name = "test";
1✔
1950
  res = pr_ctrls_unregister_module_actions(acttab, bad_actions, &m);
1✔
1951
  ck_assert_msg(res != NULL, "Failed to handle invalid action");
2✔
1952
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
1953
    strerror(errno), errno);
1954
  ck_assert_msg(strcmp(res, "bar") == 0, "Expected 'bar', got '%s'", res);
2✔
1955

1956
  mark_point();
1✔
1957
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
1958
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
1959
    "Failed to initialize acl: %s", strerror(errno));
1960
  acttab[0].act_acl = acl;
1✔
1961
  res = pr_ctrls_unregister_module_actions(acttab, good_actions, &m);
1✔
1962
  ck_assert_msg(res == NULL, "Failed to handle valid action: %s",
2✔
1963
    strerror(errno));
1964
}
1965
END_TEST
1✔
1966

1967
START_TEST (ctrls_unregister_module_actions2_test) {
1✔
1968
  int res;
1969
  const char *bad_action = NULL;
1✔
1970
  char *good_actions[] = { "test", NULL };
1✔
1971
  char *bad_actions[] = { "bar", "baz", NULL };
1✔
1972
  ctrls_acl_t *acl;
1973
  ctrls_acttab_t acttab[] = {
1✔
1974
    { "test", "desc", NULL, test_action_cb },
1975
    { NULL, NULL, NULL, NULL }
1976
  };
1977
  module m;
1978

1979
  mark_point();
1✔
1980
  res = pr_ctrls_unregister_module_actions2(NULL, NULL, NULL, NULL);
1✔
1981
  ck_assert_msg(res < 0, "Failed to handle null acttab");
1✔
1982
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1983
    strerror(errno), errno);
1984

1985
  mark_point();
1✔
1986
  res = pr_ctrls_unregister_module_actions2(acttab, NULL, NULL, NULL);
1✔
1987
  ck_assert_msg(res < 0, "Failed to handle null actions");
2✔
1988
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1989
    strerror(errno), errno);
1990

1991
  mark_point();
1✔
1992
  res = pr_ctrls_unregister_module_actions2(acttab, good_actions, NULL, NULL);
1✔
1993
  ck_assert_msg(res < 0, "Failed to handle null module");
2✔
1994
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
1995
    strerror(errno), errno);
1996

1997
  mark_point();
1✔
1998
  m.name = "test";
1✔
1999
  res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m, NULL);
1✔
2000
  ck_assert_msg(res < 0, "Failed to handle null bad_action");
2✔
2001
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
2✔
2002
    strerror(errno), errno);
2003

2004
  mark_point();
1✔
2005
  res = pr_ctrls_unregister_module_actions2(acttab, bad_actions, &m,
1✔
2006
    &bad_action);
2007
  ck_assert_msg(res < 0, "Failed to handle invalid action");
2✔
2008
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
2✔
2009
    strerror(errno), errno);
2010
  ck_assert_msg(bad_action != NULL, "Expected bad_action, got NULL");
2✔
2011
  ck_assert_msg(strcmp(bad_action, "bar") == 0,
2✔
2012
    "Expected 'bar', got '%s'", bad_action);
2013

2014
  mark_point();
1✔
2015
  acl = pcalloc(p, sizeof(ctrls_acl_t));
1✔
2016
  ck_assert_msg(pr_ctrls_init_acl(acl) == 0,
2✔
2017
    "Failed to initialize acl: %s", strerror(errno));
2018
  acttab[0].act_acl = acl;
1✔
2019
  res = pr_ctrls_unregister_module_actions2(acttab, good_actions, &m,
1✔
2020
    &bad_action);
2021
  ck_assert_msg(res == 0, "Failed to handle valid action: %s", strerror(errno));
2✔
2022
}
2023
END_TEST
1✔
2024

2025
START_TEST (ctrls_set_logfd_test) {
1✔
2026
  int fd, res;
2027

2028
  mark_point();
1✔
2029
  fd = 0;
1✔
2030
  res = pr_ctrls_set_logfd(fd);
1✔
2031
  ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
1✔
2032
     strerror(errno));
2033

2034
  mark_point();
1✔
2035
  fd = -1;
1✔
2036
  res = pr_ctrls_set_logfd(fd);
1✔
2037
  ck_assert_msg(res == 0, "Failed to set ctrls log fd %d: %s", fd,
2✔
2038
     strerror(errno));
2039
}
2040
END_TEST
1✔
2041

2042
START_TEST (ctrls_log_test) {
1✔
2043
  int fd, res;
2044

2045
  mark_point();
1✔
2046
  fd = -1;
1✔
2047
  pr_ctrls_set_logfd(fd);
1✔
2048
  res = pr_ctrls_log(NULL, NULL);
1✔
2049
  ck_assert_msg(res == 0, "Failed to handle bad logfd: %s", strerror(errno));
1✔
2050

2051
  mark_point();
1✔
2052
  fd = devnull_fd();
1✔
2053
  if (fd < 0) {
1✔
2054
    return;
2055
  }
2056

2057
  pr_ctrls_set_logfd(fd);
1✔
2058
  res = pr_ctrls_log(NULL, NULL);
1✔
2059
  ck_assert_msg(res == 0, "Failed to handle bad module_version: %s",
1✔
2060
    strerror(errno));
2061

2062
  mark_point();
1✔
2063
  res = pr_ctrls_log("test", NULL);
1✔
2064
  ck_assert_msg(res == 0, "Failed to handle null fmt: %s", strerror(errno));
2✔
2065

2066
  mark_point();
1✔
2067
  res = pr_ctrls_log("test", "%s", "foo bar baz");
1✔
2068
  ck_assert_msg(res == 0, "Failed to handle valid fmt: %s", strerror(errno));
2✔
2069

2070
  (void) close(fd);
1✔
2071
}
2072
END_TEST
2073

2074
#endif /* PR_USE_CTRLS */
2075

2076
Suite *tests_get_ctrls_suite(void) {
891✔
2077
  Suite *suite;
2078
  TCase *testcase;
2079

2080
  suite = suite_create("ctrls");
891✔
2081
  testcase = tcase_create("base");
891✔
2082

2083
#if defined(PR_USE_CTRLS)
2084
  tcase_add_checked_fixture(testcase, set_up, tear_down);
891✔
2085

2086
  tcase_add_test(testcase, ctrls_alloc_free_test);
891✔
2087
  tcase_add_test(testcase, ctrls_unregister_test);
891✔
2088
  tcase_add_test(testcase, ctrls_register_test);
891✔
2089
  tcase_add_test(testcase, ctrls_add_arg_test);
891✔
2090
  tcase_add_test(testcase, ctrls_add_response_test);
891✔
2091
  tcase_add_test(testcase, ctrls_copy_args_test);
891✔
2092
  tcase_add_test(testcase, ctrls_copy_resps_test);
891✔
2093
  tcase_add_test(testcase, ctrls_send_request_test);
891✔
2094
  tcase_add_test(testcase, ctrls_send_response_test);
891✔
2095
  tcase_add_test(testcase, ctrls_flush_response_test);
891✔
2096
  tcase_add_test(testcase, ctrls_recv_request_invalid_test);
891✔
2097
  tcase_add_test(testcase, ctrls_recv_request_valid_test);
891✔
2098
  tcase_add_test(testcase, ctrls_recv_response_test);
891✔
2099

2100
  tcase_add_test(testcase, ctrls_issock_unix_test);
891✔
2101
  tcase_add_test(testcase, ctrls_get_registered_actions_test);
891✔
2102
  tcase_add_test(testcase, ctrls_set_registered_actions_test);
891✔
2103
  tcase_add_test(testcase, ctrls_check_actions_test);
891✔
2104
  tcase_add_test(testcase, ctrls_run_ctrls_test);
891✔
2105
  tcase_add_test(testcase, ctrls_reset_ctrls_test);
891✔
2106
  tcase_add_test(testcase, ctrls_accept_test);
891✔
2107
  tcase_add_test(testcase, ctrls_connect_test);
891✔
2108

2109
  /* mod_ctrls */
2110
  tcase_add_test(testcase, ctrls_check_group_acl_test);
891✔
2111
  tcase_add_test(testcase, ctrls_check_user_acl_test);
891✔
2112
  tcase_add_test(testcase, ctrls_init_acl_test);
891✔
2113
  tcase_add_test(testcase, ctrls_check_acl_test);
891✔
2114
  tcase_add_test(testcase, ctrls_parse_acl_test);
891✔
2115
  tcase_add_test(testcase, ctrls_set_group_acl_test);
891✔
2116
  tcase_add_test(testcase, ctrls_set_user_acl_test);
891✔
2117
  tcase_add_test(testcase, ctrls_set_module_acls_test);
891✔
2118
  tcase_add_test(testcase, ctrls_set_module_acls2_test);
891✔
2119
  tcase_add_test(testcase, ctrls_unregister_module_actions_test);
891✔
2120
  tcase_add_test(testcase, ctrls_unregister_module_actions2_test);
891✔
2121
  tcase_add_test(testcase, ctrls_set_logfd_test);
891✔
2122
  tcase_add_test(testcase, ctrls_log_test);
891✔
2123
#endif /* PR_USE_CTRLS */
2124

2125
  suite_add_tcase(suite, testcase);
891✔
2126
  return suite;
891✔
2127
}
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