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

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Jakub Hrozek <jhrozek@redhat.com>
       4             :         Pavel Březina <pbrezina@redhat.com>
       5             : 
       6             :     Copyright (C) 2014 Red Hat
       7             : 
       8             :     This program is free software; you can redistribute it and/or modify
       9             :     it under the terms of the GNU General Public License as published by
      10             :     the Free Software Foundation; either version 3 of the License, or
      11             :     (at your option) any later version.
      12             : 
      13             :     This program is distributed in the hope that it will be useful,
      14             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :     GNU General Public License for more details.
      17             : 
      18             :     You should have received a copy of the GNU General Public License
      19             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include <talloc.h>
      23             : #include <errno.h>
      24             : #include <popt.h>
      25             : 
      26             : #include "sbus/sssd_dbus.h"
      27             : #include "tests/cmocka/common_mock.h"
      28             : #include "tests/common.h"
      29             : 
      30             : #define BASE_PATH "/some/path"
      31             : 
      32           1 : void test_sbus_opath_strip_prefix(void **state)
      33             : {
      34           1 :     const char *prefix = "/org/freedesktop/sssd/";
      35           1 :     const char *path = "/org/freedesktop/sssd/infopipe";
      36             :     const char *strip;
      37             : 
      38           1 :     strip = sbus_opath_strip_prefix(path, prefix);
      39           1 :     assert_non_null(prefix);
      40           1 :     assert_string_equal(strip, "infopipe");
      41             : 
      42           1 :     strip = sbus_opath_strip_prefix("/other/path", prefix);
      43           1 :     assert_null(strip);
      44           1 : }
      45             : 
      46           1 : void test_sbus_opath_escape_unescape(void **state)
      47             : {
      48             :     char *escaped;
      49             :     char *raw;
      50             :     TALLOC_CTX *mem_ctx;
      51             : 
      52           1 :     assert_true(leak_check_setup());
      53           1 :     mem_ctx = talloc_new(global_talloc_context);
      54             : 
      55           1 :     escaped = sbus_opath_escape_part(mem_ctx, "noescape");
      56           1 :     assert_non_null(escaped);
      57           1 :     assert_string_equal(escaped, "noescape");
      58           1 :     raw = sbus_opath_unescape_part(mem_ctx, escaped);
      59           1 :     talloc_free(escaped);
      60           1 :     assert_non_null(raw);
      61           1 :     assert_string_equal(raw, "noescape");
      62           1 :     talloc_free(raw);
      63             : 
      64           1 :     escaped = sbus_opath_escape_part(mem_ctx, "redhat.com");
      65           1 :     assert_non_null(escaped);
      66           1 :     assert_string_equal(escaped, "redhat_2ecom"); /* dot is 0x2E in ASCII */
      67           1 :     raw = sbus_opath_unescape_part(mem_ctx, escaped);
      68           1 :     talloc_free(escaped);
      69           1 :     assert_non_null(raw);
      70           1 :     assert_string_equal(raw, "redhat.com");
      71           1 :     talloc_free(raw);
      72             : 
      73           1 :     escaped = sbus_opath_escape_part(mem_ctx, "path_with_underscore");
      74           1 :     assert_non_null(escaped);
      75             :     /* underscore is 0x5F in ascii */
      76           1 :     assert_string_equal(escaped, "path_5fwith_5funderscore");
      77           1 :     raw = sbus_opath_unescape_part(mem_ctx, escaped);
      78           1 :     talloc_free(escaped);
      79           1 :     assert_non_null(raw);
      80           1 :     assert_string_equal(raw, "path_with_underscore");
      81           1 :     talloc_free(raw);
      82             : 
      83             :     /* empty string */
      84           1 :     escaped = sbus_opath_escape_part(mem_ctx, "");
      85           1 :     assert_non_null(escaped);
      86           1 :     assert_string_equal(escaped, "_");
      87           1 :     raw = sbus_opath_unescape_part(mem_ctx, escaped);
      88           1 :     talloc_free(escaped);
      89           1 :     assert_non_null(raw);
      90           1 :     assert_string_equal(raw, "");
      91           1 :     talloc_free(raw);
      92             : 
      93             :     /* negative tests */
      94           1 :     escaped = sbus_opath_escape_part(mem_ctx, NULL);
      95           1 :     assert_null(escaped);
      96           1 :     raw = sbus_opath_unescape_part(mem_ctx, "wrongpath_");
      97           1 :     assert_null(raw);
      98             : 
      99           1 :     assert_true(leak_check_teardown());
     100           1 : }
     101             : 
     102           1 : void test_sbus_opath_compose(void **state)
     103             : {
     104             :     char *path;
     105             : 
     106             :     /* Doesn't need escaping */
     107           1 :     path = sbus_opath_compose(NULL, BASE_PATH, "domname");
     108           1 :     assert_non_null(path);
     109           1 :     assert_string_equal(path, BASE_PATH "/domname");
     110           1 :     talloc_free(path);
     111           1 : }
     112             : 
     113           1 : void test_sbus_opath_compose_escape(void **state)
     114             : {
     115             :     char *path;
     116             : 
     117             :     /* A dot needs escaping */
     118           1 :     path = sbus_opath_compose(NULL, BASE_PATH, "redhat.com", NULL);
     119           1 :     assert_non_null(path);
     120           1 :     assert_string_equal(path, BASE_PATH "/redhat_2ecom");
     121           1 :     talloc_free(path);
     122           1 : }
     123             : 
     124           5 : static void check_opath_components(char **input,
     125             :                                    const char **expected)
     126             : {
     127             :     int i;
     128             : 
     129           5 :     assert_non_null(input);
     130           5 :     assert_non_null(expected);
     131             : 
     132          15 :     for (i = 0; input[i] != NULL; i++) {
     133          10 :         assert_non_null(input[i]);
     134          10 :         assert_non_null(expected[i]);
     135          10 :         assert_string_equal(input[i], expected[i]);
     136             :     }
     137             : 
     138           5 :     assert_null(input[i]);
     139           5 :     assert_null(expected[i]);
     140           5 : }
     141             : 
     142           4 : static void check_opath_components_and_length(char **input,
     143             :                                               size_t input_len,
     144             :                                               const char **expected,
     145             :                                               size_t expected_len)
     146             : {
     147           4 :     assert_true(input_len == expected_len);
     148           4 :     check_opath_components(input, expected);
     149           4 : }
     150             : 
     151           1 : void test_sbus_opath_decompose_noprefix(void **state)
     152             : {
     153           1 :     const char *path = "/object/path/parts";
     154           1 :     const char *expected[] = {"object", "path", "parts", NULL};
     155           1 :     size_t expected_len = sizeof(expected) / sizeof(char *) - 1;
     156             :     char **components;
     157             :     size_t len;
     158             :     errno_t ret;
     159             : 
     160           1 :     ret = sbus_opath_decompose(NULL, path, NULL, &components, &len);
     161           1 :     assert_int_equal(ret, EOK);
     162           1 :     check_opath_components_and_length(components, len, expected, expected_len);
     163           1 :     talloc_free(components);
     164           1 : }
     165             : 
     166           1 : void test_sbus_opath_decompose_prefix(void **state)
     167             : {
     168           1 :     const char *path = "/object/path/parts";
     169           1 :     const char *expected[] = {"parts", NULL};
     170           1 :     size_t expected_len = sizeof(expected) / sizeof(char *) - 1;
     171             :     char **components;
     172             :     size_t len;
     173             :     errno_t ret;
     174             : 
     175           1 :     ret = sbus_opath_decompose(NULL, path, "/object/path", &components, &len);
     176           1 :     assert_int_equal(ret, EOK);
     177           1 :     check_opath_components_and_length(components, len, expected, expected_len);
     178           1 :     talloc_free(components);
     179           1 : }
     180             : 
     181           1 : void test_sbus_opath_decompose_prefix_slash(void **state)
     182             : {
     183           1 :     const char *path = "/object/path/parts";
     184           1 :     const char *expected[] = {"parts", NULL};
     185           1 :     size_t expected_len = sizeof(expected) / sizeof(char *) - 1;
     186             :     char **components;
     187             :     size_t len;
     188             :     errno_t ret;
     189             : 
     190           1 :     ret = sbus_opath_decompose(NULL, path, "/object/path/", &components, &len);
     191           1 :     assert_int_equal(ret, EOK);
     192           1 :     check_opath_components_and_length(components, len, expected, expected_len);
     193           1 :     talloc_free(components);
     194           1 : }
     195             : 
     196           1 : void test_sbus_opath_decompose_wrong_prefix(void **state)
     197             : {
     198           1 :     const char *path = "/object/path/parts";
     199             :     char **components;
     200             :     size_t len;
     201             :     errno_t ret;
     202             : 
     203           1 :     ret = sbus_opath_decompose(NULL, path, "/wrong/prefix", &components, &len);
     204           1 :     assert_int_equal(ret, ERR_SBUS_INVALID_PATH);
     205           1 : }
     206             : 
     207           1 : void test_sbus_opath_decompose_escaped(void **state)
     208             : {
     209           1 :     const char *path = "/object/redhat_2ecom";
     210           1 :     const char *expected[] = {"object", "redhat.com", NULL};
     211           1 :     size_t expected_len = sizeof(expected) / sizeof(char *) - 1;
     212             :     char **components;
     213             :     size_t len;
     214             :     errno_t ret;
     215             : 
     216           1 :     ret = sbus_opath_decompose(NULL, path, NULL, &components, &len);
     217           1 :     assert_int_equal(ret, EOK);
     218           1 :     check_opath_components_and_length(components, len, expected, expected_len);
     219           1 :     talloc_free(components);
     220           1 : }
     221             : 
     222           1 : void test_sbus_opath_decompose_exact_correct(void **state)
     223             : {
     224           1 :     const char *path = "/object/path/parts";
     225           1 :     const char *expected[] = {"object", "path", "parts", NULL};
     226             :     char **components;
     227             :     errno_t ret;
     228             : 
     229           1 :     ret = sbus_opath_decompose_exact(NULL, path, NULL, 3, &components);
     230           1 :     assert_int_equal(ret, EOK);
     231           1 :     check_opath_components(components, expected);
     232           1 :     talloc_free(components);
     233           1 : }
     234             : 
     235           1 : void test_sbus_opath_decompose_exact_wrong(void **state)
     236             : {
     237           1 :     const char *path = "/object/path/parts";
     238             :     char **components;
     239             :     errno_t ret;
     240             : 
     241           1 :     ret = sbus_opath_decompose_exact(NULL, path, NULL, 2, &components);
     242           1 :     assert_int_equal(ret, ERR_SBUS_INVALID_PATH);
     243           1 : }
     244             : 
     245           1 : void test_sbus_opath_get_object_name(void **state)
     246             : {
     247           1 :     const char *path = BASE_PATH "/redhat_2ecom";
     248             :     char *name;
     249             : 
     250           1 :     name = sbus_opath_get_object_name(NULL, path, BASE_PATH);
     251           1 :     assert_non_null(name);
     252           1 :     assert_string_equal(name, "redhat.com");
     253           1 :     talloc_free(name);
     254             : 
     255           1 :     name = sbus_opath_get_object_name(NULL, path, BASE_PATH "/");
     256           1 :     assert_non_null(name);
     257           1 :     assert_string_equal(name, "redhat.com");
     258           1 :     talloc_free(name);
     259             : 
     260           1 :     name = sbus_opath_get_object_name(NULL, BASE_PATH, BASE_PATH);
     261           1 :     assert_null(name);
     262             : 
     263           1 :     name = sbus_opath_get_object_name(NULL, "invalid", BASE_PATH);
     264           1 :     assert_null(name);
     265           1 : }
     266             : 
     267           1 : int main(int argc, const char *argv[])
     268             : {
     269             :     poptContext pc;
     270             :     int opt;
     271           6 :     struct poptOption long_options[] = {
     272             :         POPT_AUTOHELP
     273           5 :         SSSD_DEBUG_OPTS
     274             :         POPT_TABLEEND
     275             :     };
     276             : 
     277           1 :     const struct CMUnitTest tests[] = {
     278             :         cmocka_unit_test(test_sbus_opath_strip_prefix),
     279             :         cmocka_unit_test(test_sbus_opath_escape_unescape),
     280             :         cmocka_unit_test(test_sbus_opath_compose),
     281             :         cmocka_unit_test(test_sbus_opath_compose_escape),
     282             :         cmocka_unit_test(test_sbus_opath_decompose_noprefix),
     283             :         cmocka_unit_test(test_sbus_opath_decompose_prefix),
     284             :         cmocka_unit_test(test_sbus_opath_decompose_prefix_slash),
     285             :         cmocka_unit_test(test_sbus_opath_decompose_wrong_prefix),
     286             :         cmocka_unit_test(test_sbus_opath_decompose_escaped),
     287             :         cmocka_unit_test(test_sbus_opath_decompose_exact_correct),
     288             :         cmocka_unit_test(test_sbus_opath_decompose_exact_wrong),
     289             :         cmocka_unit_test(test_sbus_opath_get_object_name)
     290             :     };
     291             : 
     292             :     /* Set debug level to invalid value so we can deside if -d 0 was used. */
     293           1 :     debug_level = SSSDBG_INVALID;
     294             : 
     295           1 :     pc = poptGetContext(argv[0], argc, argv, long_options, 0);
     296           1 :     while((opt = poptGetNextOpt(pc)) != -1) {
     297             :         switch(opt) {
     298             :         default:
     299           0 :             fprintf(stderr, "\nInvalid option %s: %s\n\n",
     300             :                     poptBadOption(pc, 0), poptStrerror(opt));
     301           0 :             poptPrintUsage(pc, stderr, 0);
     302           0 :             return 1;
     303             :         }
     304             :     }
     305           1 :     poptFreeContext(pc);
     306             : 
     307           1 :     DEBUG_CLI_INIT(debug_level);
     308             : 
     309           1 :     return cmocka_run_group_tests(tests, NULL, NULL);
     310             : }

Generated by: LCOV version 1.10