s4-torture: Separate out the notify dir subtest
[kai/samba-autobuild/.git] / source4 / torture / raw / notify.c
index d7541b7c7132c689cab2876e1904b5f590c71b1c..a41630cbde346e8222e1f9c83db5ed63fabf3831 100644 (file)
 */
 
 #include "includes.h"
-#include "torture/torture.h"
 #include "libcli/raw/libcliraw.h"
 #include "libcli/raw/raw_proto.h"
 #include "libcli/libcli.h"
 #include "system/filesys.h"
 #include "torture/util.h"
-#include "param/param.h"
+#include "torture/raw/proto.h"
 
 #define BASEDIR "\\test_notify"
 
                goto done; \
        }} while (0)
 
+#define CHECK_WSTR2(tctx, field, value, flags) \
+do { \
+       if (!field.s || strcmp(field.s, value) || \
+           wire_bad_flags(&field, flags, cli->transport)) { \
+               torture_result(tctx, TORTURE_FAIL, \
+                   "(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
+       } \
+} while (0)
 
 /* 
    basic testing of change notify on directories
 */
-static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2, 
-                           TALLOC_CTX *mem_ctx)
+static bool test_notify_dir(struct torture_context *mem_ctx,
+                           struct smbcli_state *cli,
+                           struct smbcli_state *cli2)
 {
        bool ret = true;
        NTSTATUS status;
@@ -68,13 +76,17 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        struct smbcli_request *req, *req2;
        extern int torture_numops;
 
-       printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
+       printf("TESTING CHANGE NOTIFY ON DIRECTORIES\n");
                
+       if (!torture_setup_dir(cli, BASEDIR)) {
+               return false;
+       }
+
        /*
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -102,14 +114,14 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        notify.nttrans.in.file.fnum = fnum;
        notify.nttrans.in.recursive = true;
 
-       printf("testing notify cancel\n");
+       printf("Testing notify cancel\n");
 
        req = smb_raw_changenotify_send(cli->tree, &notify);
        smb_raw_ntcancel(req);
        status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
        CHECK_STATUS(status, NT_STATUS_CANCELLED);
 
-       printf("testing notify mkdir\n");
+       printf("Testing notify mkdir\n");
 
        req = smb_raw_changenotify_send(cli->tree, &notify);
        smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
@@ -121,7 +133,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
        CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
 
-       printf("testing notify rmdir\n");
+       printf("Testing notify rmdir\n");
 
        req = smb_raw_changenotify_send(cli->tree, &notify);
        smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
@@ -132,13 +144,13 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
        CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
 
-       printf("testing notify mkdir - rmdir - mkdir - rmdir\n");
+       printf("Testing notify mkdir - rmdir - mkdir - rmdir\n");
 
        smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
        smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
        smbcli_mkdir(cli2->tree, BASEDIR "\\subdir-name");
        smbcli_rmdir(cli2->tree, BASEDIR "\\subdir-name");
-       msleep(200);
+       smb_msleep(200);
        req = smb_raw_changenotify_send(cli->tree, &notify);
        status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
        CHECK_STATUS(status, NT_STATUS_OK);
@@ -153,7 +165,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        CHECK_WSTR(notify.nttrans.out.changes[3].name, "subdir-name", STR_UNICODE);
 
        count = torture_numops;
-       printf("testing buffered notify on create of %d files\n", count);
+       printf("Testing buffered notify on create of %d files\n", count);
        for (i=0;i<count;i++) {
                char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
                int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
@@ -183,7 +195,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        /* (1st unlink) as the 2nd notify directly returns,
           this unlink is only seen by the 1st notify and 
           the 3rd notify (later) */
-       printf("testing notify on unlink for the first file\n");
+       printf("Testing notify on unlink for the first file\n");
        status = smbcli_unlink(cli2->tree, BASEDIR "\\test0.txt");
        CHECK_STATUS(status, NT_STATUS_OK);
 
@@ -210,7 +222,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
        status = smbcli_unlink(cli->tree, BASEDIR "\\nonexistant.txt");
        CHECK_STATUS(status, NT_STATUS_OBJECT_NAME_NOT_FOUND);
 
-       printf("testing notify on wildcard unlink for %d files\n", count-1);
+       printf("Testing notify on wildcard unlink for %d files\n", count-1);
        /* (2nd unlink) do a wildcard unlink */
        status = smbcli_unlink(cli2->tree, BASEDIR "\\test*.txt");
        CHECK_STATUS(status, NT_STATUS_OK);
@@ -241,7 +253,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
                CHECK_VAL(notify.nttrans.out.changes[i].action, NOTIFY_ACTION_REMOVED);
        }
 
