2 #include "utils_proc_pids.c" /* sic */
5 /***************************************************************************
11 * pids_list_get_element
14 * Gets list element at index position. Assumes list was created by
15 * pids_list_add_pid function.
19 * `index' Position of desired element relative to given list pointer.
22 * Pointer to element at index position.
23 * NULL if index exceeds list's length.
25 pids_list_t *pids_list_get_element(pids_list_t *list, const size_t index) {
28 while (list != NULL && current != index) {
35 typedef struct stub_proc_pid {
40 static const char *proc_fs = "/tmp/procfs_stub";
47 * Prepares testing environment by creating temporary
48 * PID/comm file structure.
51 * `proc_pids_array' Array of stub_proc_pid_t structs. Represents
52 * which PIDs should hold given process name.
53 * `proc_pids_array_length' Element count of input array.
57 * -1 on base dir creation error.
58 * -2 on comm file creation error.
60 int stub_procfs_setup(const stub_proc_pid_t *proc_pids_array,
61 const size_t proc_pids_array_length) {
62 if (mkdir(proc_fs, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
66 for (size_t i = 0; i < proc_pids_array_length; ++i) {
67 memset(path, 0, sizeof(path));
68 snprintf(path, STATIC_ARRAY_SIZE(path), "%s/%d", proc_fs,
69 proc_pids_array[i].pid);
70 mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
71 strncat(path, "/comm", STATIC_ARRAY_SIZE(path) - strlen(path) - 1);
73 FILE *fp = fopen(path, "w");
76 fwrite(proc_pids_array[i].comm, sizeof(char),
77 strlen(proc_pids_array[i].comm), fp);
85 * stub_procfs_teardown
88 * Clears testing environment: removes stub proc files.
89 * NOTE - This function could be implemented by usage of nftw, but this
90 * would require #define _XOPEN_SOURCE 500, which
91 * messes up intel_rdt includes.
94 * system command result
96 int stub_procfs_teardown() {
98 sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd));
99 strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1);
103 /* Max PID value. More info:
104 * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html
106 #define MAX_PID 4194304
107 #define MAX_PID_STR "4194304"
109 /***************************************************************************
112 DEF_TEST(initialize_proc_pids__on_nullptr) {
114 const char *procs_names_array[] = {"proc1", "proc2", "proc3"};
115 const size_t procs_names_array_size = STATIC_ARRAY_SIZE(procs_names_array);
116 proc_pids_t *proc_pids_array = NULL;
119 int result = initialize_proc_pids(procs_names_array, procs_names_array_size,
121 EXPECT_EQ_INT(0, result);
122 for (size_t i = 0; i < procs_names_array_size; ++i)
123 EXPECT_EQ_STR(procs_names_array[i], proc_pids_array[i].proccess_name);
126 free(proc_pids_array);
130 DEF_TEST(add_proc_pid__empty_list) {
132 proc_pids_t proc_pids_instance;
133 proc_pids_instance.pids = NULL;
137 pids_list_add_pid(&proc_pids_instance.pids, pid);
138 pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, 0);
139 EXPECT_EQ_INT(pid, added->pid);
142 pids_list_free(proc_pids_instance.pids);
146 DEF_TEST(add_proc_pid__non_empty_list) {
148 proc_pids_t proc_pids_instance;
149 proc_pids_instance.pids = NULL;
150 pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
153 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i)
154 pids_list_add_pid(&proc_pids_instance.pids, pids[i]);
156 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) {
157 pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, i);
158 EXPECT_EQ_INT(pids[i], added->pid);
162 pids_list_free(proc_pids_instance.pids);
166 DEF_TEST(pids_list_to_array__non_empty_list) {
168 pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
169 pids_list_t *pids_list = NULL;
170 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i)
171 pids_list_add_pid(&pids_list, pids[i]);
174 pid_t target_array[STATIC_ARRAY_SIZE(pids)];
175 pids_list_to_array(target_array, pids_list, STATIC_ARRAY_SIZE(target_array));
176 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i)
177 EXPECT_EQ_INT(pids[i], target_array[i]);
180 pids_list_free(pids_list);
184 DEF_TEST(pids_list_add_pids_list__non_empty_lists) {
186 pid_t pids_array_1[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
187 pid_t pids_array_2[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
188 pids_list_t *pids_list_1 = NULL;
189 pids_list_t *pids_list_2 = NULL;
191 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) {
192 pids_list_add_pid(&pids_list_1, pids_array_1[i]);
193 pids_list_add_pid(&pids_list_2, pids_array_2[i]);
197 int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
198 EXPECT_EQ_INT(0, result);
199 EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2), increase);
201 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) {
202 EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_1[i]));
203 EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_2[i]));
207 pids_list_free(pids_list_1);
208 pids_list_free(pids_list_2);
212 DEF_TEST(pids_list_add_pids_list__add_to_empty) {
214 pid_t pids_array[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
215 pids_list_t *pids_list_1 = NULL;
216 pids_list_t *pids_list_2 = NULL;
218 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i)
219 pids_list_add_pid(&pids_list_2, pids_array[i]);
222 int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
223 EXPECT_EQ_INT(0, result);
224 EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), increase);
226 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i)
227 EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array[i]));
230 pids_list_free(pids_list_1);
231 pids_list_free(pids_list_2);
235 DEF_TEST(get_pid_number__valid_dir) {
238 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
243 int pid_conversion = get_pid_number(&d, &pid);
245 EXPECT_EQ_INT(0, pid_conversion);
246 EXPECT_EQ_INT(MAX_PID, pid);
252 DEF_TEST(get_pid_number__invalid_dir_name) {
255 sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name));
260 int pid_conversion = get_pid_number(&d, &pid);
262 EXPECT_EQ_INT(-1, pid_conversion);
263 EXPECT_EQ_INT(0, pid);
269 DEF_TEST(read_proc_name__valid_name) {
271 stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}};
272 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
274 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
279 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
281 EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result);
282 EXPECT_EQ_STR(pp_stubs[0].comm, comm);
285 stub_procfs_teardown();
289 DEF_TEST(read_proc_name__invalid_name) {
292 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
297 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
299 EXPECT_EQ_INT(-1, read_result);
305 DEF_TEST(fetch_pids_for_procs__one_proc_many_pid) {
307 const char *proc_names[] = {"proc1"};
308 stub_proc_pid_t pp_stubs[] = {{"proc1", 1007},
313 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
314 proc_pids_t *output = NULL;
317 int result = fetch_pids_for_procs(proc_fs, proc_names,
318 STATIC_ARRAY_SIZE(proc_names), &output);
319 EXPECT_EQ_INT(0, result);
321 /* proc name check */
322 EXPECT_EQ_STR(proc_names[0], output[0].proccess_name);
324 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pp_stubs); ++i) {
325 if (0 == strcmp(pp_stubs[i].comm, proc_names[0]))
326 /* check if proc struct has correct pids */
327 EXPECT_EQ_INT(pids_list_contains_pid(output[0].pids, pp_stubs[i].pid), 1);
329 /* check if proc struct has no incorrect pids */
330 EXPECT_EQ_INT(pids_list_contains_pid(output[0].pids, pp_stubs[i].pid), 0);
334 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
335 pids_list_free(output[i].pids);
337 stub_procfs_teardown();
341 DEF_TEST(fetch_pids_for_procs__many_proc_many_pid) {
343 const char *proc_names[] = {"proc1", "proc2", "proc3"};
344 stub_proc_pid_t pp_stubs[] = {
345 {"proc1", 1007}, {"proc1", 1008}, {"proc1", 1009}, {"proc2", 2007},
346 {"proc2", 2008}, {"proc2", 2009}, {"proc3", 3007}, {"proc3", 3008},
347 {"proc3", 3009}, {"proc4", 4007}, {"proc4", 4008}, {"proc4", 4009},
348 {"proc5", 5007}, {"proc5", 5008}, {"proc5", 5009}};
349 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
350 proc_pids_t *output = NULL;
353 int result = fetch_pids_for_procs(proc_fs, proc_names,
354 STATIC_ARRAY_SIZE(proc_names), &output);
355 EXPECT_EQ_INT(0, result);
357 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
359 /* proc name check */
360 EXPECT_EQ_STR(proc_names[i], output[i].proccess_name);
362 for (size_t j = 0; j < STATIC_ARRAY_SIZE(pp_stubs); ++j) {
363 if (0 == strcmp(pp_stubs[j].comm, proc_names[i]))
364 /* check if proc struct has correct pids */
365 EXPECT_EQ_INT(pids_list_contains_pid(output[i].pids, pp_stubs[j].pid),
368 /* check if proc struct has no incorrect pids */
369 EXPECT_EQ_INT(pids_list_contains_pid(output[i].pids, pp_stubs[j].pid),
375 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
376 pids_list_free(output[i].pids);
378 stub_procfs_teardown();
382 DEF_TEST(pids_list_diff__all_changed) {
384 pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
385 pid_t pids_array_after[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
386 pids_list_t *pids_list_before = NULL;
387 pids_list_t *pids_list_after = NULL;
388 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i) {
389 pids_list_add_pid(&pids_list_before, pids_array_before[i]);
390 pids_list_add_pid(&pids_list_after, pids_array_after[i]);
393 pids_list_t *new_pids = NULL;
394 size_t new_pids_count = 0;
395 pids_list_t *lost_pids = NULL;
396 size_t lost_pids_count = 0;
399 int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
400 &new_pids_count, &lost_pids, &lost_pids_count);
401 EXPECT_EQ_INT(0, result);
402 EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_before), lost_pids_count);
403 EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_after), new_pids_count);
405 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i) {
406 EXPECT_EQ_INT(1, pids_list_contains_pid(new_pids, pids_array_after[i]));
407 EXPECT_EQ_INT(1, pids_list_contains_pid(lost_pids, pids_array_before[i]));
411 pids_list_free(pids_list_before);
412 pids_list_free(pids_list_after);
413 pids_list_free(new_pids);
414 pids_list_free(lost_pids);
419 DEF_TEST(pids_list_diff__nothing_changed) {
421 pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
422 pids_list_t *pids_list_before = NULL;
423 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i) {
424 pids_list_add_pid(&pids_list_before, pids_array_before[i]);
427 pids_list_t *new_pids = NULL;
428 size_t new_pids_count = 0;
429 pids_list_t *lost_pids = NULL;
430 size_t lost_pids_count = 0;
433 int result = pids_list_diff(pids_list_before, pids_list_before, &new_pids,
434 &new_pids_count, &lost_pids, &lost_pids_count);
435 EXPECT_EQ_INT(0, result);
436 EXPECT_EQ_INT(0, lost_pids_count);
437 EXPECT_EQ_INT(0, new_pids_count);
438 OK(NULL == new_pids);
439 OK(NULL == lost_pids);
442 pids_list_free(pids_list_before);
447 DEF_TEST(pids_list_diff__one_added) {
449 pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
450 pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004,
451 1005, 1006, 1007, 1008};
452 pids_list_t *pids_list_before = NULL;
453 pids_list_t *pids_list_after = NULL;
454 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i)
455 pids_list_add_pid(&pids_list_before, pids_array_before[i]);
457 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i)
458 pids_list_add_pid(&pids_list_after, pids_array_after[i]);
460 pids_list_t *new_pids = NULL;
461 size_t new_pids_count = 0;
462 pids_list_t *lost_pids = NULL;
463 size_t lost_pids_count = 0;
466 int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
467 &new_pids_count, &lost_pids, &lost_pids_count);
468 EXPECT_EQ_INT(0, result);
469 EXPECT_EQ_INT(0, lost_pids_count);
470 EXPECT_EQ_INT(1, new_pids_count);
471 EXPECT_EQ_INT(1008, new_pids->pid);
474 pids_list_free(pids_list_before);
475 pids_list_free(pids_list_after);
476 pids_list_free(new_pids);
481 DEF_TEST(pids_list_diff__one_removed) {
483 pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004,
484 1005, 1006, 1007, 1008};
485 pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
486 pids_list_t *pids_list_before = NULL;
487 pids_list_t *pids_list_after = NULL;
488 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i)
489 pids_list_add_pid(&pids_list_before, pids_array_before[i]);
491 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i)
492 pids_list_add_pid(&pids_list_after, pids_array_after[i]);
494 pids_list_t *new_pids = NULL;
495 size_t new_pids_count = 0;
496 pids_list_t *lost_pids = NULL;
497 size_t lost_pids_count = 0;
500 int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
501 &new_pids_count, &lost_pids, &lost_pids_count);
502 EXPECT_EQ_INT(0, result);
503 EXPECT_EQ_INT(1, lost_pids_count);
504 EXPECT_EQ_INT(0, new_pids_count);
505 EXPECT_EQ_INT(1008, lost_pids->pid);
508 pids_list_free(pids_list_before);
509 pids_list_free(pids_list_after);
510 pids_list_free(lost_pids);
516 stub_procfs_teardown();
517 RUN_TEST(initialize_proc_pids__on_nullptr);
518 RUN_TEST(add_proc_pid__empty_list);
519 RUN_TEST(add_proc_pid__non_empty_list);
520 RUN_TEST(pids_list_to_array__non_empty_list);
521 RUN_TEST(pids_list_add_pids_list__non_empty_lists);
522 RUN_TEST(pids_list_add_pids_list__add_to_empty);
523 RUN_TEST(get_pid_number__valid_dir);
524 RUN_TEST(get_pid_number__invalid_dir_name);
525 RUN_TEST(read_proc_name__valid_name);
526 RUN_TEST(read_proc_name__invalid_name);
527 RUN_TEST(fetch_pids_for_procs__one_proc_many_pid);
528 RUN_TEST(fetch_pids_for_procs__many_proc_many_pid);
529 RUN_TEST(pids_list_diff__all_changed);
530 RUN_TEST(pids_list_diff__nothing_changed);
531 RUN_TEST(pids_list_diff__one_added);
532 RUN_TEST(pids_list_diff__one_removed);
533 stub_procfs_teardown();