From 99e0a0f21adc36e42ecce56c88e584e38e6fb23d Mon Sep 17 00:00:00 2001 From: Joseph Sutton Date: Tue, 14 Nov 2023 12:31:07 +1300 Subject: [PATCH] =?utf8?q?util/charset/tests:=20Add=20tests=20for=20UTF?= =?utf8?q?=E2=80=9016=20string=20length=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- lib/util/charset/tests/util_unistr.c | 166 +++++++++++++++++++++++++++++++++++ source4/torture/local/local.c | 1 + source4/torture/local/wscript_build | 1 + 3 files changed, 168 insertions(+) create mode 100644 lib/util/charset/tests/util_unistr.c diff --git a/lib/util/charset/tests/util_unistr.c b/lib/util/charset/tests/util_unistr.c new file mode 100644 index 00000000000..1a9fcaafdf5 --- /dev/null +++ b/lib/util/charset/tests/util_unistr.c @@ -0,0 +1,166 @@ +/* + Unix SMB/CIFS implementation. + test suite for the util_unistr utility functions + + Copyright (C) Catalyst.Net Ltd. 2023 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" +#include "torture/torture.h" + +#undef strcasecmp +#undef strncasecmp + +struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx); + +static bool test_utf16_len(struct torture_context *tctx) +{ + static const uint16_t empty_string[] = {'\0'}; + static const uint16_t foo_bar[] = { + 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'}; + static const uint16_t foo_bar_alternative[] = {0xd83c, + 0xdd75, + 0xd83c, + 0xdd7e, + 0xd83c, + 0xdd7e, + ' ', + 0xd83c, + 0xdd31, + 0xd83c, + 0xdd30, + 0xd83c, + 0xdd41, + '\0'}; + + torture_assert_size_equal(tctx, + utf16_len(empty_string), + 0, + "length of empty string"); + torture_assert_size_equal(tctx, + utf16_null_terminated_len(empty_string), + 2, + "null‐terminated length of empty string"); + torture_assert_size_equal(tctx, + utf16_len(foo_bar), + 14, + "length of “foo bar”"); + torture_assert_size_equal(tctx, + utf16_null_terminated_len(foo_bar), + 16, + "null‐terminated length of “foo bar”"); + torture_assert_size_equal(tctx, + utf16_len(foo_bar_alternative), + 26, + "length of “🅵🅾🅾 🄱🄰🅁”"); + torture_assert_size_equal(tctx, + utf16_null_terminated_len( + foo_bar_alternative), + 28, + "null‐terminated length of “🅵🅾🅾 🄱🄰🅁”"); + + return true; +} + +static bool test_utf16_len_n(struct torture_context *tctx) +{ + static const uint16_t empty_string[] = {'\0'}; + static const uint16_t foo_bar[] = {'f', 'o', 'o', ' ', 'b', 'a', 'r'}; + static const uint16_t null_terminated_foo_bar[] = { + 'f', 'o', 'o', ' ', 'b', 'a', 'r', '\0'}; + static const uint16_t twice_null_terminated_abc[] = { + 'a', 'b', 'c', '\0', '\0'}; + + torture_assert_size_equal(tctx, + utf16_len_n(empty_string, 0), + 0, + "length of empty string"); + torture_assert_size_equal(tctx, + utf16_null_terminated_len_n(empty_string, 0), + 0, + "null‐terminated length of empty string"); + + torture_assert_size_equal(tctx, + utf16_len_n(empty_string, + sizeof empty_string), + 0, + "length of null‐terminated empty string"); + torture_assert_size_equal( + tctx, + utf16_null_terminated_len_n(empty_string, sizeof empty_string), + 2, + "null‐terminated length of null‐terminated empty string"); + + torture_assert_size_equal(tctx, + utf16_len_n(foo_bar, sizeof foo_bar), + 14, + "length of “foo bar”"); + torture_assert_size_equal(tctx, + utf16_null_terminated_len_n(foo_bar, + sizeof foo_bar), + 14, + "null‐terminated length of “foo bar”"); + + torture_assert_size_equal(tctx, + utf16_len_n(null_terminated_foo_bar, + sizeof null_terminated_foo_bar), + 14, + "length of null‐terminated “foo bar”"); + torture_assert_size_equal( + tctx, + utf16_null_terminated_len_n(null_terminated_foo_bar, + sizeof null_terminated_foo_bar), + 16, + "null‐terminated length of null‐terminated “foo bar”"); + + torture_assert_size_equal(tctx, + utf16_len_n(null_terminated_foo_bar, + sizeof null_terminated_foo_bar - + 1), + 14, + "length of “foo bar” minus one byte"); + torture_assert_size_equal( + tctx, + utf16_null_terminated_len_n(null_terminated_foo_bar, + sizeof null_terminated_foo_bar - 1), + 14, + "null‐terminated length of “foo bar” minus one byte"); + + torture_assert_size_equal(tctx, + utf16_len_n(twice_null_terminated_abc, + sizeof twice_null_terminated_abc), + 6, + "length of twice–null‐terminated “abc”"); + torture_assert_size_equal( + tctx, + utf16_null_terminated_len_n(twice_null_terminated_abc, + sizeof twice_null_terminated_abc), + 8, + "null‐terminated length of twice–null‐terminated “abc”"); + + return true; +} + +struct torture_suite *torture_local_util_unistr(TALLOC_CTX *mem_ctx) +{ + struct torture_suite *suite = torture_suite_create(mem_ctx, + "util_unistr"); + + torture_suite_add_simple_test(suite, "utf16_len", test_utf16_len); + torture_suite_add_simple_test(suite, "utf16_len_n", test_utf16_len_n); + + return suite; +} diff --git a/source4/torture/local/local.c b/source4/torture/local/local.c index b568b55ba98..95417ef7280 100644 --- a/source4/torture/local/local.c +++ b/source4/torture/local/local.c @@ -61,6 +61,7 @@ torture_local_convert_string, torture_local_string_case_handle, torture_local_string_case, + torture_local_util_unistr, torture_local_event, torture_local_tevent_req, torture_local_torture, diff --git a/source4/torture/local/wscript_build b/source4/torture/local/wscript_build index e141d902f1b..741f6673882 100644 --- a/source4/torture/local/wscript_build +++ b/source4/torture/local/wscript_build @@ -13,6 +13,7 @@ TORTURE_LOCAL_SOURCE = '''../../../lib/util/charset/tests/iconv.c ../../../lib/util/tests/file.c ../../../lib/util/tests/genrand.c ../../../lib/util/charset/tests/charset.c ../../../lib/util/charset/tests/convert_string.c + ../../../lib/util/charset/tests/util_unistr.c ../../../lib/tdr/testsuite.c ../../../lib/tevent/testsuite.c ../../param/tests/share.c ../../../lib/tevent/test_req.c -- 2.11.4.GIT