| 1 |
#!/usr/bin/env python |
| 2 |
from io import BytesIO |
| 3 |
import unittest |
| 4 |
|
| 5 |
from typing_extensions import SupportsBytes |
| 6 |
|
| 7 |
from asammdf.blocks.utils import MdfException |
| 8 |
from asammdf.blocks.v4_blocks import AttachmentBlock |
| 9 |
|
| 10 |
|
| 11 |
class TestATBLOCK(unittest.TestCase): |
| 12 |
compressed: BytesIO |
| 13 |
uncompressed: BytesIO |
| 14 |
|
| 15 |
tempdir = None |
| 16 |
data = b"\n".join(f"line {i}".encode("ascii") for i in range(50)) |
| 17 |
filename = "embedded.txt" |
| 18 |
comment = "example of embedded attachment" |
| 19 |
|
| 20 |
@classmethod |
| 21 |
def setUpClass(cls) -> None: |
| 22 |
cls.compressed = BytesIO() |
| 23 |
cls.compressed.write( |
| 24 |
b"\x00##TX\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00embedded.txt\x00\x00\x00\x00##TX\x00\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00example of embedded attachment\x00\x00##AT\x00\x00\x00\x00\xd3\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\xeb\x825\x1a\x0cri\xb9\xca\xfb\xde\xb6pT\x17k\x85\x01\x00\x00\x00\x00\x00\x00s\x00\x00\x00\x00\x00\x00\x00x\x9c5\xd0\xb9\r\xc3P\x0c\x04\xd1\\U\xb8\x04\xed\xe1C\x0590 \xa8\xffP08?\x9a\x88\x0f$\xcf\xdf\xf5}\xec\xdb\xf9\x8f&\x9ed\xd2\xc9s\xf2\x9a\xbc'\x9f\xc9\xc1\xf8bp\x04$$A\tK`B\x13\x9c\xf0\x8c\xe7\xb5\x17\x9e\xf1\x8cg<\xe3\x19\xcfx\xc6\x0b^\xf0\xb2\x0e\xc5\x0b^\xf0\x82\x17\xbc\xe0\x05\xafx\xc5+^\xd7\xe7\xf0\x8aW\xbc\xe2\x15\xaf\xc7\r\xca\xd1m \x00\x00\x00\x00" |
| 25 |
) |
| 26 |
|
| 27 |
cls.uncompressed = BytesIO() |
| 28 |
cls.uncompressed.write( |
| 29 |
b"\x00##TX\x00\x00\x00\x00(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00embedded.txt\x00\x00\x00\x00##TX\x00\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00example of embedded attachment\x00\x00##AT\x00\x00\x00\x00\xe5\x01\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\xeb\x825\x1a\x0cri\xb9\xca\xfb\xde\xb6pT\x17k\x85\x01\x00\x00\x00\x00\x00\x00\x85\x01\x00\x00\x00\x00\x00\x00line 0\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10\nline 11\nline 12\nline 13\nline 14\nline 15\nline 16\nline 17\nline 18\nline 19\nline 20\nline 21\nline 22\nline 23\nline 24\nline 25\nline 26\nline 27\nline 28\nline 29\nline 30\nline 31\nline 32\nline 33\nline 34\nline 35\nline 36\nline 37\nline 38\nline 39\nline 40\nline 41\nline 42\nline 43\nline 44\nline 45\nline 46\nline 47\nline 48\nline 49\x00\x00" |
| 30 |
) |
| 31 |
|
| 32 |
def test_read_compressed(self) -> None: |
| 33 |
self.compressed.seek(0) |
| 34 |
|
| 35 |
block = AttachmentBlock(address=97, stream=self.compressed, file_limit=1 << 20) |
| 36 |
|
| 37 |
self.assertEqual(block.file_name, self.filename) |
| 38 |
self.assertEqual(block.extract(), self.data) |
| 39 |
self.assertEqual(block.comment, self.comment) |
| 40 |
|
| 41 |
def test_read_uncompressed(self) -> None: |
| 42 |
self.uncompressed.seek(0) |
| 43 |
|
| 44 |
block = AttachmentBlock(address=97, stream=self.uncompressed, file_limit=1 << 20) |
| 45 |
|
| 46 |
self.assertEqual(block.file_name, self.filename) |
| 47 |
self.assertEqual(block.extract(), self.data) |
| 48 |
self.assertEqual(block.comment, self.comment) |
| 49 |
|
| 50 |
def test_read_wrong_id(self) -> None: |
| 51 |
self.compressed.seek(0) |
| 52 |
stream = BytesIO(self.compressed.read()) |
| 53 |
stream.seek(97) |
| 54 |
stream.write(b"_NOK") |
| 55 |
|
| 56 |
with self.assertRaises(MdfException): |
| 57 |
AttachmentBlock(address=97, stream=stream, file_limit=1 << 20) |
| 58 |
|
| 59 |
def test_bytes_compressed(self) -> None: |
| 60 |
attachment = AttachmentBlock(file_name=self.filename, data=self.data, embedded=True, compression=True) |
| 61 |
attachment.comment = self.comment |
| 62 |
|
| 63 |
stream = BytesIO() |
| 64 |
|
| 65 |
stream.write(b"\0") |
| 66 |
|
| 67 |
blocks: list[bytes | SupportsBytes] = [] |
| 68 |
attachment.to_blocks(1, blocks, {}) |
| 69 |
for block in blocks: |
| 70 |
stream.write(bytes(block)) |
| 71 |
|
| 72 |
address = attachment.address |
| 73 |
|
| 74 |
block = AttachmentBlock(address=address, stream=stream, file_limit=1 << 20) |
| 75 |
|
| 76 |
self.assertEqual(block.comment, self.comment) |
| 77 |
self.assertEqual(block.file_name, self.filename) |
| 78 |
self.assertEqual(block.extract(), self.data) |
| 79 |
|
| 80 |
def test_bytes_uncompressed(self) -> None: |
| 81 |
attachment = AttachmentBlock(file_name=self.filename, data=self.data, embedded=True, compression=False) |
| 82 |
attachment.comment = self.comment |
| 83 |
|
| 84 |
stream = BytesIO() |
| 85 |
|
| 86 |
stream.write(b"\0") |
| 87 |
|
| 88 |
blocks: list[bytes | SupportsBytes] = [] |
| 89 |
attachment.to_blocks(1, blocks, {}) |
| 90 |
for block in blocks: |
| 91 |
stream.write(bytes(block)) |
| 92 |
|
| 93 |
address = attachment.address |
| 94 |
|
| 95 |
block = AttachmentBlock(address=address, stream=stream, file_limit=1 << 20) |
| 96 |
|
| 97 |
self.assertEqual(block.comment, self.comment) |
| 98 |
self.assertEqual(block.file_name, self.filename) |
| 99 |
self.assertEqual(block.extract(), self.data) |
| 100 |
|
| 101 |
|
| 102 |
if __name__ == "__main__": |
| 103 |
unittest.main() |