diff --git a/README.rst b/README.rst index dba6888..d4fd3e2 100644 --- a/README.rst +++ b/README.rst @@ -54,6 +54,14 @@ There are some examples in the folder *examples*. $ cd stegano/examples +Running the tests +----------------- + +.. code:: bash + + $ python -m unittest discover -v + + Turorial -------- diff --git a/stegano/slsb.py b/stegano/slsb.py index bcb81c1..ae2b42b 100755 --- a/stegano/slsb.py +++ b/stegano/slsb.py @@ -81,6 +81,7 @@ def hide(input_image_file, message): index += 3 + img.close() return encoded def reveal(input_image_file): @@ -110,7 +111,9 @@ def reveal(input_image_file): pass if len(bitab)-len(str(limit))-1 == limit : + img.close() return "".join(bitab)[len(str(limit))+1:] + img.close() return "" def write(image, output_image_file): @@ -121,6 +124,8 @@ def write(image, output_image_file): except Exception as e: # If hide() returns an error (Too long message). print(e) + finally: + image.close() if __name__ == '__main__': # Point of entry in execution mode. diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_slsb.py b/tests/test_slsb.py new file mode 100644 index 0000000..0c7c63a --- /dev/null +++ b/tests/test_slsb.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- + +# Stéganô - Stéganô is a basic Python Steganography module. +# Copyright (C) 2010-2016 Cédric Bonhomme - https://www.cedricbonhomme.org +# +# For more information : https://github.com/cedricbonhomme/Stegano +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see + +__author__ = "Cedric Bonhomme" +__version__ = "$Revision: 0.1 $" +__date__ = "$Date: 2016/04/12 $" +__license__ = "GPLv3" + +import os +import unittest + +from stegano import slsb + +class TestSLSB(unittest.TestCase): + + def test_hide_empty_message(self): + """ + Test hiding the empty string. + """ + secret = slsb.hide("./examples/pictures/Lenna.png", "") + secret.save("./image.png") + + clear_message = slsb.reveal("./image.png") + + self.assertEqual("", clear_message) + + def test_hide_and_reveal(self): + messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] + + for message in messages_to_hide: + secret = slsb.hide("./examples/pictures/Lenna.png", message) + secret.save("./image.png") + + clear_message = slsb.reveal("./image.png") + + self.assertEqual(message, clear_message) + + def test_with_long_message(self): + with open("./examples/lorem_ipsum.txt") as f: + message = f.read() + secret = slsb.hide("./examples/pictures/Lenna.png", message) + + def test_with_too_long_message(self): + with open("./examples/lorem_ipsum.txt") as f: + message = f.read() + message += message*2 + with self.assertRaises(Exception): + slsb.hide("./examples/pictures/Lenna.png", message) + + def tearDown(self): + try: + os.unlink("./image.png") + except: + pass + + +if __name__ == '__main__': + unittest.main()