From c1cc80745496c08ff31cd2fade0b306606199801 Mon Sep 17 00:00:00 2001
From: Pallavi Jha <pallavikumarijha@gmail.com>
Date: Tue, 31 Dec 2013 18:04:16 +0530
Subject: [PATCH] cmocka unit test for negcache module added

---
 Makefile.am                      |  16 ++
 src/tests/cmocka/test_negcache.c | 498 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 514 insertions(+)
 create mode 100644 src/tests/cmocka/test_negcache.c

diff --git a/Makefile.am b/Makefile.am
index 4015dadd78e26ec285b7010c3e39b22458fa878b..714c941edc262530a72503b98f9dac98d649588a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -149,6 +149,7 @@ if HAVE_CMOCKA
         nss-srv-tests \
         test-find-uid \
         test-io \
+        test-negcache \
         sss_nss_idmap-tests \
         dyndns-tests \
         fqnames-tests \
@@ -1332,6 +1333,21 @@ test_io_CFLAGS = \
 test_io_LDADD = \
     $(CMOCKA_LIBS)
 
+EXTRA_test_negcache_DEPENDENCIES = \
+    $(ldblib_LTLIBRARIES)
+test_negcache_SOURCES = \
+    $(SSSD_RESPONDER_OBJ) \
+    src/tests/cmocka/test_negcache.c
+test_negcache_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(TALLOC_CFLAGS) \
+    $(DHASH_CFLAGS)
+test_negcache_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(SSSD_LIBS) \
+    $(SSSD_INTERNAL_LTLIBS) \
+    libsss_test_common.la 
+
 sss_nss_idmap_tests_SOURCES = \
     src/tests/cmocka/sss_nss_idmap-tests.c
 sss_nss_idmap_tests_CFLAGS = \
