Beispiel #1
0
def test_apply_patch_ok_revert_inplace_stange(media_path):
    path = os.path.join(media_path, 'test.bin')
    with open(path, 'wb') as fp:
        fp.write(b'1934')

    fpp = BytesIO(b'''= 1
+ 1 | 9
- 1 | 2
= 2
'''.replace(b'\n', b'\n\n').replace(b' | ', b'\n\n'))

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(
            fp1,
            fpp,
            revert=True,
            headers={
                '1-sha256sum':
                '03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4',
                '2-sha256sum': 'should be ignored',
            })

    with open(path, 'rb') as fp:
        data_actual = fp.read()

    assert data_actual == b'1234'
Beispiel #2
0
def test_apply_patch_issue32228(media_path):
    # https://bugs.python.org/issue32228

    path = os.path.join(media_path, 'wtf.bin')
    with open(path, 'wb') as fp:
        fp.write(b'\x00' * 8192)

    fpp = BytesIO(b'''abindiff 001

- 4097 skip=1

+ 4097

''' + (b'\x01' * 4097) + b'''

= 1

- 4094 skip=1

''')

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(fp1, fpp)  # проверка, что не падает

    with open(path, 'rb') as fp:
        data_actual = fp.read()

    assert data_actual == (b'\x01' * 4097) + b'\x00'
Beispiel #3
0
def test_apply_patch_ok_inplace_stange(media_path):
    path = os.path.join(media_path, 'test.bin')
    with open(path, 'wb') as fp:
        fp.write(b'1234')

    fpp = BytesIO(b'''= 1
+ 1 | 9
- 1 | 2
= 2
'''.replace(b'\n', b'\n\n').replace(b' | ', b'\n\n'))

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(
            fp1,
            fpp,
            headers={
                '1-sha256sum':
                'should be ignored',
                '2-sha256sum':
                '914c948388ae30bdb0179a2f7bc91836d6af004171af735188fc1b69c286c864',
            })

    with open(path, 'rb') as fp:
        data_actual = fp.read()

    assert data_actual == b'1934'
Beispiel #4
0
def test_apply_patch_inplace_empty_fail_hashsum():
    fp1 = BytesIO()
    fpp = BytesIO(
        b'abindiff 001\n'
        b'1-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n'
        b'2-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85f\n'
        b'\n')

    with pytest.raises(ValueError):
        patch.apply_patch_inplace(fp1, fpp, check_hashsum=True)
Beispiel #5
0
def test_apply_patch_inplace_ok_ignore_hashsum_short(f, data_path, media_path):
    path = os.path.join(media_path, os.path.split(f)[-1])
    shutil.copy2(os.path.join(data_path, 'old', f), path)

    with open(os.path.join(data_path, 'patch_short', f + '.abindiff'),
              'rb') as rfpp:
        fpp = BytesIO(rfpp.read().replace(b'1-sha256sum: ', b'1-sha256sum: e'))

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(fp1, fpp, revert=False)
Beispiel #6
0
def test_apply_patch_inplace_empty():
    fp1 = BytesIO()
    fpp = BytesIO(
        b'abindiff 001\n'
        b'1-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n'
        b'2-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n'
        b'\n')

    patch.apply_patch_inplace(fp1, fpp, check_hashsum=True)

    assert fp1.getvalue() == b''
Beispiel #7
0
def test_apply_patch_inplace_fail_hashsum_short(f, data_path, media_path):
    path = os.path.join(media_path, os.path.split(f)[-1])
    shutil.copy2(os.path.join(data_path, 'old', f), path)

    with open(os.path.join(data_path, 'patch_short', f + '.abindiff'),
              'rb') as rfpp:
        fpp = BytesIO(rfpp.read().replace(b'2-sha256sum: ', b'2-sha256sum: e'))

    with open(path, 'r+b') as fp1:
        with pytest.raises(ValueError) as excinfo:
            patch.apply_patch_inplace(fp1, fpp, revert=False)
    assert str(excinfo.value) == 'sha256sum of saved file is not equal'
Beispiel #8
0
def test_apply_patch_inplace_fail_short_revert(f, data_path, media_path):
    path = os.path.join(media_path, os.path.split(f)[-1])
    shutil.copy2(os.path.join(data_path, 'new', f), path)

    with open(os.path.join(data_path, 'patch_short', f + '.abindiff'),
              'rb') as rfpp:
        fpp = BytesIO(rfpp.read())

    with open(path, 'r+b') as fp1:
        with pytest.raises(ValueError) as excinfo:
            patch.apply_patch_inplace(fp1, fpp, revert=True)
    print(str(excinfo.value))
Beispiel #9
0
def test_apply_patch_inplace_ok_full(f, revert, data_path, media_path):
    # inplace тестируем не на BytesIO, а на реальных файлах, так как там проблемы бывают
    path = os.path.join(media_path, os.path.split(f)[-1])
    shutil.copy2(os.path.join(data_path, 'new' if revert else 'old', f), path)

    with open(os.path.join(data_path, 'patch_full', f + '.abindiff'),
              'rb') as rfpp:
        fpp = BytesIO(rfpp.read())

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(fp1, fpp, revert=revert)

    with open(os.path.join(data_path, 'old' if revert else 'new', f)) as fp:
        data_expected = fp.read()
    with open(path) as fp:
        data_actual = fp.read()
    assert data_expected == data_actual
Beispiel #10
0
def test_apply_patch_inplace_ok_ignore_hashsum_full(f, revert, data_path,
                                                    media_path):
    path = os.path.join(media_path, os.path.split(f)[-1])
    shutil.copy2(os.path.join(data_path, 'new' if revert else 'old', f), path)

    with open(os.path.join(data_path, 'patch_full', f + '.abindiff'),
              'rb') as rfpp:
        if revert:
            fpp = BytesIO(rfpp.read().replace(b'2-sha256sum: ',
                                              b'2-sha256sum: e'))
        else:
            fpp = BytesIO(rfpp.read().replace(b'1-sha256sum: ',
                                              b'1-sha256sum: e'))

    # При inplace проверять хэш исходного файла тяжеловато, так что
    # на данный момент он просто игнорируется

    with open(path, 'r+b') as fp1:
        patch.apply_patch_inplace(fp1, fpp, revert=revert)