idol-commons / tests /test_validation.py
fffiloni's picture
Deploy tested IDOL Commons private development build
26fa2b2 verified
Raw History Blame Contribute Delete
2.45 kB
import io
import numpy as np
import pytest
from PIL import Image
from backend.validation import validate_ply,normalize_input,InvalidAsset,PROPERTIES
def fixture(path,count=32768):
header=('ply\nformat binary_little_endian 1.0\nelement vertex '+str(count)+'\n'+''.join('property float '+p+'\n' for p in PROPERTIES)+'end_header\n').encode()
data=np.zeros((count,17),dtype='<f4');data[:,10:13]=-5;data[:,13]=1
path.write_bytes(header+data.tobytes());return len(header)
def test_valid_fixed_layout(tmp_path):
p=tmp_path/'model.ply';fixture(p);assert validate_ply(p,32768)['gaussian_count']==32768
def test_oversized_ply_is_rejected_before_parsing(tmp_path):
p=tmp_path/'too-big.ply'
with p.open('wb') as f:f.truncate(80*1024*1024+1)
with pytest.raises(InvalidAsset):validate_ply(p)
@pytest.mark.parametrize('mutation',['truncated','trailing','nan','rotation','scale','normals','ordinary','extra_property','too_many','mismatch'])
def test_bad_ply(tmp_path,mutation):
p=tmp_path/'m.ply';offset=fixture(p);raw=bytearray(p.read_bytes())
if mutation=='truncated':raw=raw[:-1]
elif mutation=='trailing':raw+=b'evil'
elif mutation in ['nan','rotation','scale','normals']:
a=np.frombuffer(raw,dtype='<f4',offset=offset)
a[{'nan':0,'rotation':13,'scale':10,'normals':3}[mutation]]={'nan':float('nan'),'rotation':0,'scale':100,'normals':1}[mutation]
del a
elif mutation=='ordinary':raw=raw.replace(b'property float f_dc_0',b'property float red ')
elif mutation=='extra_property':raw=raw.replace(b'end_header\n',b'property list uchar float payload\nend_header\n')
elif mutation=='too_many':raw=raw.replace(b'element vertex 32768',b'element vertex 99999999999999')
p.write_bytes(raw)
with pytest.raises(InvalidAsset):validate_ply(p,65536 if mutation=='mismatch' else 32768)
def test_symlink_rejected(tmp_path):
target=tmp_path/'t';fixture(target);p=tmp_path/'l';p.symlink_to(target)
with pytest.raises(InvalidAsset):validate_ply(p)
def test_image_normalized(tmp_path):
b=io.BytesIO();Image.new('RGBA',(64,64),(255,0,0,255)).save(b,'PNG')
p=tmp_path/'input.png';assert normalize_input(b.getvalue(),p)['width']==64
assert not Image.open(p).info
@pytest.mark.parametrize('raw',[b'#!/bin/sh\necho no',b'PK\x03\x04fake zip',b'<svg/>'])
def test_non_image_rejected(tmp_path,raw):
with pytest.raises(InvalidAsset):normalize_input(raw,tmp_path/'input.png')