>From 949033ca51ff429d74a4214be9059deb2addd9c8 Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 12 Jun 2014 10:06:45 +0100
Subject: [PATCH] TESTS: sss_ssh - textual public key format

---
 Makefile.am                     |  4 +-
 src/tests/cmocka/test_sss_ssh.c | 99 +++++++++++++++++++++++++++++++++++++++++
 src/tests/cmocka/test_utils.c   |  3 ++
 3 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/cmocka/test_sss_ssh.c

diff --git a/Makefile.am b/Makefile.am
index 98c0d139f59922c886fc29ab1fdae6fcfa9a657d..b23ab8f883b77d9e8ce9c118aec49de23252fb65 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1736,7 +1736,9 @@ test_ipa_idmap_LDADD = \
     libsss_test_common.la
 
 test_utils_SOURCES = \
-    src/tests/cmocka/test_utils.c
+    src/tests/cmocka/test_utils.c \
+    src/tests/cmocka/test_sss_ssh.c
+
 test_utils_CFLAGS = \
     $(AM_CFLAGS)
 test_utils_LDADD = \
diff --git a/src/tests/cmocka/test_sss_ssh.c b/src/tests/cmocka/test_sss_ssh.c
new file mode 100644
index 0000000000000000000000000000000000000000..03d95dbc84bc2dcbc1a36dc2dc2fad42f7075a58
--- /dev/null
+++ b/src/tests/cmocka/test_sss_ssh.c
@@ -0,0 +1,99 @@
+/*
+    Authors:
+        Pavel Reichl  <preichl@redhat.com>
+
+    Copyright (C) 2014 Red Hat
+
+    Test for the NSS Responder ID-SID mapping interface
+
+    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 "util/util.h"
+#include "util/sss_ssh.h"
+#include "tests/cmocka/common_mock.h"
+
+uint8_t key_data_noLF[] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfymad64oZkWa6q3xLXmCt/LfCRnd6yZSDp7UK6Irx5/Dv69dEKK2kBGL9Wfn+3ZDa6ov2XZrBmUthh8KOJvTw72+axox3kcJ5HwOYZCMeKbcr10RNScGuHErA1HhjTY6M9L8d0atVH2QIxw7ZHoVVnTHC4U4+541YfJkNUiOUIj65cFFZm9ULp32ZPrK+j2wW+XZkHhrZeFMlg4x4fe5FocO6ik1eqLxBejo7tMy+1m3R2a795AIguf6vNWeE5aNMd4pcmPcZHb3JOq3ItzE/3lepXD/3wqMt36EqNykBVE7aJj+LVkcEgjP9CDDsg9j9NB+AuWYmIYqrHW/Rg/vJ developer@sssd.dev.work";
+
+uint8_t key_data_LF[] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfymad64oZkWa6q3xLXmCt/LfCRnd6yZSDp7UK6Irx5/Dv69dEKK2kBGL9Wfn+3ZDa6ov2XZrBmUthh8KOJvTw72+axox3kcJ5HwOYZCMeKbcr10RNScGuHErA1HhjTY6M9L8d0atVH2QIxw7ZHoVVnTHC4U4+541YfJkNUiOUIj65cFFZm9ULp32ZPrK+j2wW+XZkHhrZeFMlg4x4fe5FocO6ik1eqLxBejo7tMy+1m3R2a795AIguf6vNWeE5aNMd4pcmPcZHb3JOq3ItzE/3lepXD/3wqMt36EqNykBVE7aJj+LVkcEgjP9CDDsg9j9NB+AuWYmIYqrHW/Rg/vJ developer@sssd.dev.work\n";
+
+uint8_t key_data_LFLF[] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfymad64oZkWa6q3xLXmCt/LfCRnd6yZSDp7UK6Irx5/Dv69dEKK2kBGL9Wfn+3ZDa6ov2XZrBmUthh8KOJvTw72+axox3kcJ5HwOYZCMeKbcr10RNScGuHErA1HhjTY6M9L8d0atVH2QIxw7ZHoVVnTHC4U4+541YfJkNUiOUIj65cFFZm9ULp32ZPrK+j2wW+XZkHhrZeFMlg4x4fe5FocO6ik1eqLxBejo7tMy+1m3R2a795AIguf6vNWeE5aNMd4pcmPcZHb3JOq3ItzE/3lepXD/3wqMt36EqNykBVE7aJj+LVkcEgjP9CDDsg9j9NB+AuWYmIYqrHW/Rg/vJ developer@sssd.dev.work\n\n";
+
+uint8_t key_data_CRLF[] = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfymad64oZkWa6q3xLXmCt/LfCRnd6yZSDp7UK6Irx5/Dv69dEKK2kBGL9Wfn+3ZDa6ov2XZrBmUthh8KOJvTw72+axox3kcJ5HwOYZCMeKbcr10RNScGuHErA1HhjTY6M9L8d0atVH2QIxw7ZHoVVnTHC4U4+541YfJkNUiOUIj65cFFZm9ULp32ZPrK+j2wW+XZkHhrZeFMlg4x4fe5FocO6ik1eqLxBejo7tMy+1m3R2a795AIguf6vNWeE5aNMd4pcmPcZHb3JOq3ItzE/3lepXD/3wqMt36EqNykBVE7aJj+LVkcEgjP9CDDsg9j9NB+AuWYmIYqrHW/Rg/vJ developer@sssd.dev.work\r\n";
+
+uint8_t key_data_CR_somewhere[] = "ssh-rsa AA\rAAB3NzaC1yc2EAAAADAQABAAABAQDfymad64oZkWa6q3xLXmCt/LfCRnd6yZSDp7UK6Irx5/Dv69dEKK2kBGL9Wf+3ZDa6ov2XZrBmUthh8KOJvTw72+axox3kcJ5HwOYZCMeKbcr10RNScGuHErA1HhjTY6M9L8d0atVH2QIxw7ZHoVVnTHC4U4+541YfJkNUiOUIj65cFFZm9ULp32ZPrK+j2wW+XZkHhrZeFMlg4x4fe5FocO6ik1eqLxBejo7tMy+1m3R2a795AIguf6vNWeE5aNMd4pcmPcZHb3JOq3ItzE/3lepXD/3wqMt36EqNykBVE7aJj+LVkcEgjP9CDDsg9j9NB+AuWYmIYqrHW/Rg/vJ developer@sssd.dev.work\n";
+
+void test_textual_public_key(void **state)
+{
+    TALLOC_CTX *mem_ctx;
+    errno_t ret;
+    char *res;
+
+    struct sss_ssh_pubkey pkey_null_terminated = {
+        .data = key_data_noLF,
+        .data_len = sizeof(key_data_noLF)
+    };
+
+    struct sss_ssh_pubkey pkey = {
+        .data = key_data_noLF,
+        .data_len = sizeof(key_data_noLF) - 1 /* ignore trailling '\0' */
+    };
+
+    struct sss_ssh_pubkey pkey_LF = {
+        .data = key_data_LF,
+        .data_len = sizeof(key_data_LF) - 1 /* ignore trailling '\0' */
+    };
+
+    struct sss_ssh_pubkey pkey_LFLF = {
+        .data = key_data_LFLF,
+        .data_len = sizeof(key_data_LFLF) - 1 /* ignore trailling '\0' */
+    };
+
+    struct sss_ssh_pubkey pkey_CRLF = {
+        .data = key_data_CRLF,
+        .data_len = sizeof(key_data_CRLF) - 1 /* ignore trailling '\0' */
+    };
+
+    struct sss_ssh_pubkey pkey_CR_somewhere = {
+        .data = key_data_CR_somewhere,
+        .data_len = sizeof(key_data_CR_somewhere) - 1 /* ignore traill. '\0' */
+    };
+
+    mem_ctx = talloc_new(NULL);
+    assert_non_null(mem_ctx);
+    check_leaks_push(mem_ctx);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey, &res);
+    assert_int_equal(ret, EOK);
+    talloc_free(res);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey_LF, &res);
+    assert_int_equal(ret, EOK);
+    talloc_free(res);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey_LFLF, &res);
+    assert_int_equal(ret, EINVAL);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey_null_terminated, &res);
+    assert_int_equal(ret, EINVAL);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey_CRLF, &res);
+    assert_int_equal(ret, EINVAL);
+
+    ret = sss_ssh_format_pubkey(mem_ctx, &pkey_CR_somewhere, &res);
+    assert_int_equal(ret, EINVAL);
+
+    assert_true(check_leaks_pop(mem_ctx) == true);
+    talloc_free(mem_ctx);
+}
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index 8b996c2fba8c6ba772dc5cb42eac621a3c4c7407..20e21a9baee0b8e09d573f2fc723db4d7f9c6f94 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -52,6 +52,8 @@ struct dom_list_test_ctx {
     struct sss_domain_info *dom_list;
 };
 
+void test_textual_public_key(void **state);
+
 void setup_dom_list(void **state)
 {
     struct dom_list_test_ctx *test_ctx;
@@ -914,6 +916,7 @@ int main(int argc, const char *argv[])
         unit_test(test_expand_homedir_template_NULL),
         unit_test_setup_teardown(test_expand_homedir_template,
                                  setup_homedir_ctx, teardown_homedir_ctx),
+        unit_test(test_textual_public_key),
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
-- 
1.8.4.2

