def test_create_bindiff_no_hashes(read_headers): fp1 = BytesIO(b'a\n') fp2 = BytesIO(b'b\n') fpp = NoSeekBytesIO() # Без хэшей seek/tell использоваться не должны diff.create_bindiff(fp1, fp2, fpp, hashsums=()) assert read_headers(BytesIO(fpp.getvalue())) == {}
def test_create_bindiff_unknown_hashes(read_headers): fp1 = BytesIO(b'a\n') fp2 = BytesIO(b'b\n') fpp = NoSeekBytesIO() # Без хэшей seek/tell использоваться не должны with pytest.raises(ValueError): diff.create_bindiff(fp1, fp2, fpp, hashsums=('wtfsum', )) assert not fpp.getvalue()
def test_create_bindiff_complex(read_headers): fp1 = BytesIO('Мама мыла раму!'.encode('utf-8')) fp2 = BytesIO( 'Папа кушал а я не придумал что он кушал, но не раму!'.encode('utf-8')) fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp, block_size_in=1) assert result['changed_blocks_count'] == 78 assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': '52ba604c1f288ae450030d619d79a2d20c61df3b52ec634e03363619bd8d5465', '2-sha256sum': '68b89b4c8ab0587e264e7810fc30d5c87e13d873efd746886dcc912fd4dedf26', } # Да, это UTF-8! Диффалка порвала символы пополам data = ( b'= 1\n\n- 1\n\n\x9c\n\n+ 1\n\n\x9f\n\n= 3\n\n- 1\n\n\xbc\n\n' b'+ 1\n\n\xbf\n\n= 4\n\n- 1\n\n\xbc\n\n+ 1\n\n\xba\n\n= 1\n\n' b'- 3\n\n\x8b\xd0\xbb\n\n+ 3\n\n\x83\xd1\x88\n\n= 2\n\n' b'- 3\n\n \xd1\x80\n\n+ 3\n\n\xd0\xbb \n\n= 2\n\n' b'- 5\n\n\xd0\xbc\xd1\x83!\n\n+ 69\n\n \xd1\x8f \xd0\xbd\xd0\xb5' b' \xd0\xbf\xd1\x80\xd0\xb8\xd0\xb4\xd1\x83\xd0\xbc\xd0\xb0\xd0\xbb' b' \xd1\x87\xd1\x82\xd0\xbe \xd0\xbe\xd0\xbd \xd0\xba\xd1\x83\xd1\x88' b'\xd0\xb0\xd0\xbb, \xd0\xbd\xd0\xbe \xd0\xbd\xd0\xb5 ' b'\xd1\x80\xd0\xb0\xd0\xbc\xd1\x83!\n\n') assert fpp.read() == data
def test_create_bindiff_custom_meta(read_headers): fp1 = BytesIO(b'') fp2 = BytesIO(b'') fpp = BytesIO() diff.create_bindiff(fp1, fp2, fpp, meta={ 'бАр': ':=;©®™', 'FoO': 'Вася', }) assert read_headers(fpp) == { '1-sha256sum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', '2-sha256sum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'foo': 'Вася', # Ключи заголовков всегда в нижем регистре 'бар': ':=;©®™', }
def test_create_bindiff_all_hashes(read_headers): fp1 = BytesIO(b'a\n') fp2 = BytesIO(b'b\n') fpp = BytesIO() diff.create_bindiff(fp1, fp2, fpp, hashsums=('md5sum', 'sha1sum', 'sha256sum')) assert read_headers(fpp) == { '1-md5sum': '60b725f10c9c85c70d97880dfe8191b3', '2-md5sum': '3b5d5c3712955042212316173ccf37be', '1-sha1sum': '3f786850e387550fdab836ed7e6dc881de23001b', '2-sha1sum': '89e6c98d92887913cadf06b2adb97f26cde4849b', '1-sha256sum': '87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7', '2-sha256sum': '0263829989b6fd954f72baaf2fc64bc2e2f01d692d4de72986ea808f6e99813f', }
def test_create_bindiff_nochanges(): fp1 = BytesIO('Без изменений\n'.encode('utf-8')) fp2 = BytesIO('Без изменений\n'.encode('utf-8')) fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp) assert len(fpp.getvalue()) == result['patch_size'] assert result['hashsums1'][ 'sha256sum'] == 'ba1b721126a113051c7eff677dca6222001b51f4e272d956b25267a3bea8ca7d' assert result['hashsums2'][ 'sha256sum'] == 'ba1b721126a113051c7eff677dca6222001b51f4e272d956b25267a3bea8ca7d' assert not result['changed_blocks_count'] assert fpp.getvalue().endswith(b'\n\n= 26\n\n')
def test_create_bindiff_middle(read_headers): fp1 = BytesIO(b'foobar') fp2 = BytesIO(b'foObar') fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp, block_size_in=1) assert result['changed_blocks_count'] == 1 assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2', '2-sha256sum': 'fcca220448e336a24021d0ae4a61a99ded738921d7eb13c96b9c40bf47b50a8e', } assert fpp.read() == b'= 2\n\n- 1\n\no\n\n+ 1\n\nO\n\n= 3\n\n'
def test_create_bindiff_smaller(read_headers): fp1 = BytesIO(b'foobarbaz') fp2 = BytesIO(b'foobar') fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp, block_size_in=1) assert result['changed_blocks_count'] == 3 assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': '97df3588b5a3f24babc3851b372f0ba71a9dcdded43b14b9d06961bfc1707d9d', '2-sha256sum': 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2', } assert fpp.read() == b'= 6\n\n- 3\n\nbaz\n\n'
def test_create_bindiff_empty(read_headers): fp1 = BytesIO(b'') fp2 = BytesIO(b'') fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp) assert not result['changed_blocks_count'] assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', '2-sha256sum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', } assert fpp.read() == b''
def test_create_bindiff_complex_2block(read_headers): fp1 = BytesIO('Мама мыла раму!'.encode('utf-8')) fp2 = BytesIO( 'Папа кушал а я не придумал что он кушал, но не раму!'. encode('utf-8')) fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp, block_size_in=2) assert result['changed_blocks_count'] == 46 assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': '5669de59e1c798bf5a580bae005f1dc9eedc5fcea8986cd74f01ba085cbe5eec', '2-sha256sum': 'f019f0ad98a3d06b11165fb5d23ade69d6b94ec616ef471487bc6ce9c132d39e', } # В отличие от предыдущего теста, текст выровнен так, чтобы оставался корректный UTF-8 data = '''- 2 | М + 2 | П = 2 - 2 | м + 2 | п = 4 - 6 | мыл + 6 | куш = 2 - 4 | р + 4 | л = 2 - 5 | му! + 78 | я не придумал что он кушал, но не раму! ''' data = data.replace('\n', '\n\n') data = data.replace(' | ', '\n\n') data = data.encode('utf-8') assert fpp.read() == data
def test_create_bindiff_middle_smallblockout(read_headers): fp1 = BytesIO(b'big YAY change') fp2 = BytesIO(b'big big change') fpp = BytesIO() result = diff.create_bindiff(fp1, fp2, fpp, block_size_in=1, block_size_out=2) assert result['changed_blocks_count'] == 3 assert len(fpp.getvalue()) == result['patch_size'] assert read_headers(fpp) == { '1-sha256sum': 'd0816c2b7ec348487a76ade68ffc6ec0857f105ff70d679b3e31fc6ec31d31d8', '2-sha256sum': 'f8a3f03cf84bb26a22e3fa234c700238b195ec60438d1fc471167e49d7f16c9a', } assert fpp.read( ) == b'= 2\n\n= 2\n\n- 2\n\nYA\n\n+ 2\n\nbi\n\n- 1\n\nY\n\n+ 1\n\ng\n\n= 2\n\n= 2\n\n= 2\n\n= 1\n\n'