-       printf("testing if a close() on the dir handle triggers the notify reply\n");
+       printf("Testing if a close() on the dir handle triggers the notify reply\n");
 
        notify.nttrans.in.file.fnum = fnum;
        req = smb_raw_changenotify_send(cli->tree, &notify);
@@ -258,6 +270,7 @@ static bool test_notify_dir(struct smbcli_state *cli, struct smbcli_state *cli2,
 
 done:
        smb_raw_exit(cli->session);
+       smbcli_deltree(cli->tree, BASEDIR);
        return ret;
 }
 
@@ -311,7 +324,7 @@ static bool test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -359,7 +372,7 @@ static bool test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
 
        notify.nttrans.in.completion_filter = 0;
        notify.nttrans.in.recursive = true;
-       msleep(200);
+       smb_msleep(200);
        req1 = smb_raw_changenotify_send(cli->tree, &notify);
 
        smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
@@ -442,7 +455,7 @@ static bool test_notify_mask_change(struct smbcli_state *cli, TALLOC_CTX *mem_ct
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -562,12 +575,12 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
 
        tv = timeval_current_ofs(1000, 0);
        t = timeval_to_nttime(&tv);
-               
+
        /*
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -596,7 +609,7 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
                notify.nttrans.in.completion_filter = (1<<i); \
                req = smb_raw_changenotify_send(cli->tree, &notify); \
                op \
-               msleep(200); smb_raw_ntcancel(req); \
+               smb_msleep(200); smb_raw_ntcancel(req); \
                status = smb_raw_changenotify_recv(req, tctx, &notify); \
                cleanup \
                smbcli_close(cli->tree, fnum); \
@@ -645,70 +658,70 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
        } while (0); \
        } while (0);
 
-       printf("testing mkdir\n");
-       NOTIFY_MASK_TEST("testing mkdir",;,
+       printf("Testing mkdir\n");
+       NOTIFY_MASK_TEST("Testing mkdir",;,
                         smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
                         smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
                         NOTIFY_ACTION_ADDED,
                         FILE_NOTIFY_CHANGE_DIR_NAME, 1);
 
-       printf("testing create file\n");
-       NOTIFY_MASK_TEST("testing create file",;,
+       printf("Testing create file\n");
+       NOTIFY_MASK_TEST("Testing create file",;,
                         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
                         smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
                         NOTIFY_ACTION_ADDED,
                         FILE_NOTIFY_CHANGE_FILE_NAME, 1);
 
-       printf("testing unlink\n");
-       NOTIFY_MASK_TEST("testing unlink",
+       printf("Testing unlink\n");
+       NOTIFY_MASK_TEST("Testing unlink",
                         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
                         smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
                         ;,
                         NOTIFY_ACTION_REMOVED,
                         FILE_NOTIFY_CHANGE_FILE_NAME, 1);
 
-       printf("testing rmdir\n");
-       NOTIFY_MASK_TEST("testing rmdir",
+       printf("Testing rmdir\n");
+       NOTIFY_MASK_TEST("Testing rmdir",
                         smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
                         smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
                         ;,
                         NOTIFY_ACTION_REMOVED,
                         FILE_NOTIFY_CHANGE_DIR_NAME, 1);
 
-       printf("testing rename file\n");
-       NOTIFY_MASK_TEST("testing rename file",
+       printf("Testing rename file\n");
+       NOTIFY_MASK_TEST("Testing rename file",
                         smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
                         smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
                         smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
                         NOTIFY_ACTION_OLD_NAME,
                         FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);
 
-       printf("testing rename dir\n");
-       NOTIFY_MASK_TEST("testing rename dir",
+       printf("Testing rename dir\n");
+       NOTIFY_MASK_TEST("Testing rename dir",
                smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
                smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
                smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
                NOTIFY_ACTION_OLD_NAME,
                FILE_NOTIFY_CHANGE_DIR_NAME, 2);
 
-       printf("testing set path attribute\n");
-       NOTIFY_MASK_TEST("testing set path attribute",
+       printf("Testing set path attribute\n");
+       NOTIFY_MASK_TEST("Testing set path attribute",
                smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
                smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
                smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
                NOTIFY_ACTION_MODIFIED,
                FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);
 
-       printf("testing set path write time\n");
-       NOTIFY_MASK_TEST("testing set path write time",
+       printf("Testing set path write time\n");
+       NOTIFY_MASK_TEST("Testing set path write time",
                smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
                smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
                smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
                NOTIFY_ACTION_MODIFIED,
                FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
 
-       printf("testing set file attribute\n");
-       NOTIFY_MASK_TEST("testing set file attribute",
+       printf("Testing set file attribute\n");
+       NOTIFY_MASK_TEST("Testing set file attribute",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
@@ -720,8 +733,8 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
                       "everywhere\n");
        }
        else {
-               printf("testing set file create time\n");
-               NOTIFY_MASK_TEST("testing set file create time",
+               printf("Testing set file create time\n");
+               NOTIFY_MASK_TEST("Testing set file create time",
                        fnum2 = create_complex_file(cli, tctx,
                                                    BASEDIR "\\tname1");,
                        smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
@@ -731,24 +744,24 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
                        FILE_NOTIFY_CHANGE_CREATION, 1);
        }
 
-       printf("testing set file access time\n");
-       NOTIFY_MASK_TEST("testing set file access time",
+       printf("Testing set file access time\n");
+       NOTIFY_MASK_TEST("Testing set file access time",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
                NOTIFY_ACTION_MODIFIED,
                FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);
 
-       printf("testing set file write time\n");
-       NOTIFY_MASK_TEST("testing set file write time",
+       printf("Testing set file write time\n");
+       NOTIFY_MASK_TEST("Testing set file write time",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
                NOTIFY_ACTION_MODIFIED,
                FILE_NOTIFY_CHANGE_LAST_WRITE, 1);
 
-       printf("testing set file change time\n");
-       NOTIFY_MASK_TEST("testing set file change time",
+       printf("Testing set file change time\n");
+       NOTIFY_MASK_TEST("Testing set file change time",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
@@ -756,16 +769,16 @@ static bool test_notify_mask(struct smbcli_state *cli, struct torture_context *t
                0, 1);
 
 
-       printf("testing write\n");
-       NOTIFY_MASK_TEST("testing write",
+       printf("Testing write\n");
+       NOTIFY_MASK_TEST("Testing write",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
                NOTIFY_ACTION_MODIFIED,
                0, 1);
 
-       printf("testing truncate\n");
-       NOTIFY_MASK_TEST("testing truncate",
+       printf("Testing truncate\n");
+       NOTIFY_MASK_TEST("Testing truncate",
                fnum2 = create_complex_file(cli, tctx, BASEDIR "\\tname1");,
                smbcli_ftruncate(cli->tree, fnum2, 10000);,
                (smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
@@ -794,7 +807,7 @@ static bool test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
        printf("TESTING CHANGE NOTIFY ON FILES\n");
 
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
        io.ntcreatex.in.create_options = 0;
@@ -817,7 +830,7 @@ static bool test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
        notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
        notify.nttrans.in.recursive = false;
 
-       printf("testing if notifies on file handles are invalid (should be)\n");
+       printf("Testing if notifies on file handles are invalid (should be)\n");
 
        req = smb_raw_changenotify_send(cli->tree, &notify);
        status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
@@ -860,7 +873,7 @@ static bool test_notify_tdis(struct torture_context *tctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -922,7 +935,7 @@ static bool test_notify_exit(struct torture_context *tctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -983,7 +996,7 @@ static bool test_notify_ulogoff(struct torture_context *tctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -1051,7 +1064,7 @@ static bool test_notify_tcp_dis(struct torture_context *tctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -1105,7 +1118,7 @@ static bool test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
          get a handle on the directory
        */
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -1198,7 +1211,7 @@ static bool test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
        printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");
 
        io.generic.level = RAW_OPEN_NTCREATEX;
-       io.ntcreatex.in.root_fid = 0;
+       io.ntcreatex.in.root_fid.fnum = 0;
        io.ntcreatex.in.flags = 0;
        io.ntcreatex.in.access_mask = SEC_FILE_ALL;
        io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
@@ -1284,20 +1297,425 @@ done:
        return ret;
 }
 
