r10656: BIG merge from trunk. Features not copied over
[amitay/samba.git] / source3 / torture / torture.c
index e612ddea926152d9685f96412ae06bb1d6755915..8ad567f17781c79682298d1317346afe43c2b3a7 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
 
+extern char *optarg;
+extern int optind;
+
 static fstring host, workgroup, share, password, username, myname;
 static int max_protocol = PROTOCOL_NT1;
 static const char *sockops="TCP_NODELAY";
@@ -45,12 +46,12 @@ static struct timeval tp1,tp2;
 
 void start_timer(void)
 {
-       gettimeofday(&tp1,NULL);
+       GetTimeOfDay(&tp1);
 }
 
 double end_timer(void)
 {
-       gettimeofday(&tp2,NULL);
+       GetTimeOfDay(&tp2);
        return((tp2.tv_sec - tp1.tv_sec) + 
               (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
 }
@@ -70,7 +71,7 @@ void *shm_setup(int size)
        int shmid;
        void *ret;
 
-       shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
+       shmid = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
        if (shmid == -1) {
                printf("can't get shared memory\n");
                exit(1);
@@ -207,16 +208,16 @@ static BOOL check_error(int line, struct cli_state *c,
                        uint8 eclass, uint32 ecode, NTSTATUS nterr)
 {
         if (cli_is_dos_error(c)) {
-                uint8 class;
+                uint8 cclass;
                 uint32 num;
 
                 /* Check DOS error */
 
-                cli_dos_error(c, &class, &num);
+                cli_dos_error(c, &cclass, &num);
 
-                if (eclass != class || ecode != num) {
+                if (eclass != cclass || ecode != num) {
                         printf("unexpected error code class=%d code=%d\n", 
-                               (int)class, (int)num);
+                               (int)cclass, (int)num);
                         printf(" expected %d/%d %s (line=%d)\n", 
                                (int)eclass, (int)ecode, nt_errstr(nterr), line);
                         return False;
@@ -493,7 +494,7 @@ static BOOL rw_torture2(struct cli_state *c1, struct cli_state *c2)
                        printf("%d\r", i); fflush(stdout);
                }
 
-               generate_random_buffer(buf, buf_size, False);
+               generate_random_buffer(buf, buf_size);
 
                if (cli_write(c1, fnum1, 0, buf, 0, buf_size) != buf_size) {
                        printf("write failed (%s)\n", cli_errstr(c1));
@@ -537,7 +538,7 @@ static BOOL rw_torture2(struct cli_state *c1, struct cli_state *c2)
 static BOOL run_readwritetest(int dummy)
 {
        static struct cli_state *cli1, *cli2;
-       BOOL test1, test2;
+       BOOL test1, test2 = False;
 
        if (!torture_open_connection(&cli1) || !torture_open_connection(&cli2)) {
                return False;
@@ -590,7 +591,7 @@ static BOOL run_readwritelarge(int dummy)
        static struct cli_state *cli1;
        int fnum1;
        const char *lockfname = "\\large.dat";
-       size_t fsize;
+       SMB_OFF_T fsize;
        char buf[126*1024];
        BOOL correct = True;
  
@@ -697,7 +698,6 @@ static BOOL run_netbench(int client)
 {
        struct cli_state *cli;
        int i;
-       fstring fname;
        pstring line;
        char cname[20];
        FILE *f;
@@ -712,7 +712,7 @@ static BOOL run_netbench(int client)
 
        nb_setup(cli);
 
-       slprintf(cname,sizeof(fname), "client%d", client);
+       slprintf(cname,sizeof(cname)-1, "client%d", client);
 
        f = fopen(client_txt, "r");
 
@@ -870,7 +870,7 @@ static BOOL run_locktest1(int dummy)
        lock_timeout = (1 + (random() % 20));
        printf("Testing lock timeout with timeout=%u\n", lock_timeout);
        t1 = time(NULL);
-       if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 500, WRITE_LOCK)) {
+       if (cli_lock(cli2, fnum3, 0, 4, lock_timeout * 1000, WRITE_LOCK)) {
                printf("lock3 succeeded! This is a locking bug\n");
                return False;
        } else {
@@ -2111,7 +2111,7 @@ test how many open files this server supports on the one socket
 static BOOL run_maxfidtest(int dummy)
 {
        struct cli_state *cli;
-       const char *template = "\\maxfid.%d.%d";
+       const char *ftemplate = "\\maxfid.%d.%d";
        fstring fname;
        int fnums[0x11000], i;
        int retries=4;
@@ -2127,7 +2127,7 @@ static BOOL run_maxfidtest(int dummy)
        cli_sockopt(cli, sockops);
 
        for (i=0; i<0x11000; i++) {
-               slprintf(fname,sizeof(fname)-1,template, i,(int)getpid());
+               slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
                if ((fnums[i] = cli_open(cli, fname, 
                                        O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
                    -1) {
@@ -2143,7 +2143,7 @@ static BOOL run_maxfidtest(int dummy)
 
        printf("cleaning up\n");
        for (;i>=0;i--) {
-               slprintf(fname,sizeof(fname)-1,template, i,(int)getpid());
+               slprintf(fname,sizeof(fname)-1,ftemplate, i,(int)getpid());
                cli_close(cli, fnums[i]);
                if (!cli_unlink(cli, fname)) {
                        printf("unlink of %s failed (%s)\n", 
@@ -2360,7 +2360,7 @@ static BOOL run_trans2test(int dummy)
 {
        struct cli_state *cli;
        int fnum;
-       size_t size;
+       SMB_OFF_T size;
        time_t c_time, a_time, m_time, w_time, m_time2;
        const char *fname = "\\trans2.tst";
        const char *dname = "\\trans2";
@@ -2666,9 +2666,10 @@ static BOOL run_oplock2(int dummy)
 
        sleep(2);
 
-       /* Ensure cli1 processes the break. */
+       /* Ensure cli1 processes the break. Empty file should always return 0
+        * bytes.  */
 
-       if (cli_read(cli1, fnum1, buf, 0, 4) != 4) {
+       if (cli_read(cli1, fnum1, buf, 0, 4) != 0) {
                printf("read on fnum1 failed (%s)\n", cli_errstr(cli1));
                correct = False;
        }
@@ -2822,12 +2823,15 @@ static BOOL run_deletetest(int dummy)
                goto fail;
        }
 
-#if 0
-       {
-               uint32 accinfo = 0;
-               cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char *)&accinfo);
-               printf("access mode = 0x%lx\n", accinfo);
-       }
+#if 0 /* JRATEST */
+        {
+                uint32 *accinfo = NULL;
+                uint32 len;
+                cli_qfileinfo_test(cli1, fnum1, SMB_FILE_ACCESS_INFORMATION, (char **)&accinfo, &len);
+               if (accinfo)
+                       printf("access mode = 0x%lx\n", *accinfo);
+                SAFE_FREE(accinfo);
+        }
 #endif
 
        if (!cli_close(cli1, fnum1)) {
@@ -3125,7 +3129,7 @@ static BOOL run_deletetest(int dummy)
                                   FILE_OVERWRITE_IF, 0, 0);
        
        if (fnum1 == -1) {
-               printf("[8] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+               printf("[8] open of %s failed (%s)\n", fname, cli_errstr(cli1));
                correct = False;
                goto fail;
        }
@@ -3135,7 +3139,7 @@ static BOOL run_deletetest(int dummy)
                                   FILE_OPEN, 0, 0);
        
        if (fnum2 == -1) {
-               printf("[8] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+               printf("[8] open 2 of %s failed (%s)\n", fname, cli_errstr(cli2));
                correct = False;
                goto fail;
        }
@@ -3202,6 +3206,49 @@ static BOOL run_deletetest(int dummy)
                correct = False;
        } else
                printf("tenth delete on close test succeeded.\n");
+
+       cli_setatr(cli1, fname, 0, 0);
+       cli_unlink(cli1, fname);
+
+       /* What error do we get when attempting to open a read-only file with
+          delete access ? */
+
+       /* Create a readonly file. */
+       fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_DATA|FILE_WRITE_DATA,
+                                  FILE_ATTRIBUTE_READONLY, FILE_SHARE_NONE, FILE_OVERWRITE_IF, 0, 0);
+       if (fnum1 == -1) {
+               printf("[11] open of %s failed (%s)\n", fname, cli_errstr(cli1));
+               correct = False;
+               goto fail;
+       }
+
+       if (!cli_close(cli1, fnum1)) {
+               printf("[11] close failed (%s)\n", cli_errstr(cli1));
+               correct = False;
+               goto fail;
+       }
+
+       /* Now try open for delete access. */
+       fnum1 = cli_nt_create_full(cli1, fname, 0, FILE_READ_ATTRIBUTES|DELETE_ACCESS,
+                                  0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+                                  FILE_OVERWRITE_IF, 0, 0);
+       
+       if (fnum1 != -1) {
+               printf("[11] open of %s succeeded should have been denied with ACCESS_DENIED!\n", fname);
+               cli_close(cli1, fnum1);
+               goto fail;
+               correct = False;
+       } else {
+               NTSTATUS nterr = cli_nt_error(cli1);
+               if (!NT_STATUS_EQUAL(nterr,NT_STATUS_ACCESS_DENIED)) {
+                       printf("[11] open of %s should have been denied with ACCESS_DENIED! Got error %s\n", fname, nt_errstr(nterr));
+                       goto fail;
+                       correct = False;
+               } else {
+                       printf("eleventh delete on close test succeeded.\n");
+               }
+       }
+       
        printf("finished delete test\n");
 
   fail:
@@ -3342,9 +3389,9 @@ static BOOL run_rename(int dummy)
        }
 
        if (!cli_rename(cli1, fname, fname1)) {
-               printf("First rename failed (this is correct) - %s\n", cli_errstr(cli1));
+               printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
        } else {
-               printf("First rename succeeded - this should have failed !\n");
+               printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
                correct = False;
        }
 
@@ -3368,10 +3415,10 @@ static BOOL run_rename(int dummy)
        }
 
        if (!cli_rename(cli1, fname, fname1)) {
-               printf("Second rename failed - this should have succeeded - %s\n", cli_errstr(cli1));
+               printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
                correct = False;
        } else {
-               printf("Second rename succeeded\n");
+               printf("Second rename succeeded (SHARE_DELETE | SHARE_READ)\n");
        }
 
        if (!cli_close(cli1, fnum1)) {
@@ -3415,10 +3462,10 @@ static BOOL run_rename(int dummy)
 #endif
 
        if (!cli_rename(cli1, fname, fname1)) {
-               printf("Third rename failed - this should have succeeded - %s\n", cli_errstr(cli1));
+               printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
                correct = False;
        } else {
-               printf("Third rename succeeded\n");
+               printf("Third rename succeeded (SHARE_NONE)\n");
        }
 
        if (!cli_close(cli1, fnum1)) {
@@ -3429,6 +3476,75 @@ static BOOL run_rename(int dummy)
        cli_unlink(cli1, fname);
        cli_unlink(cli1, fname1);
 
+        /*----*/
+
+       fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
+                                  FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_OVERWRITE_IF, 0, 0);
+
+       if (fnum1 == -1) {
+               printf("Fourth open failed - %s\n", cli_errstr(cli1));
+               return False;
+       }
+
+       if (!cli_rename(cli1, fname, fname1)) {
+               printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
+       } else {
+               printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
+               correct = False;
+       }
+
+       if (!cli_close(cli1, fnum1)) {
+               printf("close - 4 failed (%s)\n", cli_errstr(cli1));
+               return False;
+       }
+
+       cli_unlink(cli1, fname);
+       cli_unlink(cli1, fname1);
+
+        /*--*/
+
+       fnum1 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
+                                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0);
+
+       if (fnum1 == -1) {
+               printf("Fifth open failed - %s\n", cli_errstr(cli1));
+               return False;
+       }
+
+       if (!cli_rename(cli1, fname, fname1)) {
+               printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have failed ! \n");
+               correct = False;
+       } else {
+               printf("Fifth rename succeeded (SHARE_READ | SHARE_WRITE | SHARE_DELETE) (this is correct) - %s\n", cli_errstr(cli1));
+       }
+
+        /*
+         * Now check if the first name still exists ...
+         */
+
+        /*fnum2 = cli_nt_create_full(cli1, fname, 0, GENERIC_READ_ACCESS, FILE_ATTRIBUTE_NORMAL,
+                                  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_OVERWRITE_IF, 0, 0);
+
+        if (fnum2 == -1) {
+          printf("Opening original file after rename of open file fails: %s\n",
+              cli_errstr(cli1));
+        }
+        else {
+          printf("Opening original file after rename of open file works ...\n");
+          (void)cli_close(cli1, fnum2);
+          } */
+
+        /*--*/
+
+
+       if (!cli_close(cli1, fnum1)) {
+               printf("close - 5 failed (%s)\n", cli_errstr(cli1));
+               return False;
+       }
+
+       cli_unlink(cli1, fname);
+       cli_unlink(cli1, fname1);
+        
        if (!torture_close_connection(cli1)) {
                correct = False;
        }
@@ -3475,7 +3591,7 @@ static BOOL run_opentest(int dummy)
        const char *fname = "\\readonly.file";
        int fnum1, fnum2;
        char buf[20];
-       size_t fsize;
+       SMB_OFF_T fsize;
        BOOL correct = True;
        char *tmp_path;
 
@@ -4023,7 +4139,7 @@ static BOOL run_openattrtest(int dummy)
        return correct;
 }
 
-static void list_fn(file_info *finfo, const char *name, void *state)
+static void list_fn(const char *mnt, file_info *finfo, const char *name, void *state)
 {
        
 }
@@ -4083,7 +4199,7 @@ static BOOL run_dirtest(int dummy)
        return correct;
 }
 
-static void del_fn(file_info *finfo, const char *mask, void *state)
+static void del_fn(const char *mnt, file_info *finfo, const char *mask, void *state)
 {
        struct cli_state *pcli = (struct cli_state *)state;
        fstring fname;
@@ -4238,8 +4354,114 @@ BOOL torture_chkpath_test(int dummy)
        return ret;
 }
 
+static BOOL run_eatest(int dummy)
+{
+       static struct cli_state *cli;
+       const char *fname = "\\eatest.txt";
+       BOOL correct = True;
+       int fnum, i;
+       size_t num_eas;
+       struct ea_struct *ea_list = NULL;
+       TALLOC_CTX *mem_ctx = talloc_init("eatest");
 
+       printf("starting eatest\n");
+       
+       if (!torture_open_connection(&cli)) {
+               return False;
+       }
+       
+       cli_unlink(cli, fname);
+       fnum = cli_nt_create_full(cli, fname, 0,
+                                  FIRST_DESIRED_ACCESS, FILE_ATTRIBUTE_ARCHIVE,
+                                  FILE_SHARE_NONE, FILE_OVERWRITE_IF, 
+                                  0x4044, 0);
 
+       if (fnum == -1) {
+               printf("open failed - %s\n", cli_errstr(cli));
+               return False;
+       }
+
+       for (i = 0; i < 10; i++) {
+               fstring ea_name, ea_val;
+
+               slprintf(ea_name, sizeof(ea_name), "EA_%d", i);
+               memset(ea_val, (char)i+1, i+1);
+               if (!cli_set_ea_fnum(cli, fnum, ea_name, ea_val, i+1)) {
+                       printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
+                       return False;
+               }
+       }
+       
+       cli_close(cli, fnum);
+       for (i = 0; i < 10; i++) {
+               fstring ea_name, ea_val;
+
+               slprintf(ea_name, sizeof(ea_name), "EA_%d", i+10);
+               memset(ea_val, (char)i+1, i+1);
+               if (!cli_set_ea_path(cli, fname, ea_name, ea_val, i+1)) {
+                       printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
+                       return False;
+               }
+       }
+       
+       if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
+               printf("ea_get list failed - %s\n", cli_errstr(cli));
+               correct = False;
+       }
+
+       printf("num_eas = %d\n", num_eas);
+
+       if (num_eas != 20) {
+               printf("Should be 20 EA's stored... failing.\n");
+               correct = False;
+       }
+
+       for (i = 0; i < num_eas; i++) {
+               printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
+               dump_data(0, ea_list[i].value.data, ea_list[i].value.length);
+       }
+
+       /* Setting EA's to zero length deletes them. Test this */
+       printf("Now deleting all EA's - case indepenent....\n");
+
+       for (i = 0; i < 20; i++) {
+               fstring ea_name;
+               slprintf(ea_name, sizeof(ea_name), "ea_%d", i);
+               if (!cli_set_ea_path(cli, fname, ea_name, "", 0)) {
+                       printf("ea_set of name %s failed - %s\n", ea_name, cli_errstr(cli));
+                       return False;
+               }
+       }
+       
+       if (!cli_get_ea_list_path(cli, fname, mem_ctx, &num_eas, &ea_list)) {
+               printf("ea_get list failed - %s\n", cli_errstr(cli));
+               correct = False;
+       }
+
+       printf("num_eas = %d\n", num_eas);
+       for (i = 0; i < num_eas; i++) {
+               printf("%d: ea_name = %s. Val = ", i, ea_list[i].name);
+               dump_data(0, ea_list[i].value.data, ea_list[i].value.length);
+       }
+
+       if (num_eas != 0) {
+               printf("deleting EA's failed.\n");
+               correct = False;
+       }
+
+       /* Try and delete a non existant EA. */
+       if (!cli_set_ea_path(cli, fname, "foo", "", 0)) {
+               printf("deleting non-existant EA 'foo' should succeed. %s\n", cli_errstr(cli));
+               correct = False;
+       }
+
+       talloc_destroy(mem_ctx);
+       if (!torture_close_connection(cli)) {
+               correct = False;
+       }
+       
+       return correct;
+}
 
 static BOOL run_dirtest1(int dummy)
 {
@@ -4578,6 +4800,7 @@ static struct {
        {"IOCTL",  torture_ioctl_test, 0},
        {"CHKPATH",  torture_chkpath_test, 0},
        {"FDSESS", run_fdsesstest, 0},
+       { "EATEST", run_eatest, 0},
        {NULL, NULL, 0}};
 
 
@@ -4638,6 +4861,9 @@ static void usage(void)
 {
        int i;
 
+       printf("WARNING samba4 test suite is much more complete nowadays.\n");
+       printf("Please use samba4 torture.\n\n");
+
        printf("Usage: smbtorture //server/share <options> TEST1 TEST2 ...\n");
 
        printf("\t-d debuglevel\n");
@@ -4676,8 +4902,6 @@ static void usage(void)
        char *p;
        int gotuser = 0;
        int gotpass = 0;
-       extern char *optarg;
-       extern int optind;
        BOOL correct = True;
 
        dbf = x_stdout;