LCOV - code coverage report
Current view: top level - source4/torture/basic - mangle_test.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 53 82 64.6 %
Date: 2021-08-25 13:27:56 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    SMB torture tester - mangling test
       4             :    Copyright (C) Andrew Tridgell 2002
       5             :    
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             :    
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "system/filesys.h"
      22             : #include "system/dir.h"
      23             : #include <tdb.h>
      24             : #include "../lib/util/util_tdb.h"
      25             : #include "libcli/libcli.h"
      26             : #include "torture/util.h"
      27             : #include "torture/basic/proto.h"
      28             : 
      29             : #undef strcasecmp
      30             : 
      31             : static TDB_CONTEXT *tdb;
      32             : 
      33             : #define NAME_LENGTH 20
      34             : 
      35             : static unsigned int total, collisions, failures;
      36             : 
      37          60 : static bool test_one(struct torture_context *tctx ,struct smbcli_state *cli, 
      38             :                      const char *name)
      39             : {
      40             :         int fnum;
      41             :         const char *shortname;
      42             :         const char *name2;
      43             :         NTSTATUS status;
      44             :         TDB_DATA data;
      45             : 
      46          60 :         total++;
      47             : 
      48          60 :         fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
      49          60 :         if (fnum == -1) {
      50           0 :                 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
      51           0 :                 return false;
      52             :         }
      53             : 
      54          60 :         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
      55           0 :                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
      56           0 :                 return false;
      57             :         }
      58             : 
      59             :         /* get the short name */
      60          60 :         status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname);
      61          60 :         if (!NT_STATUS_IS_OK(status)) {
      62           0 :                 printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
      63           0 :                 return false;
      64             :         }
      65             : 
      66          60 :         name2 = talloc_asprintf(tctx, "\\mangle_test\\%s", shortname);
      67          60 :         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) {
      68           0 :                 printf("unlink of %s  (%s) failed (%s)\n", 
      69             :                        name2, name, smbcli_errstr(cli->tree));
      70           0 :                 return false;
      71             :         }
      72             : 
      73             :         /* recreate by short name */
      74          60 :         fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
      75          60 :         if (fnum == -1) {
      76           0 :                 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
      77           0 :                 return false;
      78             :         }
      79          60 :         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
      80           0 :                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
      81           0 :                 return false;
      82             :         }
      83             : 
      84             :         /* and unlink by long name */
      85          60 :         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) {
      86           0 :                 printf("unlink2 of %s  (%s) failed (%s)\n", 
      87             :                        name, name2, smbcli_errstr(cli->tree));
      88           0 :                 failures++;
      89           0 :                 smbcli_unlink(cli->tree, name2);
      90           0 :                 return true;
      91             :         }
      92             : 
      93             :         /* see if the short name is already in the tdb */
      94          60 :         data = tdb_fetch_bystring(tdb, shortname);
      95          60 :         if (data.dptr) {
      96             :                 /* maybe its a duplicate long name? */
      97           0 :                 if (strcasecmp(name, (const char *)data.dptr) != 0) {
      98             :                         /* we have a collision */
      99           0 :                         collisions++;
     100           0 :                         printf("Collision between %s and %s   ->  %s "
     101             :                                 " (coll/tot: %u/%u)\n", 
     102             :                                 name, data.dptr, shortname, collisions, total);
     103             :                 }
     104           0 :                 free(data.dptr);
     105             :         } else {
     106             :                 TDB_DATA namedata;
     107             :                 /* store it for later */
     108          60 :                 namedata.dptr = discard_const_p(uint8_t, name);
     109          60 :                 namedata.dsize = strlen(name)+1;
     110          60 :                 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
     111             :         }
     112             : 
     113          60 :         return true;
     114             : }
     115             : 
     116             : 
     117          60 : static char *gen_name(TALLOC_CTX *mem_ctx)
     118             : {
     119          60 :         const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
     120          60 :         unsigned int max_idx = strlen(chars);
     121             :         unsigned int len;
     122             :         int i;
     123             :         char *p;
     124             :         char *name;
     125             : 
     126          60 :         name = talloc_strdup(mem_ctx, "\\mangle_test\\");
     127             : 
     128          60 :         len = 1 + random() % NAME_LENGTH;
     129             : 
     130          60 :         name = talloc_realloc(mem_ctx, name, char, strlen(name) + len + 6);
     131          60 :         p = name + strlen(name);
     132             :         
     133         695 :         for (i=0;i<len;i++) {
     134         635 :                 p[i] = chars[random() % max_idx];
     135             :         }
     136             : 
     137          60 :         p[i] = 0;
     138             : 
     139          60 :         if (ISDOT(p) || ISDOTDOT(p)) {
     140           1 :                 p[0] = '_';
     141             :         }
     142             : 
     143             :         /* have a high probability of a common lead char */
     144          60 :         if (random() % 2 == 0) {
     145          31 :                 p[0] = 'A';
     146             :         }
     147             : 
     148             :         /* and a medium probability of a common lead string */
     149          60 :         if ((len > 5) && (random() % 10 == 0)) {
     150           4 :                 strlcpy(p, "ABCDE", 6);
     151             :         }
     152             : 
     153             :         /* and a high probability of a good extension length */
     154          60 :         if (random() % 2 == 0) {
     155          28 :                 char *s = strrchr(p, '.');
     156          28 :                 if (s) {
     157           8 :                         s[4] = 0;
     158             :                 }
     159             :         }
     160             : 
     161          60 :         return name;
     162             : }
     163             : 
     164             : 
     165           6 : bool torture_mangle(struct torture_context *torture, 
     166             :                     struct smbcli_state *cli)
     167             : {
     168             :         extern int torture_numops;
     169             :         int i;
     170             : 
     171             :         /* we will use an internal tdb to store the names we have used */
     172           6 :         tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
     173           6 :         if (!tdb) {
     174           0 :                 printf("ERROR: Failed to open tdb\n");
     175           0 :                 return false;
     176             :         }
     177             : 
     178           6 :         if (!torture_setup_dir(cli, "\\mangle_test")) {
     179           0 :                 return false;
     180             :         }
     181             : 
     182          66 :         for (i=0;i<torture_numops;i++) {
     183             :                 char *name;
     184             : 
     185          60 :                 name = gen_name(torture);
     186             : 
     187          60 :                 if (!test_one(torture, cli, name)) {
     188           0 :                         break;
     189             :                 }
     190          60 :                 if (total && total % 100 == 0) {
     191           0 :                         if (torture_setting_bool(torture, "progress", true)) {
     192           0 :                                 printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
     193           0 :                                        collisions, total, (100.0*collisions) / total, failures);
     194             :                         }
     195             :                 }
     196             :         }
     197             : 
     198           6 :         smbcli_unlink(cli->tree, "\\mangle_test\\*");
     199           6 :         if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) {
     200           0 :                 printf("ERROR: Failed to remove directory\n");
     201           0 :                 return false;
     202             :         }
     203             : 
     204          12 :         printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
     205           6 :                collisions, total, (100.0*collisions) / total, failures);
     206             : 
     207           6 :         return (failures == 0);
     208             : }

Generated by: LCOV version 1.13