]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
librpc ndr: Tests for ndr_pull_string
authorGary Lockyer <gary@catalyst.net.nz>
Mon, 2 Dec 2019 02:54:08 +0000 (15:54 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 4 Dec 2019 05:10:31 +0000 (05:10 +0000)
Tests to ensure that ndr_pull_string handles zero and one byte length
data correctly for both character strings and UTF-16 strings.

Bug: https://bugzilla.samba.org/show_bug.cgi?id=13874

Signed-off-by: Gary Lockyer <gary@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
librpc/tests/test_ndr_string.c [new file with mode: 0644]
librpc/wscript_build
selftest/knownfail.d/bug-13874
source4/selftest/tests.py

diff --git a/librpc/tests/test_ndr_string.c b/librpc/tests/test_ndr_string.c
new file mode 100644 (file)
index 0000000..6baa41b
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Tests for librpc ndr_string.c
+ *
+ * Copyright (C) Catalyst.NET Ltd 2019
+ *
+ * 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/>.
+ *
+ */
+
+/*
+ * from cmocka.c:
+ * These headers or their equivalents should be included prior to
+ * including
+ * this header file.
+ *
+ * #include <stdarg.h>
+ * #include <stddef.h>
+ * #include <setjmp.h>
+ *
+ * This allows test applications to use custom definitions of C standard
+ * library functions and types.
+ *
+ */
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "librpc/ndr/ndr_string.c"
+
+/*
+ * Try and pull a null terminated string from a zero length buffer
+ * Should fail for both 1 byte, and 2 byte character strings.
+ */
+static void test_pull_string_zero_len_nul_term(void **state)
+{
+       struct ndr_pull ndr = {0};
+       enum ndr_err_code err;
+       int flags = NDR_SCALARS;
+       uint8_t data[] = {0x0, 0x0};
+       const char *s = NULL;
+
+       ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
+       ndr.data = data;
+       ndr.data_size = 0;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_BUFSIZE);
+       assert_null(s);
+       assert_int_equal(0, ndr.offset);
+
+       ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
+       ndr.offset = 0;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_BUFSIZE);
+       assert_null(s);
+       assert_int_equal(0, ndr.offset);
+
+}
+
+/*
+ * Try and pull a null terminated string from a 1 byte buffer
+ * Should succeed for 1 byte character and
+ *        fail    for 2 byte character strings.
+ */
+static void test_pull_string_len_1_nul_term(void **state)
+{
+       struct ndr_pull ndr = {0};
+       enum ndr_err_code err;
+       int flags = NDR_SCALARS;
+       const char *s = NULL;
+       uint8_t data[] = {0x0, 0x0};
+
+       ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
+       ndr.data = data;
+       ndr.data_size = 1;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_SUCCESS);
+       assert_non_null(s);
+       assert_int_equal(1, ndr.offset);
+
+       ndr.offset = 0;
+       ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_BUFSIZE);
+       assert_int_equal(0, ndr.offset);
+}
+
+/*
+ * Try and pull a null terminated string from a 2 byte buffer
+ * Should succeed for both 1 byte, and 2 byte character strings.
+ */
+static void test_pull_string_len_2_nul_term(void **state)
+{
+       struct ndr_pull ndr = {0};
+       enum ndr_err_code err;
+       int flags = NDR_SCALARS;
+       const char *s;
+       uint8_t data[] = {0x0, 0x0};
+
+       ndr.flags = LIBNDR_FLAG_STR_UTF8 | LIBNDR_FLAG_STR_NULLTERM;
+       ndr.data = data;
+       ndr.data_size = 2;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_SUCCESS);
+       assert_non_null(s);
+       assert_int_equal(1, ndr.offset);
+
+       ndr.offset = 0;
+       ndr.flags = LIBNDR_FLAG_STR_NULLTERM;
+       err = ndr_pull_string(&ndr, flags, &s);
+       assert_int_equal(err, NDR_ERR_SUCCESS);
+       assert_non_null(s);
+       assert_int_equal(2, ndr.offset);
+
+
+}
+
+int main(int argc, const char **argv)
+{
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_pull_string_zero_len_nul_term),
+               cmocka_unit_test(test_pull_string_len_1_nul_term),
+               cmocka_unit_test(test_pull_string_len_2_nul_term)
+       };
+
+       cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
index 2582a2811398c9fc3fa924523b0c00eaa7dc8f53..88964ed2a9ad5d58fb3604bd45ada70bb67bfdde 100644 (file)
@@ -664,3 +664,14 @@ bld.SAMBA_SUBSYSTEM('NDR_FSRVP_STATE',
     source='gen_ndr/ndr_fsrvp_state.c',
     public_deps='ndr'
     )
+#
+# Cmocka tests
+#
+bld.SAMBA_BINARY('test_ndr_string',
+                 source='tests/test_ndr_string.c',
+                 deps='''
+                      cmocka
+                      talloc
+                      ndr
+                      ''',
+                 for_selftest=True)
index 9603bac8ca25baf710b13b79779cff952b500f86..0dccf1aea28009c6d7972d41749500bd81f8c905 100644 (file)
@@ -1 +1,3 @@
 ^samba.tests.blackbox.ndrdump.samba.tests.blackbox.ndrdump.NdrDumpTests.test_ndrdump_fuzzed_PackagesBlob\(none\)
+^librpc.ndr.ndr_string.test_pull_string_zero_len_nul_term\(none\)
+^librpc.ndr.ndr_string.test_pull_string_len_1_nul_term\(none\)
index 642dc680fa4c3076b4824e9cb0385195e072dd5c..e9d83a0acf6156a98974e398b56d6e21b4d4c7bd 100755 (executable)
@@ -1317,6 +1317,8 @@ plantestsuite("samba4.dcerpc.dnsserver.dnsutils", "none",
               [os.path.join(bindir(), "test_rpc_dns_server_dnsutils")])
 plantestsuite("libcli.drsuapi.repl_decrypt", "none",
               [os.path.join(bindir(), "test_repl_decrypt")])
+plantestsuite("librpc.ndr.ndr_string", "none",
+              [os.path.join(bindir(), "test_ndr_string")])
 
 # process restart and limit tests, these break the environment so need to run
 # in their own specific environment