Files
metin2-client/tools/foxfs/test_foxfs.py
T

45 lines
2.2 KiB
Python

import struct
import tempfile
import unittest
from pathlib import Path
from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes
from foxfs import Archive,MAGIC,filename_crc,xxh32,decompress_lz4
class ReaderTests(unittest.TestCase):
def fixture(self,path,compressed=False):
self.name='d:/ymir work/monster2/test/wait.msa'
data=b'ScriptType MotionData\r\nMotionDuration 1.0\r\n'
stored=data
if compressed:stored=b'\xf0'+bytes([len(data)-15])+data
key=bytes(range(32));iv=bytes(range(32,64))
encoder=Cipher(algorithms.AES(key),modes.CFB(iv[:16])).encryptor()
encrypted=encoder.update(stored)+encoder.finalize()
path.write_bytes(struct.pack('<I',MAGIC)+key+iv+struct.pack('<5I',len(data),xxh32(data),88,len(encrypted),filename_crc(self.name))+encrypted)
return data
def test_read_and_corruption(self):
with tempfile.TemporaryDirectory() as folder:
path=Path(folder)/'pack'
for compressed in (False,True):
data=self.fixture(path,compressed)
pack=Archive(path)
self.assertEqual(pack.get(self.name.upper().replace('/','\\')),data)
self.assertIsNone(pack.get('missing.msa'))
with self.assertRaises(ValueError):pack.get(self.name,max_bytes=5)
raw=bytearray(path.read_bytes());raw[-1]^=1;path.write_bytes(raw)
with self.assertRaises(ValueError):Archive(path).get(self.name)
def test_invalid_index_and_blocks(self):
with tempfile.TemporaryDirectory() as folder:
path=Path(folder)/'pack';self.fixture(path)
good=path.read_bytes()
for raw in (good[:3],good[:70],good[:68]+struct.pack('<5I',1,0,5,100,0)):
path.write_bytes(raw)
with self.assertRaises(ValueError):Archive(path)
self.assertEqual(xxh32(b'',0),0x02cc5d05)
self.assertEqual(decompress_lz4(b'\x10a\x01\x00\x50bcdef',10),b'aaaaabcdef')
for data,size in [(b'\xf0',20),(b'\x00\x00\x00',4),(b'\x10a',0),(b'\x10a\x01',5)]:
with self.assertRaises(ValueError):decompress_lz4(data,size)
if __name__=='__main__':unittest.main()