import io import os from pathlib import Path from xml.etree.ElementTree import fromstring import pytest from PIL import Image from app.parser.schdoc_parser import parse_schdoc from app.render import svg_document def test_svg_escapes_untrusted_content(ascii_schematic): malicious=b'|RECORD=4|Location.X=1|Text=&\n' doc=parse_schdoc(ascii_schematic+malicious,'x.SchDoc','x') svg=svg_document(doc); tree=fromstring(svg) assert not list(tree.iter('{http://www.w3.org/2000/svg}script')) assert b'<script>' in svg assert b'viewBox="0.0 -760.0 1150.0 760.0"' in svg def test_custom_page_and_rotation(ascii_schematic): raw=ascii_schematic.replace(b'FontIdCount=1',b'UseCustomSheet=T|CustomX=500|CustomY=900|FontIdCount=1') doc=parse_schdoc(raw,'x.SchDoc','x') assert doc.sheet.bounds.width==500 and doc.sheet.bounds.height==900 assert b'rotate(-90.0)' in svg_document(doc) def test_no_substituted_unsupported_power_symbol(ascii_schematic): raw=ascii_schematic+b'|RECORD=17|Style=6|Location.X=55|Text=EARTH|ShowNetName=T\n' doc=parse_schdoc(raw,'x.SchDoc','x') assert any(d.code=='power_style' for d in doc.diagnostics) assert all(p.kind=='text' for p in doc.primitives if p.object_id==doc.objects[-1].id) FIXTURE=Path(__file__).resolve().parents[2]/'.runtime/fixtures/Channel.SchDoc' @pytest.mark.parametrize('path',[FIXTURE, Path(os.getenv('SCHDOC_SAMPLE', '/nonexistent'))]) def test_real_binary_fixture(path): if not path.is_file(): pytest.skip('Fetch the native fixture or set SCHDOC_SAMPLE') doc=parse_schdoc(path.read_bytes(),path.name,'real') assert doc.metadata['format']=='ole' assert len(doc.components)>0 and len(doc.primitives)>5 assert all(o.properties for o in doc.objects) import cairosvg png=cairosvg.svg2png(bytestring=svg_document(doc),output_width=1600) with Image.open(io.BytesIO(png)) as image: assert image.width==1600 and image.height>0 def test_mirrored_parameter_placement_keeps_glyphs_readable(ascii_schematic): raw=ascii_schematic+b'|RECORD=41|Text=DDR3[7A]|IsMirrored=T|Justification=5|Location.X=1077|Location.Y=781\n' doc=parse_schdoc(raw,'x.SchDoc','x') text=next(p for p in doc.primitives if p.text=='DDR3[7A]') assert not text.mirrored and text.anchor=='end' and text.position.x==1077 def test_svg_keeps_fractional_native_coordinates(ascii_schematic): raw=ascii_schematic+b'|RECORD=13|Location.X=1000|Location.X_FRAC=12345|Location.Y=500|Location.Y_FRAC=67890|Corner.X=1001|Corner.Y=500\n' doc=parse_schdoc(raw,'x.SchDoc','x') primitive=next(p for p in doc.primitives if p.object_id==doc.objects[-1].id) assert primitive.path=='M1000.12345,-500.6789 L1001,-500' def test_rounded_rectangle_preserves_elliptical_corner_radii(ascii_schematic): raw=ascii_schematic+b'|RECORD=10|Corner.X=20|Corner.Y=10|CornerXRadius=2|CornerYRadius=3\n' doc=parse_schdoc(raw,'x.SchDoc','x') primitive=next(p for p in doc.primitives if p.object_id==doc.objects[-1].id) assert primitive.path.count('A2.0,3.0')==4 and ' Q' not in primitive.path