Commit 109229bb authored by David Zeuthen's avatar David Zeuthen

also check for file descriptor leaks

parent 9fe5005e
......@@ -35,6 +35,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <kit/kit.h>
#include "kit-test.h"
......@@ -263,6 +264,44 @@ out:
return ret;
}
/**
* _kit_get_num_fd:
*
* Determines the number of open file descriptors
*
* Returns: Number of open file descriptors
*/
size_t
_kit_get_num_fd (void)
{
DIR *dir;
char buf[128];
ssize_t num;
struct dirent64 *d;
num = -1;
snprintf (buf, sizeof (buf), "/proc/%d/fd", getpid ());
dir = opendir (buf);
if (dir == NULL) {
kit_warning ("error calling opendir on %s: %m\n", buf);
goto out;
}
num = -2;
while ((d = readdir64 (dir)) != NULL) {
if (d->d_name == NULL)
continue;
num++;
}
out:
if (dir != NULL)
closedir (dir);
return num;
}
#ifdef KIT_BUILD_TESTS
......
......@@ -37,6 +37,8 @@ KIT_BEGIN_DECLS
kit_bool_t kit_file_get_contents (const char *path, char **out_contents, size_t *out_contents_size);
kit_bool_t kit_file_set_contents (const char *path, mode_t mode, const char *contents, size_t contents_size);
size_t _kit_get_num_fd (void);
KIT_END_DECLS
#endif /* KIT_FILE_H */
......
......@@ -140,7 +140,7 @@ again:
goto out;
}
kit_debug ("Wrote %d bytes from '%s'", num_written, str);
//kit_debug ("Wrote %d bytes from '%s'", num_written, str);
out:
return num_written;
......
......@@ -47,6 +47,10 @@ static KitTest *tests[] = {
&_test_spawn,
};
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
main (int argc, char *argv[])
{
......@@ -63,6 +67,8 @@ main (int argc, char *argv[])
int m;
int total_allocs;
int delta;
int num_fd;
int num_fd_after;
KitTest *test = tests[n];
_kit_memory_reset ();
......@@ -70,12 +76,14 @@ main (int argc, char *argv[])
if (test->setup != NULL)
test->setup ();
num_fd = _kit_get_num_fd ();
printf ("Running: %s\n", test->name);
if (!test->run ()) {
printf ("Failed\n");
ret = 1;
goto test_done;
}
num_fd_after = _kit_get_num_fd ();
total_allocs = _kit_memory_get_total_allocations ();
printf (" Unit test made %d allocations in total\n", total_allocs);
......@@ -85,24 +93,35 @@ main (int argc, char *argv[])
printf (" Unit test leaked %d allocations\n", delta);
ret = 1;
}
if (num_fd != num_fd_after) {
printf (" Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
ret = 1;
}
for (m = 0; m < total_allocs; m++) {
printf (" Failing allocation %d of %d\n", m + 1, total_allocs);
_kit_memory_reset ();
_kit_memory_fail_nth_alloc (m);
num_fd = _kit_get_num_fd ();
if (!test->run ()) {
printf (" Failed\n");
ret = 1;
continue;
}
num_fd_after = _kit_get_num_fd ();
delta = _kit_memory_get_current_allocations ();
if (delta != 0) {
printf (" Unit test leaked %d allocations\n", delta);
ret = 1;
}
if (num_fd != num_fd_after) {
printf (" Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
ret = 1;
}
}
test_done:
......
......@@ -340,7 +340,7 @@ dump_auths_all (const char *root)
out:
if (dir != NULL)
closedir(dir);
closedir (dir);
return ret;
}
......
......@@ -94,7 +94,6 @@ PolKitPolicyCache *
_polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions, PolKitError **error)
{
DIR *dir;
int dfd;
struct dirent64 *d;
PolKitPolicyCache *pc;
......@@ -109,15 +108,14 @@ _polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions,
pc->refcount = 1;
dir = opendir (dirname);
if (dir == NULL || (dfd = dirfd (dir)) == -1) {
if (dir == NULL) {
polkit_error_set_error (error, POLKIT_ERROR_POLICY_FILE_INVALID,
"Cannot load policy files from directory %s: %m",
dirname);
goto out;
}
while ((d = readdir64(dir)) != NULL) {
while ((d = readdir64 (dir)) != NULL) {
char *path;
PolKitPolicyFile *pf;
PolKitError *pk_error;
......@@ -167,7 +165,7 @@ _polkit_policy_cache_new (const char *dirname, polkit_bool_t load_descriptions,
}
polkit_policy_file_unref (pf);
}
closedir(dir);
closedir (dir);
return pc;
out:
......@@ -502,7 +500,6 @@ _run_test (void)
polkit_policy_cache_ref (pc);
polkit_policy_cache_unref (pc);
polkit_policy_cache_unref (pc);
out:
return TRUE;
}
......
......@@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <polkit/polkit-test.h>
#include <polkit/polkit-private.h>
#include <polkit/polkit-private.h>
......@@ -68,11 +69,20 @@ main (int argc, char *argv[])
num_tests = sizeof (tests) / sizeof (PolKitTest*);
/* Some of the code will log to syslog because .policy files
* etc. may be malformed. Since this will open a socket to the
* system logger preempt this so the fd-leak checking don't
* freak out.
*/
syslog (LOG_INFO, "libpolkit: initiating test; bogus alerts may be written to syslog");
printf ("Running %d unit tests\n", num_tests);
for (n = 0; n < num_tests; n++) {
int m;
int total_allocs;
int delta;
int num_fd;
int num_fd_after;
PolKitTest *test = tests[n];
_kit_memory_reset ();
......@@ -80,12 +90,14 @@ main (int argc, char *argv[])
if (test->setup != NULL)
test->setup ();
num_fd = _kit_get_num_fd ();
printf ("Running: %s\n", test->name);
if (!test->run ()) {
printf ("Failed\n");
ret = 1;
goto test_done;
}
num_fd_after = _kit_get_num_fd ();
total_allocs = _kit_memory_get_total_allocations ();
printf (" Unit test made %d allocations in total\n", total_allocs);
......@@ -95,6 +107,10 @@ main (int argc, char *argv[])
printf (" Unit test leaked %d allocations\n", delta);
ret = 1;
}
if (num_fd != num_fd_after) {
printf (" Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
ret = 1;
}
for (m = 0; m < total_allocs; m++) {
printf (" Failing allocation %d of %d\n", m + 1, total_allocs);
......@@ -102,17 +118,23 @@ main (int argc, char *argv[])
_kit_memory_reset ();
_kit_memory_fail_nth_alloc (m);
num_fd = _kit_get_num_fd ();
if (!test->run ()) {
printf (" Failed\n");
ret = 1;
continue;
}
num_fd_after = _kit_get_num_fd ();
delta = _kit_memory_get_current_allocations ();
if (delta != 0) {
printf (" Unit test leaked %d allocations\n", delta);
ret = 1;
}
if (num_fd != num_fd_after) {
printf (" Unit test leaked %d file descriptors\n", num_fd_after - num_fd);
ret = 1;
}
}
test_done:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment