Beispiel #1
0
 def __init__(self, methodName='runTest'):
     super().__init__(methodName)
     _lang = Translations()
     self._info_storage = InfoRam(_lang)
     self._data_storage = DataRam(_lang)
     target = TargetSearch(_lang, self._info_storage, self._data_storage)
     self._drive_file = DriveFile(_lang, self._info_storage, Json(),
                                  Key(_lang, target))
     self._processor = Processor(_lang, self._drive_file,
                                 self._data_storage, Hashed())
 def test_shared_fail(self):
     lang = Translations()
     lib = Random(lang, TargetSearch(lang, InfoRam(lang), DataRam(lang)))
     try:
         lib.get_shared_key()  # no key set!
         assert False, 'Got empty shared key'
     except UploadException as ex:
         assert 'SHARED KEY IS EMPTY' == ex.get_message()
 def test_fail_no_remote(self):
     lang = Translations()
     lib = TargetSearch(lang, InfoRam(lang), DataRam(lang))
     try:
         lib.process()
         assert False, 'No remote and passed'
     except UploadException as ex:
         assert 'SENT FILE NAME IS EMPTY' == ex.get_message()
 def test_init_fail(self):
     lang = Translations()
     target = TargetSearch(lang, InfoRam(lang), DataRam(lang))
     try:
         Factory.get_variant(lang, target, 0)
         assert False, 'Use unknown variant'
     except UploadException as ex:
         assert 'KEY VARIANT NOT SET' == ex.get_message()
 def test_process_name_lookup(self):
     lang = Translations()
     data_ram = DataRam(lang)
     data_ram.add_part(self._get_test_dir() + 'dummyFile.tst',
                       'asdfghjklqwertzuiopyxcvbnm')
     data_ram.add_part(self._get_test_dir() + 'dummyFile.0.tst',
                       'asdfghjklqwertzuiopyxcvbnm')
     data_ram.add_part(self._get_test_dir() + 'dummyFile.1.tst',
                       'asdfghjklqwertzuiopyxcvbnm')
     data_ram.add_part(self._get_test_dir() + 'dummyFile.2.tst',
                       'asdfghjklqwertzuiopyxcvbnm')
     lib = TargetSearch(lang, InfoRam(lang), data_ram, False, False)
     lib.set_target_dir(self._get_test_dir()).set_remote_file_name(
         'dummyFile.tst').process()
     assert self._get_test_dir(
     ) + 'dummyFile.3.tst' + TargetSearch.FILE_UPLOAD_SUFF == lib.get_temporary_target_location(
     )
 def test_fail_no_base(self):
     lang = Translations()
     lib = TargetSearch(lang, InfoRam(lang), DataRam(lang))
     try:
         lib.get_final_target_name()
         assert False, 'No final target name and passed'
     except UploadException as ex:
         assert 'UPLOAD FILE NAME IS EMPTY' == ex.get_message()
 def test_fail_no_target(self):
     lang = Translations()
     lib = TargetSearch(lang, InfoRam(lang), DataRam(lang))
     lib.set_remote_file_name('abcdefg')
     try:
         lib.process()
         assert False, 'No target and passed'
     except UploadException as ex:
         assert 'TARGET DIR IS NOT SET' == ex.get_message()
    def test_redis(self):
        import hashlib
        lang = Translations()
        target = TargetSearch(lang, InfoRam(lang), DataRam(lang))
        target.set_remote_file_name('poiuztrewq').set_target_dir(self._get_test_dir()).process()
        lib = Redis(lang, target)
        lib.generate_keys()

        key1 = 'poiuztrewq'
        key2 = '/tmp/lkjhg'
        assert hashlib.md5(key1.encode("utf-8")).hexdigest() == lib.get_shared_key()
        assert Redis.PREFIX + key2 == lib.from_shared_key(key2)
 def test_process_no_clear(self):
     lang = Translations()
     lib = TargetSearch(lang, InfoRam(lang), DataRam(lang), False, False)
     lib.set_target_dir(self._get_test_dir()).set_remote_file_name(
         'what el$e can be found').process()
     assert 'what el$e can be found' == lib.get_final_target_name()
     assert self._get_test_dir(
     ) + 'what el$e can be found' + TargetSearch.FILE_DRIVER_SUFF == lib.get_driver_location(
     )
     assert self._get_test_dir(
     ) + 'what el$e can be found' + TargetSearch.FILE_UPLOAD_SUFF == lib.get_temporary_target_location(
     )
    def test_volume(self):
        import base64
        lang = Translations()
        target = TargetSearch(lang, InfoRam(lang), DataRam(lang))
        target.set_remote_file_name('poiuztrewq').set_target_dir('/tmp/').process()
        lib = SimpleVolume(lang, target)
        lib.generate_keys()

        key1 = '/tmp/poiuztrewq' + TargetSearch.FILE_DRIVER_SUFF
        key2 = '/tmp/lkjhg'
        assert base64.encodebytes(key1.encode("utf-8")).decode("utf-8").strip() == lib.get_shared_key()
        assert key2 == lib.from_shared_key(base64.encodebytes(key2.encode("utf-8")).decode("utf-8").strip())
        try:
            lib.from_shared_key('**' + key2)  # aaand failed... - chars outside the b64
            assert False, 'Decode some unknown chars'
        except UploadException as ex:
            assert 'SHARED KEY IS INVALID' == ex.get_message()
Beispiel #11
0
 def _get_drive_file(self) -> DriveFile:
     lang = Translations()
     storage = InfoRam(lang)
     target = TargetSearch(lang, storage, DataRam(lang))
     return DriveFile(lang, storage, Json(), Key(lang, target))
