LCOV - code coverage report
Current view: top level - lib/util/tests - strv_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 36 41 87.8 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Tests for strv_util
       3             :  *
       4             :  * Copyright Martin Schwenke <martin@meltin.net> 2016
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <talloc.h>
      21             : 
      22             : #include "replace.h"
      23             : 
      24             : #include "libcli/util/ntstatus.h"
      25             : #include "torture/torture.h"
      26             : #include "lib/util/data_blob.h"
      27             : #include "torture/local/proto.h"
      28             : 
      29             : #include "lib/util/strv.h"
      30             : #include "lib/util/strv_util.h"
      31             : 
      32           1 : static bool test_strv_split_none(struct torture_context *tctx)
      33             : {
      34           1 :         char *strv = NULL;
      35           1 :         int ret;
      36             : 
      37             :         /* NULL has 0 entries */
      38           1 :         ret = strv_split(tctx, &strv, NULL, " ");
      39           1 :         torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
      40           1 :         torture_assert_int_equal(tctx,
      41             :                                  strv_count(strv),
      42             :                                  0,
      43             :                                  "strv_split() on NULL failed");
      44           1 :         TALLOC_FREE(strv);
      45             : 
      46             :         /* Empty string has 0 entries */
      47           1 :         ret = strv_split(tctx, &strv, "", " ");
      48           1 :         torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
      49           1 :         torture_assert_int_equal(tctx,
      50             :                                  strv_count(strv),
      51             :                                  0,
      52             :                                  "strv_split() on \"\" failed");
      53           1 :         TALLOC_FREE(strv);
      54             : 
      55             :         /* String containing only separators has 0 entries */
      56           1 :         ret = strv_split(tctx, &strv, "abcabcabc", "cba ");
      57           1 :         torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
      58           1 :         torture_assert_int_equal(tctx,
      59             :                                  strv_count(strv),
      60             :                                  0,
      61             :                                  "strv_split() on seps-only failed");
      62           1 :         TALLOC_FREE(strv);
      63             : 
      64           0 :         return true;
      65             : }
      66             : 
      67             : struct test_str_split_data {
      68             :         const char *in;
      69             :         const char *sep;
      70             :         const char *out[10]; /* Hardcoded maximum! */
      71             : };
      72             : 
      73           1 : static bool test_strv_split_some(struct torture_context *tctx)
      74             : {
      75           1 :         const struct test_str_split_data data[] = {
      76             :                 {
      77             :                         /* Single string */
      78             :                         .in = "foo",
      79             :                         .sep = " \t",
      80             :                         .out = { "foo" }
      81             :                 },
      82             :                 {
      83             :                         /* Single string, single leading separator */
      84             :                         .in = " foo",
      85             :                         .sep = " \t",
      86             :                         .out = { "foo" }
      87             :                 },
      88             :                 {
      89             :                         /* Single string, single trailing separator */
      90             :                         .in = " foo",
      91             :                         .sep = " \t",
      92             :                         .out = { "foo" }
      93             :                 },
      94             :                 {
      95             :                         /* Single string, lots of separators */
      96             :                         .in = " \t foo\t ",
      97             :                         .sep = " \t",
      98             :                         .out = { "foo" }
      99             :                 },
     100             :                 {
     101             :                         /* Multiple strings, many separators */
     102             :                         .in = " \t foo   bar\t\tx\t        samba\t ",
     103             :                         .sep = " \t",
     104             :                         .out = { "foo", "bar", "x", "samba" }
     105             :                 },
     106             :         };
     107           1 :         const char *t;
     108           1 :         char *strv = NULL;
     109           1 :         size_t j;
     110             : 
     111           6 :         for (j = 0; j < ARRAY_SIZE(data); j++) {
     112             :                 size_t i, num;
     113             :                 int ret;
     114           0 :                 const struct test_str_split_data *d = &data[j];
     115             : 
     116           0 :                 num = 0;
     117          13 :                 while (num < ARRAY_SIZE(d->out) && d->out[num] != NULL) {
     118           8 :                         num++;
     119             :                 }
     120           5 :                 ret = strv_split(tctx, &strv, d->in, d->sep);
     121           5 :                 torture_assert(tctx, ret == 0, "strv_split() on NULL failed");
     122           5 :                 torture_assert_int_equal(tctx,
     123             :                                          strv_count(strv),
     124             :                                          num,
     125             :                                          "strv_split() failed");
     126           0 :                 t = NULL;
     127          13 :                 for (i = 0; i < num; i++) {
     128           8 :                         t = strv_next(strv, t);
     129           8 :                         torture_assert(tctx,
     130             :                                        strcmp(t, d->out[i]) == 0,
     131             :                                        "strv_split() failed");
     132             :                 }
     133           5 :                 TALLOC_FREE(strv);
     134             :         }
     135           0 :         return true;
     136             : }
     137             : 
     138        2354 : struct torture_suite *torture_local_util_strv_util(TALLOC_CTX *mem_ctx)
     139             : {
     140         125 :         struct torture_suite *suite =
     141        2354 :                 torture_suite_create(mem_ctx, "strv_util");
     142             : 
     143        2354 :         torture_suite_add_simple_test(suite,
     144             :                                       "strv_split_none",
     145             :                                       test_strv_split_none);
     146        2354 :         torture_suite_add_simple_test(suite,
     147             :                                       "strv_split_some",
     148             :                                       test_strv_split_some);
     149        2354 :         return suite;
     150             : }

Generated by: LCOV version 1.14