LCOV - code coverage report
Current view: top level - source4/torture/ndr - string.c (source / functions) Hit Total Coverage
Test: coverage report for master 70ed9daf Lines: 74 74 100.0 %
Date: 2024-01-11 09:59:51 Functions: 4 4 100.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 const libndr_flags fl_ascii_null = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NULLTERM;
      22             : static const libndr_flags fl_ascii_noterm = LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING;
      23             : static const libndr_flags fl_utf8_null = LIBNDR_FLAG_STR_UTF8|LIBNDR_FLAG_STR_NULLTERM;
      24             : static const libndr_flags fl_raw8_null = LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_NULLTERM;
      25             : 
      26             : static bool
      27          18 : test_ndr_push_string (struct torture_context *tctx, const char *string,
      28             :                       libndr_flags flags, enum ndr_err_code exp_ndr_err,
      29             :                       bool strcmp_pass)
      30             : {
      31          18 :         TALLOC_CTX *mem_ctx;
      32          18 :         struct ndr_push *ndr;
      33          18 :         enum ndr_err_code err;
      34             : 
      35          22 :         torture_comment(tctx,
      36             :                         "test_ndr_push_string %s flags 0x%"PRI_LIBNDR_FLAGS" expecting "
      37             :                         "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
      38             :                         strcmp_pass?"pass":"fail");
      39          18 :         if (exp_ndr_err != NDR_ERR_SUCCESS) {
      40           4 :                 torture_comment(tctx, "(ignore any Conversion error) ");
      41             :         }
      42             : 
      43          18 :         mem_ctx = talloc_named (NULL, 0, "test_ndr_push_string");
      44          18 :         ndr = ndr_push_init_ctx(mem_ctx);
      45          18 :         ndr_set_flags (&ndr->flags, flags);
      46             : 
      47          18 :         err = ndr_push_string (ndr, NDR_SCALARS, string);
      48          18 :         torture_assert_ndr_err_equal(tctx, err, exp_ndr_err,
      49             :                        "ndr_push_string: unexpected return code");
      50             : 
      51          18 :         if (exp_ndr_err == NDR_ERR_SUCCESS) {
      52          14 :                 uint32_t expected_offset = strlen(string);
      53             : 
      54          14 :                 if (flags & LIBNDR_FLAG_STR_NULLTERM) {
      55          10 :                         expected_offset += 1;
      56             :                 }
      57             : 
      58          14 :                 torture_assert_int_equal(tctx,
      59             :                                          ndr->offset, expected_offset,
      60             :                                          "ndr_push_string: invalid length");
      61             : 
      62          14 :                 torture_assert(tctx, ndr->data != NULL,
      63             :                                "ndr_push_string: succeeded but NULL data");
      64             : 
      65          14 :                 torture_assert(tctx,
      66             :                                strcmp_pass == !strcmp(string, (char *)ndr->data),
      67             :                                "ndr_push_string: post-push strcmp");
      68             : 
      69             :         }
      70             : 
      71          18 :         talloc_free(mem_ctx);
      72          18 :         return true;
      73             : }
      74             : 
      75             : static bool
      76          16 : test_ndr_pull_string (struct torture_context *tctx, const char *string,
      77             :                       libndr_flags flags, enum ndr_err_code exp_ndr_err,
      78             :                       bool strcmp_pass)
      79             : {
      80          16 :         TALLOC_CTX *mem_ctx;
      81          16 :         DATA_BLOB blob;
      82          16 :         struct ndr_pull *ndr;
      83          16 :         enum ndr_err_code err;
      84          16 :         const char *result = NULL;
      85             : 
      86          24 :         torture_comment(tctx,
      87             :                         "test_ndr_pull_string '%s' flags 0x%"PRI_LIBNDR_FLAGS" expecting "
      88             :                         "err 0x%x and strcmp %s\n", string, flags, exp_ndr_err,
      89             :                         strcmp_pass?"pass":"fail");
      90          16 :         if (exp_ndr_err != NDR_ERR_SUCCESS) {
      91           4 :                 torture_comment(tctx, "(ignore any Conversion error) ");
      92             :         }
      93             : 
      94          16 :         mem_ctx = talloc_named (NULL, 0, "test_ndr_pull_string");
      95             : 
      96          16 :         blob = data_blob_string_const(string);
      97          16 :         ndr = ndr_pull_init_blob(&blob, mem_ctx);
      98          16 :         torture_assert(mem_ctx, ndr, "ndr init failed");
      99          16 :         ndr_set_flags (&ndr->flags, flags);
     100             : 
     101          16 :         err = ndr_pull_string (ndr, NDR_SCALARS, &result);
     102          16 :         torture_assert_ndr_err_equal(tctx, err, exp_ndr_err,
     103             :                        "ndr_pull_string: unexpected return code");
     104             : 
     105          16 :         if (exp_ndr_err == NDR_ERR_SUCCESS) {
     106          12 :                 torture_assert(tctx, result != NULL,
     107             :                                "ndr_pull_string: NULL data");
     108          12 :                 torture_assert(tctx, strcmp_pass == !strcmp(string, result),
     109             :                                "ndr_pull_string: post-pull strcmp");
     110          16 :                 torture_assert(tctx, result != NULL,
     111             :                                "ndr_pull_string succeeded but result NULL");
     112             :         }
     113             : 
     114          16 :         talloc_free(mem_ctx);
     115          16 :         return true;
     116             : }
     117             : 
     118             : static bool
     119           2 : torture_ndr_string(struct torture_context *torture)
     120             : {
     121           2 :         const char *saved_dos_cp = talloc_strdup(torture, lpcfg_dos_charset(torture->lp_ctx));
     122             : 
     123           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         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           2 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "ASCII");
     186           2 :         reload_charcnv(torture->lp_ctx);
     187             : 
     188           2 :         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           2 :         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           2 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", "CP850");
     198           2 :         reload_charcnv(torture->lp_ctx);
     199             : 
     200           2 :         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           2 :         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           2 :         lpcfg_do_global_parameter(torture->lp_ctx, "dos charset", saved_dos_cp);
     210           2 :         reload_charcnv(torture->lp_ctx);
     211             : 
     212           2 :         return true;
     213             : }
     214             : 
     215        2358 : struct torture_suite *ndr_string_suite(TALLOC_CTX *ctx)
     216             : {
     217        2358 :         struct torture_suite *suite = torture_suite_create(ctx, "ndr_string");
     218             : 
     219        2358 :         torture_suite_add_simple_test(suite, "ndr_string", torture_ndr_string);
     220        2358 :         suite->description = talloc_strdup(suite, "NDR - string-conversion focused push/pull tests");
     221             : 
     222        2358 :         return suite;
     223             : }

Generated by: LCOV version 1.14