def test_unwriteable(self):
     file = self._mock_test_file()
     storage = self._mock_storage()
     storage.save(file, 'abcdefghijklmnopqrstuvwxyz')
     Files.chmod(file, 0o444)
     try:
         storage.save(file, 'abcdefghijklmnopqrstuvwxyz')  # fail
         assert False, 'Accessing unreadable!'
     except UploadException as ex:
         assert 'CANNOT WRITE DRIVEFILE' == ex.get_message()
     finally:
         Files.chmod(file, 0o666)
         storage.remove(self._mock_test_file())
 def test_unwriteable_seek(self):
     file = self._mock_test_file()
     storage = self._mock_storage()
     storage.add_part(file, b'abcdefghijklmnopqrstuvwxyz', 0)
     Files.chmod(file, 0o444)
     try:
         storage.add_part(file, b'abcdefghijklmnopqrstuvwxyz', 26)  # fail
         assert False, 'Writing to non-available seek in file!'
     except UploadException as ex:
         assert 'CANNOT WRITE FILE' == ex.get_message()
     finally:
         Files.chmod(file, 0o666)
         storage.remove(self._mock_test_file())
Example #3
0
    def test_cancel(self):
        from kw_upload.responses import InitResponse, UploadResponse, CancelResponse
        lib = UploaderMock()  # must stay same, because it's only in the ram
        content = Files.file_get_contents(self._get_test_file(),
                                          8000)  # read test content into ram
        max_size = len(content)

        # step 1 - init driver
        result1 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result1.get_result()['status']
        shared_key = result1.get_result()[
            'sharedKey']  # for this test it's zero care

        # step 2 - send data
        result2 = lib.upload(shared_key,
                             bytes(content, encoding='utf-8'))  # flush it all
        assert UploadResponse.STATUS_OK == result2.get_result()['status']

        # step 3 - cancel upload
        target = lib.get_lib_driver().read(shared_key).temp_location
        result3 = lib.cancel(shared_key)
        assert CancelResponse.STATUS_OK == result3.get_result()['status']

        # check content
        assert not lib.get_storage().get_all(target)
Example #4
0
    def test_simple_upload(self):
        from kw_upload.responses import InitResponse, UploadResponse, DoneResponse
        lib = UploaderMock()  # must stay same, because it's only in the ram
        content = Files.file_get_contents(self._get_test_file(),
                                          8000)  # read test content into ram
        max_size = len(content)

        # step 1 - init driver
        result1 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result1.get_result()['status']
        bytes_per_part = result1.get_result()['partSize']
        shared_key = result1.get_result()[
            'sharedKey']  # for this test it's zero care
        assert 1024 == bytes_per_part

        # step 2 - send data
        i = 0
        while True:
            if i * bytes_per_part > max_size:
                break
            part = Strings.substr(content, i * bytes_per_part, bytes_per_part)
            result2 = lib.upload(shared_key, bytes(part, encoding='utf-8'))
            assert UploadResponse.STATUS_OK == result2.get_result()['status']
            i = i + 1

        # step 3 - close upload
        target = lib.get_lib_driver().read(shared_key).temp_location
        result3 = lib.done(shared_key)
        assert DoneResponse.STATUS_OK == result3.get_result()['status']

        # check content
        uploaded = lib.get_storage().get_all(target)
        assert 0 < len(uploaded)
        assert content == uploaded.decode()
 def test_thru(self):
     file = self._mock_test_file()
     storage = self._mock_storage()
     storage.add_part(file, b'abcdefghijklmnopqrstuvwxyz')
     assert b'abcdefghijklmnopqrstuvwxyz' == storage.get_part(file, 0)
     storage.truncate(file, 16)
     assert b'abcdefghijklmnop' == storage.get_part(file, 0)
     storage.remove(file)
     assert not Files.is_file(file)
Example #6
0
 def test_simple_all(self):
     cont = Files.file_get_contents(self._get_test_file(), 80)
     pack = self._mock_data()
     pack.file_size = 80
     pack.bytes_per_part = 10
     pack.last_known_part = 7
     pack.parts_count = 8
     self._processor.init(pack, self._mock_shared_key())
     self._processor.upload(self._mock_shared_key(),
                            bytes(cont, encoding='utf-8'))
     data = self._processor.done(self._mock_shared_key())
     assert 8 == data.last_known_part
     assert 8 == data.parts_count
     self._clear()
Example #7
0
    def test_truncate_fail(self):
        from kw_upload.responses import InitResponse, UploadResponse, CancelResponse
        lib = UploaderMock()  # must stay same, because it's only in the ram
        content = Files.file_get_contents(self._get_test_file(),
                                          8000)  # read test content into ram
        max_size = len(content)

        # step 1 - init driver
        result1 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result1.get_result()['status']
        shared_key = result1.get_result()[
            'sharedKey']  # for this test it's zero care

        # step 2 - truncate data - non existing segment
        result2 = lib.truncate_from(shared_key, 35)
        assert UploadResponse.STATUS_FAIL == result2.get_result()['status']

        # step 3 - cancel upload
        result3 = lib.cancel(shared_key)
        assert CancelResponse.STATUS_OK == result3.get_result()['status']
Example #8
0
    def test_upload_fail(self):
        from kw_upload.responses import InitResponse, UploadResponse, CancelResponse
        lib = UploaderMock()  # must stay same, because it's only in the ram
        content = Files.file_get_contents(self._get_test_file(),
                                          8000)  # read test content into ram
        max_size = len(content)

        # step 1 - init driver
        result1 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result1.get_result()['status']
        shared_key = result1.get_result()[
            'sharedKey']  # for this test it's zero care

        # step 2 - upload data - not continuous
        result2 = lib.upload(
            shared_key,
            bytes(Strings.substr(content, 23, 47568), encoding='utf-8'), 66)
        assert UploadResponse.STATUS_FAIL == result2.get_result()['status']

        # step 3 - cancel upload
        result3 = lib.cancel(shared_key)
        assert CancelResponse.STATUS_OK == result3.get_result()['status']
