]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
util/charset/tests: Add tests for UTF‐16 string length functions
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Mon, 13 Nov 2023 23:31:07 +0000 (12:31 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 15 Nov 2023 22:07:36 +0000 (22:07 +0000)
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/charset/tests/util_unistr.c [new file with mode: 0644]
source4/torture/local/local.c
source4/torture/local/wscript_build

diff --git a/lib/util/charset/tests/util_unistr.c b/lib/util/charset/tests/util_unistr.c
new file mode 100644 (file)
index 0000000..1a9fcaa
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+*/
+
+#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;
+}
index b568b55ba98f036fa56c8ebd4ffe8006c93d133f..95417ef7280e059b48d32130f1d05c3f131f6906 100644 (file)
@@ -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,
index e141d902f1b7be7e9a1027e3e0360f0b46256a05..741f6673882c28e7160a3589d6539fbca83585cb 100644 (file)
@@ -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