import datetime as dt import unittest import recording_format as fmt class FormatTests(unittest.TestCase): def setUp(self): self.row=dict(family='a'*16,camera='b'*16,id='c'*32, started=dt.datetime(2026,10,18,23,58,tzinfo=fmt.ZONE).timestamp(),duration=300, path='2026-10-18_15-58-00-000000.mp4') self.instance='d'*16 def path(self):return fmt.remote_path('智家',self.instance,self.row,'济南博翠明湖','门口摄像头') def test_family_first_camera_type_date_and_cross_midnight(self): path=self.path() self.assertTrue(path.startswith('/apps/智家/济南博翠明湖_'+self.row['family']+'/摄像头/门口摄像头_')) self.assertIn('/2026/10/18/2026-10-18_23-58-00-000000_至_2026-10-19_00-03-00-000000_UTC+0800_',path) self.assertTrue(fmt.valid_path(path,'智家',self.instance,self.row)) def test_wrong_instance_household_camera_and_record_are_rejected(self): path=self.path() for value in (path.replace(self.instance,'e'*16),path.replace(self.row['family'],'e'*16), path.replace(self.row['camera'],'e'*16),path.replace(self.row['id'],'e'*32), path.replace('/2026/10/18/','/2026/10/19/'),path.replace('/摄像头/','/../')): self.assertFalse(fmt.valid_path(value,'智家',self.instance,self.row)) def test_legacy_deletion_is_still_narrowly_scoped(self): p='/apps/智家/监控录像/'+self.instance+'/'+self.row['family']+'/'+self.row['camera']+'/2026-10-18/'+self.row['path'] self.assertTrue(fmt.valid_path(p,'智家',self.instance,self.row)) self.assertFalse(fmt.valid_path(p.replace('/2026-10-18/','/2026-10-19/'),'智家',self.instance,self.row)) def test_unsafe_names_and_extreme_names_have_safe_components(self): path=fmt.remote_path('智家',self.instance,self.row,'../../\n家\\a','x'*200+'?') self.assertTrue(fmt.valid_path(path,'智家',self.instance,self.row)) self.assertNotIn('/../',path) self.assertNotIn('\n',path) if __name__=='__main__':unittest.main()