LCOV - code coverage report
Current view: top level - tests/cmocka - test_string_utils.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 75 75 100.0 %
Date: 2015-10-19 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Lukas Slebodnik  <slebodnikl@redhat.com>
       4             : 
       5             :     Copyright (C) 2014 Red Hat
       6             : 
       7             :     This program is free software; you can redistribute it and/or modify
       8             :     it under the terms of the GNU General Public License as published by
       9             :     the Free Software Foundation; either version 3 of the License, or
      10             :     (at your option) any later version.
      11             : 
      12             :     This program is distributed in the hope that it will be useful,
      13             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :     GNU General Public License for more details.
      16             : 
      17             :     You should have received a copy of the GNU General Public License
      18             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "util/util.h"
      22             : #include "tests/cmocka/common_mock.h"
      23             : 
      24           1 : void test_replace_whitespaces(void **state)
      25             : {
      26             :     TALLOC_CTX *mem_ctx;
      27           1 :     const char *input_str = "Lorem ipsum dolor sit amet";
      28             :     const char *res;
      29             :     size_t i;
      30             : 
      31             :     struct {
      32             :         const char *input;
      33             :         const char *output;
      34             :         const char replace_char;
      35           1 :     } data_set[] = {
      36             :         { "", "", '-' },
      37             :         { " ", "-", '-' },
      38             :         { "abcd", "abcd", '-' },
      39             :         { "a b c d", "a-b-c-d", '-' },
      40             :         { " a b c d ", "-a-b-c-d-", '-' },
      41             :         { " ", "^", '^' },
      42             :         { "abcd", "abcd", '^' },
      43             :         { "a b c d", "a^b^c^d", '^' },
      44             :         { " a b c d ", "^a^b^c^d^", '^' },
      45             :         { " ", "^", '^' },
      46             :         { " ", " ", ' ' },
      47             :         { "    ", "    ", ' ' },
      48             :         { "abcd", "abcd", ' ' },
      49             :         { "a b c d", "a b c d", ' ' },
      50             :         { "a b^c d", "a b^c d", '^' },
      51             :         { NULL, NULL, '\0' },
      52             :     };
      53             : 
      54           1 :     mem_ctx = talloc_new(NULL);
      55           1 :     assert_non_null(mem_ctx);
      56           1 :     check_leaks_push(mem_ctx);
      57             : 
      58           1 :     res = sss_replace_space(mem_ctx, input_str, '\0');
      59           1 :     assert_string_equal(res, input_str);
      60           1 :     talloc_zfree(res);
      61             : 
      62           1 :     res = sss_replace_space(mem_ctx, input_str, '\0');
      63           1 :     assert_string_equal(res, input_str);
      64           1 :     talloc_zfree(res);
      65             : 
      66          16 :     for (i=0; data_set[i].input != NULL; ++i) {
      67          15 :         res = sss_replace_space(mem_ctx, data_set[i].input,
      68          15 :                                 data_set[i].replace_char);
      69          15 :         assert_non_null(res);
      70          15 :         assert_string_equal(res, data_set[i].output);
      71          15 :         talloc_zfree(res);
      72             :     }
      73             : 
      74           1 :     assert_true(check_leaks_pop(mem_ctx) == true);
      75           1 :     talloc_free(mem_ctx);
      76           1 : }
      77             : 
      78           1 : void test_reverse_replace_whitespaces(void **state)
      79             : {
      80             :     TALLOC_CTX *mem_ctx;
      81           1 :     char *input_str = discard_const_p(char, "Lorem ipsum dolor sit amet");
      82             :     char *res;
      83             :     size_t i;
      84             : 
      85             :     struct {
      86             :         const char *input;
      87             :         const char *output;
      88             :         const char replace_char;
      89           1 :     } data_set[] = {
      90             :         { "", "", '-' },
      91             :         { "-", " ", '-' },
      92             :         { "----", "    ", '-' },
      93             :         { "abcd", "abcd", '-' },
      94             :         { "a-b-c-d", "a b c d", '-' },
      95             :         { "-a-b-c-d-", " a b c d ", '-' },
      96             :         { "a b c d", "a b c d", '-' },
      97             :         { " a b c d ", " a b c d ", '-' },
      98             :         { "^", " ", '^' },
      99             :         { "^^^^", "    ", '^' },
     100             :         { "abcd", "abcd", '^' },
     101             :         { "a^b^c^d", "a b c d", '^' },
     102             :         { "^a^b^c^d^", " a b c d ", '^' },
     103             :         { " ", " ", ' ' },
     104             :         { "    ", "    ", ' ' },
     105             :         { "abcd", "abcd", ' ' },
     106             :         { "a b c d", "a b c d", ' ' },
     107             :         { " a b c d ", " a b c d ", ' ' },
     108             :         { "a b^c d", "a b^c d", '^' },
     109             :         { NULL, NULL, '\0' },
     110             :     };
     111             : 
     112           1 :     mem_ctx = talloc_new(NULL);
     113           1 :     assert_non_null(mem_ctx);
     114           1 :     check_leaks_push(mem_ctx);
     115             : 
     116           1 :     res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
     117           1 :     assert_string_equal(res, input_str);
     118           1 :     talloc_free(res);
     119             : 
     120           1 :     res = sss_reverse_replace_space(mem_ctx, input_str, '\0');
     121           1 :     assert_string_equal(res, input_str);
     122           1 :     talloc_free(res);
     123             : 
     124          20 :     for (i=0; data_set[i].input != NULL; ++i) {
     125          19 :         input_str = discard_const_p(char, data_set[i].input);
     126          19 :         res = sss_reverse_replace_space(mem_ctx, input_str,
     127          19 :                                         data_set[i].replace_char);
     128          19 :         assert_non_null(res);
     129          19 :         assert_string_equal(res, data_set[i].output);
     130          19 :         talloc_zfree(res);
     131             :     }
     132             : 
     133           1 :     assert_true(check_leaks_pop(mem_ctx) == true);
     134           1 :     talloc_free(mem_ctx);
     135           1 : }
     136             : 
     137           1 : void test_guid_blob_to_string_buf(void **state)
     138             : {
     139             :     int ret;
     140             :     char str_buf[GUID_STR_BUF_SIZE];
     141             :     size_t c;
     142             : 
     143             :     /* How to get test data:
     144             :      * The objectGUID attribute contains a 16byte long binary value
     145             :      * representing the GUID of the object. This data can be converted
     146             :      * manually to the string representation but it might be easier to use
     147             :      * LDAP_SERVER_EXTENDED_DN_OID as described in [MS-ADST] section
     148             :      * 3.1.1.3.4.1.5. This is an LDAP extended control which adds the GUID and
     149             :      * the SID to the DN of an object. This can be activate with the -E
     150             :      * ldapsearch option like:
     151             :      *
     152             :      *  ldapsearch -E 1.2.840.113556.1.4.529=::MAMCAQE= ....
     153             :      *
     154             :      * where 'MAMCAQE=' is the base64 encoded BER sequence with the integer
     155             :      * value 1 (see [MS-ADTS] for details about possible values).
     156             :      *
     157             :      * Btw, if you want to use the string representation of a GUID to search
     158             :      * for an object in AD you have to use the GUID as the search base in the
     159             :      * following form:
     160             :      *
     161             :      *  ldapsearch b '<GUID=fea80d8d-dbd5-4f84-8574-7db0477f962e>' ...
     162             :      *
     163             :      * (please note that the '<' and '>' are really needed).
     164             :      */
     165             :     struct test_data {
     166             :         uint8_t blob[16];
     167             :         const char *guid_str;
     168           1 :     } test_data[] = {
     169             :         {{0x8d, 0x0d, 0xa8, 0xfe, 0xd5, 0xdb, 0x84, 0x4f,
     170             :           0x85, 0x74, 0x7d, 0xb0, 0x47, 0x7f, 0x96, 0x2e},
     171             :         "fea80d8d-dbd5-4f84-8574-7db0477f962e"},
     172             :         {{0x91, 0x7e, 0x2e, 0xf8, 0x4e, 0x44, 0xfa, 0x4e,
     173             :          0xb1, 0x13, 0x08, 0x98, 0x63, 0x49, 0x6c, 0xc6},
     174             :         "f82e7e91-444e-4efa-b113-089863496cc6"},
     175             :         {{0}, NULL}
     176             :     };
     177             : 
     178           1 :     ret = guid_blob_to_string_buf(NULL, str_buf, GUID_STR_BUF_SIZE);
     179           1 :     assert_int_equal(ret, EINVAL);
     180             : 
     181           1 :     ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", NULL,
     182             :                                   GUID_STR_BUF_SIZE);
     183           1 :     assert_int_equal(ret, EINVAL);
     184             : 
     185           1 :     ret = guid_blob_to_string_buf((const uint8_t *) "1234567812345678", str_buf, 0);
     186           1 :     assert_int_equal(ret, EINVAL);
     187             : 
     188           3 :     for (c = 0; test_data[c].guid_str != NULL; c++) {
     189           2 :         ret = guid_blob_to_string_buf(test_data[c].blob, str_buf,
     190             :                                       sizeof(str_buf));
     191           2 :         assert_int_equal(ret, EOK);
     192           2 :         assert_string_equal(test_data[c].guid_str, str_buf);
     193             :     }
     194           1 : }
     195             : 
     196           1 : void test_get_last_x_chars(void **state)
     197             : {
     198             :     const char *s;
     199             : 
     200           1 :     s = get_last_x_chars(NULL, 0);
     201           1 :     assert_null(s);
     202             : 
     203           1 :     s = get_last_x_chars("abc", 0);
     204           1 :     assert_non_null(s);
     205           1 :     assert_string_equal(s, "");
     206             : 
     207           1 :     s = get_last_x_chars("abc", 1);
     208           1 :     assert_non_null(s);
     209           1 :     assert_string_equal(s, "c");
     210             : 
     211           1 :     s = get_last_x_chars("abc", 2);
     212           1 :     assert_non_null(s);
     213           1 :     assert_string_equal(s, "bc");
     214             : 
     215           1 :     s = get_last_x_chars("abc", 3);
     216           1 :     assert_non_null(s);
     217           1 :     assert_string_equal(s, "abc");
     218             : 
     219           1 :     s = get_last_x_chars("abc", 4);
     220           1 :     assert_non_null(s);
     221           1 :     assert_string_equal(s, "abc");
     222           1 : }

Generated by: LCOV version 1.10