Greenbone Vulnerability Management Libraries 22.32.0
pidfile_tests.c File Reference
#include "../util/fileutils.h"
#include "pidfile.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <glib.h>
#include <sys/stat.h>
#include <unistd.h>
Include dependency graph for pidfile_tests.c:

Go to the source code of this file.

Functions

 Describe (pidfile)
 BeforeEach (pidfile)
 AfterEach (pidfile)
 Ensure (pidfile, pidfile_create_returns_error_for_null_path)
 Ensure (pidfile, pidfile_create_creates_file_with_correct_pid)
 Ensure (pidfile, pidfile_create_creates_directory_if_needed)
 Ensure (pidfile, pidfile_create_returns_error_when_cannot_create_file)
 Ensure (pidfile, pidfile_remove_deletes_file_with_matching_pid)
 Ensure (pidfile, pidfile_remove_does_not_delete_file_with_different_pid)
 Ensure (pidfile, pidfile_remove_handles_nonexistent_file)
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( pidfile )

Definition at line 20 of file pidfile_tests.c.

21{
22}

◆ BeforeEach()

BeforeEach ( pidfile )

Definition at line 16 of file pidfile_tests.c.

17{
18}

◆ Describe()

Describe ( pidfile )

◆ Ensure() [1/7]

Ensure ( pidfile ,
pidfile_create_creates_directory_if_needed  )

Definition at line 58 of file pidfile_tests.c.

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}
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
int pidfile_create(gchar *pid_file_path)
Create a PID-file.
Definition pidfile.c:40

References gvm_file_check_is_dir(), gvm_file_exists(), and pidfile_create().

Here is the call graph for this function:

◆ Ensure() [2/7]

Ensure ( pidfile ,
pidfile_create_creates_file_with_correct_pid  )

Definition at line 31 of file pidfile_tests.c.

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}

References gvm_file_exists(), and pidfile_create().

Here is the call graph for this function:

◆ Ensure() [3/7]

Ensure ( pidfile ,
pidfile_create_returns_error_for_null_path  )

Definition at line 26 of file pidfile_tests.c.

27{
28 assert_that (pidfile_create (NULL), is_equal_to (-1));
29}

References pidfile_create().

Here is the call graph for this function:

◆ Ensure() [4/7]

Ensure ( pidfile ,
pidfile_create_returns_error_when_cannot_create_file  )

Definition at line 79 of file pidfile_tests.c.

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}

References pidfile_create().

Here is the call graph for this function:

◆ Ensure() [5/7]

Ensure ( pidfile ,
pidfile_remove_deletes_file_with_matching_pid  )

Definition at line 89 of file pidfile_tests.c.

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}
void pidfile_remove(gchar *pid_file_path)
Remove PID file.
Definition pidfile.c:87

References gvm_file_exists(), and pidfile_remove().

Here is the call graph for this function:

◆ Ensure() [6/7]

Ensure ( pidfile ,
pidfile_remove_does_not_delete_file_with_different_pid  )

Definition at line 115 of file pidfile_tests.c.

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}

References gvm_file_exists(), and pidfile_remove().

Here is the call graph for this function:

◆ Ensure() [7/7]

Ensure ( pidfile ,
pidfile_remove_handles_nonexistent_file  )

Definition at line 144 of file pidfile_tests.c.

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}

References gvm_file_exists(), and pidfile_remove().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 161 of file pidfile_tests.c.

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}