Beispiel #12
0
 def _get_data_storage(self, lang: Translations) -> DataStorage:
     super()._get_data_storage(lang)
     return DataRam(lang)
Beispiel #13
0
class ProcessorTest(CommonTestClass):
    def __init__(self, methodName='runTest'):
        super().__init__(methodName)
        _lang = Translations()
        self._info_storage = InfoRam(_lang)
        self._data_storage = DataRam(_lang)
        target = TargetSearch(_lang, self._info_storage, self._data_storage)
        self._drive_file = DriveFile(_lang, self._info_storage, Json(),
                                     Key(_lang, target))
        self._processor = Processor(_lang, self._drive_file,
                                    self._data_storage, Hashed())

    def tearDown(self):
        if self._drive_file.exists(self._mock_key()):
            self._drive_file.remove(self._mock_key())
        super().tearDown()

    def test_init(self):
        pack = self._mock_data()
        pack.last_known_part = 5
        data = self._processor.init(pack, self._mock_shared_key())

        assert isinstance(data, DataPack)
        assert 'abcdef' == data.file_name
        assert self._get_test_dir() + 'abcdef' == data.temp_location
        assert 123456 == data.file_size
        assert 12 == data.parts_count
        assert 64 == data.bytes_per_part
        assert 5 == data.last_known_part

        data2 = self._processor.done(self._mock_key())
        assert isinstance(data2, DataPack)
        assert 'abcdef' == data.file_name
        assert self._get_test_dir() + 'abcdef' == data.temp_location
        assert 123456 == data.file_size
        assert 12 == data.parts_count
        assert 64 == data.bytes_per_part
        assert 5 == data.last_known_part
        self._clear()

    def test_init_fail(self):
        pack = self._mock_data()
        pack.last_known_part = 4
        data = self._processor.init(pack, self._mock_shared_key())

        assert isinstance(data, DataPack)
        assert 4 == data.last_known_part

        pack.last_known_part = 8
        data2 = self._processor.init(pack, self._mock_shared_key())
        assert 4 == data2.last_known_part
        assert 8 != data2.last_known_part
        self._clear()

    def test_upload_early(self):
        pack = self._mock_data()
        pack.file_size = 80
        pack.bytes_per_part = 10
        pack.last_known_part = 4
        pack.parts_count = 8
        self._processor.init(pack, self._mock_shared_key())
        data_cont = b'asdfghjklyxcvbnmqwertzuiop1234567890'
        self._processor.upload(self._mock_shared_key(), data_cont,
                               5)  # pass, last is 4, wanted 5
        try:
            self._processor.upload(self._mock_shared_key(), data_cont,
                                   7)  # fail, last is 5, wanted 6
            assert False, 'Early reading processed'
        except UploadException as ex:
            assert 'READ TOO EARLY' == ex.get_message()
        self._clear()

    def test_check_segment_sub_zero(self):
        pack = self._mock_data()
        pack.file_size = 80
        pack.bytes_per_part = 10
        pack.last_known_part = 4
        pack.parts_count = 8
        self._processor.init(pack, self._mock_shared_key())
        try:
            self._processor.check(self._mock_shared_key(),
                                  -5)  # fail, sub zero
            assert False, 'Sub zero wins'
        except UploadException as ex:
            assert 'SEGMENT OUT OF BOUNDS' == ex.get_message()
        self._clear()

    def test_check_segment_available_parts(self):
        pack = self._mock_data()
        pack.file_size = 80
        pack.bytes_per_part = 10
        pack.last_known_part = 4
        pack.parts_count = 8
        self._processor.init(pack, self._mock_shared_key())
        try:
            self._processor.check(self._mock_shared_key(),
                                  10)  # fail, out of size
            assert False, 'Overweight segment'
        except UploadException as ex:
            assert 'SEGMENT OUT OF BOUNDS' == ex.get_message()
        self._clear()

    def test_check_segment_not_uploaded(self):
        pack = self._mock_data()
        pack.file_size = 80
        pack.bytes_per_part = 10
        pack.last_known_part = 4
        pack.parts_count = 8
        self._processor.init(pack, self._mock_shared_key())
        try:
            self._processor.check(self._mock_shared_key(),
                                  6)  # fail, outside upload
            assert False, 'Outside upload'
        except UploadException as ex:
            assert 'SEGMENT NOT UPLOADED YET' == ex.get_message()
        self._clear()

    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()

    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()

    def _mock_key(self) -> str:
        return 'fghjkl' + TargetSearch.FILE_DRIVER_SUFF

    def _clear(self):
        self._data_storage.remove(self._mock_shared_key())
        self._info_storage.remove(self._mock_shared_key())
    def test_random(self):
        assert 'aaaaaaa' == Random.generate_random_text(7, ['a','a','a','a'])

        lang = Translations()
        lib = Random(lang, TargetSearch(lang, InfoRam(lang), DataRam(lang)))
        assert 'abcdefghi' + TargetSearch.FILE_DRIVER_SUFF == lib.from_shared_key('abcdefghi')
 def test_init(self):
     lang = Translations()
     target = TargetSearch(lang, InfoRam(lang), DataRam(lang))
     assert isinstance(Factory.get_variant(lang, target, Factory.VARIANT_VOLUME), SimpleVolume)
     assert isinstance(Factory.get_variant(lang, target, Factory.VARIANT_RANDOM), Random)
     assert isinstance(Factory.get_variant(lang, target, Factory.VARIANT_REDIS), Redis)