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

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    test suite for the util_unistr utility functions
       4             : 
       5             :    Copyright (C) Catalyst.Net Ltd. 2023
       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_util_unistr(TALLOC_CTX *mem_ctx);
      28             : 
      29           1 : static bool test_utf16_len(struct torture_context *tctx)
      30             : {
      31           1 :         static const uint16_t empty_string[] = {'\0'};
      32           1 :         static const uint16_t foo_bar[] = {
      33             :                 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
      34           1 :         static const uint16_t foo_bar_alternative[] = {0xd83c,
      35             :                                                        0xdd75,
      36             :                                                        0xd83c,
      37             :                                                        0xdd7e,
      38             :                                                        0xd83c,
      39             :                                                        0xdd7e,
      40             :                                                        ' ',
      41             :                                                        0xd83c,
      42             :                                                        0xdd31,
      43             :                                                        0xd83c,
      44             :                                                        0xdd30,
      45             :                                                        0xd83c,
      46             :                                                        0xdd41,
      47             :                                                        '\0'};
      48             : 
      49           1 :         torture_assert_size_equal(tctx,
      50             :                                   utf16_len(empty_string),
      51             :                                   0,
      52             :                                   "length of empty string");
      53           1 :         torture_assert_size_equal(tctx,
      54             :                                   utf16_null_terminated_len(empty_string),
      55             :                                   2,
      56             :                                   "null‐terminated length of empty string");
      57           1 :         torture_assert_size_equal(tctx,
      58             :                                   utf16_len(foo_bar),
      59             :                                   14,
      60             :                                   "length of “foo bar”");
      61           1 :         torture_assert_size_equal(tctx,
      62             :                                   utf16_null_terminated_len(foo_bar),
      63             :                                   16,
      64             :                                   "null‐terminated length of “foo bar”");
      65           1 :         torture_assert_size_equal(tctx,
      66             :                                   utf16_len(foo_bar_alternative),
      67             :                                   26,
      68             :                                   "length of “🅵🅾🅾 🄱🄰🅁”");
      69           1 :         torture_assert_size_equal(tctx,
      70             :                                   utf16_null_terminated_len(
      71             :                                           foo_bar_alternative),
      72             :                                   28,
      73             :                                   "null‐terminated length of “🅵🅾🅾 🄱🄰🅁”");
      74             : 
      75           0 :         return true;
      76             : }
      77             : 
      78           1 : static bool test_utf16_len_n(struct torture_context *tctx)
      79             : {
      80           1 :         static const uint16_t empty_string[] = {'\0'};
      81           1 :         static const uint16_t foo_bar[] = {'f', 'o', 'o', ' ', 'b', 'a', 'r'};
      82           1 :         static const uint16_t null_terminated_foo_bar[] = {
      83             :                 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'};
      84           1 :         static const uint16_t twice_null_terminated_abc[] = {
      85             :                 'a', 'b', 'c', '\0', '\0'};
      86             : 
      87           1 :         torture_assert_size_equal(tctx,
      88             :                                   utf16_len_n(empty_string, 0),
      89             :                                   0,
      90             :                                   "length of empty string");
      91           1 :         torture_assert_size_equal(tctx,
      92             :                                   utf16_null_terminated_len_n(empty_string, 0),
      93             :                                   0,
      94             :                                   "null‐terminated length of empty string");
      95             : 
      96           1 :         torture_assert_size_equal(tctx,
      97             :                                   utf16_len_n(empty_string,
      98             :                                               sizeof empty_string),
      99             :                                   0,
     100             :                                   "length of null‐terminated empty string");
     101           1 :         torture_assert_size_equal(
     102             :                 tctx,
     103             :                 utf16_null_terminated_len_n(empty_string, sizeof empty_string),
     104             :                 2,
     105             :                 "null‐terminated length of null‐terminated empty string");
     106             : 
     107           1 :         torture_assert_size_equal(tctx,
     108             :                                   utf16_len_n(foo_bar, sizeof foo_bar),
     109             :                                   14,
     110             :                                   "length of “foo bar”");
     111           1 :         torture_assert_size_equal(tctx,
     112             :                                   utf16_null_terminated_len_n(foo_bar,
     113             :                                                               sizeof foo_bar),
     114             :                                   14,
     115             :                                   "null‐terminated length of “foo bar”");
     116             : 
     117           1 :         torture_assert_size_equal(tctx,
     118             :                                   utf16_len_n(null_terminated_foo_bar,
     119             :                                               sizeof null_terminated_foo_bar),
     120             :                                   14,
     121             :                                   "length of null‐terminated “foo bar”");
     122           1 :         torture_assert_size_equal(
     123             :                 tctx,
     124             :                 utf16_null_terminated_len_n(null_terminated_foo_bar,
     125             :                                             sizeof null_terminated_foo_bar),
     126             :                 16,
     127             :                 "null‐terminated length of null‐terminated “foo bar”");
     128             : 
     129           1 :         torture_assert_size_equal(tctx,
     130             :                                   utf16_len_n(null_terminated_foo_bar,
     131             :                                               sizeof null_terminated_foo_bar -
     132             :                                                       1),
     133             :                                   14,
     134             :                                   "length of “foo bar” minus one byte");
     135           1 :         torture_assert_size_equal(
     136             :                 tctx,
     137             :                 utf16_null_terminated_len_n(null_terminated_foo_bar,
     138             :                                             sizeof null_terminated_foo_bar - 1),
     139             :                 14,
     140             :                 "null‐terminated length of “foo bar” minus one byte");
     141             : 
     142           1 :         torture_assert_size_equal(tctx,
     143             :                                   utf16_len_n(twice_null_terminated_abc,
     144             :                                               sizeof twice_null_terminated_abc),
     145             :                                   6,
     146             :                                   "length of twice–null‐terminated “abc”");
     147           1 :         torture_assert_size_equal(
     148             :                 tctx,
     149             :                 utf16_null_terminated_len_n(twice_null_terminated_abc,
     150             :                                             sizeof twice_null_terminated_abc),
     151             :                 8,
     152             :                 "null‐terminated length of twice–null‐terminated “abc”");
     153             : 
     154           0 :         return true;
     155             : }
     156             : 
     157        2354 : struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx)
     158             : {
     159        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx,
     160             :                                                            "util_unistr");
     161             : 
     162        2354 :         torture_suite_add_simple_test(suite, "utf16_len", test_utf16_len);
     163        2354 :         torture_suite_add_simple_test(suite, "utf16_len_n", test_utf16_len_n);
     164             : 
     165        2354 :         return suite;
     166             : }

Generated by: LCOV version 1.14