r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/samba.git] / source / torture / mangle_test.c
index 7f05b953c5e3d26735f48e692c1da4adf0b0269c..0bcddd6ebdab22912c239b32e6b78de32f724090 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
+extern int torture_numops;
+
 static TDB_CONTEXT *tdb;
 
 #define NAME_LENGTH 20
 
-static unsigned total, collisions;
+static unsigned total, collisions, failures;
 
 static BOOL test_one(struct cli_state *cli, const char *name)
 {
@@ -54,7 +55,7 @@ static BOOL test_one(struct cli_state *cli, const char *name)
                return False;
        }
 
-       snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname);
+       fstr_sprintf(name2, "\\mangle_test\\%s", shortname);
        if (!cli_unlink(cli, name2)) {
                printf("unlink of %s  (%s) failed (%s)\n", 
                       name2, name, cli_errstr(cli));
@@ -76,23 +77,29 @@ static BOOL test_one(struct cli_state *cli, const char *name)
        if (!cli_unlink(cli, name)) {
                printf("unlink2 of %s  (%s) failed (%s)\n", 
                       name, name2, cli_errstr(cli));
-               return False;
+               failures++;
+               cli_unlink(cli, name2);
+               return True;
        }
 
        /* see if the short name is already in the tdb */
-       data = tdb_fetch_by_string(tdb, shortname);
+       data = tdb_fetch_bystring(tdb, shortname);
        if (data.dptr) {
                /* maybe its a duplicate long name? */
-               if (strcasecmp(name, data.dptr) != 0) {
+               if (!strequal(name, (const char *)data.dptr)) {
                        /* we have a collision */
                        collisions++;
-                       printf("Collision between %s and %s   ->  %s\n", 
-                              name, data.dptr, shortname);
+                       printf("Collision between %s and %s   ->  %s "
+                               " (coll/tot: %u/%u)\n", 
+                               name, data.dptr, shortname, collisions, total);
                }
                free(data.dptr);
        } else {
+               TDB_DATA namedata;
                /* store it for later */
-               tdb_store_by_string(tdb, shortname, name, strlen(name)+1);
+               namedata.dptr = CONST_DISCARD(uint8 *, name);
+               namedata.dsize = strlen(name)+1;
+               tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
        }
 
        return True;
@@ -101,7 +108,7 @@ static BOOL test_one(struct cli_state *cli, const char *name)
 
 static void gen_name(char *name)
 {
-       const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
+       const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
        unsigned max_idx = strlen(chars);
        unsigned len;
        int i;
@@ -127,6 +134,16 @@ static void gen_name(char *name)
                p[0] = 'A';
        }
 
+       /* and a medium probability of a common lead string */
+       if (random() % 10 == 0) {
+               if (strlen(p) <= 5) {
+                       fstrcpy(p, "ABCDE");
+               } else {
+                       /* try not to kill off the null termination */
+                       memcpy(p, "ABCDE", 5);
+               }
+       }
+
        /* and a high probability of a good extension length */
        if (random() % 2 == 0) {
                char *s = strrchr(p, '.');
@@ -134,18 +151,22 @@ static void gen_name(char *name)
                        s[4] = 0;
                }
        }
+
+       /* ..... and a 100% proability of a file not ending in "." */
+       if (p[strlen(p)-1] == '.')
+               p[strlen(p)-1] = '_';
 }
 
 
 BOOL torture_mangle(int dummy)
 {
-       extern int torture_numops;
-       static struct cli_state cli;
+       static struct cli_state *cli;
        int i;
+       BOOL ret = True;
 
        printf("starting mangle test\n");
 
-       if (!torture_open_connection(&cli)) {
+       if (!torture_open_connection(&cli, 0)) {
                return False;
        }
 
@@ -156,38 +177,41 @@ BOOL torture_mangle(int dummy)
                return False;
        }
 
-       cli_unlink(&cli, "\\mangle_test\\*");
-       cli_rmdir(&cli, "\\mangle_test");
+       cli_unlink(cli, "\\mangle_test\\*");
+       cli_rmdir(cli, "\\mangle_test");
 
-       if (!cli_mkdir(&cli, "\\mangle_test")) {
+       if (!cli_mkdir(cli, "\\mangle_test")) {
                printf("ERROR: Failed to make directory\n");
                return False;
        }
 
        for (i=0;i<torture_numops;i++) {
                fstring name;
+               ZERO_STRUCT(name);
 
                gen_name(name);
-
-               if (!test_one(&cli, name)) {
+               
+               if (!test_one(cli, name)) {
+                       ret = False;
                        break;
                }
                if (total && total % 100 == 0) {
-                       printf("collisions %u/%u  - %.2f%%\r",
-                              collisions, total, (100.0*collisions) / total);
+                       printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
+                              collisions, total, (100.0*collisions) / total, failures);
                }
        }
 
-       if (!cli_rmdir(&cli, "\\mangle_test")) {
+       cli_unlink(cli, "\\mangle_test\\*");
+       if (!cli_rmdir(cli, "\\mangle_test")) {
                printf("ERROR: Failed to remove directory\n");
                return False;
        }
 
-       printf("\nTotal collisions %u/%u  - %.2f%%\n",
-              collisions, total, (100.0*collisions) / total);
+       printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
+              collisions, total, (100.0*collisions) / total, failures);
 
-       torture_close_connection(&cli);
+       torture_close_connection(cli);
 
        printf("mangle test finished\n");
-       return True;
+       return (ret && (failures == 0));
 }