LCOV - code coverage report
Current view: top level - source4/torture/smb2 - charset.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 93 102 91.2 %
Date: 2021-09-23 10:06:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    SMB torture tester - charset test routines
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2001
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "libcli/smb2/smb2.h"
      24             : #include "libcli/smb2/smb2_calls.h"
      25             : #include "torture/torture.h"
      26             : #include "torture/smb2/proto.h"
      27             : #include "libcli/libcli.h"
      28             : #include "torture/util.h"
      29             : #include "param/param.h"
      30             : 
      31             : #define BASEDIR "chartest"
      32             : 
      33             : /*
      34             :    open a file using a set of unicode code points for the name
      35             : 
      36             :    the prefix BASEDIR is added before the name
      37             : */
      38          40 : static NTSTATUS unicode_open(struct torture_context *tctx,
      39             :                              struct smb2_tree *tree,
      40             :                              TALLOC_CTX *mem_ctx,
      41             :                              uint32_t create_disposition,
      42             :                              const uint32_t *u_name,
      43             :                              size_t u_name_len)
      44             : {
      45          40 :         struct smb2_create io = {0};
      46          40 :         char *fname = NULL;
      47          40 :         char *fname2 = NULL;
      48          40 :         char *ucs_name = NULL;
      49             :         size_t i;
      50             :         NTSTATUS status;
      51             : 
      52          40 :         ucs_name = talloc_size(mem_ctx, (1+u_name_len)*2);
      53          40 :         if (!ucs_name) {
      54           0 :                 torture_comment(tctx, "Failed to create UCS2 Name - talloc() failure\n");
      55           0 :                 return NT_STATUS_NO_MEMORY;
      56             :         }
      57             : 
      58          90 :         for (i=0;i<u_name_len;i++) {
      59          50 :                 SSVAL(ucs_name, i*2, u_name[i]);
      60             :         }
      61          40 :         SSVAL(ucs_name, i*2, 0);
      62             : 
      63          40 :         if (!convert_string_talloc_handle(ucs_name, lpcfg_iconv_handle(tctx->lp_ctx), CH_UTF16, CH_UNIX, ucs_name, (1+u_name_len)*2, (void **)&fname, &i)) {
      64           5 :                 torture_comment(tctx, "Failed to convert UCS2 Name into unix - convert_string_talloc() failure\n");
      65           5 :                 talloc_free(ucs_name);
      66           5 :                 return NT_STATUS_NO_MEMORY;
      67             :         }
      68             : 
      69          35 :         fname2 = talloc_asprintf(ucs_name, "%s\\%s", BASEDIR, fname);
      70          35 :         if (!fname2) {
      71           0 :                 talloc_free(ucs_name);
      72           0 :                 torture_comment(tctx, "Failed to create fname - talloc() failure\n");
      73           0 :                 return NT_STATUS_NO_MEMORY;
      74             :         }
      75             : 
      76          35 :         io.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
      77          35 :         io.in.desired_access = SEC_RIGHTS_FILE_ALL;
      78          35 :         io.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
      79          35 :         io.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
      80          35 :         io.in.create_options = 0;
      81          35 :         io.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
      82          35 :         io.in.security_flags = 0;
      83          35 :         io.in.fname = fname2;
      84          35 :         io.in.create_disposition = create_disposition;
      85             : 
      86          35 :         status = smb2_create(tree, tctx, &io);
      87          35 :         if (!NT_STATUS_IS_OK(status)) {
      88           5 :                 talloc_free(ucs_name);
      89           5 :                 return status;
      90             :         }
      91             : 
      92          30 :         smb2_util_close(tree, io.out.file.handle);
      93          30 :         talloc_free(ucs_name);
      94          30 :         return NT_STATUS_OK;
      95             : }
      96             : 
      97             : 
      98             : /*
      99             :   see if the server recognises composed characters
     100             : */
     101           5 : static bool test_composed(struct torture_context *tctx,
     102             :                           struct smb2_tree *tree)
     103             : {
     104           5 :         const uint32_t name1[] = {0x61, 0x308};
     105           5 :         const uint32_t name2[] = {0xe4};
     106             :         NTSTATUS status;
     107           5 :         bool ret = true;
     108             : 
     109           5 :         ret = smb2_util_setup_dir(tctx, tree, BASEDIR);
     110           5 :         torture_assert_goto(tctx, ret, ret, done, "setting up basedir");
     111             : 
     112           5 :         status = unicode_open(tctx, tree, tctx,
     113             :                               NTCREATEX_DISP_CREATE, name1, 2);
     114           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     115             :                                         "Failed to create composed name");
     116             : 
     117           5 :         status = unicode_open(tctx, tree, tctx,
     118             :                               NTCREATEX_DISP_CREATE, name2, 1);
     119           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     120             :                                         "Failed to create accented character");
     121             : 
     122           5 : done:
     123           5 :         smb2_deltree(tree, BASEDIR);
     124           5 :         return ret;
     125             : }
     126             : 
     127             : /*
     128             :   see if the server recognises a naked diacritical
     129             : */
     130           5 : static bool test_diacritical(struct torture_context *tctx,
     131             :                              struct smb2_tree *tree)
     132             : {
     133           5 :         const uint32_t name1[] = {0x308};
     134           5 :         const uint32_t name2[] = {0x308, 0x308};
     135             :         NTSTATUS status;
     136           5 :         bool ret = true;
     137             : 
     138           5 :         ret = smb2_util_setup_dir(tctx, tree, BASEDIR);
     139           5 :         torture_assert_goto(tctx, ret, ret, done, "setting up basedir");
     140             : 
     141           5 :         status = unicode_open(tctx, tree, tctx,
     142             :                               NTCREATEX_DISP_CREATE, name1, 1);
     143           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     144             :                                         "Failed to create naked diacritical");
     145             : 
     146             :         /* try a double diacritical */
     147           5 :         status = unicode_open(tctx, tree, tctx,
     148             :                               NTCREATEX_DISP_CREATE, name2, 2);
     149           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     150             :                                         "Failed to create double "
     151             :                                         "naked diacritical");
     152             : 
     153           5 : done:
     154           5 :         smb2_deltree(tree, BASEDIR);
     155           5 :         return ret;
     156             : }
     157             : 
     158             : /*
     159             :   see if the server recognises a partial surrogate pair
     160             : */
     161           5 : static bool test_surrogate(struct torture_context *tctx,
     162             :                            struct smb2_tree *tree)
     163             : {
     164           5 :         const uint32_t name1[] = {0xd800};
     165           5 :         const uint32_t name2[] = {0xdc00};
     166           5 :         const uint32_t name3[] = {0xd800, 0xdc00};
     167             :         NTSTATUS status;
     168           5 :         bool ret = true;
     169             : 
     170           5 :         ret = smb2_util_setup_dir(tctx, tree, BASEDIR);
     171           5 :         torture_assert_goto(tctx, ret, ret, done, "setting up basedir");
     172             : 
     173           5 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name1, 1);
     174           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     175             :                                         "Failed to create partial surrogate 1");
     176             : 
     177           0 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name2, 1);
     178           0 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     179             :                                         "Failed to create partial surrogate 2");
     180             : 
     181           0 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name3, 2);
     182           0 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     183             :                                         "Failed to create full surrogate");
     184             : 
     185           5 : done:
     186           5 :         smb2_deltree(tree, BASEDIR);
     187           5 :         return true;
     188             : }
     189             : 
     190             : /*
     191             :   see if the server recognises wide-a characters
     192             : */
     193           5 : static bool test_widea(struct torture_context *tctx,
     194             :                        struct smb2_tree *tree)
     195             : {
     196           5 :         const uint32_t name1[] = {'a'};
     197           5 :         const uint32_t name2[] = {0xff41};
     198           5 :         const uint32_t name3[] = {0xff21};
     199             :         NTSTATUS status;
     200           5 :         bool ret = true;
     201             : 
     202           5 :         ret = smb2_util_setup_dir(tctx, tree, BASEDIR);
     203           5 :         torture_assert_goto(tctx, ret, ret, done, "setting up basedir");
     204             : 
     205           5 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name1, 1);
     206           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     207             :                                         "Failed to create 'a'");
     208             : 
     209           5 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name2, 1);
     210           5 :         torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
     211             :                                         "Failed to create wide-a");
     212             : 
     213           5 :         status = unicode_open(tctx, tree, tctx, NTCREATEX_DISP_CREATE, name3, 1);
     214           5 :         torture_assert_ntstatus_equal_goto(tctx,
     215             :                                            status,
     216             :                                            NT_STATUS_OBJECT_NAME_COLLISION,
     217             :                                            ret, done,
     218             :                                            "Failed to create wide-A");
     219             : 
     220           5 : done:
     221           5 :         smb2_deltree(tree, BASEDIR);
     222           5 :         return ret;
     223             : }
     224             : 
     225        2355 : struct torture_suite *torture_smb2_charset(TALLOC_CTX *mem_ctx)
     226             : {
     227        2355 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "charset");
     228             : 
     229        2355 :         torture_suite_add_1smb2_test(suite, "Testing composite character (a umlaut)", test_composed);
     230        2355 :         torture_suite_add_1smb2_test(suite, "Testing naked diacritical (umlaut)", test_diacritical);
     231        2355 :         torture_suite_add_1smb2_test(suite, "Testing partial surrogate", test_surrogate);
     232        2355 :         torture_suite_add_1smb2_test(suite, "Testing wide-a", test_widea);
     233             : 
     234        2355 :         return suite;
     235             : }

Generated by: LCOV version 1.13