diff --git a/src/tests/cmocka/test_negcache.c b/src/tests/cmocka/test_negcache.c
new file mode 100644
index 0000000000000000000000000000000000000000..1ae00fc6c8cde6056c3fbb02874bb0e98c973308
--- /dev/null
+++ b/src/tests/cmocka/test_negcache.c
@@ -0,0 +1,498 @@
+/*
+ SSSD
+
+     NSS Responder
+ 
+     Authors:
+         Pallavi Jha <pallavikumarijha@gmail.com>
+ 
+         Copyright (C) 2013 Red Hat
+ 
+         This program is free software; you can redistribute it and/or modify
+         it under the terms of the GNU General Public License as published by
+         the Free Software Foundation; either version 3 of the License, or
+         (at your option) any later version.
+
+         This program is distributed in the hope that it will be useful,
+         but WITHOUT ANY WARRANTY; without even the implied warranty of
+         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+         GNU General Public License for more details.
+         You should have received a copy of the GNU General Public License
+         along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <cmocka.h>
+
+#include "util/util.h"
+#include "responder/common/responder.h"
+#include "responder/common/negcache.h"
+ 
+#define PORT 21
+#define SID "10001"
+#define PROTO "TCP"
+#define LIFETIME 200
+#define NAME "foo_name"
+ 
+ 
+struct cli_protocol_version *register_cli_protocol_version(void)
+{
+    static struct cli_protocol_version responder_test_cli_protocol_version[] = {
+        {0, NULL, NULL}
+    };
+ 
+    return responder_test_cli_protocol_version;
+}
+
+struct test_state {
+    struct sss_nc_ctx *ctx;
+};
+
+static void setup(void **state)
+{
+    int ret;
+    struct test_state *ts;
+
+    ts = talloc(NULL, struct test_state);
+    assert_non_null(ts);
+
+    ret  = sss_ncache_init(ts, &ts->ctx);
+    assert_int_equal(ret, EOK);
+    assert_non_null(ts->ctx);
+
+    *state = (void *)ts;
+}
+
+static void teardown(void **state)
+{
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
+    talloc_free(ts);
+}
+
+static void test_sss_ncache_init(void **state)
+{
+    int ret;
+    struct test_state *ts;
+    struct sss_nc_ctx *ctx;
+
+    ts = talloc_get_type_abort(*state, struct test_state);
+    ret = sss_ncache_init(ts, &ctx);
+    assert_int_equal(ret, EOK);
+    assert_non_null(ctx);
+}
+
+/* @test_sss_ncache_uid : test following functions
+ * sss_ncache_set_uid
+ * sss_ncache_check_uid
+ */
+static void test_sss_ncache_uid(void **state)
+{
+    uid_t uid;
+    int ret, ttl;
+    bool permanent;
+    TALLOC_CTX *memctx;
+    struct test_state *ts;
+ 
+    ttl = LIFETIME;
+    uid = getuid();
+    ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    /* test when uid not present in database */
+    ret = sss_ncache_check_uid(ts->ctx, ttl, uid);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when uid is present in database */
+    permanent = true;
+    ret = sss_ncache_set_uid(ts->ctx, permanent, uid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_uid(ts->ctx, ttl, uid);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_uid(ts->ctx, permanent, uid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_uid(ts->ctx, ttl, uid);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with uid present in database*/
+    ttl = -1;
+    ret = sss_ncache_check_uid(ts->ctx, ttl, uid);
+    assert_int_equal(ret, EEXIST);
+}
+ 
+/* @test_sss_ncache_gid : test following functions
+ * sss_ncache_set_gid
+ * sss_ncache_check_gid
+ */
+static void test_sss_ncache_gid(void **state)
+{
+    gid_t gid;
+    int ret, ttl;
+    bool permanent;
+    struct test_state *ts;
+ 
+    ttl = LIFETIME;
+    gid = getgid();
+    ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    /* test when gid is not present in database */
+    ret = sss_ncache_check_gid(ts->ctx, ttl, gid);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when gid is present in database */
+    permanent = true;
+    ret = sss_ncache_set_gid(ts->ctx, permanent, gid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_gid(ts->ctx, ttl, gid);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_uid(ts->ctx, permanent, gid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_uid(ts->ctx, ttl, gid);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with gid present in database*/
+    ttl = -1;
+    ret = sss_ncache_check_gid(ts->ctx, ttl, gid);
+    assert_int_equal(ret, EEXIST);
+}
+ 
+
+/* @test_sss_ncache_sid : test following functions
+ * sss_ncache_set_sid
+ * sss_ncache_check_sid
+ */
+static void test_sss_ncache_sid(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    const char *sid = NULL;
+    struct test_state *ts;
+ 
+    ttl = LIFETIME;
+    sid = SID;
+    ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    /*test when sid in not present in database */
+    ret = sss_ncache_check_sid(ts->ctx, ttl, sid);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when sid is present in database */
+    permanent = true;
+    ret = sss_ncache_set_sid(ts->ctx, permanent, sid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_sid(ts->ctx, ttl, sid);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_sid(ts->ctx, permanent, sid);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_sid(ts->ctx, ttl, sid);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with sid present in database*/
+    ttl = -1;
+    ret = sss_ncache_check_sid(ts->ctx, ttl, sid);
+    assert_int_equal(ret, EEXIST);
+}
+
+/* @test_sss_ncache_user : test following functions
+ * sss_ncache_check_user
+ * sss_ncache_set_user
+ */
+static void test_sss_ncache_user(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    const char *name = NAME;
+    struct test_state *ts;
+    struct sss_domain_info *dom;
+ 
+    ttl = LIFETIME;
+    ts = talloc_get_type_abort(*state, struct test_state);
+    dom = talloc(ts, struct sss_domain_info);
+    dom->name = NAME;
+
+    /* test when domain name is not present in database */
+    dom->case_sensitive = false;
+    ret = sss_ncache_check_user(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+ 
+    dom->case_sensitive = true;
+    ret = sss_ncache_check_user(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when domain name is present in database */
+    permanent = true;
+    ret = sss_ncache_set_user(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_user(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_user(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_user(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with domain name present in database */
+    ttl = -1;
+    ret = sss_ncache_check_user(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+}
+
+/* @test_sss_ncache_group : test following functions
+ * sss_ncache_check_group
+ * sss_ncache_set_group
+ */
+static void test_sss_ncache_group(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    const char *name = NAME;
+    struct test_state *ts;
+    struct sss_domain_info *dom;
+ 
+    ttl = LIFETIME;
+    ts = talloc_get_type_abort(*state, struct test_state);
+    dom = talloc(ts, struct sss_domain_info);
+    dom->name = NAME;
+
+    /* test when domain name is not present in database */
+    dom->case_sensitive = false;
+    ret = sss_ncache_check_group(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+
+    dom->case_sensitive = true;
+    ret = sss_ncache_check_group(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when domain name is present in database */
+    permanent = true;
+    ret = sss_ncache_set_group(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_group(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_group(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_group(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with domain name present in database */
+    ttl = -1;
+    ret = sss_ncache_check_group(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+}
+ 
+/* @test_sss_ncache_netgr : test following functions
+ * sss_ncache_check_netgr
+ * sss_ncache_set_netgr
+ */
+static void test_sss_ncache_netgr(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    const char *name = NAME;
+    struct test_state *ts;
+    struct sss_domain_info *dom;
+ 
+    ttl = LIFETIME;
+    ts = talloc_get_type_abort(*state, struct test_state);
+    dom = talloc(ts, struct sss_domain_info);
+    dom->name = NAME;
+
+    /* test when domain name is not present in database */
+    dom->case_sensitive = false;
+    ret = sss_ncache_check_netgr(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+ 
+    dom->case_sensitive = true;
+    ret = sss_ncache_check_netgr(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when domain name is present in database */
+    permanent = true;
+    ret = sss_ncache_set_netgr(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_netgr(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_netgr(ts->ctx, permanent, dom, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_netgr(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with domain name present in database */
+    ttl = -1;
+    ret = sss_ncache_check_netgr(ts->ctx, ttl, dom, name);
+    assert_int_equal(ret, EEXIST);
+}
+
+/* @test_sss_ncache_service_name : test following functions
+ * sss_ncache_check_service
+ * sss_ncache_set_service_name
+ */
+static void test_sss_ncache_service_name(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    const char *name = NAME;
+    struct test_state *ts;
+    struct sss_domain_info *dom;
+ 
+    ttl = LIFETIME;
+    ts = talloc_get_type_abort(*state, struct test_state);
+    dom = talloc(ts, struct sss_domain_info);
+    dom->name = NAME;
+
+    /* test when domain name and protocol are not present in database */
+    dom->case_sensitive = false;
+    ret = sss_ncache_check_service(ts->ctx, ttl, dom, name, PROTO);
+    assert_int_equal(ret, ENOENT);
+ 
+    dom->case_sensitive = true;
+    ret = sss_ncache_check_service(ts->ctx, ttl, dom, name, PROTO);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when domain name and protocol are present in database */
+    permanent = true;
+    ret = sss_ncache_set_service_name(ts->ctx, permanent, dom, name, PROTO);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_service(ts->ctx, ttl, dom, name, PROTO);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_service_name(ts->ctx, permanent, dom, name, PROTO);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_service(ts->ctx, ttl, dom, name, PROTO);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with domain name present in database */
+    ttl = -1;
+    ret = sss_ncache_check_service(ts->ctx, ttl, dom, name, PROTO);
+    assert_int_equal(ret, EEXIST);
+}
+
+/* @test_sss_ncache_service_port : test following functions
+ * sss_ncache_check_service_port
+ * sss_ncache_set_service_port
+ */
+static void test_sss_ncache_service_port(void **state)
+{
+    int ret, ttl;
+    bool permanent;
+    struct test_state *ts;
+    struct sss_domain_info *dom;
+ 
+    ttl = LIFETIME;
+    ts = talloc_get_type_abort(*state, struct test_state);
+    dom = talloc(ts, struct sss_domain_info);
+    dom->name = NAME;
+
+    /* test when domain name, port and protocol are not present in database */
+    dom->case_sensitive = false;
+    ret = sss_ncache_check_service_port(ts->ctx, ttl, dom, (uint16_t)PORT,
+                                        PROTO);
+    assert_int_equal(ret, ENOENT);
+ 
+    dom->case_sensitive = true;
+    ret = sss_ncache_check_service_port(ts->ctx, ttl, dom, (uint16_t)PORT,
+                                        PROTO);
+    assert_int_equal(ret, ENOENT);
+
+    /* test when domain name, port and protocol are present in database */
+    permanent = true;
+    ret = sss_ncache_set_service_port(ts->ctx, permanent, dom, (uint16_t)PORT,
+                                      PROTO);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_service_port(ts->ctx, ttl, dom, (uint16_t)PORT,
+                                        PROTO);
+    assert_int_equal(ret, EEXIST);
+
+    permanent = false;
+    ret = sss_ncache_set_service_port(ts->ctx, permanent, dom, (uint16_t)PORT,
+                                      PROTO);
+    assert_int_equal(ret, EOK);
+
+    ret = sss_ncache_check_service_port(ts->ctx, ttl, dom, (uint16_t)PORT,
+                                        PROTO);
+    assert_int_equal(ret, EEXIST);
+
+    /* test when ttl is -1 with domain name present in database */
+    ttl = -1;
+    ret = sss_ncache_check_service_port(ts->ctx, ttl, dom, (uint16_t)PORT,
+                                        PROTO);
+    assert_int_equal(ret, EEXIST);
+}
+ 
+static void test_sss_ncache_reset_permanent(void **state)
+{
+    int ret;
+    struct test_state *ts;
+
+    ts = talloc_get_type_abort(*state, struct test_state);
+    ret = sss_ncache_reset_permament(ts->ctx);
+    assert_int_equal(ret, EOK);
+}
+ 
+/*
+static void test_sss_ncache_prepopulate(void **state)
+{
+    int ret;
+    struct resp_ctx *rctx;
+    struct confdb_ctx *cdb;
+    struct sss_nc_ctx *ncache;
+ 
+    // initialization need to be done
+    ret = sss_ncache_prepopulate(ncache, cdb, rctx);
+    assert_int_equal(ret, EOK);
+}
+*/
+
+int main(void)
+{
+    const UnitTest tests[] = {
+        unit_test_setup_teardown(test_sss_ncache_init, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_uid, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_gid, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_sid, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_user, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_group, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_netgr, setup, teardown),
+        unit_test_setup_teardown(test_sss_ncache_service_name, setup,
+                                 teardown),
+        unit_test_setup_teardown(test_sss_ncache_service_port, setup,
+                                  teardown),
+        unit_test_setup_teardown(test_sss_ncache_reset_permanent, setup,
+                                  teardown)
+        //unit_test(test_sss_ncache_prepopulate)
+    };
+ 
+    return run_tests(tests);
+}
-- 
1.8.1.4