+/*
+   Test response when cached server events exceed single NT NOTFIY response
+   packet size.
+*/
+static bool test_notify_overflow(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
+{
+       bool ret = true;
+       NTSTATUS status;
+       union smb_notify notify;
+       union smb_open io;
+       int fnum;
+       int count = 100;
+       struct smbcli_request *req1;
+       int i;
+
+       printf("TESTING CHANGE NOTIFY EVENT OVERFLOW\n");
+
+       /* get a handle on the directory */
+       io.generic.level = RAW_OPEN_NTCREATEX;
+       io.ntcreatex.in.root_fid.fnum = 0;
+       io.ntcreatex.in.flags = 0;
+       io.ntcreatex.in.access_mask = SEC_FILE_ALL;
+       io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
+       io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
+       io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+           NTCREATEX_SHARE_ACCESS_WRITE;
+       io.ntcreatex.in.alloc_size = 0;
+       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
+       io.ntcreatex.in.security_flags = 0;
+       io.ntcreatex.in.fname = BASEDIR;
+
+       status = smb_raw_open(cli->tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       fnum = io.ntcreatex.out.file.fnum;
+
+       /* ask for a change notify, on name changes. */
+       notify.nttrans.level = RAW_NOTIFY_NTTRANS;
+       notify.nttrans.in.buffer_size = 1000;
+       notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
+       notify.nttrans.in.file.fnum = fnum;
+
+       notify.nttrans.in.recursive = true;
+       req1 = smb_raw_changenotify_send(cli->tree, &notify);
+
+       /* cancel initial requests so the buffer is setup */
+       smb_raw_ntcancel(req1);
+       status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
+       CHECK_STATUS(status, NT_STATUS_CANCELLED);
+
+       /* open a lot of files, filling up the server side notify buffer */
+       printf("Testing overflowed buffer notify on create of %d files\n",
+              count);
+       for (i=0;i<count;i++) {
+               char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
+               int fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR,
+                                       DENY_NONE);
+               if (fnum2 == -1) {
+                       printf("Failed to create %s - %s\n",
+                              fname, smbcli_errstr(cli->tree));
+                       ret = false;
+                       goto done;
+               }
+               talloc_free(fname);
+               smbcli_close(cli->tree, fnum2);
+       }
+
+       /* expect that 0 events will be returned with NT_STATUS_OK */
+       req1 = smb_raw_changenotify_send(cli->tree, &notify);
+       status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(notify.nttrans.out.num_changes, 0);
+
+done:
+       smb_raw_exit(cli->session);
+       return ret;
+}
+
+/*
+   Test if notifications are returned for changes to the base directory.
+   They shouldn't be.
+*/
+static bool test_notify_basedir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
+{
+       bool ret = true;
+       NTSTATUS status;
+       union smb_notify notify;
+       union smb_open io;
+       int fnum;
+       struct smbcli_request *req1;
+
+       printf("TESTING CHANGE NOTIFY BASEDIR EVENTS\n");
+
+       /* get a handle on the directory */
+       io.generic.level = RAW_OPEN_NTCREATEX;
+       io.ntcreatex.in.root_fid.fnum = 0;
+       io.ntcreatex.in.flags = 0;
+       io.ntcreatex.in.access_mask = SEC_FILE_ALL;
+       io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
+       io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
+       io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+           NTCREATEX_SHARE_ACCESS_WRITE;
+       io.ntcreatex.in.alloc_size = 0;
+       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
+       io.ntcreatex.in.security_flags = 0;
+       io.ntcreatex.in.fname = BASEDIR;
+
+       status = smb_raw_open(cli->tree, mem_ctx, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       fnum = io.ntcreatex.out.file.fnum;
+
+       /* create a test file that will also be modified */
+       smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1",
+                                           O_CREAT, 0));
+
+       /* ask for a change notify, on attribute changes. */
+       notify.nttrans.level = RAW_NOTIFY_NTTRANS;
+       notify.nttrans.in.buffer_size = 1000;
+       notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_ATTRIBUTES;
+       notify.nttrans.in.file.fnum = fnum;
+       notify.nttrans.in.recursive = true;
+
+       req1 = smb_raw_changenotify_send(cli->tree, &notify);
+
+       /* set attribute on the base dir */
+       smbcli_setatr(cli->tree, BASEDIR, FILE_ATTRIBUTE_HIDDEN, 0);
+
+       /* set attribute on a file to assure we receive a notification */
+       smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);
+       smb_msleep(200);
+
+       /* check how many responses were given, expect only 1 for the file */
+       status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_MODIFIED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "tname1", STR_UNICODE);
+
+done:
+       smb_raw_exit(cli->session);
+       return ret;
+}
+
+
+/*
+  create a secondary tree connect - used to test for a bug in Samba3 messaging
+  with change notify
+*/
+static struct smbcli_tree *secondary_tcon(struct smbcli_state *cli, 
+                                         struct torture_context *tctx)
+{
+       NTSTATUS status;
+       const char *share, *host;
+       struct smbcli_tree *tree;
+       union smb_tcon tcon;
+
+       share = torture_setting_string(tctx, "share", NULL);
+       host  = torture_setting_string(tctx, "host", NULL);
+       
+       printf("create a second tree context on the same session\n");
+       tree = smbcli_tree_init(cli->session, tctx, false);
+
+       tcon.generic.level = RAW_TCON_TCONX;
+       tcon.tconx.in.flags = 0;
+       tcon.tconx.in.password = data_blob(NULL, 0);
+       tcon.tconx.in.path = talloc_asprintf(tctx, "\\\\%s\\%s", host, share);
+       tcon.tconx.in.device = "A:";    
+       status = smb_raw_tcon(tree, tctx, &tcon);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(tree);
+               printf("Failed to create secondary tree\n");
+               return NULL;
+       }
+
+       tree->tid = tcon.tconx.out.tid;
+       printf("tid1=%d tid2=%d\n", cli->tree->tid, tree->tid);
+
+       return tree;
+}
+
+
 /* 
-   basic testing of change notify
+   very simple change notify test
 */
