torture: split pattern write into helper function
authorDavid Disseldorp <ddiss@samba.org>
Tue, 2 Sep 2014 18:07:16 +0000 (20:07 +0200)
committerJeremy Allison <jra@samba.org>
Mon, 8 Sep 2014 17:11:12 +0000 (19:11 +0200)
This allows for patterned writes after file creation, as needed for
sparse file integrity testing.

Signed-off-by: David Disseldorp <ddiss@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source4/torture/smb2/ioctl.c

index 3f8795872c579a92cf59b4eaae83a310f124913b..df380a8ef27a756ab64af989ba08887ddc6988dd 100644 (file)
@@ -119,6 +119,42 @@ static uint64_t patt_hash(uint64_t off)
        return off;
 }
 
+static bool write_pattern(struct torture_context *torture,
+                         struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
+                         struct smb2_handle h, uint64_t off, uint64_t len,
+                         uint64_t patt_off)
+{
+       NTSTATUS status;
+       uint64_t i;
+       uint8_t *buf;
+       uint64_t buf_off = 0;
+
+       if (len == 0) {
+               return true;
+       }
+
+       buf = talloc_zero_size(mem_ctx, len);
+       torture_assert(torture, (buf != NULL), "no memory for file data buf");
+
+       for (i = 0; i <= len - 8; i += 8) {
+               SBVAL(buf, i, patt_hash(patt_off));
+               patt_off += 8;
+       }
+
+       while (len > 0) {
+               uint64_t io_sz = MIN(1024 * 1024, len);
+               status = smb2_util_write(tree, h,
+                                        buf + buf_off, off, io_sz);
+               torture_assert_ntstatus_ok(torture, status, "file write");
+
+               len -= io_sz;
+               buf_off += io_sz;
+               off += io_sz;
+       }
+
+       return true;
+}
+
 static bool check_pattern(struct torture_context *torture,
                          struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
                          struct smb2_handle h, uint64_t off, uint64_t len,
@@ -189,11 +225,7 @@ static bool test_setup_create_fill(struct torture_context *torture,
                                   uint32_t desired_access,
                                   uint32_t file_attributes)
 {
-       NTSTATUS status;
        bool ok;
-       uint64_t i;
-       uint8_t *buf = talloc_zero_size(mem_ctx, size);
-       torture_assert(torture, (buf != NULL), "no memory for file data buf");
 
        smb2_util_unlink(tree, fname);
 
@@ -205,19 +237,8 @@ static bool test_setup_create_fill(struct torture_context *torture,
        torture_assert(torture, ok, "file open");
 
        if (size > 0) {
-               uint64_t cur_off = 0;
-               for (i = 0; i <= size - 8; i += 8) {
-                       SBVAL(buf, i, patt_hash(i));
-               }
-               while (size > 0) {
-                       uint64_t io_sz = MIN(1024 * 1024, size);
-                       status = smb2_util_write(tree, *fh,
-                                                buf + cur_off, cur_off, io_sz);
-                       torture_assert_ntstatus_ok(torture, status, "file write");
-
-                       size -= io_sz;
-                       cur_off += io_sz;
-               }
+               ok = write_pattern(torture, tree, mem_ctx, *fh, 0, size, 0);
+               torture_assert(torture, ok, "write pattern");
        }
        return true;
 }