LCOV - code coverage report
Current view: top level - lib/util/tests - strv.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 67 74 90.5 %
Date: 2024-04-21 15:09:00 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Tests for strv
       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             : 
      21             : #include <talloc.h>
      22             : 
      23             : #include "replace.h"
      24             : 
      25             : #include "libcli/util/ntstatus.h"
      26             : #include "torture/torture.h"
      27             : #include "lib/util/data_blob.h"
      28             : #include "torture/local/proto.h"
      29             : 
      30             : #include "lib/util/strv.h"
      31             : 
      32           1 : static bool test_strv_empty(struct torture_context *tctx)
      33             : {
      34             :         /* NULL strv contains 0 entries */
      35           1 :         torture_assert_int_equal(tctx,
      36             :                                  strv_count(NULL),
      37             :                                  0,
      38             :                                  "strv_count() on NULL failed");
      39             : 
      40             :         /* NULL strv has no next entry */
      41           1 :         torture_assert(tctx,
      42             :                        strv_next(NULL, NULL) == NULL,
      43             :                        "strv_next() on NULL failed");
      44             : 
      45           0 :         return true;
      46             : }
      47             : 
      48           1 : static bool test_strv_single(struct torture_context *tctx)
      49             : {
      50           1 :         const char *data = "foo";
      51           1 :         char *strv = NULL;
      52           1 :         char *t;
      53           1 :         int ret;
      54             : 
      55             :         /* Add an item */
      56           1 :         ret = strv_add(tctx, &strv, data);
      57           1 :         torture_assert(tctx, ret == 0, "strv_add() failed");
      58             : 
      59             :         /* Is there 1 item? */
      60           1 :         torture_assert_int_equal(tctx,
      61             :                                  strv_count(strv), 1,
      62             :                                  "strv_count() failed");
      63             : 
      64             :         /* Is the expected item the first one? */
      65           1 :         t = strv_next(strv, NULL);
      66           1 :         torture_assert(tctx,
      67             :                        strcmp(t, data) == 0,
      68             :                        "strv_next() failed");
      69             : 
      70             :         /* Can the expected item be found? */
      71           1 :         t = strv_find(strv, data);
      72           1 :         torture_assert(tctx,
      73             :                        strcmp(t, data) == 0,
      74             :                        "strv_next() failed");
      75             : 
      76             :         /* Delete it */
      77           1 :         strv_delete(&strv, t);
      78             : 
      79             :         /* Should have no items */
      80           1 :         torture_assert_int_equal(tctx,
      81             :                                  strv_count(strv), 0,
      82             :                                  "strv_count() failed");
      83           0 :         return true;
      84             : }
      85             : 
      86           1 : static bool test_strv_multi(struct torture_context *tctx)
      87             : {
      88           1 :         const char *data[] = { "foo", "bar", "", "samba", "x"};
      89           1 :         char *strv = NULL;
      90           1 :         char *t;
      91           1 :         int i, ret;
      92           1 :         const int num = ARRAY_SIZE(data);
      93             : 
      94             :         /* Add items */
      95           6 :         for (i = 0; i < num; i++) {
      96           5 :                 ret = strv_add(tctx, &strv, data[i]);
      97           5 :                 torture_assert(tctx, ret == 0, "strv_add() failed");
      98             :         }
      99             : 
     100           1 :         torture_assert_int_equal(tctx,
     101             :                                  strv_count(strv), num,
     102             :                                  "strv_count() failed");
     103             : 
     104             :         /* Check that strv_next() finds the expected values */
     105           0 :         t = NULL;
     106           6 :         for (i = 0; i < num; i++) {
     107           5 :                 t = strv_next(strv, t);
     108           5 :                 torture_assert(tctx,
     109             :                                strcmp(t, data[i]) == 0,
     110             :                                "strv_next() failed");
     111             :         }
     112             : 
     113             : 
     114             :         /* Check that strv_next() finds the expected values */
     115           0 :         t = NULL;
     116           6 :         for (i = 0; i < num; i++) {
     117           5 :                 t = strv_next(strv, t);
     118           5 :                 torture_assert(tctx,
     119             :                                strcmp(t, data[i]) == 0,
     120             :                                "strv_next() failed");
     121             :         }
     122             : 
     123             :         /* Find each item, delete it, check count */
     124           6 :         for (i = 0; i < num; i++) {
     125           5 :                 t = strv_find(strv, data[i]);
     126           5 :                 torture_assert(tctx,
     127             :                                strcmp(t, data[i]) == 0,
     128             :                                "strv_next() failed");
     129           5 :                 strv_delete(&strv, t);
     130           5 :                 torture_assert_int_equal(tctx,
     131             :                                          strv_count(strv), num - i - 1,
     132             :                                          "strv_delete() failed");
     133             :         }
     134             : 
     135             :         /* Add items */
     136           6 :         for (i = 0; i < num; i++) {
     137           5 :                 ret = strv_add(tctx, &strv, data[i]);
     138           5 :                 torture_assert(tctx, ret == 0, "strv_add() failed");
     139             :         }
     140             : 
     141           1 :         torture_assert_int_equal(tctx,
     142             :                                  strv_count(strv), num,
     143             :                                  "strv_count() failed");
     144             : 
     145             :         /* Find items in reverse, delete, check count */
     146           6 :         for (i = num - 1; i >= 0; i--) {
     147           5 :                 t = strv_find(strv, data[i]);
     148           5 :                 torture_assert(tctx,
     149             :                                strcmp(t, data[i]) == 0,
     150             :                                "strv_next() failed");
     151           5 :                 strv_delete(&strv, t);
     152           5 :                 torture_assert_int_equal(tctx,
     153             :                                          strv_count(strv), i,
     154             :                                          "strv_delete() failed");
     155             :         }
     156             : 
     157           0 :         return true;
     158             : }
     159             : 
     160             : /* Similar to above but only add/check first 2 chars of each string */
     161           1 : static bool test_strv_addn(struct torture_context *tctx)
     162             : {
     163           1 :         const char *data[] = { "foo", "bar", "samba" };
     164           1 :         char *strv = NULL;
     165           1 :         char *t;
     166           1 :         int i, ret;
     167           1 :         const int num = ARRAY_SIZE(data);
     168             : 
     169             :         /* Add first 2 chars of each item */
     170           4 :         for (i = 0; i < num; i++) {
     171           3 :                 ret = strv_addn(tctx, &strv, data[i], 2);
     172           3 :                 torture_assert(tctx, ret == 0, "strv_add() failed");
     173             :         }
     174             : 
     175           1 :         torture_assert_int_equal(tctx,
     176             :                                  strv_count(strv), num,
     177             :                                  "strv_count() failed");
     178             : 
     179             :         /* Check that strv_next() finds the expected values */
     180           0 :         t = NULL;
     181           4 :         for (i = 0; i < num; i++) {
     182           3 :                 t = strv_next(strv, t);
     183           3 :                 torture_assert(tctx,
     184             :                                strncmp(t, data[i], 2) == 0,
     185             :                                "strv_next() failed");
     186             :         }
     187             : 
     188           0 :         return true;
     189             : }
     190             : 
     191        2354 : struct torture_suite *torture_local_util_strv(TALLOC_CTX *mem_ctx)
     192             : {
     193        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "strv");
     194             : 
     195        2354 :         torture_suite_add_simple_test(suite, "strv_empty",  test_strv_empty);
     196        2354 :         torture_suite_add_simple_test(suite, "strv_single", test_strv_single);
     197        2354 :         torture_suite_add_simple_test(suite, "strv_multi",  test_strv_multi);
     198        2354 :         torture_suite_add_simple_test(suite, "strv_addn",   test_strv_addn);
     199             : 
     200        2354 :         return suite;
     201             : }

Generated by: LCOV version 1.14