def test_invalid_version_format(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = _get_valid_metainfo(version_format='foo') with self.assertRaises(MetadataInvalid): ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save())
def test_missing_firmware(self): cabarchive = CabArchive() cabarchive['firmware123.bin'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = _get_valid_metainfo() with self.assertRaises(MetadataInvalid): ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save())
def test_metainfo_invalid(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = CabFile(b'<compoXXXXnent/>') with self.assertRaises(MetadataInvalid): ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save())
def test_metadata(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = _get_valid_metainfo() ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save()) self.assertTrue(ufile.fw.mds[0].inhibit_download) self.assertTrue(ufile.fw.mds[0].version_format == 'quad')
def test_valid_zipfile(self): imz = InMemoryZip() imz.append('DriverPackage\\firmware.bin', _get_valid_firmware().buf) imz.append('DriverPackage\\firmware.metainfo.xml', _get_valid_metainfo().buf) ufile = UploadedFile() ufile.parse('foo.zip', imz.read()) cabarchive2 = ufile.cabarchive_repacked self.assertIsNotNone(cabarchive2['firmware.bin']) self.assertIsNotNone(cabarchive2['firmware.metainfo.xml'])
def test_valid_path_back(self): cabarchive = CabArchive() cabarchive['DriverPackage\\firmware.bin'] = _get_valid_firmware() cabarchive['DriverPackage\\firmware.metainfo.xml'] = _get_valid_metainfo() ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save()) cabarchive2 = ufile.cabarchive_repacked self.assertIsNotNone(cabarchive2['firmware.bin']) self.assertIsNotNone(cabarchive2['firmware.metainfo.xml'])
def test_release_mentions_file(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['README.txt'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = \ _get_valid_metainfo(release_description='See README.txt for details.') with self.assertRaises(MetadataInvalid): ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save())
def test_multiple_metainfo_same_firmware(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['firmware1.metainfo.xml'] = _get_valid_metainfo() cabarchive['firmware2.metainfo.xml'] = _get_valid_metainfo() ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save()) cabarchive2 = ufile.cabarchive_repacked self.assertIsNotNone(cabarchive2['firmware.bin']) self.assertIsNotNone(cabarchive2['firmware1.metainfo.xml']) self.assertIsNotNone(cabarchive2['firmware2.metainfo.xml'])
def test_extra_files(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() cabarchive['firmware.metainfo.xml'] = _get_valid_metainfo() cabarchive['README.txt'] = CabFile(b'fubar') ufile = UploadedFile() ufile.parse('foo.cab', cabarchive.save()) cabarchive2 = ufile.cabarchive_repacked self.assertIsNotNone(cabarchive2['firmware.bin']) self.assertIsNotNone(cabarchive2['firmware.metainfo.xml']) with self.assertRaises(KeyError): self.assertIsNotNone(cabarchive2['README.txt'])
def _repair(): # fix any timestamps that are incorrect for md in db.session.query(Component).filter(Component.release_timestamp < 1980).all(): fn = _get_absolute_path(md.fw) print(fn, md.release_timestamp) try: ufile = UploadedFile(is_strict=False) for cat in db.session.query(Category).all(): ufile.category_map[cat.value] = cat.category_id for pro in db.session.query(Protocol).all(): ufile.protocol_map[pro.value] = pro.protocol_id with open(fn, 'rb') as f: ufile.parse(os.path.basename(fn), f.read()) except MetadataInvalid as e: print('failed to parse file: {}'.format(str(e))) continue for md_local in ufile.fw.mds: if md_local.appstream_id == md.appstream_id: print('repairing timestamp from {} to {}'.format(md.release_timestamp, md_local.release_timestamp)) md.release_timestamp = md_local.release_timestamp md.fw.mark_dirty() # fix all the checksums and file sizes for fw in db.session.query(Firmware).all(): try: with open(fw.filename_absolute, 'rb') as f: checksum_pulp = hashlib.sha256(f.read()).hexdigest() if checksum_pulp != fw.checksum_pulp: print('repairing checksum from {} to {}'.format(fw.checksum_pulp, checksum_pulp)) fw.checksum_pulp = checksum_pulp fw.mark_dirty() for md in fw.mds: sz = os.path.getsize(fw.filename_absolute) if sz != md.release_download_size: print('repairing size from {} to {}'.format(md.release_download_size, sz)) md.release_download_size = sz md.fw.mark_dirty() except FileNotFoundError as _: pass # ensure the test has been added for the firmware type if not fw.is_deleted: ploader.ensure_test_for_fw(fw) # all done db.session.commit()
def create_metadata(archive_dir, basename, metadata_fn): # process all archives in directory fws = [] print('Searching %s' % archive_dir) for root, dirs, files in os.walk(archive_dir): #pylint: disable=unused-variable for filename in files: if not filename.endswith('.cab'): continue print('Processing %s...' % filename) ufile = UploadedFile() with open(os.path.join(root, filename), 'r') as f: ufile.parse(filename, f.read(), use_hashed_prefix=False) fws.append(ufile.fw) # write metadata print('Writing %s' % metadata_fn) _generate_metadata_kind(metadata_fn, fws, firmware_baseuri="%s/" % basename, local=True)
def test_src_empty(self): with self.assertRaises(FileTooSmall): ufile = UploadedFile() ufile.parse('foo.cab', '') self.assertEqual(ufile.fwupd_min_version, '0.8.0')
def test_invalid_type(self): cabarchive = CabArchive() cabarchive['firmware.bin'] = _get_valid_firmware() with self.assertRaises(FileNotSupported): ufile = UploadedFile() ufile.parse('foo.doc', cabarchive.save())