CI: add a test listing a snapshotted directory
authorRalph Boehme <slow@samba.org>
Thu, 24 Mar 2022 16:31:00 +0000 (17:31 +0100)
committerJule Anger <janger@samba.org>
Mon, 11 Apr 2022 08:32:10 +0000 (08:32 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15035

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
(cherry picked from commit ba9c5ba8ec54e72d68e3f753a5350afe0fb50a7c)

selftest/knownfail.d/samba3.blackbox.shadow_copy_torture [new file with mode: 0644]
source3/script/tests/test_shadow_copy_torture.sh
source4/torture/smb2/create.c

diff --git a/selftest/knownfail.d/samba3.blackbox.shadow_copy_torture b/selftest/knownfail.d/samba3.blackbox.shadow_copy_torture
new file mode 100644 (file)
index 0000000..36f2acb
--- /dev/null
@@ -0,0 +1 @@
+^samba3.blackbox.shadow_copy_torture.fix inodes when listing directory\(fileserver\)
index 4ae2f9f707f7bb44847d892f3c683735e363c676..8f9d1be69279a7d2fefab250757e01ac5908c0dc 100755 (executable)
@@ -191,6 +191,25 @@ test_hiddenfile()
     return 0
 }
 
+test_shadow_copy_listdir_fix_inodes()
+{
+    local msg
+
+    msg=$1
+
+    #delete snapshots from previous tests
+    find $WORKDIR -name ".snapshots" -exec rm -rf {} \; 1>/dev/null 2>&1
+    build_snapshots
+
+    testit "$msg" \
+          $SMBTORTURE \
+          -U$USERNAME%$PASSWORD \
+          "//$SERVER/shadow_write" \
+          --option="torture:twrp_snapshot=$SNAPSHOT" \
+          smb2.twrp.listdir || \
+        failed=`expr $failed + 1`
+}
+
 build_files $WORKDIR
 
 # test open for writing and write behaviour of snapshoted files
