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

proftpd / proftpd / 14562012249

20 Apr 2025 06:08PM UTC coverage: 93.03% (+0.4%) from 92.667%
14562012249

push

github

51358 of 55206 relevant lines covered (93.03%)

200.28 hits per line

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

98.24
/tests/api/ctrls.c
1
/*
2
 * ProFTPD - FTP server testsuite
3
 * Copyright (c) 2020-2023 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, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18
 *
19
 * As a special exemption, The ProFTPD Project team and other respective
20
 * copyright holders give permission to link this program with OpenSSL, and
21
 * distribute the resulting executable, without including the source code for
22
 * OpenSSL in the source distribution.
23
 */
24

25
/* Controls API tests */
26

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

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

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

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

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

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

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

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

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

61
  (void) unlink(tmpfile_path);
34✔
62
}
34✔
63

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

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

73
  return fd;
74
}
75

76
static int tmpfile_fd(void) {
20✔
77
  int fd;
20✔
78

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

85
  (void) unlink(tmpfile_path);
20✔
86
  return fd;
20✔
87
}
88

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

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

99
  return 0;
100
}
101

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

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

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

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

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

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

145
  return fd;
146
}
147

148
/* Tests */
149

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

423
  res = pr_ctrls_copy_resps(src_ctrl, dst_ctrl);
1✔
424
  ck_assert_msg(res < 0, "Failed to handle dst ctrl with responses");
1✔
425
  ck_assert_msg(errno == EPERM, "Expected EPERM (%d), got %s (%d)", EPERM,
1✔
426
    strerror(errno), errno);
427

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

536
  mark_point();
1✔
537
  status = -24;
1✔
538
  res = pr_ctrls_send_response(p, fd, status, msgargc, msgargv);
1✔
539
  ck_assert_msg(res == 0, "Failed to send ctrl message: %s", strerror(errno));
1✔
540

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

820
  /* next_action present */
821

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1221
  mark_point();
1✔
1222
  action = "test";
1✔
1223
  desc = "desc";
1✔
1224
  m.name = "test";
1✔
1225
  m2.name = "test2";
1✔
1226
  res = pr_ctrls_register(&m, action, desc, ctrls_test_cb);
1✔
1227
  ck_assert_msg(res >= 0, "Failed to register ctrls action: %s", strerror(errno));
1✔
1228

1229
  mark_point();
1✔
1230
  res = pr_run_ctrls(NULL, NULL);
1✔
1231
  ck_assert_msg(res == 0, "Failed to run ctrls: %s", strerror(errno));
1✔
1232

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1681
  mark_point();
1✔
1682
  res = pr_ctrls_set_user_acl(NULL, NULL, NULL, NULL);
1✔
1683
  ck_assert_msg(res < 0, "Failed to handle null pool");
1✔
1684
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1685
    strerror(errno), errno);
1686

1687
  mark_point();
1✔
1688
  res = pr_ctrls_set_user_acl(p, NULL, NULL, NULL);
1✔
1689
  ck_assert_msg(res < 0, "Failed to handle null user_acl");
1✔
1690
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1691
    strerror(errno), errno);
1692

1693
  mark_point();
1✔
1694
  user_acl = pcalloc(p, sizeof(ctrls_user_acl_t));
1✔
1695
  res = pr_ctrls_set_user_acl(p, user_acl, NULL, NULL);
1✔
1696
  ck_assert_msg(res < 0, "Failed to handle null allow");
1✔
1697
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1698
    strerror(errno), errno);
1699

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1842
  mark_point();
1✔
1843
  res = pr_ctrls_set_module_acls2(acttab, p, NULL, NULL, NULL, NULL, NULL);
1✔
1844
  ck_assert_msg(res < 0, "Failed to handle null actions");
1✔
1845
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1846
    strerror(errno), errno);
1847

1848
  mark_point();
1✔
1849
  res = pr_ctrls_set_module_acls2(acttab, p, good_actions, NULL, NULL, NULL,
1✔
1850
    NULL);
1851
  ck_assert_msg(res < 0, "Failed to handle null allow");
1✔
1852
  ck_assert_msg(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
1✔
1853
    strerror(errno), errno);
1854

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2075
#endif /* PR_USE_CTRLS */
2076

2077
Suite *tests_get_ctrls_suite(void) {
890✔
2078
  Suite *suite;
890✔
2079
  TCase *testcase;
890✔
2080

2081
  suite = suite_create("ctrls");
890✔
2082
  testcase = tcase_create("base");
890✔
2083

2084
#if defined(PR_USE_CTRLS)
2085
  tcase_add_checked_fixture(testcase, set_up, tear_down);
890✔
2086

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

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

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

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

© 2026 Coveralls, Inc