add test-friendly abstractions for getpwnam and getpwuid
authorDavid Zeuthen <davidz@redhat.com>
Thu, 29 Nov 2007 05:07:40 +0000 (00:07 -0500)
committerDavid Zeuthen <davidz@redhat.com>
Thu, 29 Nov 2007 05:07:40 +0000 (00:07 -0500)
src/kit/Makefile.am
src/kit/kit-entity.c [new file with mode: 0644]
src/kit/kit-entity.h [new file with mode: 0644]
src/kit/kit-test-main.c
src/kit/kit-test.h
src/kit/kit.h

index 16eb69f..abd12bf 100644 (file)
@@ -28,6 +28,7 @@ libkit_la_SOURCES =                                   \
        kit-file.h              kit-file.c              \
        kit-spawn.h             kit-spawn.c             \
        kit-message.h           kit-message.c           \
+       kit-entity.h            kit-entity.c            \
        $(NULL)
 
 
diff --git a/src/kit/kit-entity.c b/src/kit/kit-entity.c
new file mode 100644 (file)
index 0000000..0bd550f
--- /dev/null
@@ -0,0 +1,163 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * kit-entity.c : Entity management
+ *
+ * Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef BUILT_R_DYNAMIC
+#include <execinfo.h>
+#endif
+
+#include <kit/kit-entity.h>
+#include <kit/kit-test.h>
+
+/**
+ * SECTION:kit-entity
+ * @title: Entity management
+ * @short_description: Entity management
+ *
+ * Functions used for entity management.
+ **/
+
+#ifdef KIT_BUILD_TESTS
+
+/**
+ * kit_getpwnam:
+ * @username: user name to look up
+ *
+ * Like getpwnam(3) from the standard C library but tweaked for unit
+ * testing. TODO: explain how.
+ *
+ * Returns: See getpwnam(3)
+ */
+struct passwd *
+kit_getpwnam (const char *username)
+{
+        struct passwd *pw;
+        FILE *f;
+        const char *passwd_file;
+
+        f = NULL;
+        pw = NULL;
+
+        if ((passwd_file = getenv ("KIT_TEST_PASSWD_FILE")) == NULL)
+                return getpwnam (username);
+
+        f = fopen (passwd_file, "r");
+        if (f == NULL)
+                goto out;
+
+        while ((pw = fgetpwent (f)) != NULL) {
+                if (strcmp (pw->pw_name, username) == 0)
+                        goto out;
+        }
+
+out:
+        if (f != NULL)
+                fclose (f);
+        return pw;
+}
+
+/**
+ * kit_getpwuid:
+ * @username: user name to look up
+ *
+ * Like getpwuid(3) from the standard C library but tweaked for unit
+ * testing. TODO: explain how.
+ *
+ * Returns: See getpwuid(3)
+ */
+struct passwd *
+kit_getpwuid (uid_t uid)
+{
+        struct passwd *pw;
+        FILE *f;
+        const char *passwd_file;
+
+        f = NULL;
+        pw = NULL;
+
+        if ((passwd_file = getenv ("KIT_TEST_PASSWD_FILE")) == NULL)
+                return getpwuid (uid);
+
+        f = fopen (passwd_file, "r");
+        if (f == NULL)
+                goto out;
+
+        while ((pw = fgetpwent (f)) != NULL) {
+                if (pw->pw_uid == uid)
+                        goto out;
+        }
+
+out:
+        if (f != NULL)
+                fclose (f);
+        return pw;
+}
+
+#else
+
+struct passwd *
+kit_getpwnam (const char *username)
+{
+        return getpwnam (username);
+}
+
+struct passwd *
+kit_getpwuid (uid_t uid)
+{
+        return getpwuid (uid);
+}
+#endif
+
+
+#ifdef KIT_BUILD_TESTS
+
+static kit_bool_t
+_run_test (void)
+{
+        return TRUE;
+}
+
+KitTest _test_entity = {
+        "kit_entity",
+        NULL,
+        NULL,
+        _run_test
+};
+
+#endif /* KIT_BUILD_TESTS */
diff --git a/src/kit/kit-entity.h b/src/kit/kit-entity.h
new file mode 100644 (file)
index 0000000..91b4e51
--- /dev/null
@@ -0,0 +1,52 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/***************************************************************************
+ *
+ * kit-entity.h : Entity management
+ *
+ * Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+#if !defined (KIT_COMPILATION) && !defined(_KIT_INSIDE_KIT_H)
+#error "Only <kit/kit.h> can be included directly, this file may disappear or change contents."
+#endif
+
+#ifndef KIT_ENTITY_H
+#define KIT_ENTITY_H
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <kit/kit.h>
+
+KIT_BEGIN_DECLS
+
+struct passwd *kit_getpwnam (const char *username);
+struct passwd *kit_getpwuid (uid_t uid);
+
+KIT_END_DECLS
+
+#endif /* KIT_ENTITY_H */
+
+
index 2505d9b..ccdcf79 100644 (file)
@@ -39,6 +39,7 @@ static KitTest *tests[] = {
         &_test_hash,
         &_test_file,
         &_test_spawn,
+        &_test_entity,
 };
 
 int 
index c43c65c..a3869c4 100644 (file)
@@ -63,6 +63,7 @@ extern KitTest _test_list;
 extern KitTest _test_file;
 extern KitTest _test_spawn;
 extern KitTest _test_message;
+extern KitTest _test_entity;
 
 KIT_END_DECLS
 
index c5ab267..6030e57 100644 (file)
@@ -144,6 +144,7 @@ do {
 #include <kit/kit-spawn.h>
 #include <kit/kit-message.h>
 #include <kit/kit-test.h>
+#include <kit/kit-entity.h>
 
 #undef _KIT_INSIDE_KIT_H