Example #1
0
def test_file_info():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_read_attr_file.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    assert 'test_read_attr_file.txt' in str(ntfile)

    ntfile.read_attributes()
    assert 'change_time:' in str(ntfile.basic_info)
Example #2
0
def test_uid():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_uid.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    assert ntfile.uid > 0

    ntfile2 = NTFile(os.path.join(work_dir, 'non_existing.txt'))

    with pytest.raises(NTException):
        print(ntfile2.uid)
Example #3
0
def test_read_attributes():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_read_attr_file.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    # On appveyor calling as_datetime fails because
    # ntfile.basic_info.change_time is equal to 0. Ignore this until
    # we have a real reproducer
    assert datetime.now() - \
        ntfile.basic_info.change_time.as_datetime < \
        timedelta(seconds=10)
Example #4
0
def test_uid():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_uid.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    assert ntfile.uid > 0

    ntfile2 = NTFile(os.path.join(work_dir, 'non_existing.txt'))

    with pytest.raises(NTException):
        ntfile2.uid
Example #5
0
def test_read_attributes():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_read_attr_file.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    # On appveyor calling as_datetime fails because
    # ntfile.basic_info.change_time is equal to 0. Ignore this until
    # we have a real reproducer
    assert datetime.now() - \
        ntfile.basic_info.change_time.as_datetime < \
        timedelta(seconds=10)
Example #6
0
def test_unlink():
    work_dir = os.getcwd()

    test_dir_path = os.path.join(work_dir, 'dir')
    deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt')
    mkdir(test_dir_path)

    ntfile = NTFile(test_dir_path)
    ntfile3 = NTFile(test_dir_path)
    ntfile2 = NTFile(deleted_file_path)

    try:
        # delete inexisting file
        ntfile2.unlink()

        # delete file with readonly attribute
        touch(deleted_file_path)
        ntfile2.read_attributes()
        ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY
        ntfile2.write_attributes()
        ntfile2.unlink()

        # delete file already pending deletion
        touch(deleted_file_path)
        ntfile2.open(Access.DELETE, Share.DELETE)
        ntfile2.dispose()
        ntfile2.unlink()

        # delete containing directory
        ntfile.unlink()

        ntfile.close()
        ntfile2.close()

        mkdir(test_dir_path)
        ntfile.open(Access.LIST_DIRECTORY, Share.ALL)
        ntfile3.unlink()

    finally:
        ntfile.close()
        ntfile2.close()
        ntfile2.close()
Example #7
0
def test_unlink():
    work_dir = os.getcwd()

    test_dir_path = os.path.join(work_dir, 'dir')
    deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt')
    mkdir(test_dir_path)

    ntfile = NTFile(test_dir_path)
    ntfile3 = NTFile(test_dir_path)
    ntfile2 = NTFile(deleted_file_path)

    try:
        # delete inexisting file
        ntfile2.unlink()

        # delete file with readonly attribute
        touch(deleted_file_path)
        ntfile2.read_attributes()
        ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY
        ntfile2.write_attributes()
        ntfile2.unlink()

        # delete file already pending deletion
        touch(deleted_file_path)
        ntfile2.open(Access.DELETE, Share.DELETE)
        ntfile2.dispose()
        ntfile2.unlink()

        # delete containing directory
        ntfile.unlink()

        ntfile.close()
        ntfile2.close()

        mkdir(test_dir_path)
        ntfile.open(Access.LIST_DIRECTORY, Share.ALL)
        ntfile3.unlink()

    finally:
        ntfile.close()
        ntfile2.close()
        ntfile2.close()
Example #8
0
def test_write_attributes():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_write_attr_file.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    ntfile.open(Access.READ_ATTRS)
    ntfile.basic_info.change_time = FileTime(
        datetime.now() - timedelta(seconds=3600))
    try:
        with pytest.raises(NTException):
            ntfile.write_attributes()
    finally:
        ntfile.close()
    ntfile.basic_info.change_time = FileTime(
        datetime.now() - timedelta(days=3))
    ntfile.write_attributes()
    assert datetime.now() - \
        ntfile.basic_info.change_time.as_datetime > \
        timedelta(seconds=3000)
Example #9
0
def test_write_attributes():
    work_dir = os.getcwd()

    test_file_path = os.path.join(work_dir, 'test_write_attr_file.txt')
    touch(test_file_path)
    ntfile = NTFile(test_file_path)
    ntfile.read_attributes()
    ntfile.open(Access.READ_ATTRS)
    ntfile.basic_info.change_time = FileTime(
        datetime.now() - timedelta(seconds=3600))
    try:
        with pytest.raises(NTException):
            ntfile.write_attributes()
    finally:
        ntfile.close()
    ntfile.basic_info.change_time = FileTime(
        datetime.now() - timedelta(days=3))
    ntfile.write_attributes()
    assert datetime.now() - \
        ntfile.basic_info.change_time.as_datetime > \
        timedelta(seconds=3000)
Example #10
0
def test_unlink():
    work_dir = os.getcwd()

    test_dir_path = os.path.join(work_dir, 'dir')
    deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt')
    mkdir(test_dir_path)

    ntfile = NTFile(test_dir_path)
    ntfile3 = NTFile(test_dir_path)
    ntfile2 = NTFile(deleted_file_path)

    try:
        # delete inexisting file
        ntfile2.unlink()

        # delete file with readonly attribute
        touch(deleted_file_path)
        ntfile2.read_attributes()
        ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY

        assert 'READONLY' in str(ntfile2.basic_info.file_attributes)
        ntfile2.write_attributes()
        ntfile2.unlink()

        # delete file already pending deletion
        touch(deleted_file_path)
        ntfile2.open(Access.DELETE, Share.DELETE)
        ntfile2.dispose()
        ntfile2.unlink()

        # delete containing directory
        ntfile.unlink()

        ntfile.close()
        ntfile2.close()

        mkdir(test_dir_path)
        ntfile.open(Access.LIST_DIRECTORY, Share.ALL)
        ntfile3.unlink()

    finally:
        ntfile.close()
        ntfile2.close()
        ntfile2.close()

    ntfile = NTFile('nul')
    with pytest.raises(NTException) as err:
        ntfile.unlink()
    ntfile.close()
    assert 'NTFile.read_attributes:' in str(err)

    # A directory that is not empty cannot be deleted
    dir_to_delete = os.path.join(test_dir_path, 'dir_to_delete')
    mkdir(dir_to_delete)
    touch(os.path.join(dir_to_delete, 'afile.txt'))
    ntfile = NTFile(dir_to_delete)
    try:
        with pytest.raises(NTException) as err:
            ntfile.unlink()
    finally:
        ntfile.close()

    # A directory that is already opened and not empty cannot be
    # moved to trash
    dir_to_delete = os.path.join(test_dir_path, 'dir_to_delete')
    mkdir(dir_to_delete)
    touch(os.path.join(dir_to_delete, 'afile.txt'))

    ntfile = NTFile(dir_to_delete)
    ntfile2 = NTFile(dir_to_delete)
    try:
        ntfile.open(Access.LIST_DIRECTORY, Share.ALL)
        with pytest.raises(NTException) as err:
            ntfile2.unlink()
    finally:
        ntfile.close()
        ntfile2.close()

    # Try to delete a file that we cannot open
    ntfile = NTFile(deleted_file_path)
    ntfile2 = NTFile(deleted_file_path)
    try:
        touch(deleted_file_path)
        ntfile.open(Access.READ_DATA, Share.NOTHING)
        with pytest.raises(NTException) as err:
            ntfile2.unlink()
    finally:
        ntfile.close()
        ntfile2.close()