77 lines
3.7 KiB
Python
77 lines
3.7 KiB
Python
from pathlib import Path
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ORIGINAL = ROOT.parent/'Original'
|
|
sys.path.insert(0, str(ROOT/'tools/extractors'))
|
|
from bubble_resources import ARCHIVE_SHA256, PAYLOAD_SHA256, RGBA_SHA256, export_bubble
|
|
from omt import FormatError
|
|
from png import read_png
|
|
|
|
|
|
@unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures')
|
|
class BubbleResourceTests(unittest.TestCase):
|
|
def test_exact_canvas_pixels_and_source_provenance(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory)/'bubble'
|
|
manifest = export_bubble(ORIGINAL, output)
|
|
self.assertEqual(manifest, json.loads((output/'bubble.json').read_text()))
|
|
self.assertEqual((manifest['schema'], manifest['source'], manifest['sourceSha256']),
|
|
(1, 'RetainedSprites.omt', ARCHIVE_SHA256))
|
|
self.assertEqual(len(manifest['textures']), 1)
|
|
entry = manifest['textures'][0]
|
|
self.assertEqual((entry['id'], entry['name'], entry['width'], entry['height']),
|
|
(1, 'orangebubble', 146, 146))
|
|
self.assertEqual((entry['sourceOffset'], entry['sourceBytes'], entry['sourceSha256']),
|
|
(0x17FCE, 66322, PAYLOAD_SHA256))
|
|
rgba = (output/entry['path']).read_bytes()
|
|
self.assertEqual((len(rgba), hashlib.sha256(rgba).hexdigest()), (146*146*4, RGBA_SHA256))
|
|
self.assertEqual(read_png(output/entry['png'])['rgba'], rgba)
|
|
self.assertEqual((rgba[3::4].count(0), rgba[3::4].count(255)), (5492, 15824))
|
|
|
|
def test_original_subtrees_and_ancestors_rejected_before_writes(self):
|
|
for output in (ORIGINAL, ORIGINAL/'omt/generated-bubble', ORIGINAL.parent):
|
|
with self.subTest(output=output), self.assertRaises(ValueError):
|
|
export_bubble(ORIGINAL, output)
|
|
|
|
def test_output_child_symlinks_cannot_overwrite_another_file(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
target = root/'unchanged'
|
|
target.write_bytes(b'unchanged')
|
|
output = root/'bubble'
|
|
output.mkdir()
|
|
for name in ('canvas_1.rgba', 'canvas_1.png', 'bubble.json'):
|
|
(output/name).symlink_to(target)
|
|
with self.subTest(name=name), self.assertRaises(ValueError):
|
|
export_bubble(ORIGINAL, output)
|
|
self.assertEqual(target.read_bytes(), b'unchanged')
|
|
(output/name).unlink()
|
|
|
|
def test_bad_decoded_dimensions_or_pixels_rejected_before_output(self):
|
|
for canvas in (dict(width=1, height=1, rgba=b'\0'*4),
|
|
dict(width=146, height=146, rgba=b'\0'*(146*146*4))):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory)/'bubble'
|
|
with patch('bubble_resources.read_canvas', return_value=canvas), self.assertRaises(FormatError):
|
|
export_bubble(ORIGINAL, output)
|
|
self.assertFalse(output.exists())
|
|
|
|
def test_bad_archive_identity_rejected_before_decode(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory)/'bubble'
|
|
with patch('bubble_resources.digest', return_value='0'*64), \
|
|
patch('bubble_resources.read_canvas') as decoder, self.assertRaises(FormatError):
|
|
export_bubble(ORIGINAL, output)
|
|
decoder.assert_not_called()
|
|
self.assertFalse(output.exists())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|