def test_validate_ota_image_simple_valid(): image = OTAImage() image.subelements = [ create_subelement(ElementTagId.UPGRADE_IMAGE, VALID_EBL_IMAGE), ] assert validators.validate_ota_image(image) == ValidationResult.VALID
def test_validate_ota_image_mixed_valid(): image = OTAImage() image.subelements = [ create_subelement(ElementTagId.UPGRADE_IMAGE, b"unknown1"), create_subelement(ElementTagId.UPGRADE_IMAGE, VALID_EBL_IMAGE), ] assert validators.validate_ota_image(image) == ValidationResult.UNKNOWN
def test_validate_ota_image_invalid(): image = OTAImage() image.subelements = [ create_subelement(ElementTagId.UPGRADE_IMAGE, VALID_EBL_IMAGE[:-1]), ] with pytest.raises(ValidationError): validators.validate_ota_image(image)
def test_validate_ota_image_complex_valid(): image = OTAImage() image.subelements = [ create_subelement(ElementTagId.ECDSA_SIGNATURE, b"asd"), create_subelement(ElementTagId.UPGRADE_IMAGE, VALID_EBL_IMAGE), create_subelement(ElementTagId.UPGRADE_IMAGE, VALID_GBL_IMAGE), create_subelement(ElementTagId.ECDSA_SIGNING_CERTIFICATE, b"foo"), ] assert validators.validate_ota_image(image) == ValidationResult.VALID
async def fetch_image(self) -> Optional[OTAImage]: async with aiohttp.ClientSession() as req: LOGGER.debug("Downloading %s for %s", self.url, self.key) async with req.get(self.url) as rsp: data = await rsp.read() img, _ = OTAImage.deserialize(data) assert img.key == self.key LOGGER.debug( "%s: version: %s, hw_ver: (%s, %s), OTA string: %s", img.key, img.header.file_version, img.header.minimum_hardware_version, img.header.maximum_hardware_version, img.header.header_string, ) LOGGER.debug( "Finished downloading %s bytes from %s for %s ver %s", self.image_size, self.url, self.key, self.version, ) return img
def _fetch_image(self) -> Optional[OTAImage]: """Loads full OTA Image from the file.""" try: with open(self.file_name, mode="rb") as file: data = file.read() offset = data.index(self.OTA_HEADER) if offset >= 0: img = OTAImage.deserialize(data[offset:])[0] return img except (OSError, ValueError) as exc: LOGGER.debug("Couldn't load '%s' OTA image: %s", self.file_name, exc) return None
async def fetch_image(self) -> Optional[OTAImage]: async with aiohttp.ClientSession() as req: LOGGER.debug("Downloading %s for %s", self.url, self.key) async with req.get(self.url) as rsp: data = await rsp.read() offset = data.index(self.OTA_HEADER) LOGGER.debug( "Finished downloading %s bytes from %s for %s ver %s", self.image_size, self.url, self.key, self.version, ) return OTAImage.deserialize(data[offset:])[0]
def test_check_invalid(): image = OTAImage() with mock.patch("zigpy.ota.validators.validate_ota_image") as m: m.side_effect = [ValidationResult.VALID] assert not validators.check_invalid(image) with mock.patch("zigpy.ota.validators.validate_ota_image") as m: m.side_effect = [ValidationResult.UNKNOWN] assert not validators.check_invalid(image) with mock.patch("zigpy.ota.validators.validate_ota_image") as m: m.side_effect = [ValidationError("error")] assert validators.check_invalid(image)
async def fetch_image(self) -> Optional[OTAImage]: async with aiohttp.ClientSession() as req: LOGGER.debug("Downloading %s for %s", self.url, self.key) async with req.get(self.url) as rsp: data = await rsp.read() assert len(data) > 24 offset = int.from_bytes(data[16:20], "little") size = int.from_bytes(data[20:24], "little") assert len(data) > offset + size ota_image, _ = OTAImage.deserialize(data[offset:offset + size]) assert ota_image.key == self.key LOGGER.debug( "Finished downloading %s bytes from %s for %s ver %s", self.image_size, self.url, self.key, self.version, ) return ota_image
def test_validate_ota_image_empty(): image = OTAImage() assert validators.validate_ota_image(image) == ValidationResult.UNKNOWN
def test_validate_ota_image_empty(): image = OTAImage() image.subelements = [] assert validators.validate_ota_image(image) == ValidationResult.UNKNOWN