|
| 1 | +import pytest |
| 2 | +import random |
| 3 | + |
| 4 | +from dataclasses import asdict |
| 5 | +from middlewared.test.integration.assets.account import user, group |
| 6 | +from middlewared.test.integration.assets.pool import dataset |
| 7 | + |
| 8 | +from protocols import smb_connection |
| 9 | +from protocols.SMB import ( |
| 10 | + FsctlQueryFileRegionsRequest, |
| 11 | + FileRegionInfo, |
| 12 | + FileUsage, |
| 13 | +) |
| 14 | + |
| 15 | +from samba import ntstatus |
| 16 | +from samba import NTSTATUSError |
| 17 | + |
| 18 | +SHARE_NAME = 'ioctl_share' |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope='module') |
| 22 | +def setup_smb_tests(request): |
| 23 | + with dataset('smbclient-testing', data={'share_type': 'SMB'}) as ds: |
| 24 | + with user({ |
| 25 | + 'username': 'smbuser', |
| 26 | + 'full_name': 'smbuser', |
| 27 | + 'group_create': True, |
| 28 | + 'password': 'Abcd1234' |
| 29 | + }) as u: |
| 30 | + with smb_share(os.path.join('/mnt', ds), SHARE_NAME) as s: |
| 31 | + try: |
| 32 | + call('service.start', 'cifs') |
| 33 | + yield {'dataset': ds, 'share': s, 'user': u} |
| 34 | + finally: |
| 35 | + call('service.stop', 'cifs') |
| 36 | + |
| 37 | + |
| 38 | +def test__query_file_regions_normal(setup_smb_tests): |
| 39 | + ds, share, smb_user = setup_smb_tests |
| 40 | + with smb_connection( |
| 41 | + share=SHARE_NAME, |
| 42 | + username=smbuser['username'], |
| 43 | + password='Abcd1234', |
| 44 | + smb1=False |
| 45 | + ) as c: |
| 46 | + fd = c.create_file("file_regions_normal", "w") |
| 47 | + buf = random.randbytes(1024) |
| 48 | + |
| 49 | + for offset in range(0, 128): |
| 50 | + c.write(fd, offset=offset * 1024, data=buf) |
| 51 | + |
| 52 | + # First get with region omitted. This should return entire file |
| 53 | + fsctl_request_null_region = FsctlQueryFileRegionsRequest(region=None) |
| 54 | + fsctl_resp = c.fsctl(fd, fsctl_request_null_region) |
| 55 | + |
| 56 | + assert fsctl_resp.flags == 0 |
| 57 | + assert fsctl_resp.total_region_entry_count == 1 |
| 58 | + assert fsctl_resp.region_entry_count == 1 |
| 59 | + assert fsctl_resp.reserved == 1 |
| 60 | + assert fsctl_resp.region is not None |
| 61 | + |
| 62 | + assert fsctl_resp.region.offset == 0 |
| 63 | + assert fsctl_resp.region.length == 128 * 1024 |
| 64 | + assert fsctl_resp.region.desired_usage == FileUsage.VALID_CACHED_DATA |
| 65 | + assert fsctl_resp.region.reserved == 0 |
| 66 | + |
| 67 | + # Take same region we retrieved from server and use with new request |
| 68 | + fsctl_request_with_region = FsctlQueryFileRegionsRequest(region=fsctl_resp.region) |
| 69 | + fsctl_resp2 = c.fsctl(fd, fsctl_request_with_region) |
| 70 | + |
| 71 | + assert asdict(fsctl_resp) == asdict(fsctl_resp2) |
| 72 | + |
| 73 | + |
| 74 | +def test__query_file_regions_with_holes(setup_smb_tests): |
| 75 | + ds, share, smb_user = setup_smb_tests |
| 76 | + with smb_connection( |
| 77 | + share=SHARE_NAME, |
| 78 | + username=smbuser['username'], |
| 79 | + password='Abcd1234', |
| 80 | + smb1=False |
| 81 | + ) as c: |
| 82 | + fd = c.create_file("file_regions_normal", "w") |
| 83 | + buf = random.randbytes(4096) |
| 84 | + |
| 85 | + # insert some holes in file |
| 86 | + for offset in range(0, 130): |
| 87 | + if offset % 2 == 0: |
| 88 | + c.write(fd, offset=offset * 4096, data=buf) |
| 89 | + |
| 90 | + fsctl_request_null_region = FsctlQueryFileRegionsRequest(region=None) |
| 91 | + fsctl_resp = c.fsctl(fd, fsctl_request_null_region) |
| 92 | + |
| 93 | + assert fsctl_resp.flags == 0 |
| 94 | + assert fsctl_resp.total_region_entry_count == 1 |
| 95 | + assert fsctl_resp.region_entry_count == 1 |
| 96 | + assert fsctl_resp.reserved == 1 |
| 97 | + assert fsctl_resp.region is not None |
| 98 | + |
| 99 | + assert fsctl_resp.region.offset == 0 |
| 100 | + assert fsctl_resp.region.length == 128 * 4096 |
| 101 | + assert fsctl_resp.region.desired_usage == FileUsage.VALID_CACHED_DATA |
| 102 | + assert fsctl_resp.region.reserved == 0 |
| 103 | + |
| 104 | + # Take same region we retrieved from server and use with new request |
| 105 | + fsctl_request_with_region = FsctlQueryFileRegionsRequest(region=fsctl_resp.region) |
| 106 | + fsctl_resp2 = c.fsctl(fd, fsctl_request_with_region) |
| 107 | + |
| 108 | + assert asdict(fsctl_resp) == asdict(fsctl_resp2) |
| 109 | + |
| 110 | + |
| 111 | +def test__query_file_regions_trailing_zeroes(setup_smb_tests): |
| 112 | + """ |
| 113 | + FileRegionInfo should contain Valid Data Length which is length in bytes of data |
| 114 | + that has been written to the file in the specified region, from the beginning of |
| 115 | + the region untile the last byte that has not been zeroed or uninitialized |
| 116 | + """ |
| 117 | + ds, share, smb_user = setup_smb_tests |
| 118 | + with smb_connection( |
| 119 | + share=SHARE_NAME, |
| 120 | + username=smbuser['username'], |
| 121 | + password='Abcd1234', |
| 122 | + smb1=False |
| 123 | + ) as c: |
| 124 | + fd = c.create_file("file_regions_normal", "w") |
| 125 | + buf = random.randbytes(4096) |
| 126 | + |
| 127 | + # insert a hole in file |
| 128 | + c.write(fd, offset=0, data=buf) |
| 129 | + c.write(fd, offset=8192, data=buf) |
| 130 | + |
| 131 | + # requesting entire file should give full length |
| 132 | + fsctl_request_null_region = FsctlQueryFileRegionsRequest(region=None) |
| 133 | + fsctl_resp = c.fsctl(fd, fsctl_request_null_region) |
| 134 | + assert fsctl_resp.region.length == 12288 |
| 135 | + |
| 136 | + # requesting region that has hole at end of it should only give data length |
| 137 | + limited_region = FileRegionInfo(offset=0, length=8192) |
| 138 | + fsctl_request_limited_region = FsctlQueryFileRegionsRequest(region=limited_region) |
| 139 | + fsctl_resp = c.fsctl(fd, fsctl_request_limited_region) |
| 140 | + assert fsctl_resp.region.offset == 0 |
| 141 | + assert fsctl_resp.region.length == 4096 |
0 commit comments