-bool torture_raw_notify(struct torture_context *torture, 
-                       struct smbcli_state *cli, 
-                       struct smbcli_state *cli2)
+static bool test_notify_tcon(struct torture_context *torture,
+                            struct smbcli_state *cli)
 {
        bool ret = true;
+       NTSTATUS status;
+       union smb_notify notify;
+       union smb_open io;
+       int fnum;
+       struct smbcli_request *req;
+       extern int torture_numops;
+       struct smbcli_tree *tree = NULL;
+               
+       printf("TESTING SIMPLE CHANGE NOTIFY\n");
                
        if (!torture_setup_dir(cli, BASEDIR)) {
                return false;
        }
 
-       ret &= test_notify_dir(cli, cli2, torture);
+       /*
+         get a handle on the directory
+       */
+       io.generic.level = RAW_OPEN_NTCREATEX;
+       io.ntcreatex.in.root_fid.fnum = 0;
+       io.ntcreatex.in.flags = 0;
+       io.ntcreatex.in.access_mask = SEC_FILE_ALL;
+       io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
+       io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
+       io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
+       io.ntcreatex.in.alloc_size = 0;
+       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
+       io.ntcreatex.in.security_flags = 0;
+       io.ntcreatex.in.fname = BASEDIR;
+
+       status = smb_raw_open(cli->tree, torture, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       fnum = io.ntcreatex.out.file.fnum;
+
+       status = smb_raw_open(cli->tree, torture, &io);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       /* ask for a change notify,
+          on file or directory name changes */
+       notify.nttrans.level = RAW_NOTIFY_NTTRANS;
+       notify.nttrans.in.buffer_size = 1000;
+       notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
+       notify.nttrans.in.file.fnum = fnum;
+       notify.nttrans.in.recursive = true;
+
+       printf("Testing notify mkdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("Testing notify rmdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("SIMPLE CHANGE NOTIFY OK\n");
+
+       printf("TESTING WITH SECONDARY TCON\n");
+       tree = secondary_tcon(cli, torture);
+
+       printf("Testing notify mkdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("Testing notify rmdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("CHANGE NOTIFY WITH TCON OK\n");
+
+       printf("Disconnecting secondary tree\n");
+       status = smb_tree_disconnect(tree);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       talloc_free(tree);
+
+       printf("Testing notify mkdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_ADDED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("Testing notify rmdir\n");
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
+
+       status = smb_raw_changenotify_recv(req, torture, &notify);
+       CHECK_STATUS(status, NT_STATUS_OK);
+       CHECK_VAL(notify.nttrans.out.num_changes, 1);
+       CHECK_VAL(notify.nttrans.out.changes[0].action, NOTIFY_ACTION_REMOVED);
+       CHECK_WSTR(notify.nttrans.out.changes[0].name, "subdir-name", STR_UNICODE);
+
+       printf("CHANGE NOTIFY WITH TDIS OK\n");
+done:
+       smb_raw_exit(cli->session);
+       smbcli_deltree(cli->tree, BASEDIR);
+       return ret;
+}
+
+
+/*
+   testing alignment of multiple change notify infos
+*/
+static bool test_notify_alignment(struct smbcli_state *cli,
+    struct torture_context *tctx)
+{
+       NTSTATUS status;
+       union smb_notify notify;
+       union smb_open io;
+       int i, fnum, fnum2;
+       struct smbcli_request *req;
+       const char *fname = BASEDIR "\\starter";
+       const char *fnames[] = { "a",
+                                "ab",
+                                "abc",
+                                "abcd" };
+       int num_names = ARRAY_SIZE(fnames);
+       char *fpath = NULL;
+
+       torture_comment(tctx, "TESTING CHANGE NOTIFY REPLY ALIGNMENT\n");
+
+       /* get a handle on the directory */
+       io.generic.level = RAW_OPEN_NTCREATEX;
+       io.ntcreatex.in.root_fid.fnum = 0;
+       io.ntcreatex.in.flags = 0;
+       io.ntcreatex.in.access_mask = SEC_FILE_ALL;
+       io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
+       io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
+       io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+                                      NTCREATEX_SHARE_ACCESS_WRITE;
+       io.ntcreatex.in.alloc_size = 0;
+       io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
+       io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
+       io.ntcreatex.in.security_flags = 0;
+       io.ntcreatex.in.fname = BASEDIR;
+
+       status = smb_raw_open(cli->tree, tctx, &io);
+       torture_assert_ntstatus_ok(tctx, status, "");
+       fnum = io.ntcreatex.out.file.fnum;
+
+       /* ask for a change notify, on file creation */
+       notify.nttrans.level = RAW_NOTIFY_NTTRANS;
+       notify.nttrans.in.buffer_size = 1000;
+       notify.nttrans.in.completion_filter = FILE_NOTIFY_CHANGE_FILE_NAME;
+       notify.nttrans.in.file.fnum = fnum;
+       notify.nttrans.in.recursive = false;
+
+       /* start change tracking */
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+
+       fnum2 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
+       torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
+       smbcli_close(cli->tree, fnum2);
+
+       status = smb_raw_changenotify_recv(req, tctx, &notify);
+       torture_assert_ntstatus_ok(tctx, status, "");
+
+       /* create 4 files that will cause CHANGE_NOTIFY_INFO structures
+        * to be returned in the same packet with all possible 4-byte padding
+        * permutations.  As per MS-CIFS 2.2.7.4.2 these structures should be
+        * 4-byte aligned. */
+
+       for (i = 0; i < num_names; i++) {
+               fpath = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fnames[i]);
+               fnum2 = smbcli_open(cli->tree, fpath,
+                   O_CREAT|O_RDWR, DENY_NONE);
+               torture_assert(tctx, fnum2 != -1, smbcli_errstr(cli->tree));
+               smbcli_close(cli->tree, fnum2);
+               talloc_free(fpath);
+       }
+
+       /* We send a notify packet, and let smb_raw_changenotify_recv() do
+        * the alignment checking for us. */
+       req = smb_raw_changenotify_send(cli->tree, &notify);
+       status = smb_raw_changenotify_recv(req, tctx, &notify);
+       torture_assert_ntstatus_ok(tctx, status, "");
+
+       /* Do basic checking for correctness. */
+       torture_assert(tctx, notify.nttrans.out.num_changes == num_names, "");
+       for (i = 0; i < num_names; i++) {
+               torture_assert(tctx, notify.nttrans.out.changes[i].action ==
+                   NOTIFY_ACTION_ADDED, "");
+               CHECK_WSTR2(tctx, notify.nttrans.out.changes[i].name, fnames[i],
+                   STR_UNICODE);
+       }
+
+       return true;
+}
+
+/*
+   basic testing of change notify
+*/
+static bool test_raw_notify_all(struct torture_context *torture,
+                               struct smbcli_state *cli,
+                               struct smbcli_state *cli2)
+{
+       bool ret = true;
+
+       if (!torture_setup_dir(cli, BASEDIR)) {
+               return false;
+       }
+
        ret &= test_notify_mask(cli, torture);
        ret &= test_notify_recursive(cli, torture);
        ret &= test_notify_mask_change(cli, torture);
@@ -1308,8 +1726,22 @@ bool torture_raw_notify(struct torture_context *torture,
        ret &= test_notify_tcp_dis(torture);
        ret &= test_notify_double(cli, torture);
        ret &= test_notify_tree(cli, torture);
+       ret &= test_notify_overflow(cli, torture);
+       ret &= test_notify_basedir(cli, torture);
+       ret &= test_notify_alignment(cli, torture);
 
        smb_raw_exit(cli->session);
        smbcli_deltree(cli->tree, BASEDIR);
        return ret;
 }
+
+struct torture_suite *torture_raw_notify(TALLOC_CTX *mem_ctx)
+{
+       struct torture_suite *suite = torture_suite_create(mem_ctx, "notify");
+
+       torture_suite_add_1smb_test(suite, "tcon", test_notify_tcon);
+       torture_suite_add_2smb_test(suite, "dir", test_notify_dir);
+       torture_suite_add_2smb_test(suite, "all", test_raw_notify_all);
+
+       return suite;
+}