Beispiel #1
0
def test_apply_patch_ok_revert_stange(media_path):
    path = os.path.join(media_path, 'test.bin')

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

    with open(path, 'wb') as fp2:
        patch.apply_patch(
            BytesIO(b'1934'),
            fp2,
            fpp,
            revert=True,
            headers={
                '1-sha256sum':
                '03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4',
                '2-sha256sum':
                '914c948388ae30bdb0179a2f7bc91836d6af004171af735188fc1b69c286c864',
            })

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

    assert data_actual == b'1234'
Beispiel #2
0
def test_apply_patch_ok_stange(media_path):
    # Далее проверяется кривые патчи, когда сперва идёт +, а только потом -.
    # abindiff такие патчи не создаёт, но проверить не помешает (при revert
    # патчи по сути получаются такие). Также эти тесты демонстрируют, что +
    # означает не добавление, а замену куска данных в случае inplace
    # (в отличие от классических текстовых диффов)

    # FIXME: вариант, когда минуса вообще нет, хорошо бы тоже поддерживать,
    # но на данный момент не поддерживается

    path = os.path.join(media_path, 'test.bin')

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

    with open(path, 'wb') as fp2:
        patch.apply_patch(
            BytesIO(b'1234'),
            fp2,
            fpp,
            headers={
                '1-sha256sum':
                '03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4',
                '2-sha256sum':
                '914c948388ae30bdb0179a2f7bc91836d6af004171af735188fc1b69c286c864',
            })

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

    assert data_actual == b'1934'
Beispiel #3
0
def test_apply_patch_empty_fail_hashsum():
    fp1 = BytesIO()
    fp2 = 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(fp1, fp2, fpp, check_hashsum=True)
Beispiel #4
0
def test_apply_patch_empty():
    fp1 = BytesIO()
    fp2 = BytesIO()
    fpp = BytesIO(
        b'abindiff 001\n'
        b'1-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n'
        b'2-sha256sum: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n'
        b'\n')

    patch.apply_patch(fp1, fp2, fpp, check_hashsum=True)

    assert fp2.getvalue() == b''
Beispiel #5
0
def test_apply_patch_revert_fail_short(data_path):
    with open(os.path.join(data_path, 'new', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_short',
                         'unicode_file.txt.abindiff'), 'rb') as rfpp:
        fpp = BytesIO(rfpp.read())

    fp2 = BytesIO()

    with pytest.raises(ValueError):
        patch.apply_patch(fp1, fp2, fpp, revert=True)
Beispiel #6
0
def test_apply_patch_revert_ok_full(data_path):
    with open(os.path.join(data_path, 'new', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_full', 'unicode_file.txt.abindiff'),
            'rb') as rfpp:
        fpp = BytesIO(rfpp.read())

    fp2 = BytesIO()

    patch.apply_patch(fp1, fp2, fpp, revert=True)

    assert fp2.getvalue() == 'Вася\n'.encode('utf-8')
Beispiel #7
0
def test_apply_patch_ok_short(data_path):
    with open(os.path.join(data_path, 'old', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_short',
                         'unicode_file.txt.abindiff'), 'rb') as rfpp:
        fpp = BytesIO(rfpp.read())

    fp2 = BytesIO()

    patch.apply_patch(fp1, fp2, fpp)

    assert fp2.getvalue() == 'Петя\n'.encode('utf-8')
Beispiel #8
0
def test_apply_patch_fail_invalid_new_hash(data_path):
    with open(os.path.join(data_path, 'old', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_full', 'unicode_file.txt.abindiff'),
            'rb') as rfpp:
        fpp = BytesIO(rfpp.read().replace(b'2-sha256sum: 7',
                                          b'2-sha256sum: f'))

    fp2 = BytesIO()

    with pytest.raises(ValueError) as excinfo:
        patch.apply_patch(fp1, fp2, fpp)

    assert str(excinfo.value) == 'sha256sum of new file is not equal'
Beispiel #9
0
def test_apply_patch_ok_empty_hash(data_path):
    with open(os.path.join(data_path, 'old', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_full', 'unicode_file.txt.abindiff'),
            'rb') as rfpp:
        fpp = BytesIO(rfpp.read().replace(b'1-sha256sum:',
                                          b'1-sha000sum:').replace(
                                              b'2-sha256sum: 7',
                                              b'2-sha000sum: f'))

    fp2 = BytesIO()

    patch.apply_patch(fp1, fp2, fpp)

    assert fp2.getvalue() == 'Петя\n'.encode('utf-8')
Beispiel #10
0
def test_apply_patch_fail_empty_hash_if_force_check(data_path):
    with open(os.path.join(data_path, 'old', 'unicode_file.txt'),
              'rb') as rfp1:
        fp1 = BytesIO(rfp1.read())
    with open(
            os.path.join(data_path, 'patch_full', 'unicode_file.txt.abindiff'),
            'rb') as rfpp:
        fpp = BytesIO(rfpp.read().replace(b'1-sha256sum:',
                                          b'1-sha000sum:').replace(
                                              b'2-sha256sum:',
                                              b'2-sha000sum:'))

    fp2 = BytesIO()

    with pytest.raises(ValueError) as excinfo:
        patch.apply_patch(fp1, fp2, fpp, check_hashsum=True)

    assert str(excinfo.value) == 'This patch has no checksums'