LCOV - code coverage report
Current view: top level - lib/util/tests - str.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 13 70 18.6 %
Date: 2024-06-13 04:01:37 Functions: 1 11 9.1 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    util_str testing
       5             : 
       6             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
       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 "includes.h"
      23             : #include "torture/torture.h"
      24             : #include "torture/local/proto.h"
      25             : 
      26           0 : static bool test_string_sub_simple(struct torture_context *tctx)
      27             : {
      28             :         char tmp[100];
      29           0 :         strlcpy(tmp, "foobar", sizeof(tmp));
      30           0 :         string_sub(tmp, "foo", "bar", sizeof(tmp));
      31           0 :         torture_assert_str_equal(tctx, tmp, "barbar", "invalid sub");
      32           0 :         return true;
      33             : }
      34             : 
      35           0 : static bool test_string_sub_multiple(struct torture_context *tctx)
      36             : {
      37             :         char tmp[100];
      38           0 :         strlcpy(tmp, "fooblafoo", sizeof(tmp));
      39           0 :         string_sub(tmp, "foo", "bar", sizeof(tmp));
      40           0 :         torture_assert_str_equal(tctx, tmp, "barblabar", "invalid sub");
      41           0 :         return true;
      42             : }
      43             : 
      44           0 : static bool test_string_sub_longer(struct torture_context *tctx)
      45             : {
      46             :         char tmp[100];
      47           0 :         strlcpy(tmp, "foobla", sizeof(tmp));
      48           0 :         string_sub(tmp, "foo", "blie", sizeof(tmp));
      49           0 :         torture_assert_str_equal(tctx, tmp, "bliebla", "invalid sub");
      50           0 :         return true;
      51             : }
      52             : 
      53           0 : static bool test_string_sub_shorter(struct torture_context *tctx)
      54             : {
      55             :         char tmp[100];
      56           0 :         strlcpy(tmp, "foobla", sizeof(tmp));
      57           0 :         string_sub(tmp, "foo", "bl", sizeof(tmp));
      58           0 :         torture_assert_str_equal(tctx, tmp, "blbla", "invalid sub");
      59           0 :         return true;
      60             : }
      61             : 
      62           0 : static bool test_string_sub_special_char(struct torture_context *tctx)
      63             : {
      64             :         char tmp[100];
      65           0 :         strlcpy(tmp, "foobla", sizeof(tmp));
      66           0 :         string_sub(tmp, "foo", "%b;l", sizeof(tmp));
      67           0 :         torture_assert_str_equal(tctx, tmp, "_b_lbla", "invalid sub");
      68           0 :         return true;
      69             : }
      70             : 
      71           0 : static bool test_talloc_string_sub_simple(struct torture_context *tctx)
      72             : {
      73             :         char *t;
      74             :         
      75           0 :         t = talloc_string_sub(tctx, "foobla", "foo", "bl");
      76             : 
      77           0 :         torture_assert_str_equal(tctx, t, "blbla", "invalid sub");
      78             : 
      79           0 :         return true;
      80             : }
      81             : 
      82           0 : static bool test_talloc_string_sub_multiple(struct torture_context *tctx)
      83             : {
      84             :         char *t;
      85             :         
      86           0 :         t = talloc_string_sub(tctx, "fooblafoo", "foo", "aapnootmies");
      87             : 
      88           0 :         torture_assert_str_equal(tctx, t, "aapnootmiesblaaapnootmies", 
      89             :                                  "invalid sub");
      90             : 
      91           0 :         return true;
      92             : }
      93             : 
      94             : /*
      95             :  * with these next three tests, the failure is that the pattern looks like
      96             :  * "+++" because the \x.. bytes encode a zero byte in UTF-8. If we are not
      97             :  * careful with these strings we will see crashes instead of failures.
      98             :  */
      99             : 
     100           0 : static bool test_talloc_string_sub_tricky_utf8_4(struct torture_context *tctx)
     101             : {
     102           0 :         const char string[] =  "++++--\xD8\xBB";
     103           0 :         const char pattern[] = "+++\xF0\x80\x80\x80++";
     104           0 :         const char replace[] = "...";
     105             : 
     106           0 :         char *t = talloc_string_sub(tctx, string, pattern, replace);
     107           0 :         torture_assert_str_equal(tctx, t, string,
     108             :                                  "should reject 4 byte NUL char");
     109           0 :         talloc_free(t);
     110           0 :         return true;
     111             : }
     112             : 
     113           0 : static bool test_talloc_string_sub_tricky_utf8_3(struct torture_context *tctx)
     114             : {
     115           0 :         const char string[] =  "++++--\xD8\xBB";
     116           0 :         const char pattern[] = "+++\xE0\x80\x80++";
     117           0 :         const char replace[] = "...";
     118             : 
     119           0 :         char *t = talloc_string_sub(tctx, string, pattern, replace);
     120           0 :         torture_assert_str_equal(tctx, t, string,
     121             :                                  "should reject 3 byte NUL char");
     122           0 :         talloc_free(t);
     123           0 :         return true;
     124             : }
     125             : 
     126           0 : static bool test_talloc_string_sub_tricky_utf8_2(struct torture_context *tctx)
     127             : {
     128           0 :         const char string[] =  "++++--\xD8\xBB";
     129           0 :         const char pattern[] = "+++\xC0\x80++";
     130           0 :         const char replace[] = "...";
     131             : 
     132           0 :         char *t = talloc_string_sub(tctx, string, pattern, replace);
     133           0 :         torture_assert_str_equal(tctx, t, string,
     134             :                                  "should reject 2 byte NUL char");
     135           0 :         talloc_free(t);
     136           0 :         return true;
     137             : }
     138             : 
     139             : 
     140             : 
     141             : 
     142         964 : struct torture_suite *torture_local_util_str(TALLOC_CTX *mem_ctx)
     143             : {
     144         964 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "str");
     145             : 
     146         964 :         torture_suite_add_simple_test(suite, "string_sub_simple", 
     147             :                                       test_string_sub_simple);
     148             : 
     149         964 :         torture_suite_add_simple_test(suite, "string_sub_multiple", 
     150             :                                       test_string_sub_multiple);
     151             : 
     152         964 :         torture_suite_add_simple_test(suite, "string_sub_shorter", 
     153             :                                       test_string_sub_shorter);
     154             : 
     155         964 :         torture_suite_add_simple_test(suite, "string_sub_longer", 
     156             :                                       test_string_sub_longer);
     157             : 
     158         964 :         torture_suite_add_simple_test(suite, "string_sub_special_chars", 
     159             :                                       test_string_sub_special_char);
     160             : 
     161         964 :         torture_suite_add_simple_test(suite, "talloc_string_sub_simple",
     162             :                                       test_talloc_string_sub_simple);
     163             : 
     164         964 :         torture_suite_add_simple_test(suite, "string_sub_talloc_multiple", 
     165             :                                       test_talloc_string_sub_multiple);
     166             : 
     167         964 :         torture_suite_add_simple_test(suite,
     168             :                                       "test_talloc_string_sub_tricky_utf8_4",
     169             :                                       test_talloc_string_sub_tricky_utf8_4);
     170             : 
     171         964 :         torture_suite_add_simple_test(suite,
     172             :                                       "test_talloc_string_sub_tricky_utf8_3",
     173             :                                       test_talloc_string_sub_tricky_utf8_3);
     174             : 
     175         964 :         torture_suite_add_simple_test(suite,
     176             :                                       "test_talloc_string_sub_tricky_utf8_2",
     177             :                                       test_talloc_string_sub_tricky_utf8_2);
     178             : 
     179         964 :         return suite;
     180             : }

Generated by: LCOV version 1.13