chg: [flake] Addressed some flake and mypy issues.

This commit is contained in:
Cédric Bonhomme 2022-11-13 23:28:32 +01:00
parent fc53f2a6b9
commit e290d1260e
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
6 changed files with 17 additions and 24 deletions

View file

@ -6,3 +6,5 @@ from . import exifHeader
from . import lsb from . import lsb
from . import steganalysis from . import steganalysis
__all__ = ["red", "exifHeader", "lsb", "steganalysis"]

View file

@ -183,8 +183,8 @@ class Revealer:
self.encoded_image = open_image(encoded_image) self.encoded_image = open_image(encoded_image)
self._encoding_length = ENCODINGS[encoding] self._encoding_length = ENCODINGS[encoding]
self._buff, self._count = 0, 0 self._buff, self._count = 0, 0
self._bitab = [] self._bitab: List[str] = []
self._limit = None self._limit: Union[None, int] = None
self.secret_message = "" self.secret_message = ""
def decode_pixel(self, coordinate: tuple): def decode_pixel(self, coordinate: tuple):
@ -209,7 +209,7 @@ class Revealer:
raise IndexError("Impossible to detect message.") raise IndexError("Impossible to detect message.")
if len(self._bitab) - len(str(self._limit)) - 1 == self._limit: if len(self._bitab) - len(str(self._limit)) - 1 == self._limit:
self.secret_message = "".join(self._bitab)[len(str(self._limit)) + 1 :] self.secret_message = "".join(self._bitab)[len(str(self._limit)) + 1:]
self.encoded_image.close() self.encoded_image.close()
return True return True

View file

@ -35,10 +35,9 @@ from stegano import exifHeader
class TestEXIFHeader(unittest.TestCase): class TestEXIFHeader(unittest.TestCase):
def test_hide_empty_message(self): def test_hide_empty_message(self):
"""Test hiding the empty string.""" """Test hiding the empty string."""
secret = exifHeader.hide( exifHeader.hide(
"./tests/sample-files/20160505T130442.jpg", "./image.jpg", secret_message="" "./tests/sample-files/20160505T130442.jpg", "./image.jpg", secret_message=""
) )
# secret.save(""./image.png"")
clear_message = exifHeader.reveal("./image.jpg") clear_message = exifHeader.reveal("./image.jpg")
@ -48,7 +47,7 @@ class TestEXIFHeader(unittest.TestCase):
messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] messages_to_hide = ["a", "foo", "Hello World!", ":Python:"]
for message in messages_to_hide: for message in messages_to_hide:
secret = exifHeader.hide( exifHeader.hide(
"./tests/sample-files/20160505T130442.jpg", "./tests/sample-files/20160505T130442.jpg",
"./image.jpg", "./image.jpg",
secret_message=message, secret_message=message,
@ -56,13 +55,12 @@ class TestEXIFHeader(unittest.TestCase):
clear_message = exifHeader.reveal("./image.jpg") clear_message = exifHeader.reveal("./image.jpg")
self.assertEqual(message, message) self.assertEqual(message, clear_message.decode())
def test_with_image_without_exif_data(self): def test_with_image_without_exif_data(self):
secret = exifHeader.hide( exifHeader.hide(
"./tests/sample-files/Lenna.jpg", "./image.jpg", secret_message="" "./tests/sample-files/Lenna.jpg", "./image.jpg", secret_message=""
) )
# secret.save(""./image.png"")
clear_message = exifHeader.reveal("./image.jpg") clear_message = exifHeader.reveal("./image.jpg")
@ -72,7 +70,7 @@ class TestEXIFHeader(unittest.TestCase):
text_file_to_hide = "./tests/sample-files/lorem_ipsum.txt" text_file_to_hide = "./tests/sample-files/lorem_ipsum.txt"
with open(text_file_to_hide, "rb") as f: with open(text_file_to_hide, "rb") as f:
message = f.read() message = f.read()
secret = exifHeader.hide( exifHeader.hide(
"./tests/sample-files/20160505T130442.jpg", "./tests/sample-files/20160505T130442.jpg",
img_enc="./image.jpg", img_enc="./image.jpg",
secret_file=text_file_to_hide, secret_file=text_file_to_hide,
@ -82,13 +80,12 @@ class TestEXIFHeader(unittest.TestCase):
self.assertEqual(message, clear_message) self.assertEqual(message, clear_message)
def test_with_png_image(self): def test_with_png_image(self):
secret = exifHeader.hide( exifHeader.hide(
"./tests/sample-files/Lenna.png", "./image.png", secret_message="Secret" "./tests/sample-files/Lenna.png", "./image.png", secret_message="Secret"
) )
# secret.save(""./image.png"")
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
clear_message = exifHeader.reveal("./image.png") exifHeader.reveal("./image.png")
def test_with_bytes(self): def test_with_bytes(self):
outputBytes = io.BytesIO() outputBytes = io.BytesIO()
@ -102,11 +99,11 @@ class TestEXIFHeader(unittest.TestCase):
def tearDown(self): def tearDown(self):
try: try:
os.unlink("./image.jpg") os.unlink("./image.jpg")
except: except Exception:
pass pass
try: try:
os.unlink("./image.png") os.unlink("./image.png")
except: except Exception:
pass pass

View file

@ -36,7 +36,7 @@ class TestRed(unittest.TestCase):
Test hiding the empty string. Test hiding the empty string.
""" """
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
secret = red.hide("./tests/sample-files/Lenna.png", "") red.hide("./tests/sample-files/Lenna.png", "")
def test_hide_and_reveal(self): def test_hide_and_reveal(self):
messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] messages_to_hide = ["a", "foo", "Hello World!", ":Python:"]
@ -47,7 +47,7 @@ class TestRed(unittest.TestCase):
clear_message = red.reveal("./image.png") clear_message = red.reveal("./image.png")
self.assertEqual(message, message) self.assertEqual(message, clear_message)
def test_with_too_long_message(self): def test_with_too_long_message(self):
with open("./tests/sample-files/lorem_ipsum.txt") as f: with open("./tests/sample-files/lorem_ipsum.txt") as f:
@ -58,7 +58,7 @@ class TestRed(unittest.TestCase):
def tearDown(self): def tearDown(self):
try: try:
os.unlink("./image.png") os.unlink("./image.png")
except: except Exception:
pass pass

View file

@ -25,11 +25,7 @@ __date__ = "$Date: 2019/06/06 $"
__revision__ = "$Date: 2019/06/06 $" __revision__ = "$Date: 2019/06/06 $"
__license__ = "GPLv3" __license__ = "GPLv3"
import io
import os
import base64
import unittest import unittest
from unittest.mock import patch
from stegano import lsb from stegano import lsb
from stegano.steganalysis import parity, statistics from stegano.steganalysis import parity, statistics

View file

@ -25,9 +25,7 @@ __date__ = "$Date: 2017/02/22 $"
__revision__ = "$Date: 2017/02/22 $" __revision__ = "$Date: 2017/02/22 $"
__license__ = "GPLv3" __license__ = "GPLv3"
import os
import unittest import unittest
import io
from stegano import tools from stegano import tools