How else can I pass params into an smbtorture test?

Richard Sharpe realrichardsharpe at gmail.com
Tue Oct 31 17:39:57 UTC 2023


Hi folks,

I have written an smbtorture test to check Windows Previous versions
behavior.

I used environment variables to pass in the file on the share to access. Is
there a way to do this via normal smbtorture parameters?

The following code tries to do what Windows does when accessing previous
versions:

 /*
+ * Test the GET_SHADOW_COPY code. This tests the performance and checks if
+ * there are duplicate labels and that we can open the files referred to.
+ */
+#define GMT_FMT "@GMT-%Y.%m.%d-%H.%M.%S"
+
+static bool test_get_shadow_copy_1(struct torture_context *tctx,
+                                  struct smb2_tree *tree)
+{
+       bool ret = true;
+       char *file_name = getenv("TORTURE_PDFS_WPV_FILE");
+       NTSTATUS status;
+       struct smb2_handle h;
+       struct smb2_create first;
+       union smb_ioctl ioctl;
+       uint32_t num_labels = 0;
+       uint32_t num_returned = 0;
+       const uint8_t *gmt_str;
+       int i;
+       NTTIME *version_ctimes = NULL;
+
+       if (file_name == NULL) {
+               file_name = "torture_pdfs_wpv_file.txt";
+       }
+
+       ZERO_STRUCT(first);
+       first.in.fname              = file_name;
+       first.in.desired_access     = SEC_FILE_READ_DATA;
+       first.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       first.in.file_attributes    = 0;
+       first.in.share_access       = NTCREATEX_SHARE_ACCESS_READ |
+                                     NTCREATEX_SHARE_ACCESS_WRITE;
+       first.in.create_options     = 0;
+
+       status = smb2_create(tree, tctx, &first);
+       torture_assert_ntstatus_ok(tctx, status,
+                                  talloc_asprintf(tctx, "Opening file %s "
+                                                        "failed (%s)\n",
+                                                        file_name,
+
 nt_errstr(status)));
+       h = first.out.file.handle;
+
+       ZERO_STRUCT(ioctl);
+
+       /* First, get the number of snaps */
+       ioctl.smb2.level = RAW_IOCTL_SMB2;
+       ioctl.smb2.in.file.handle = h;
+       ioctl.smb2.in.function = FSCTL_SRV_ENUM_SNAPS;
+       ioctl.smb2.in.max_response_size = 16;
+       ioctl.smb2.in.flags = SMB2_IOCTL_FLAG_IS_FSCTL;
+
+       status = smb2_ioctl(tree, tree, &ioctl.smb2);
+       torture_assert_ntstatus_ok(tctx, status,
+                                  "GET_SHADOW_DATA for #labels failed\n");
+
+       dump_data(1, (const uint8_t *)ioctl.smb2.out.out.data, 16);
+
+       /* Ask again with the right size this time */
+       num_labels = *(uint32_t *)ioctl.smb2.out.out.data;
+
+       ioctl.smb2.in.max_response_size = 16 + (num_labels * 50);
+
+       status = smb2_ioctl(tree, tree, &ioctl.smb2);
+       torture_assert_ntstatus_ok(tctx, status,
+                                  "GET_SHADOW_DATA for the labels
failed\n");
+
+       dump_data(1, (const uint8_t *)ioctl.smb2.out.out.data,
+                    16 + (num_labels * 50));
+
+       num_returned = ((uint32_t *)ioctl.smb2.out.out.data)[1];
+
+       torture_assert(tctx, (num_labels == num_returned),
+                      talloc_asprintf(tctx,
+                                      "num_labels (%u) !=
num_returned(%u)\n",
+                                      num_labels, num_returned));
+
+       gmt_str = &((const uint8_t *)ioctl.smb2.out.out.data)[12];
+
+       version_ctimes = talloc_array(tctx, NTTIME, num_returned);
+       torture_assert(tctx, (version_ctimes != NULL),
+                      "Unable to allocate space for version_ctimes");
+
+       for (i = 0; i < num_returned; i++) {
+               char *str, *match;
+               bool converted = false;
+               size_t converted_size = 0;
+               struct tm tm;
+               struct timeval timeval;
+               NTTIME nttime;
+               struct smb2_create io;
+
+               converted = convert_string_talloc(tctx, CH_UTF16LE, CH_UNIX,
+                                                 gmt_str, 50,
+                                                 &str, &converted_size);
+               torture_assert(tctx, (converted == true),
+                              talloc_asprintf(tctx,
+                                              "Failed to convert label
%d\n",
+                                              i));
+
+               DEBUG(1, ("Label %i is \"%s\"\n", i, str));
+
+               match = strptime(str, GMT_FMT, &tm);
+               torture_assert(tctx, (*match == 0),
+                                     "Failed to convert label to time\n");
+
+               timeval.tv_sec = mktime(&tm);
+               timeval.tv_usec = 0;
+
+               nttime = timeval_to_nttime(&timeval);
+
+               ZERO_STRUCT(io);
+               io.in.desired_access         = SEC_FILE_READ_DATA;
+               io.in.file_attributes        = FILE_ATTRIBUTE_NORMAL;
+               io.in.create_disposition     = NTCREATEX_DISP_OPEN_IF;
+               io.in.share_access           = NTCREATEX_SHARE_ACCESS_READ |
+                                              NTCREATEX_SHARE_ACCESS_WRITE;
+               io.in.timewarp               = nttime;
+
+                io.in.fname = file_name;
+
+               status = smb2_create(tree, tctx, &io);
+               CHECK_STATUS(status, NT_STATUS_OK);
+
+               torture_assert(tctx,
+                              (first.out.change_time !=
io.out.change_time),
+                              talloc_asprintf(tctx, "change_time for
version "
+                                              "%i same as original file\n",
+                                              i));
+
+               version_ctimes[i] = io.out.change_time;
+
+               gmt_str += 50;
+       }
+
+       /* We don't need to do all these comparisons ... */
+       for (i = 0; i < num_returned; i++) {
+               int j;
+
+               for (j = 0; j < num_returned; j++) {
+                       if (i != j) {
+                               torture_assert(tctx, (version_ctimes[i] !=
+                                                     version_ctimes[j]),
+                                              talloc_asprintf(tctx,
+                                                              "ctime[%d]
== "
+
 "ctime[%d]\n", i,
+                                                              j));
+                       }
+               }
+       }
+
+       return ret;
+}

-- 
Regards,
Richard Sharpe
(何以解憂?唯有杜康。--曹操)(传说杜康是酒的发明者)


More information about the samba-technical mailing list