pacemaker  2.1.7-0f7f88312f
Scalable High-Availability cluster resource manager
pcmk__procfs_pid2path_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 the Pacemaker project contributors
3  *
4  * The version control history for this file may have further details.
5  *
6  * This source code is licensed under the GNU General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
13 
14 #include "mock_private.h"
15 
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 
20 static void
21 no_exe_file(void **state)
22 {
23  size_t len = PATH_MAX;
24  char *path = calloc(len, sizeof(char));
25 
26  // Set readlink() errno and link contents
27  pcmk__mock_readlink = true;
28 
29  expect_string(__wrap_readlink, path, "/proc/1000/exe");
30  expect_value(__wrap_readlink, buf, path);
31  expect_value(__wrap_readlink, bufsize, len - 1);
32  will_return(__wrap_readlink, ENOENT);
33  will_return(__wrap_readlink, NULL);
34 
35  assert_int_equal(pcmk__procfs_pid2path(1000, path, len), ENOENT);
36 
37  pcmk__mock_readlink = false;
38 
39  free(path);
40 }
41 
42 static void
43 contents_too_long(void **state)
44 {
45  size_t len = 10;
46  char *path = calloc(len, sizeof(char));
47 
48  // Set readlink() errno and link contents
49  pcmk__mock_readlink = true;
50 
51  expect_string(__wrap_readlink, path, "/proc/1000/exe");
52  expect_value(__wrap_readlink, buf, path);
53  expect_value(__wrap_readlink, bufsize, len - 1);
54  will_return(__wrap_readlink, 0);
55  will_return(__wrap_readlink, "/more/than/10/characters");
56 
57  assert_int_equal(pcmk__procfs_pid2path(1000, path, len),
58  ENAMETOOLONG);
59 
60  pcmk__mock_readlink = false;
61 
62  free(path);
63 }
64 
65 static void
66 contents_ok(void **state)
67 {
68  size_t len = PATH_MAX;
69  char *path = calloc(len, sizeof(char));
70 
71  // Set readlink() errno and link contents
72  pcmk__mock_readlink = true;
73 
74  expect_string(__wrap_readlink, path, "/proc/1000/exe");
75  expect_value(__wrap_readlink, buf, path);
76  expect_value(__wrap_readlink, bufsize, len - 1);
77  will_return(__wrap_readlink, 0);
78  will_return(__wrap_readlink, "/ok");
79 
80  assert_int_equal(pcmk__procfs_pid2path((pid_t) 1000, path, len),
81  pcmk_rc_ok);
82  assert_string_equal(path, "/ok");
83 
84  pcmk__mock_readlink = false;
85 
86  free(path);
87 }
88 
89 PCMK__UNIT_TEST(NULL, NULL,
90  cmocka_unit_test(no_exe_file),
91  cmocka_unit_test(contents_too_long),
92  cmocka_unit_test(contents_ok))
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(bad_input), cmocka_unit_test(not_found), cmocka_unit_test(find_attrB), cmocka_unit_test(find_attrA_matching))
bool pcmk__mock_readlink
Definition: mock.c:366
const char * path
Definition: cib.c:28
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
Definition: procfs.c:179
ssize_t __wrap_readlink(const char *restrict path, char *restrict buf, size_t bufsize)
Definition: mock.c:369