LCOV - code coverage report
Current view: top level - lib/util/charset/tests - charset.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 25 213 11.7 %
Date: 2024-06-13 04:01:37 Functions: 1 23 4.3 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    test suite for the charcnv functions
       4             : 
       5             :    Copyright (C) Jelmer Vernooij 2007
       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 "includes.h"
      22             : #include "torture/torture.h"
      23             : 
      24             : #undef strcasecmp
      25             : #undef strncasecmp
      26             : 
      27             : struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx);
      28             : 
      29           0 : static bool test_toupper_m(struct torture_context *tctx)
      30             : {
      31           0 :         torture_assert_int_equal(tctx, toupper_m('c'), 'C', "c");
      32           0 :         torture_assert_int_equal(tctx, toupper_m('Z'), 'Z', "z");
      33           0 :         torture_assert_int_equal(tctx, toupper_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
      34           0 :         return true;
      35             : }
      36             : 
      37           0 : static bool test_tolower_m(struct torture_context *tctx)
      38             : {
      39           0 :         torture_assert_int_equal(tctx, tolower_m('C'), 'c', "c");
      40           0 :         torture_assert_int_equal(tctx, tolower_m('z'), 'z', "z");
      41           0 :         torture_assert_int_equal(tctx, tolower_m(0xFFFF4565), 0xFFFF4565, "0xFFFF4565");
      42           0 :         return true;
      43             : }
      44             : 
      45           0 : static bool test_codepoint_cmpi(struct torture_context *tctx)
      46             : {
      47           0 :         torture_assert_int_equal(tctx, codepoint_cmpi('a', 'a'), 0, "same char");
      48           0 :         torture_assert_int_equal(tctx, codepoint_cmpi('A', 'a'), 0, "upcase version");
      49           0 :         torture_assert_int_equal(tctx, codepoint_cmpi('b', 'a'), 1, "right diff");
      50           0 :         torture_assert_int_equal(tctx, codepoint_cmpi('a', 'b'), -1, "right diff");
      51           0 :         return true;
      52             : }
      53             : 
      54           0 : static bool test_strcasecmp(struct torture_context *tctx)
      55             : {
      56           0 :         torture_assert_int_equal(tctx, strcasecmp("foo", "bar"), 4, "different strings both lower");
      57           0 :         torture_assert_int_equal(tctx, strcasecmp("foo", "Bar"), 4, "different strings lower/upper");
      58           0 :         torture_assert_int_equal(tctx, strcasecmp("Foo", "bar"), 4, "different strings upper/lower");
      59           0 :         torture_assert_int_equal(tctx, strcasecmp("AFoo", "_bar"), 2, "different strings upper/lower");
      60           0 :         torture_assert_int_equal(tctx, strcasecmp("foo", "foo"), 0, "same case strings");
      61           0 :         torture_assert_int_equal(tctx, strcasecmp("foo", "Foo"), 0, "different case strings");
      62             : 
      63             :         /*
      64             :          * Note that strcasecmp() doesn't allow NULL arguments
      65             :          */
      66           0 :         return true;
      67             : }
      68             : 
      69           0 : static bool test_strcasecmp_m(struct torture_context *tctx)
      70             : {
      71             :         /* file.{accented e} in iso8859-1 */
      72           0 :         const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
      73             :         /* file.{accented e} in utf8 */
      74           0 :         const char file_utf8[8] =      { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
      75           0 :         torture_assert_int_equal(tctx, strcasecmp_m("foo", "bar"), 4, "different strings both lower");
      76           0 :         torture_assert_int_equal(tctx, strcasecmp_m("foo", "Bar"), 4, "different strings lower/upper");
      77           0 :         torture_assert_int_equal(tctx, strcasecmp_m("Foo", "bar"), 4, "different strings upper/lower");
      78           0 :         torture_assert_int_equal(tctx, strcasecmp_m("AFoo", "_bar"), 2, "different strings upper/lower");
      79           0 :         torture_assert_int_equal(tctx, strcasecmp_m("foo", "foo"), 0, "same case strings");
      80           0 :         torture_assert_int_equal(tctx, strcasecmp_m("foo", "Foo"), 0, "different case strings");
      81           0 :         torture_assert_int_equal(tctx, strcasecmp_m(NULL, "Foo"),  -1, "one NULL");
      82           0 :         torture_assert_int_equal(tctx, strcasecmp_m("foo", NULL),  1, "other NULL");
      83           0 :         torture_assert_int_equal(tctx, strcasecmp_m(NULL, NULL),   0, "both NULL");
      84           0 :         torture_assert_int_equal(tctx, strcasecmp_m(file_iso8859_1, file_utf8), 38,
      85             :                 "file.{accented e} should differ");
      86           0 :         return true;
      87             : }
      88             : 
      89             : 
      90           0 : static bool test_strequal_m(struct torture_context *tctx)
      91             : {
      92           0 :         torture_assert(tctx, !strequal_m("foo", "bar"), "different strings");
      93           0 :         torture_assert(tctx, strequal_m("foo", "foo"), "same case strings");
      94           0 :         torture_assert(tctx, strequal_m("foo", "Foo"), "different case strings");
      95           0 :         torture_assert(tctx, !strequal_m(NULL, "Foo"), "one NULL");
      96           0 :         torture_assert(tctx, !strequal_m("foo", NULL), "other NULL");
      97           0 :         torture_assert(tctx, strequal_m(NULL, NULL), "both NULL");
      98           0 :         return true;
      99             : }
     100             : 
     101           0 : static bool test_strcsequal(struct torture_context *tctx)
     102             : {
     103           0 :         torture_assert(tctx, !strcsequal("foo", "bar"), "different strings");
     104           0 :         torture_assert(tctx, strcsequal("foo", "foo"), "same case strings");
     105           0 :         torture_assert(tctx, !strcsequal("foo", "Foo"), "different case strings");
     106           0 :         torture_assert(tctx, !strcsequal(NULL, "Foo"), "one NULL");
     107           0 :         torture_assert(tctx, !strcsequal("foo", NULL), "other NULL");
     108           0 :         torture_assert(tctx, strcsequal(NULL, NULL), "both NULL");
     109           0 :         return true;
     110             : }
     111             : 
     112           0 : static bool test_string_replace_m(struct torture_context *tctx)
     113             : {
     114           0 :         char data[6] = "bla";
     115           0 :         string_replace_m(data, 'b', 'c');
     116           0 :         torture_assert_str_equal(tctx, data, "cla", "first char replaced");
     117           0 :         memcpy(data, "bab", 4);
     118           0 :         string_replace_m(data, 'b', 'c');
     119           0 :         torture_assert_str_equal(tctx, data, "cac", "other chars replaced");
     120           0 :         memcpy(data, "bba", 4);
     121           0 :         string_replace_m(data, 'b', 'c');
     122           0 :         torture_assert_str_equal(tctx, data, "cca", "other chars replaced");
     123           0 :         memcpy(data, "blala", 6);
     124           0 :         string_replace_m(data, 'o', 'c');
     125           0 :         torture_assert_str_equal(tctx, data, "blala", "no chars replaced");
     126           0 :         string_replace_m(NULL, 'b', 'c');
     127           0 :         return true;
     128             : }
     129             : 
     130           0 : static bool test_strncasecmp(struct torture_context *tctx)
     131             : {
     132           0 :         torture_assert_int_equal(tctx, strncasecmp("foo", "bar", 3), 4, "different strings both lower");
     133           0 :         torture_assert_int_equal(tctx, strncasecmp("foo", "Bar", 3), 4, "different strings lower/upper");
     134           0 :         torture_assert_int_equal(tctx, strncasecmp("Foo", "bar", 3), 4, "different strings upper/lower");
     135           0 :         torture_assert_int_equal(tctx, strncasecmp("AFoo", "_bar", 4), 2, "different strings upper/lower");
     136           0 :         torture_assert_int_equal(tctx, strncasecmp("foo", "foo", 3), 0, "same case strings");
     137           0 :         torture_assert_int_equal(tctx, strncasecmp("foo", "Foo", 3), 0, "different case strings");
     138           0 :         torture_assert_int_equal(tctx, strncasecmp("fool", "Foo", 3),0, "different case strings");
     139           0 :         torture_assert_int_equal(tctx, strncasecmp("fool", "Fool", 40), 0, "over size");
     140           0 :         torture_assert_int_equal(tctx, strncasecmp("BLA", "Fool", 0),0, "empty");
     141             : 
     142             :         /*
     143             :          * Note that strncasecmp() doesn't allow NULL arguments
     144             :          */
     145           0 :         return true;
     146             : }
     147             : 
     148           0 : static bool test_strncasecmp_m(struct torture_context *tctx)
     149             : {
     150             :         /* file.{accented e} in iso8859-1 */
     151           0 :         const char file_iso8859_1[7] = { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xe9, 0 };
     152             :         /* file.{accented e} in utf8 */
     153           0 :         const char file_utf8[8] =      { 0x66, 0x69, 0x6c, 0x65, 0x2d, 0xc3, 0xa9, 0 };
     154           0 :         torture_assert_int_equal(tctx, strncasecmp_m("foo", "bar", 3), 4, "different strings both lower");
     155           0 :         torture_assert_int_equal(tctx, strncasecmp_m("foo", "Bar", 3), 4, "different strings lower/upper");
     156           0 :         torture_assert_int_equal(tctx, strncasecmp_m("Foo", "bar", 3), 4, "different strings upper/lower");
     157           0 :         torture_assert_int_equal(tctx, strncasecmp_m("AFoo", "_bar", 4), 2, "different strings upper/lower");
     158           0 :         torture_assert_int_equal(tctx, strncasecmp_m("foo", "foo", 3), 0, "same case strings");
     159           0 :         torture_assert_int_equal(tctx, strncasecmp_m("foo", "Foo", 3), 0, "different case strings");
     160           0 :         torture_assert_int_equal(tctx, strncasecmp_m("fool", "Foo", 3),0, "different case strings");
     161           0 :         torture_assert_int_equal(tctx, strncasecmp_m("fool", "Fool", 40), 0, "over size");
     162           0 :         torture_assert_int_equal(tctx, strncasecmp_m("BLA", "Fool", 0),0, "empty");
     163           0 :         torture_assert_int_equal(tctx, strncasecmp_m(NULL, "Foo", 3),  -1, "one NULL");
     164           0 :         torture_assert_int_equal(tctx, strncasecmp_m("foo", NULL, 3),  1, "other NULL");
     165           0 :         torture_assert_int_equal(tctx, strncasecmp_m(NULL, NULL, 3),   0, "both NULL");
     166           0 :         torture_assert_int_equal(tctx, strncasecmp_m(file_iso8859_1, file_utf8, 6), 38,
     167             :                 "file.{accented e} should differ");
     168           0 :         return true;
     169             : }
     170             : 
     171           0 : static bool test_next_token_null(struct torture_context *tctx)
     172             : {
     173             :         char buf[20];
     174           0 :         torture_assert(tctx, !next_token(NULL, buf, " ", 20), "null ptr works");
     175           0 :         return true;
     176             : }
     177             : 
     178           0 : static bool test_next_token(struct torture_context *tctx)
     179             : {
     180           0 :         const char *teststr = "foo bar bla";
     181             :         char buf[20];
     182           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     183           0 :         torture_assert_str_equal(tctx, buf, "foo", "token matches");
     184           0 :         torture_assert_str_equal(tctx, teststr, "bar bla", "ptr modified correctly");
     185             : 
     186           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     187           0 :         torture_assert_str_equal(tctx, buf, "bar", "token matches");
     188           0 :         torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
     189             : 
     190           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     191           0 :         torture_assert_str_equal(tctx, buf, "bla", "token matches");
     192           0 :         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
     193             : 
     194           0 :         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
     195           0 :         return true;
     196             : }
     197             : 
     198           0 : static bool test_next_token_implicit_sep(struct torture_context *tctx)
     199             : {
     200           0 :         const char *teststr = "foo\tbar\n bla";
     201             :         char buf[20];
     202           0 :         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
     203           0 :         torture_assert_str_equal(tctx, buf, "foo", "token matches");
     204           0 :         torture_assert_str_equal(tctx, teststr, "bar\n bla", "ptr modified correctly");
     205             : 
     206           0 :         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
     207           0 :         torture_assert_str_equal(tctx, buf, "bar", "token matches");
     208           0 :         torture_assert_str_equal(tctx, teststr, " bla", "ptr modified correctly");
     209             : 
     210           0 :         torture_assert(tctx, next_token(&teststr, buf, NULL, 20), "finding token works");
     211           0 :         torture_assert_str_equal(tctx, buf, "bla", "token matches");
     212           0 :         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
     213             : 
     214           0 :         torture_assert(tctx, !next_token(&teststr, buf, NULL, 20), "finding token doesn't work");
     215           0 :         return true;
     216             : }
     217             : 
     218           0 : static bool test_next_token_seps(struct torture_context *tctx)
     219             : {
     220           0 :         const char *teststr = ",foo bla";
     221             :         char buf[20];
     222           0 :         torture_assert(tctx, next_token(&teststr, buf, ",", 20), "finding token works");
     223           0 :         torture_assert_str_equal(tctx, buf, "foo bla", "token matches");
     224           0 :         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
     225             : 
     226           0 :         torture_assert(tctx, !next_token(&teststr, buf, ",", 20), "finding token doesn't work");
     227           0 :         return true;
     228             : }
     229             : 
     230           0 : static bool test_next_token_quotes(struct torture_context *tctx)
     231             : {
     232           0 :         const char *teststr = "\"foo bar\" bla";
     233             :         char buf[20];
     234           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     235           0 :         torture_assert_str_equal(tctx, buf, "foo bar", "token matches");
     236           0 :         torture_assert_str_equal(tctx, teststr, "bla", "ptr modified correctly");
     237             : 
     238           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     239           0 :         torture_assert_str_equal(tctx, buf, "bla", "token matches");
     240           0 :         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
     241             : 
     242           0 :         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
     243           0 :         return true;
     244             : }
     245             : 
     246           0 : static bool test_next_token_quote_wrong(struct torture_context *tctx)
     247             : {
     248           0 :         const char *teststr = "\"foo bar bla";
     249             :         char buf[20];
     250           0 :         torture_assert(tctx, next_token(&teststr, buf, " ", 20), "finding token works");
     251           0 :         torture_assert_str_equal(tctx, buf, "foo bar bla", "token matches");
     252           0 :         torture_assert_str_equal(tctx, teststr, "", "ptr modified correctly");
     253             : 
     254           0 :         torture_assert(tctx, !next_token(&teststr, buf, " ", 20), "finding token doesn't work");
     255           0 :         return true;
     256             : }
     257             : 
     258           0 : static bool test_strlen_m(struct torture_context *tctx)
     259             : {
     260           0 :         torture_assert_int_equal(tctx, strlen_m("foo"), 3, "simple len");
     261           0 :         torture_assert_int_equal(tctx, strlen_m("foo\x83l"), 6, "extended len");
     262           0 :         torture_assert_int_equal(tctx, strlen_m(""), 0, "empty");
     263           0 :         torture_assert_int_equal(tctx, strlen_m(NULL), 0, "NULL");
     264           0 :         return true;
     265             : }
     266             : 
     267           0 : static bool test_strlen_m_term(struct torture_context *tctx)
     268             : {
     269           0 :         torture_assert_int_equal(tctx, strlen_m_term("foo"), 4, "simple len");
     270           0 :         torture_assert_int_equal(tctx, strlen_m_term("foo\x83l"), 7, "extended len");
     271           0 :         torture_assert_int_equal(tctx, strlen_m_term(""), 1, "empty");
     272           0 :         torture_assert_int_equal(tctx, strlen_m_term(NULL), 0, "NULL");
     273           0 :         return true;
     274             : }
     275             : 
     276           0 : static bool test_strlen_m_term_null(struct torture_context *tctx)
     277             : {
     278           0 :         torture_assert_int_equal(tctx, strlen_m_term_null("foo"), 4, "simple len");
     279           0 :         torture_assert_int_equal(tctx, strlen_m_term_null("foo\x83l"), 7, "extended len");
     280           0 :         torture_assert_int_equal(tctx, strlen_m_term_null(""), 0, "empty");
     281           0 :         torture_assert_int_equal(tctx, strlen_m_term_null(NULL), 0, "NULL");
     282           0 :         return true;
     283             : }
     284             : 
     285           0 : static bool test_strhaslower(struct torture_context *tctx)
     286             : {
     287           0 :         torture_assert(tctx, strhaslower("a"), "one low char");
     288           0 :         torture_assert(tctx, strhaslower("aB"), "one low, one up char");
     289           0 :         torture_assert(tctx, !strhaslower("B"), "one up char");
     290           0 :         torture_assert(tctx, !strhaslower(""), "empty string");
     291           0 :         torture_assert(tctx, !strhaslower("3"), "one digit");
     292           0 :         return true;
     293             : }
     294             : 
     295           0 : static bool test_strhasupper(struct torture_context *tctx)
     296             : {
     297           0 :         torture_assert(tctx, strhasupper("B"), "one up char");
     298           0 :         torture_assert(tctx, strhasupper("aB"), "one low, one up char");
     299           0 :         torture_assert(tctx, !strhasupper("a"), "one low char");
     300           0 :         torture_assert(tctx, !strhasupper(""), "empty string");
     301           0 :         torture_assert(tctx, !strhasupper("3"), "one digit");
     302           0 :         return true;
     303             : }
     304             : 
     305           0 : static bool test_count_chars_m(struct torture_context *tctx)
     306             : {
     307           0 :         torture_assert_int_equal(tctx, count_chars_m("foo", 'o'), 2, "simple");
     308           0 :         torture_assert_int_equal(tctx, count_chars_m("", 'o'), 0, "empty");
     309           0 :         torture_assert_int_equal(tctx, count_chars_m("bla", 'o'), 0, "none");
     310           0 :         torture_assert_int_equal(tctx, count_chars_m("bla", '\0'), 0, "null");
     311           0 :         return true;
     312             : }
     313             : 
     314         964 : struct torture_suite *torture_local_charset(TALLOC_CTX *mem_ctx)
     315             : {
     316         964 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
     317             : 
     318         964 :         torture_suite_add_simple_test(suite, "toupper_m", test_toupper_m);
     319         964 :         torture_suite_add_simple_test(suite, "tolower_m", test_tolower_m);
     320         964 :         torture_suite_add_simple_test(suite, "codepoint_cmpi", test_codepoint_cmpi);
     321         964 :         torture_suite_add_simple_test(suite, "strcasecmp", test_strcasecmp);
     322         964 :         torture_suite_add_simple_test(suite, "strcasecmp_m", test_strcasecmp_m);
     323         964 :         torture_suite_add_simple_test(suite, "strequal_m", test_strequal_m);
     324         964 :         torture_suite_add_simple_test(suite, "strcsequal", test_strcsequal);
     325         964 :         torture_suite_add_simple_test(suite, "string_replace_m", test_string_replace_m);
     326         964 :         torture_suite_add_simple_test(suite, "strncasecmp", test_strncasecmp);
     327         964 :         torture_suite_add_simple_test(suite, "strncasecmp_m", test_strncasecmp_m);
     328         964 :         torture_suite_add_simple_test(suite, "next_token", test_next_token);
     329         964 :         torture_suite_add_simple_test(suite, "next_token_null", test_next_token_null);
     330         964 :         torture_suite_add_simple_test(suite, "next_token_implicit_sep", test_next_token_implicit_sep);
     331         964 :         torture_suite_add_simple_test(suite, "next_token_quotes", test_next_token_quotes);
     332         964 :         torture_suite_add_simple_test(suite, "next_token_seps", test_next_token_seps);
     333         964 :         torture_suite_add_simple_test(suite, "next_token_quote_wrong", test_next_token_quote_wrong);
     334         964 :         torture_suite_add_simple_test(suite, "strlen_m", test_strlen_m);
     335         964 :         torture_suite_add_simple_test(suite, "strlen_m_term", test_strlen_m_term);
     336         964 :         torture_suite_add_simple_test(suite, "strlen_m_term_null", test_strlen_m_term_null);
     337         964 :         torture_suite_add_simple_test(suite, "strhaslower", test_strhaslower);
     338         964 :         torture_suite_add_simple_test(suite, "strhasupper", test_strhasupper);
     339         964 :         torture_suite_add_simple_test(suite, "count_chars_m", test_count_chars_m);
     340             : 
     341         964 :         return suite;
     342             : }

Generated by: LCOV version 1.13