r25554: Convert last instances of BOOL, True and False to the standard types.
[jelmer/samba4-debian.git] / source / torture / basic / mangle_test.c
index 94dac45b9d17e6a9a9601a56c69f8b43cb536268..58d7098972de4be08b1618cfcc521a2f6ce2691e 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"
+#include "torture/torture.h"
+#include "system/filesys.h"
+#include "system/dir.h"
+#include "lib/tdb/include/tdb.h"
+#include "lib/util/util_tdb.h"
+#include "libcli/libcli.h"
+#include "torture/util.h"
 
 static TDB_CONTEXT *tdb;
 
 #define NAME_LENGTH 20
 
-static unsigned total, collisions, failures;
+static uint_t total, collisions, failures;
 
-static BOOL test_one(struct cli_state *cli, const char *name)
+static bool test_one(struct torture_context *tctx ,struct smbcli_state *cli, 
+                    const char *name)
 {
        int fnum;
        const char *shortname;
-       fstring name2;
+       const char *name2;
        NTSTATUS status;
        TDB_DATA data;
 
        total++;
 
-       fnum = cli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
+       fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
        if (fnum == -1) {
-               printf("open of %s failed (%s)\n", name, cli_errstr(cli->tree));
-               return False;
+               printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
+               return false;
        }
 
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               printf("close of %s failed (%s)\n", name, cli_errstr(cli->tree));
-               return False;
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
+               return false;
        }
 
        /* get the short name */
-       status = cli_qpathinfo_alt_name(cli->tree, name, &shortname);
+       status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname);
        if (!NT_STATUS_IS_OK(status)) {
-               printf("query altname of %s failed (%s)\n", name, cli_errstr(cli->tree));
-               return False;
+               printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
+               return false;
        }
 
-       snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname);
-       if (NT_STATUS_IS_ERR(cli_unlink(cli->tree, name2))) {
+       name2 = talloc_asprintf(tctx, "\\mangle_test\\%s", shortname);
+       if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) {
                printf("unlink of %s  (%s) failed (%s)\n", 
-                      name2, name, cli_errstr(cli->tree));
-               return False;
+                      name2, name, smbcli_errstr(cli->tree));
+               return false;
        }
 
        /* recreate by short name */
-       fnum = cli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
+       fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
        if (fnum == -1) {
-               printf("open2 of %s failed (%s)\n", name2, cli_errstr(cli->tree));
-               return False;
+               printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
+               return false;
        }
-       if (NT_STATUS_IS_ERR(cli_close(cli->tree, fnum))) {
-               printf("close of %s failed (%s)\n", name, cli_errstr(cli->tree));
-               return False;
+       if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
+               printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
+               return false;
        }
 
        /* and unlink by long name */
-       if (NT_STATUS_IS_ERR(cli_unlink(cli->tree, name))) {
+       if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) {
                printf("unlink2 of %s  (%s) failed (%s)\n", 
-                      name, name2, cli_errstr(cli->tree));
+                      name, name2, smbcli_errstr(cli->tree));
                failures++;
-               cli_unlink(cli->tree, name2);
-               return True;
+               smbcli_unlink(cli->tree, name2);
+               return true;
        }
 
        /* see if the short name is already in the tdb */
        data = tdb_fetch_bystring(tdb, shortname);
        if (data.dptr) {
                /* maybe its a duplicate long name? */
-               if (strcasecmp(name, data.dptr) != 0) {
+               if (strcasecmp(name, (const char *)data.dptr) != 0) {
                        /* we have a collision */
                        collisions++;
                        printf("Collision between %s and %s   ->  %s "
@@ -96,27 +103,30 @@ static BOOL test_one(struct cli_state *cli, const char *name)
        } else {
                TDB_DATA namedata;
                /* store it for later */
-               namedata.dptr = name;
+               namedata.dptr = discard_const_p(uint8_t, name);
                namedata.dsize = strlen(name)+1;
                tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
        }
 
-       return True;
+       return true;
 }
 
 
-static void gen_name(char *name)
+static char *gen_name(TALLOC_CTX *mem_ctx)
 {
        const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
-       unsigned max_idx = strlen(chars);
-       unsigned len;
+       uint_t max_idx = strlen(chars);
+       uint_t len;
        int i;
        char *p;
+       char *name;
 
-       fstrcpy(name, "\\mangle_test\\");
-       p = name + strlen(name);
+       name = talloc_strdup(mem_ctx, "\\mangle_test\\");
 
        len = 1 + random() % NAME_LENGTH;
+
+       name = talloc_realloc(mem_ctx, name, char, strlen(name) + len + 6);
+       p = name + strlen(name);
        
        for (i=0;i<len;i++) {
                p[i] = chars[random() % max_idx];
@@ -124,7 +134,7 @@ static void gen_name(char *name)
 
        p[i] = 0;
 
-       if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) {
+       if (ISDOT(p) || ISDOTDOT(p)) {
                p[0] = '_';
        }
 
@@ -134,7 +144,7 @@ static void gen_name(char *name)
        }
 
        /* and a medium probability of a common lead string */
-       if (random() % 10 == 0) {
+       if ((len > 5) && (random() % 10 == 0)) {
                strncpy(p, "ABCDE", 5);
        }
 
@@ -145,61 +155,52 @@ static void gen_name(char *name)
                        s[4] = 0;
                }
        }
+
+       return name;
 }
 
 
-BOOL torture_mangle(int dummy)
+bool torture_mangle(struct torture_context *torture, 
+                   struct smbcli_state *cli)
 {
        extern int torture_numops;
-       static struct cli_state *cli;
        int i;
 
-       printf("starting mangle test\n");
-
-       if (!torture_open_connection(&cli)) {
-               return False;
-       }
-
        /* we will use an internal tdb to store the names we have used */
        tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
        if (!tdb) {
                printf("ERROR: Failed to open tdb\n");
-               return False;
+               return false;
        }
 
-       cli_unlink(cli->tree, "\\mangle_test\\*");
-       cli_rmdir(cli->tree, "\\mangle_test");
-
-       if (NT_STATUS_IS_ERR(cli_mkdir(cli->tree, "\\mangle_test"))) {
-               printf("ERROR: Failed to make directory\n");
-               return False;
+       if (!torture_setup_dir(cli, "\\mangle_test")) {
+               return false;
        }
 
        for (i=0;i<torture_numops;i++) {
-               fstring name;
+               char *name;
 
-               gen_name(name);
+               name = gen_name(torture);
 
-               if (!test_one(cli, name)) {
+               if (!test_one(torture, cli, name)) {
                        break;
                }
                if (total && total % 100 == 0) {
-                       printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
-                              collisions, total, (100.0*collisions) / total, failures);
+                       if (torture_setting_bool(torture, "progress", true)) {
+                               printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
+                                      collisions, total, (100.0*collisions) / total, failures);
+                       }
                }
        }
 
-       cli_unlink(cli->tree, "\\mangle_test\\*");
-       if (NT_STATUS_IS_ERR(cli_rmdir(cli->tree, "\\mangle_test"))) {
+       smbcli_unlink(cli->tree, "\\mangle_test\\*");
+       if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) {
                printf("ERROR: Failed to remove directory\n");
-               return False;
+               return false;
        }
 
        printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
               collisions, total, (100.0*collisions) / total, failures);
 
-       torture_close_connection(cli);
-
-       printf("mangle test finished\n");
        return (failures == 0);
 }