File: test_hash.py

package info (click to toggle)
python-pyfunceble 4.2.29.dev-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,108 kB
  • sloc: python: 27,413; sh: 142; makefile: 27
file content (192 lines) | stat: -rw-r--r-- 6,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
The tool to check the availability or syntax of domain, IP or URL.

::


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Tests of the hash helper.

Author:
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:
    https://pyfunceble.github.io/special-thanks.html

Contributors:
    https://pyfunceble.github.io/contributors.html

Project link:
    https://github.com/funilrys/PyFunceble

Project documentation:
    https://docs.pyfunceble.com

Project homepage:
    https://pyfunceble.github.io/

License:
::


    Copyright 2017, 2018, 2019, 2020, 2021, 2021 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

import tempfile
import unittest

from PyFunceble.helpers.hash import HashHelper


class TestHashHelper(unittest.TestCase):
    """
    Tests our hash helpers.
    """

    def setUp(self) -> None:
        """
        Setups everything needed by the tests.
        """

        self.hashing_subject = ["Hello World!", "Thanks for using PyFunceble"]

        self.hashing_table = {
            "md5": "ba2e0e1774c2e60e2327f263402facd4",
            "sha1": "b5c8520cd2c422019997dc6fdbc9cb9d7002356e",
            "sha224": "863c46d5ed52b439da8f62a791e77c0cbbfb7d92af7c5549279f580d",
            "sha384": "6492f4b5732e0af4b9edf2c29ee4622c62ee418e5d6e0f34b13cb80560a28256c6e21e949119872d26d2327fc112a63b",  # pylint: disable=line-too-long
            "sha512": "f193ad6ee2cfbecd580225d8e6bfb9df1910e5ca6135b21b03ae208a007f71e9b57b55e299d27157551a18ef4dfdde23c96aaea796064846edc6cd25ac7eaf7f",  # pylint: disable=line-too-long
            "sha512_224": "7c43867047942e9d441f5e3e29ad63ad579bc038bf9eba925ff6896b",
        }

    def tearDown(self) -> None:
        """
        Destroys everything which was setup for the tests.
        """

        del self.hashing_table
        del self.hashing_subject

    def test_set_algo_return(self) -> None:
        """
        Tests the response from the method which let us set the algoritm to
        work with.
        """

        given = "SHA512_224"

        hash_helper = HashHelper()
        actual = hash_helper.set_algo(given)

        self.assertIsInstance(actual, HashHelper)

    def test_set_algo(self) -> None:
        """
        Tests the method which let us set the algorithm to work with.
        """

        given = "SHA512_224"
        expected = "SHA512_224"

        hash_helper = HashHelper()
        hash_helper.set_algo(given)

        actual = hash_helper.algo

        self.assertEqual(expected, actual)

        hash_helper = HashHelper(given)

        actual = hash_helper.algo

        self.assertEqual(expected, actual)

    def test_set_algo_not_str(self) -> None:
        """
        Tests the method which let us set the the algorithm to work with.
        """

        given = ["Hello", "World"]

        hash_helper = HashHelper()

        self.assertRaises(TypeError, lambda: hash_helper.set_algo(given))

    def test_set_algo_not_valid(self) -> None:
        """
        Tests the method which let us set the the algorithm to work with.
        """

        given = "Hello"

        hash_helper = HashHelper()

        self.assertRaises(ValueError, lambda: hash_helper.set_algo(given))

    def test_hash_data(self) -> None:
        """
        Tests the method which let us get the hash of a given data.
        """

        given = "\n".join(self.hashing_subject)

        hash_helper = HashHelper()

        for algo, expected in self.hashing_table.items():
            actual = hash_helper.set_algo(algo).hash_data(given)

            self.assertEqual(expected, actual)

            actual = hash_helper.hash_data(given.encode())

            self.assertEqual(expected, actual)

    def test_hash_data_wrong_type(self) -> None:
        """
        Tests the method which let us get the hash of a given data for the case
        that the given data is not a bytes nor a string.
        """

        given = 0
        hash_helper = HashHelper()

        self.assertRaises(TypeError, lambda: hash_helper.hash_data(given))

    def test_hash_file(self) -> None:
        """
        Tests the method which let us hash the content of a given file.
        """

        our_file = tempfile.NamedTemporaryFile(delete=False)

        our_file.write("\n".join(self.hashing_subject).encode())
        our_file.seek(0)

        hash_helper = HashHelper()

        for algo, expected in self.hashing_table.items():
            actual = hash_helper.set_algo(algo).hash_file(our_file.name)

            self.assertEqual(expected, actual)


if __name__ == "__main__":
    unittest.main()