Example #9
0
    def test_simple_thru(self):
        from hashlib import md5
        cont = Files.file_get_contents(self._get_test_file(), 80)
        pack = self._mock_data()
        pack.file_size = 80
        pack.bytes_per_part = 10
        pack.last_known_part = 4
        pack.parts_count = 8
        data = self._processor.init(pack, self._mock_shared_key())
        data_cont = Strings.substr(cont, 0, 30) + 'asdfghjklyxcvbnmqwer'
        # set problematic content
        self._processor.upload(self._mock_shared_key(),
                               bytes(data_cont, encoding='utf-8'))
        # now checks
        for i in range(0, data.last_known_part + 1):
            remote_md5 = self._processor.check(self._mock_shared_key(), i)
            local_str = Strings.substr(cont, i * data.bytes_per_part,
                                       data.bytes_per_part)
            local_md5 = md5(local_str.encode()).hexdigest()
            if remote_md5 != local_md5:
                self._processor.truncate_from(self._mock_shared_key(), i)
                break

        data2 = self._drive_file.read(self._mock_shared_key())
        assert 3 == data2.last_known_part

        # set rest
        for i in range(data2.last_known_part + 1, data2.parts_count):
            data_pack = Strings.substr(str(cont), i * data2.last_known_part,
                                       data2.bytes_per_part)
            self._processor.upload(self._mock_shared_key(),
                                   bytes(data_pack, encoding='utf-8'))

        self._processor.cancel(self._mock_shared_key(
        ))  # intended, because pass will be checked in upload itself
        self._clear()
Example #10
0
    def test_stopped_upload(self):
        from kw_upload.responses import InitResponse, UploadResponse, DoneResponse
        import hashlib
        import math
        # from pprint import pprint

        lib = UploaderMock()  # must stay same, because it's only in the ram
        content = Files.file_get_contents(self._get_test_file(),
                                          900000)  # read test content into ram
        max_size = len(content)

        # step 1 - init driver
        result1 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result1.get_result()['status']
        bytes_per_part = result1.get_result()['partSize']
        shared_key = result1.get_result()[
            'sharedKey']  # for this test it's zero care
        assert 1024 == bytes_per_part
        assert 629 == result1.get_result()['totalParts']

        # step 2 - send first part of data
        i = 0
        limited = math.floor(max_size / 2)
        while True:
            if i * bytes_per_part > limited:
                break
            part = Strings.substr(content, i * bytes_per_part, bytes_per_part)
            result2 = lib.upload(shared_key, bytes(part, encoding='utf-8'))
            assert UploadResponse.STATUS_OK == result2.get_result()['status']
            i = i + 1

        # step 3 - again from the beginning
        result3 = lib.init(self._get_test_dir(), 'lorem-ipsum.txt', max_size)
        assert InitResponse.STATUS_OK == result3.get_result()['status']
        bytes_per_part = result3.get_result()['partSize']
        last_known_part = result3.get_result()['lastKnownPart']
        shared_key = result3.get_result()[
            'sharedKey']  # for this test it's zero care
        assert 315 == last_known_part  # NOT ZERO
        assert 1024 == bytes_per_part

        # step 4 - check first part
        i = 0
        while True:
            if i > last_known_part:
                break
            part = Strings.substr(content, i * bytes_per_part, bytes_per_part)
            result4 = lib.check(shared_key, i)
            assert UploadResponse.STATUS_OK == result4.get_result()['status']
            if hashlib.md5(
                    part.encode('utf-8')) != result4.get_result()['checksum']:
                # step 5 - truncate of failed part
                result5 = lib.truncate_from(shared_key, i - 2)
                assert UploadResponse.STATUS_OK == result5.get_result(
                )['status']
                break
            else:
                assert hashlib.md5(
                    part.encode('utf-8')) == result4.get_result()['checksum']
            i = i + 1

        last_known_part = result5.get_result()['lastKnownPart']
        assert 313 == last_known_part

        # step 6 - send second part
        i = last_known_part
        while True:
            if i * bytes_per_part > max_size:
                break
            part = Strings.substr(content, i * bytes_per_part, bytes_per_part)
            result6 = lib.upload(shared_key, bytes(part, encoding='utf-8'))
            assert UploadResponse.STATUS_OK == result6.get_result()['status']
            i = i + 1

        # step 7 - close upload
        target = lib.get_lib_driver().read(shared_key).temp_location
        result7 = lib.done(shared_key)
        assert DoneResponse.STATUS_OK == result7.get_result()['status']

        # check content
        uploaded = lib.get_storage().get_all(target)
        assert 0 < len(uploaded)
        assert content == uploaded.decode()
 def tearDown(self):
     if Files.is_file(self._mock_test_file()):
         self._mock_storage().remove(self._mock_test_file())
     super().tearDown()
 def tearDown(self):
     if Files.is_file(self._mock_test_file()):
         Files.unlink(self._mock_test_file())
     if Dirs.is_dir(self._mock_test_file()):
         Dirs.rmdir(self._mock_test_file())
     super().tearDown()
 def tearDown(self):
     if Files.is_file(self._mock_test_file()):
         lib = Volume(Translations())
         lib.remove(self._mock_test_file())
     super().tearDown()