@@ -204,4 +223,6 @@ testit "fix inodes with hardlink" test_shadow_copy_fix_inodes || failed=`expr $f
 
 testit "Test reading DOS attribute" test_hiddenfile || failed=`expr $failed + 1`
 
+test_shadow_copy_listdir_fix_inodes "fix inodes when listing directory"
+
 exit $failed
index 41a6ed6e4e407fb458435bc21c96113b0a17138a..09eab3f1331074d1134ba4bb40416cb74fae7390 100644 (file)
@@ -1965,6 +1965,173 @@ done:
        return ret;
 }
 
+static bool test_twrp_listdir(struct torture_context *tctx,
+                             struct smb2_tree *tree)
+{
+       struct smb2_create create;
+       struct smb2_handle h = {{0}};
+       struct smb2_find find;
+       unsigned int count;
+       union smb_search_data *d;
+       char *p = NULL;
+       struct tm tm;
+       time_t t;
+       uint64_t nttime;
+       const char *snapshot = NULL;
+       uint64_t normal_fileid;
+       uint64_t snapshot_fileid;
+       NTSTATUS status;
+       bool ret = true;
+
+       snapshot = torture_setting_string(tctx, "twrp_snapshot", NULL);
+       if (snapshot == NULL) {
+               torture_fail(tctx, "missing 'twrp_snapshot' option\n");
+       }
+
+       torture_comment(tctx, "Testing File-Ids of directory listing "
+                       "with timewarp (%s)\n",
+                       snapshot);
+
+       setenv("TZ", "GMT", 1);
+
+       /* strptime does not set tm.tm_isdst but mktime assumes DST is in
+        * effect if it is greather than 1. */
+       ZERO_STRUCT(tm);
+
+       p = strptime(snapshot, "@GMT-%Y.%m.%d-%H.%M.%S", &tm);
+       torture_assert_goto(tctx, p != NULL, ret, done, "strptime\n");
+       torture_assert_goto(tctx, *p == '\0', ret, done, "strptime\n");
+
+       t = mktime(&tm);
+       unix_to_nt_time(&nttime, t);
+
+       /*
+        * 1: Query the file's File-Id
+        */
+       create = (struct smb2_create) {
+               .in.desired_access = SEC_FILE_READ_DATA,
+               .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
+               .in.file_attributes = FILE_ATTRIBUTE_NORMAL,
+               .in.create_disposition = NTCREATEX_DISP_OPEN,
+               .in.fname = "subdir/hardlink",
+               .in.query_on_disk_id = true,
+       };
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "test file could not be created\n");
+       smb2_util_close(tree, create.out.file.handle);
+       normal_fileid = BVAL(&create.out.on_disk_id, 0);
+
+       /*
+        * 2: check directory listing of the file returns same File-Id
+        */
+
+       create = (struct smb2_create) {
+               .in.desired_access = SEC_DIR_LIST,
+               .in.file_attributes = FILE_ATTRIBUTE_DIRECTORY,
+               .in.create_disposition = NTCREATEX_DISP_OPEN,
+               .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
+               .in.fname = "subdir",
+               .in.create_options = NTCREATEX_OPTIONS_DIRECTORY,
+       };
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create\n");
+       h = create.out.file.handle;
+
+       find = (struct smb2_find) {
+               .in.file.handle = h,
+               .in.pattern = "*",
+               .in.max_response_size = 0x1000,
+               .in.level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
+       };
+
+       status = smb2_find_level(tree, tree, &find, &count, &d);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_find_level failed\n");
+
+       smb2_util_close(tree, h);
+
+       torture_assert_int_equal_goto(tctx, count, 3, ret, done, "Bad count\n");
+       torture_assert_str_equal_goto(tctx,
+                                     d[2].id_both_directory_info.name.s,
+                                     "hardlink",
+                                     ret, done, "bad name");
+       torture_assert_u64_equal_goto(tctx,
+                                     d[2].id_both_directory_info.file_id,
+                                     normal_fileid,
+                                     ret, done, "bad fileid\n");
+
+       /*
+        * 3: Query File-Id of snapshot of the file and check the File-Id is
+        * different compared to the basefile
+        */
+
+       create = (struct smb2_create) {
+               .in.desired_access = SEC_FILE_READ_DATA,
+               .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
+               .in.file_attributes = FILE_ATTRIBUTE_NORMAL,
+               .in.create_disposition = NTCREATEX_DISP_OPEN,
+               .in.fname = "subdir/hardlink",
+               .in.query_on_disk_id = true,
+               .in.timewarp = nttime,
+       };
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "test file could not be created\n");
+       smb2_util_close(tree, create.out.file.handle);
+
+       snapshot_fileid = BVAL(&create.out.on_disk_id, 0);
+
+       /*
+        * 4: List directory of the snapshot and check the File-Id returned here
+        * is the same as in 3.
+        */
+
+       create = (struct smb2_create) {
+               .in.desired_access = SEC_DIR_LIST,
+               .in.file_attributes = FILE_ATTRIBUTE_DIRECTORY,
+               .in.create_disposition = NTCREATEX_DISP_OPEN,
+               .in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
+               .in.fname = "subdir",
+               .in.create_options = NTCREATEX_OPTIONS_DIRECTORY,
+               .in.timewarp = nttime,
+       };
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_create\n");
+       h = create.out.file.handle;
+
+       find = (struct smb2_find) {
+               .in.file.handle = h,
+               .in.pattern = "*",
+               .in.max_response_size = 0x1000,
+               .in.level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
+       };
+
+       status = smb2_find_level(tree, tree, &find, &count, &d);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "smb2_find_level failed\n");
+       smb2_util_close(tree, h);
+
+       torture_assert_int_equal_goto(tctx, count, 3, ret, done, "Bad count\n");
+       torture_assert_str_equal_goto(tctx,
+                                     d[2].id_both_directory_info.name.s,
+                                     "hardlink",
+                                     ret, done, "bad name");
+       torture_assert_u64_equal_goto(tctx,
+                                     snapshot_fileid,
+                                     d[2].id_both_directory_info.file_id,
+                                     ret, done, "bad fileid\n");
+
+done:
+       return ret;
+}
+
 static bool test_fileid(struct torture_context *tctx,
                        struct smb2_tree *tree)
 {
@@ -2988,6 +3155,7 @@ struct torture_suite *torture_smb2_twrp_init(TALLOC_CTX *ctx)
        torture_suite_add_1smb2_test(suite, "write", test_twrp_write);
        torture_suite_add_1smb2_test(suite, "stream", test_twrp_stream);
        torture_suite_add_1smb2_test(suite, "openroot", test_twrp_openroot);
+       torture_suite_add_1smb2_test(suite, "listdir", test_twrp_listdir);
 
        suite->description = talloc_strdup(suite, "SMB2-TWRP tests");