am-freescan/scripts/verify_framework_url_validation.py

53 lines
3.8 KiB
Python
Raw Permalink Normal View History

"""HTTP validation matrix only; never registers files or executes SDK controls."""
import json
from pathlib import Path
from verify_cmvr_native import Client,TargetReadOnlyDb,require
ROOT=Path(__file__).resolve().parents[1]
def main():
client=Client().login();db=TargetReadOnlyDb(client)
config=client.native('GET','/scanner/configuration')['data']
def snapshot():return {table:db.select('SELECT COUNT(*) n FROM '+table)[0]['n'] for table in ('lab_scanner_command','lab_scanner_framework_transfer','te_task_inst')}
before=snapshot();file_id='a'*32;url='/api/v1/files/'+file_id
file=dict(name='框架.p3',fileId=file_id,downloadUrl=url,bytes=448)
cases=[
('相对URL',url,True),('多行去重',url+'\n'+url,True),
('当前扫描服务绝对URL',f"http://{config['host']}:{config['port']}"+url,True),
('平台代理相对URL','/admin-api/cmvr/scanner/files/'+file_id,True),
('导出文件对象',file,True),('导出文件列表',[file],True),
('粘贴JSON文件列表',json.dumps([file]),True),
('完整导出结果',dict(result=dict(files=[file])),True),
('筛选框架并忽略大型网格',[file,dict(name='mesh.stl',bytes=999999999,downloadUrl='invalid')],True),
('空列表',[],False),('空URL','',False),('null来源',None,False),
('外部URL','http://invalid.example'+url,False),('协议相对URL','//invalid.example'+url,False),
('文件路径','D:/framework.p3',False),('file协议','file://'+url,False),
('嵌入身份','http://user:password@invalid.example'+url,False),
('查询参数',url+'?x=1',False),('片段',url+'#x',False),
('路径跳转','/api/v1/files/../'+file_id,False),('编码路径','/api/v1/files/%61'+file_id[1:],False),
('文件编号不一致',dict(file,fileId='b'*32),False),
('零字节',dict(file,bytes=0),False),('超100MiB',dict(file,bytes=100*1024*1024+1),False),
('字节数不是整数',dict(file,bytes=1.5),False),('字节数溢出',dict(file,bytes=10**30),False),
('超过旧16MiB限制',dict(file,bytes=16*1024*1024+1),True),
('最大100MiB只校验元信息',dict(file,bytes=100*1024*1024),True),
('只有网格文件',[dict(name='mesh.ply',downloadUrl=url)],False),
('超过8个框架URL',['/api/v1/files/'+format(i,'032x') for i in range(9)],False),
('同一URL信息冲突',[file,dict(file,name='another.p3')],False),
]
results=[]
for name,source,valid in cases:
response=client.native('POST','/scanner/validate',dict(operation='FRAMEWORK_IMPORT',requestJson='{"confirm":true}',frameworkUrls=source),expected=None)
require((response.get('code')==200 and response.get('data') is True) if valid else response.get('code')==400,'校验行为不符:'+name+' '+str(response))
results.append(dict(name=name,passed=True,code=response['code']))
for name,body in [('缺少确认','{}'),('拒绝确认','{"confirm":false}'),('同时填写旧编号','{"confirm":true,"fileIds":[]}')]:
response=client.native('POST','/scanner/validate',dict(operation='FRAMEWORK_IMPORT',requestJson=body,frameworkUrls=url),expected=None)
require(response.get('code')==400,'非法确认参数被放行:'+name);results.append(dict(name=name,passed=True,code=400))
after=snapshot();require(after==before,'纯参数校验竟产生执行或设备命令记录')
report=dict(passed=True,checks=results,count=len(results),deviceControlRequests=0,businessRowsUnchanged=True)
(ROOT/'.local/framework-url-validation-matrix-20261027.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
db.close();print(json.dumps(dict(passed=True,count=len(results),deviceControlRequests=0,businessRowsUnchanged=True)))
if __name__=='__main__':main()