Greenbone Vulnerability Management Libraries 22.32.0
pidfile_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "../util/fileutils.h"
7#include "pidfile.c"
8
9#include <cgreen/cgreen.h>
10#include <cgreen/mocks.h>
11#include <glib.h>
12#include <sys/stat.h>
13#include <unistd.h>
14
15Describe (pidfile);
16BeforeEach (pidfile)
17{
18}
19
20AfterEach (pidfile)
21{
22}
23
24/* pidfile_create */
25
26Ensure (pidfile, pidfile_create_returns_error_for_null_path)
27{
28 assert_that (pidfile_create (NULL), is_equal_to (-1));
29}
30
31Ensure (pidfile, pidfile_create_creates_file_with_correct_pid)
32{
33 gchar *test_pidfile = "test_pidfile.tmp";
34 pid_t current_pid = getpid ();
35 gchar *expected_content;
36 gchar *actual_content;
37
38 // Remove file if it exists
39 g_remove (test_pidfile);
40
41 assert_that (pidfile_create (test_pidfile), is_equal_to (0));
42
43 // Check that file exists
44 assert_that (gvm_file_exists (test_pidfile), is_equal_to (1));
45
46 // Check that file contains correct PID
47 expected_content = g_strdup_printf ("%d\n", current_pid);
48 g_file_get_contents (test_pidfile, &actual_content, NULL, NULL);
49 assert_that (actual_content, is_equal_to_string (expected_content));
50
51 g_free (expected_content);
52 g_free (actual_content);
53
54 // Clean up
55 g_remove (test_pidfile);
56}
57
58Ensure (pidfile, pidfile_create_creates_directory_if_needed)
59{
60 gchar *test_dir = "test_pid_dir";
61 gchar *test_pidfile = "test_pid_dir/test_pidfile.tmp";
62
63 // Remove directory if it exists
64 g_rmdir (test_dir);
65
66 assert_that (pidfile_create (test_pidfile), is_equal_to (0));
67
68 // Check that directory exists
69 assert_that (gvm_file_check_is_dir (test_dir), is_equal_to (1));
70
71 // Check that file exists
72 assert_that (gvm_file_exists (test_pidfile), is_equal_to (1));
73
74 // Clean up
75 g_remove (test_pidfile);
76 g_rmdir (test_dir);
77}
78
79Ensure (pidfile, pidfile_create_returns_error_when_cannot_create_file)
80{
81 gchar *test_pidfile = "/dev/null/invalid/test_pidfile.tmp";
82
83 // This should fail as we can't write to /dev/null/invalid
84 assert_that (pidfile_create (test_pidfile), is_not_equal_to (0));
85}
86
87/* pidfile_remove */
88
89Ensure (pidfile, pidfile_remove_deletes_file_with_matching_pid)
90{
91 gchar *test_pidfile = "test_pidfile_remove.tmp";
92 pid_t current_pid = getpid ();
93 gchar *pid_content;
94 FILE *file;
95
96 // Create a pidfile with current PID
97 pid_content = g_strdup_printf ("%d\n", current_pid);
98 file = fopen (test_pidfile, "w");
99 assert_that (file, is_not_null);
100 fputs (pid_content, file);
101 fclose (file);
102
103 // Verify file exists
104 assert_that (gvm_file_exists (test_pidfile), is_equal_to (1));
105
106 // Remove pidfile
107 pidfile_remove (test_pidfile);
108
109 // Verify file is deleted
110 assert_that (gvm_file_exists (test_pidfile), is_equal_to (0));
111
112 g_free (pid_content);
113}
114
115Ensure (pidfile, pidfile_remove_does_not_delete_file_with_different_pid)
116{
117 gchar *test_pidfile = "test_pidfile_remove_diff.tmp";
118 pid_t current_pid = getpid ();
119 pid_t different_pid = current_pid + 1;
120 gchar *pid_content;
121 FILE *file;
122
123 // Create a pidfile with different PID
124 pid_content = g_strdup_printf ("%d\n", different_pid);
125 file = fopen (test_pidfile, "w");
126 assert_that (file, is_not_null);
127 fputs (pid_content, file);
128 fclose (file);
129
130 // Verify file exists
131 assert_that (gvm_file_exists (test_pidfile), is_equal_to (1));
132
133 // Try to remove pidfile (should not delete it)
134 pidfile_remove (test_pidfile);
135
136 // Verify file still exists
137 assert_that (gvm_file_exists (test_pidfile), is_equal_to (1));
138
139 // Clean up
140 g_remove (test_pidfile);
141 g_free (pid_content);
142}
143
144Ensure (pidfile, pidfile_remove_handles_nonexistent_file)
145{
146 gchar *test_pidfile = "nonexistent_pidfile.tmp";
147
148 // Ensure file doesn't exist
149 g_remove (test_pidfile);
150
151 // Should not crash when trying to remove nonexistent file
152 pidfile_remove (test_pidfile);
153
154 // File should still not exist
155 assert_that (gvm_file_exists (test_pidfile), is_equal_to (0));
156}
157
158/* Test suite. */
159
160int
161main (int argc, char **argv)
162{
163 int ret;
164 TestSuite *suite;
165
166 suite = create_test_suite ();
167
168 add_test_with_context (suite, pidfile,
169 pidfile_create_returns_error_for_null_path);
170 add_test_with_context (suite, pidfile,
171 pidfile_create_creates_file_with_correct_pid);
172 add_test_with_context (suite, pidfile,
173 pidfile_create_creates_directory_if_needed);
174 add_test_with_context (suite, pidfile,
175 pidfile_create_returns_error_when_cannot_create_file);
176 add_test_with_context (suite, pidfile,
177 pidfile_remove_deletes_file_with_matching_pid);
178 add_test_with_context (
179 suite, pidfile, pidfile_remove_does_not_delete_file_with_different_pid);
180 add_test_with_context (suite, pidfile,
181 pidfile_remove_handles_nonexistent_file);
182
183 if (argc > 1)
184 ret = run_single_test (suite, argv[1], create_text_reporter ());
185 else
186 ret = run_test_suite (suite, create_text_reporter ());
187
188 destroy_test_suite (suite);
189
190 return ret;
191}
int gvm_file_exists(const char *name)
Checks whether a file or directory exists.
Definition fileutils.c:71
int gvm_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
Definition fileutils.c:45
Protos for file utility functions.
PID-file management.
int pidfile_create(gchar *pid_file_path)
Create a PID-file.
Definition pidfile.c:40
void pidfile_remove(gchar *pid_file_path)
Remove PID file.
Definition pidfile.c:87
int main(int argc, char **argv)
BeforeEach(pidfile)
AfterEach(pidfile)
Ensure(pidfile, pidfile_create_returns_error_for_null_path)
Describe(pidfile)