LCOV - code coverage report
Current view: top level - source4/torture/ndr - string.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 5 67 7.5 %
Date: 2024-06-13 04:01:37 Functions: 1 4 25.0 %

          Line data    Source code
       1             : #include "includes.h"
       2             : #include "torture/ndr/ndr.h"
       3             : #include "torture/ndr/proto.h"
       4             : #include "../lib/util/dlinklist.h"
       5             : #include "param/param.h"
       6             : 
       7             : static const char *ascii = "ascii";
       8             : /* the following is equivalent to "kamelåså öäüÿéèóò" in latin1 */
       9             : static const char latin1[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xe5, 0x73,
      10             :                                0xe5, 0x20, 0xF6, 0xE4, 0xFC, 0xFF, 0xE9,
      11             :                                0xE8, 0xF3, 0xF2, 0x00 };
      12             : /* the following is equivalent to "kamelåså ☺☺☺ öäüÿéèóò" in utf8 */
      13             : static const char utf8[] = { 0x6b, 0x61, 0x6d, 0x65, 0x6c, 0xc3, 0xa5,
      14             :                              0x73, 0xc3, 0xa5, 0x20, 0xE2, 0x98, 0xBA,
      15             :                              0xE2, 0x98, 0xBA, 0xE2, 0x98, 0xBA, 0x20,
      16             :                              0xc3, 0xb6, 0xc3, 0xa4, 0xc3, 0xbc, 0xc3,
      17             :                              0xbf, 0xc3, 0xa9, 0xc3, 0xa8, 0xc3, 0xb3,
      18             :                              0xc3, 0xb2, 0x00 };
      19             : 
      20             : /* purely for convenience */
      21             : static int fl_ascii_null = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM;
      22             : static int fl_ascii_noterm = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING;
      23             : static int fl_utf8_null = LIBNDR_FLAG_STR_UTF8|LIBNDR_FLAG_STR_NULLTERM;
      24             : static int fl_raw8_null = LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_NULLTERM;
      25             : 
      26             : static bool
      27           0 : test_ndr_push_string (struct torture_context *tctx, const char *string,
      28             :                       int flags, enum ndr_err_code exp_ndr_err,
      29             :                       bool strcmp_pass)
      30             : {
      31             :         TALLOC_CTX *mem_ctx;
      32             :         struct ndr_push *ndr;
      33             :         enum ndr_err_code err;
      34             : 
      35           0 :         torture_comment(tctx,
      36             :                         "test_ndr_push_string %s flags 0x%x expecting "
      37             :                         "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
      38             :                         strcmp_pass?"pass":"fail");
      39           0 :         if (exp_ndr_err != NDR_ERR_SUCCESS) {
      40           0 :                 torture_comment(tctx, "(ignore any Conversion error) ");
      41             :         }
      42             : 
      43           0 :         mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string");
      44           0 :         ndr = ndr_push_init_ctx(mem_ctx);
      45           0 :         ndr_set_flags (&ndr->flags, flags);
      46             : 
      47           0 :         err = ndr_push_string (ndr, NDR_SCALARS, string);
      48           0 :         torture_assert_ndr_err_equal(tctx, err, exp_ndr_err,
      49             :                        "ndr_push_string: unexpected return code");
      50             : 
      51           0 :         if (exp_ndr_err == NDR_ERR_SUCCESS) {
      52           0 :                 uint32_t expected_offset = strlen(string);
      53             : 
      54           0 :                 if (flags & LIBNDR_FLAG_STR_NULLTERM) {
      55           0 :                         expected_offset += 1;
      56             :                 }
      57             : 
      58           0 :                 torture_assert_int_equal(tctx,
      59             :                                          ndr->offset, expected_offset,
      60             :                                          "ndr_push_string: invalid length");
      61             : 
      62           0 :                 torture_assert(tctx, ndr->data != NULL,
      63             :                                "ndr_push_string: succeeded but NULL data");
      64             : 
      65           0 :                 torture_assert(tctx,
      66             :                                strcmp_pass == !strcmp(string, (char *)ndr->data),
      67             :                                "ndr_push_string: post-push strcmp");
      68             : 
      69             :         }
      70             : 
      71           0 :         talloc_free(mem_ctx);
      72           0 :         return true;
      73             : }
      74             : 
      75             : static bool
      76           0 : test_ndr_pull_string (struct torture_context *tctx, const char *string,
      77             :                       int flags, enum ndr_err_code exp_ndr_err,
      78             :                       bool strcmp_pass)
      79             : {
      80             :         TALLOC_CTX *mem_ctx;
      81             :         DATA_BLOB blob;
      82             :         struct ndr_pull *ndr;
      83             :         enum ndr_err_code err;
      84           0 :         const char *result = NULL;
      85             : 
      86           0 :         torture_comment(tctx,
      87             :                         "test_ndr_pull_string '%s' flags 0x%x expecting "
      88             :                         "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
      89             :                         strcmp_pass?"pass":"fail");
      90           0 :         if (exp_ndr_err != NDR_ERR_SUCCESS) {
      91           0 :                 torture_comment(tctx, "(ignore any Conversion error) ");
      92             :         }
      93             : 
      94           0 :         mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string");
      95             : 
      96           0 :         blob = data_blob_string_const(string);
      97           0 :         ndr = ndr_pull_init_blob(&blob, mem_ctx);
      98           0 :         torture_assert(mem_ctx, ndr, "ndr init failed");
      99           0 :         ndr_set_flags (&ndr->flags, flags);
     100             : 
     101           0 :         err = ndr_pull_string (ndr, NDR_SCALARS, &result);
     102           0 :         torture_assert_ndr_err_equal(tctx, err, exp_ndr_err,
     103             :                        "ndr_pull_string: unexpected return code");
     104             : 
     105           0 :         if (exp_ndr_err == NDR_ERR_SUCCESS) {
     106           0 :                 torture_assert(tctx, result != NULL,
     107             :                                "ndr_pull_string: NULL data");
     108           0 :                 torture_assert(tctx, strcmp_pass == !strcmp(string, result),
     109             :                                "ndr_pull_string: post-pull strcmp");
     110           0 :                 torture_assert(tctx, result != NULL,
     111             :                                "ndr_pull_string succeeded but result NULL");
     112             :         }
     113             : 
     114           0 :         talloc_free(mem_ctx);
     115           0 :         return true;
     116             : }
     117             : 
     118             : static bool
     119           0 : torture_ndr_string(struct torture_context *torture)
     120             : {
     121           0 :         const char *saved_dos_cp = talloc_strdup(torture, lpcfg_dos_charset(torture->lp_ctx));
     122             : 
     123           0 :         torture_assert(torture,
     124             :                        test_ndr_push_string (torture, ascii, fl_ascii_null,
     125             :                                              NDR_ERR_SUCCESS, true),
     126             :                        "test_ndr_push_string(ASCII, STR_ASCII|STR_NULL)");
     127           0 :         torture_assert(torture,
     128             :                        test_ndr_push_string (torture, ascii, fl_ascii_noterm,
     129             :                                              NDR_ERR_SUCCESS, true),
     130             :                        "test_ndr_push_string(ASCII, STR_ASCII|STR_NOTERM|REMAINING)");
     131           0 :         torture_assert(torture,
     132             :                        test_ndr_push_string (torture, "", fl_ascii_null,
     133             :                                              NDR_ERR_SUCCESS, true),
     134             :                        "test_ndr_push_string('', STR_ASCII|STR_NULL)");
     135           0 :         torture_assert(torture,
     136             :                        test_ndr_push_string (torture, "", fl_ascii_noterm,
     137             :                                              NDR_ERR_SUCCESS, true),
     138             :                        "test_ndr_push_string('', STR_ASCII|STR_NOTERM|REMAINING)");
     139           0 :         torture_assert(torture,
     140             :                        test_ndr_push_string (torture, utf8, fl_utf8_null,
     141             :                                              NDR_ERR_SUCCESS, true),
     142             :                        "test_ndr_push_string(UTF8, STR_UTF8|STR_NULL)");
     143           0 :         torture_assert(torture,
     144             :                        test_ndr_push_string (torture, utf8, fl_raw8_null,
     145             :                                              NDR_ERR_SUCCESS, true),
     146             :                        "test_ndr_push_string(UTF8, STR_RAW8|STR_NULL)");
     147           0 :         torture_assert(torture,
     148             :                        test_ndr_push_string (torture, latin1, fl_raw8_null,
     149             :                                              NDR_ERR_SUCCESS, true),
     150             :                        "test_ndr_push_string(LATIN1, STR_RAW8|STR_NULL)");
     151           0 :         torture_assert(torture,
     152             :                        test_ndr_push_string (torture, utf8, fl_ascii_null,
     153             :                                              NDR_ERR_CHARCNV, false),
     154             :                        "test_ndr_push_string(UTF8, STR_ASCII|STR_NULL)");
     155           0 :         torture_assert(torture,
     156             :                        test_ndr_push_string (torture, latin1, fl_ascii_null,
     157             :                                              NDR_ERR_CHARCNV, false),
     158             :                        "test_ndr_push_string(LATIN1, STR_ASCII|STR_NULL)");
     159             : 
     160             : 
     161           0 :         torture_assert(torture,
     162             :                        test_ndr_pull_string (torture, ascii, fl_ascii_null,
     163             :                                              NDR_ERR_SUCCESS, true),
     164             :                        "test_ndr_pull_string(ASCII, STR_ASCII|STR_NULL)");
     165           0 :         torture_assert(torture,
     166             :                        test_ndr_pull_string (torture, utf8, fl_utf8_null,
     167             :                                              NDR_ERR_SUCCESS, true),
     168             :                        "test_ndr_pull_string(UTF8, STR_UTF8|STR_NULL)");
     169           0 :         torture_assert(torture,
     170             :                        test_ndr_pull_string (torture, utf8, fl_raw8_null,
     171             :                                              NDR_ERR_SUCCESS, true),
     172             :                        "test_ndr_pull_string(UTF8, STR_RAW8|STR_NULL)");
     173           0 :         torture_assert(torture,
     174             :                        test_ndr_pull_string (torture, latin1, fl_raw8_null,
     175             :                                              NDR_ERR_SUCCESS, true),
     176             :                        "test_ndr_pull_string(LATIN1, STR_RAW8|STR_NULL)");
     177             : 
     178             :         /* Depending on runtime config, the behavior of ndr_pull_string on
     179             :          * incorrect combinations of strings and flags (latin1 with ASCII
     180             :          * flags, for example) may differ; it may return NDR_ERR_CHARCNV, or
     181             :          * it may return NDR_ERR_SUCCESS but with a string that has been
     182             :          * mutilated, depending on the value of "dos charset".  We test for
     183             :          * both cases here. */
     184             : 
     185           0 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "ASCII");
     186           0 :         reload_charcnv(torture->lp_ctx);
     187             : 
     188           0 :         torture_assert(torture,
     189             :                        test_ndr_pull_string (torture, latin1, fl_ascii_null,
     190             :                                              NDR_ERR_CHARCNV, false),
     191             :                        "test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)");
     192           0 :         torture_assert(torture,
     193             :                        test_ndr_pull_string (torture, utf8, fl_ascii_null,
     194             :                                              NDR_ERR_CHARCNV, false),
     195             :                        "test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)");
     196             : 
     197           0 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "CP850");
     198           0 :         reload_charcnv(torture->lp_ctx);
     199             : 
     200           0 :         torture_assert(torture,
     201             :                        test_ndr_pull_string (torture, latin1, fl_ascii_null,
     202             :                                              NDR_ERR_SUCCESS, false),
     203             :                        "test_ndr_pull_string(LATIN1, STR_ASCII|STR_NULL)");
     204           0 :         torture_assert(torture,
     205             :                        test_ndr_pull_string (torture, utf8, fl_ascii_null,
     206             :                                              NDR_ERR_SUCCESS, false),
     207             :                        "test_ndr_pull_string(UTF8, STR_ASCII|STR_NULL)");
     208             : 
     209           0 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", saved_dos_cp);
     210           0 :         reload_charcnv(torture->lp_ctx);
     211             : 
     212           0 :         return true;
     213             : }
     214             : 
     215         964 : struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx)
     216             : {
     217         964 :         struct torture_suite *suite = torture_suite_create(ctx, "ndr_string");
     218             : 
     219         964 :         torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string);
     220         964 :         suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests");
     221             : 
     222         964 :         return suite;
     223             : }

Generated by: LCOV version 1.13