From 407cc46e38e1056f3be5355b909f19e8089fdb47 Mon Sep 17 00:00:00 2001
From: Pallavi Jha <pallavikumarijha@gmail.com>
Date: Sat, 9 Nov 2013 18:57:31 +0530
Subject: [PATCH] cmocka unit test for authtok module added.

---
 Makefile.am                     |  15 ++
 src/tests/cmocka/test_authtok.c | 310 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 325 insertions(+)
 create mode 100644 src/tests/cmocka/test_authtok.c

diff --git a/Makefile.am b/Makefile.am
index e9bed47e3454867dadf7a2afdf90513c6bd0a796..cfa0e8d99b4133851212456fcb5122597bd0ad7b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -149,6 +149,7 @@ if HAVE_CMOCKA
         nss-srv-tests \
         test-find-uid \
         test-io \
+        test-authtok \
         sss_nss_idmap-tests \
         dyndns-tests \
         fqnames-tests \
@@ -1331,6 +1332,20 @@ test_io_CFLAGS = \
 test_io_LDADD = \
     $(CMOCKA_LIBS)
 
+test_authtok_SOURCES = \
+    src/tests/cmocka/test_authtok.c \
+    src/util/authtok.c \
+    src/util/util.c
+test_authtok_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(TALLOC_CFLAGS) \
+    $(DHASH_CFLAGS)
+test_authtok_LDADD = \
+    $(TALLOC_LIBS) \
+    $(CMOCKA_LIBS) \
+    $(DHASH_LIBS) \
+    libsss_debug.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_authtok.c b/src/tests/cmocka/test_authtok.c
new file mode 100644
index 0000000000000000000000000000000000000000..43b3d142b3a18b3db829d17d9d760704e9e60cff
--- /dev/null
+++ b/src/tests/cmocka/test_authtok.c
@@ -0,0 +1,310 @@
+/*
+    SSSD
+
+    authtok - Utilities tests
+
+    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 <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <string.h>
+
+#include "util/authtok.h"
+
+
+struct test_state
+{
+    struct sss_auth_token *authtoken;
+};
+
+static void setup(void **state)
+{
+    struct test_state *ts = NULL;
+
+    ts = talloc(NULL, struct test_state);
+    assert_non_null(ts);
+
+    ts->authtoken = sss_authtok_new(ts);
+    assert_non_null(ts->authtoken);
+
+    *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_authtok_new(void **state)
+{
+    TALLOC_CTX *mem_ctx;
+    struct sss_auth_token *authtoken;
+
+    mem_ctx = talloc_new(NULL);
+    if (mem_ctx == NULL) {
+        return ENOMEM;
+    }
+
+    authtoken = sss_authtok_new(mem_ctx);
+    assert_non_null(authtoken);
+
+    talloc_free(mem_ctx);
+}
+
+static void test_sss_authtok_set(void **state)
+{
+    size_t len;
+    errno_t ret;
+    uint8_t *data;
+    enum sss_authtok_type type;
+
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    data = strdup("password");
+    len = strlen(data) + 1;
+
+    /* Test when type has value SSS_AUTHTOK_TYPE_PASSWORD */
+    type = SSS_AUTHTOK_TYPE_PASSWORD;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+
+    ret = sss_authtok_set(ts->authtoken, type, data, 0);
+    assert_int_equal(ret, EOK);
+
+    /*ret = sss_authtok_set(ts->authtoken, type, '\0', 0);
+    assert_int_not_equal(ret, EINVAL);*/
+
+ 
+
+    /* Test when type has value SSS_AUTHTOK_TYPE_CCFILE */
+    type = SSS_AUTHTOK_TYPE_PASSWORD;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+
+    ret = sss_authtok_set(ts->authtoken, type, data, 0);
+    assert_int_equal(ret, EOK);
+
+    /*ret = sss_authtok_set(ts->authtoken, type, '\0', 0);
+    assert_int_equal(ret, EINVAL);*/
+
+    /* Test when type has value SSS_AUTHTOK_TYPE_EMPTY*/
+    type = SSS_AUTHTOK_TYPE_EMPTY;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+
+    ret = sss_authtok_set(ts->authtoken, type, '\0', 0);
+    assert_int_equal(ret, EOK);
+
+    /* Test when authtoken is NULL */
+    //ret = sss_authtok_set(NULL, type, data, len);
+    //assert_int_equal(ret, EINVAL);
+}
+
+/* @test_authtok : tests following functions -
+ * sss_authtok_get_type
+ * sss_authtok_get_size
+ * sss_authtok_get_data
+ * sss_authtok_get_password
+ * sss_authtok_get_ccfile
+ */
+static void test_sss_authtok(void **state)
+{
+    size_t len;
+    errno_t ret;
+    uint8_t *data;
+    enum sss_authtok_type type;
+
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
+
+    data = strdup("password");
+    len = strlen(data) + 1;
+ 
+ 
+    /* Test with NULL authtoken */
+    /*type = SSS_AUTHTOK_TYPE_PASSWORD;
+    ret = sss_authtok_set(NULL, type, data, len);
+
+    assert_int_equal(ret, EINVAL);
+    assert_int_equal(EINVAL, sss_authtok_get_type(NULL));
+    assert_int_equal(EINVAL, sss_authtok_get_size(ts->authtoken));
+    assert_string_equal(EINVAL, sss_authtok_get_data(ts->authtoken));
+    */
+
+    /* test when type is SSS_AUTHTOK_TYPE_PASSWORD */
+    type = SSS_AUTHTOK_TYPE_PASSWORD;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+    assert_int_equal(type, sss_authtok_get_type(ts->authtoken));
+    assert_int_equal(len, sss_authtok_get_size(ts->authtoken));
+    assert_string_equal(data, sss_authtok_get_data(ts->authtoken));
+
+    size_t *ret_len = (size_t *)malloc(sizeof(size_t *));
+    const char **pwd = (const char **)malloc(sizeof(const char **));
+
+    ret = sss_authtok_get_password(ts->authtoken, pwd, ret_len);
+
+    assert_int_equal(ret, EOK);
+    assert_string_equal(data, *pwd);
+    assert_int_equal(len - 1, *ret_len);
+
+    ret = sss_authtok_get_ccfile(ts->authtoken, pwd, ret_len);
+    assert_int_equal(ret, EACCES);
+
+    /* test when type is SSS_AUTHTOK_TYPE_CCFILE */
+    type = SSS_AUTHTOK_TYPE_CCFILE;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+    assert_int_equal(type, sss_authtok_get_type(ts->authtoken));
+    assert_int_equal(len, sss_authtok_get_size(ts->authtoken));
+    assert_string_equal(data, sss_authtok_get_data(ts->authtoken));
+
+    ret = sss_authtok_get_password(ts->authtoken, pwd, ret_len);
+ 
+    assert_int_equal(ret, EACCES);
+
+    ret = sss_authtok_get_ccfile(ts->authtoken, pwd, ret_len);
+
+    assert_int_equal(ret, EOK);
+    assert_string_equal(data, *pwd);
+    assert_int_equal(len - 1, *ret_len);
+
+    /* test when type is SSS_AUTHTOK_TYPE_EMPTY */
+    type = SSS_AUTHTOK_TYPE_EMPTY;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+    assert_int_equal(type, sss_authtok_get_type(ts->authtoken));
+    assert_int_equal(0, sss_authtok_get_size(ts->authtoken));
+    assert_null(sss_authtok_get_data(ts->authtoken));
+ 
+    ret = sss_authtok_get_password(ts->authtoken, pwd, ret_len);
+
+    assert_int_equal(ret, ENOENT);
+
+    ret = sss_authtok_get_ccfile(ts->authtoken, pwd, ret_len);
+
+    assert_int_equal(ret, ENOENT);
+
+    free(pwd);
+    free(ret_len);
+}
+
+static void test_sss_authtok_wipe_password(void **state)
+{
+    size_t len;
+    errno_t ret;
+    uint8_t *data;
+    enum sss_authtok_type type;
+ 
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    data = strdup("password");
+    len = strlen(data) + 1;
+    type = SSS_AUTHTOK_TYPE_PASSWORD;
+
+    /* Test with NULL authtoken */
+    /*ret = sss_wipe_password(NULL);
+
+    assert_int_equal(ret, EINVAL);
+    */
+
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+
+    sss_authtok_wipe_password(ts->authtoken);
+
+    size_t *ret_len = malloc(sizeof(size_t));
+    const char **pwd = malloc(sizeof(const char));
+
+    ret = sss_authtok_get_password(ts->authtoken, pwd, ret_len);
+ 
+    assert_int_equal(ret, EOK);
+    assert_string_equal(*pwd, "");
+    assert_int_equal(len - 1, *ret_len);
+
+    free(ret_len);
+    free(pwd);
+}
+
+static void test_sss_authtok_copy(void **state)
+{
+    size_t len;
+    errno_t ret;
+    uint8_t *data;
+    enum sss_authtok_type type;
+ 
+    struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
+ 
+    TALLOC_CTX *mem_ctx;
+    struct sss_auth_token *dest_authtoken;
+
+    mem_ctx = talloc_new(NULL);
+    if (mem_ctx == NULL) {
+        return ENOMEM;
+    }
+
+    dest_authtoken = sss_authtok_new(mem_ctx);
+    assert_non_null(dest_authtoken);
+
+    data = strdup("password");
+    len = strlen(data) + 1;
+    type = SSS_AUTHTOK_TYPE_EMPTY;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+    assert_int_equal(EOK, sss_authtok_copy(ts->authtoken, dest_authtoken));
+
+    type = SSS_AUTHTOK_TYPE_PASSWORD;
+    ret = sss_authtok_set(ts->authtoken, type, data, len);
+
+    assert_int_equal(ret, EOK);
+
+    ret = sss_authtok_copy(ts->authtoken, dest_authtoken);
+
+    assert_int_equal(ret, EOK);
+    assert_int_equal(type, sss_authtok_get_type(dest_authtoken));
+    assert_string_equal(data, sss_authtok_get_data(dest_authtoken));
+    assert_int_equal(len, sss_authtok_get_size(dest_authtoken));
+
+    talloc_free(mem_ctx);
+}
+
+int main(void)
+{
+    const UnitTest tests[] = {
+        unit_test(test_sss_authtok_new),
+        unit_test_setup_teardown(test_sss_authtok_set, setup, teardown),
+        unit_test_setup_teardown(test_sss_authtok, setup, teardown),
+        unit_test_setup_teardown(test_sss_authtok_wipe_password, setup,
+                                 teardown),
+        unit_test_setup_teardown(test_sss_authtok_copy, setup, teardown)
+    };
+
+    return run_tests(tests);
+}
-- 
1.8.1.4

