librpc: Add test for ndr_string_length()
authorAndreas Schneider <asn@samba.org>
Mon, 16 Dec 2019 15:45:38 +0000 (16:45 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 20 Dec 2019 09:01:30 +0000 (09:01 +0000)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Fri Dec 20 09:01:30 UTC 2019 on sn-devel-184

librpc/tests/test_ndr_string.c

index 6baa41bec3148d9175badad538931f504ef47422..b3b297c550cfff5a8c895095c60881448122a30c 100644 (file)
@@ -127,12 +127,47 @@ static void test_pull_string_len_2_nul_term(void **state)
 
 }
 
+static void test_ndr_string_n_length(void **state)
+{
+       char test_str1[5] = "Test";
+       char test_str2[5] = {0};
+       char test_str3[32] = "This is a test too";
+       uint8_t test_str_u16[64] = {
+               0x5C, 0x00, 0x5C, 0x00, 0x4C, 0x00, 0x6F, 0x00,
+               0x67, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x2D, 0x00,
+               0x6D, 0x00, 0x75, 0x00, 0x63, 0x00, 0x5C, 0x00,
+               0x6B, 0x00, 0x79, 0x00, 0x6F, 0x00, 0x63, 0x00,
+               0x65, 0x00, 0x72, 0x00, 0x61, 0x00, 0x2D, 0x00,
+               0x6D, 0x00, 0x75, 0x00, 0x63, 0x00, 0x2D, 0x00,
+               0x6E, 0x00, 0x00, 0x00 };
+       size_t len;
+
+       len = ndr_string_n_length(test_str1, sizeof(test_str1), 1);
+       assert_int_equal(len, 5);
+
+       len = ndr_string_n_length(test_str1, sizeof(test_str1) - 1, 1);
+       assert_int_equal(len, 4);
+
+       len = ndr_string_n_length(test_str2, sizeof(test_str2), 1);
+       assert_int_equal(len, 1);
+
+       len = ndr_string_n_length(test_str3, sizeof(test_str3), 1);
+       assert_int_equal(len, 19);
+
+       len = ndr_string_n_length(test_str3, 0, 1);
+       assert_int_equal(len, 0);
+
+       len = ndr_string_n_length(test_str_u16, 32, 2);
+       assert_int_equal(len, 26);
+}
+
 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_unit_test(test_pull_string_len_2_nul_term),
+               cmocka_unit_test(test_ndr_string_n_length)
        };
 
        cmocka_set_message_output(CM_OUTPUT_SUBUNIT);