LCOV - code coverage report
Current view: top level - lib/util/charset/tests - charset.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 200 222 90.1 %
Date: 2024-04-21 15:09:00 Functions: 23 23 100.0 %

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

Generated by: LCOV version 1.14