summaryrefslogtreecommitdiffstats
path: root/tests/test_hashes.py
blob: d322a1fa1648ebe986f93d5680d88f3f8891a388 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python3
#
# Copyright (C) 2009 Julian Andres Klode <jak@debian.org>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
"""Unit tests for verifying the correctness of hashsums in apt_pkg.

Unit tests to verify the correctness of Hashes, HashString and the various
functions like md5sum."""
import hashlib
import unittest
import warnings

import apt_pkg
import testcommon


class TestHashes(testcommon.TestCase):
    """Test apt_pkg.Hashes() and the various apt_pkg.*sum() functions."""

    def setUp(self):
        """Prepare the tests, create reference values..."""
        testcommon.TestCase.setUp(self)
        self.file = open(apt_pkg.__file__, "rb")
        self.value = self.file.read()
        self.hashes = apt_pkg.Hashes(self.value)
        self.file.seek(0)
        self.fhashes = apt_pkg.Hashes(self.file)
        # Reference values.
        self.md5 = hashlib.md5(self.value).hexdigest()
        self.sha1 = hashlib.sha1(self.value).hexdigest()
        self.sha256 = hashlib.sha256(self.value).hexdigest()
        self.file.seek(0)

        warnings.filterwarnings("ignore", category=DeprecationWarning)

    def tearDown(self):
        """Cleanup, Close the file object used for the tests."""
        testcommon.TestCase.tearDown(self)
        warnings.resetwarnings()
        self.file.close()

    def test_md5sum(self):
        """hashes: Test apt_pkg.md5sum()"""
        self.assertEqual(apt_pkg.md5sum(self.value), self.md5)
        self.assertEqual(apt_pkg.md5sum(self.file), self.md5)

    def test_sha1sum(self):
        """hashes: Test apt_pkg.sha1sum()"""
        self.assertEqual(apt_pkg.sha1sum(self.value), self.sha1)
        self.assertEqual(apt_pkg.sha1sum(self.file), self.sha1)

    def test_sha256sum(self):
        """hashes: Test apt_pkg.sha256sum()"""
        self.assertEqual(apt_pkg.sha256sum(self.value), self.sha256)
        self.assertEqual(apt_pkg.sha256sum(self.file), self.sha256)

    def test_bytes(self):
        """hashes: Test apt_pkg.Hashes(bytes)"""
        self.assertEqual(self.hashes.hashes.find("md5sum").hashvalue, self.md5)
        self.assertEqual(
            self.hashes.hashes.find("md5sum"), apt_pkg.HashString("MD5Sum", self.md5)
        )
        self.assertEqual(
            self.hashes.hashes.find("sha1"), apt_pkg.HashString("SHA1", self.sha1)
        )
        self.assertEqual(
            self.hashes.hashes.find("sha256"), apt_pkg.HashString("SHA256", self.sha256)
        )
        self.assertRaises(KeyError, self.hashes.hashes.find, "md5")

    def test_file(self):
        """hashes: Test apt_pkg.Hashes(file)."""
        self.assertEqual(self.fhashes.hashes.find("md5sum").hashvalue, self.md5)
        self.assertEqual(
            self.fhashes.hashes.find("md5sum"), apt_pkg.HashString("MD5Sum", self.md5)
        )
        self.assertEqual(
            self.fhashes.hashes.find("sha1"), apt_pkg.HashString("SHA1", self.sha1)
        )
        self.assertEqual(
            self.fhashes.hashes.find("sha256"),
            apt_pkg.HashString("SHA256", self.sha256),
        )

    def test_unicode(self):
        """hashes: Test apt_pkg.Hashes(unicode)."""
        self.assertRaises(TypeError, apt_pkg.Hashes, "D")
        self.assertRaises(TypeError, apt_pkg.md5sum, "D")
        self.assertRaises(TypeError, apt_pkg.sha1sum, "D")
        self.assertRaises(TypeError, apt_pkg.sha256sum, "D")


class TestHashString(testcommon.TestCase):
    """Test apt_pkg.HashString()."""

    def setUp(self):
        """Prepare the test by reading the file."""
        testcommon.TestCase.setUp(self)
        self.file = open(apt_pkg.__file__)
        self.hashes = apt_pkg.Hashes(self.file)

        self.md5 = self.hashes.hashes.find("md5sum")
        self.sha1 = self.hashes.hashes.find("sha1")
        self.sha256 = self.hashes.hashes.find("sha256")

    def tearDown(self):
        """Cleanup, Close the file object used for the tests."""
        self.file.close()

    def test_md5(self):
        """hashes: Test apt_pkg.HashString().md5"""
        self.assertIn("MD5Sum:", str(self.md5))
        self.assertTrue(self.md5.verify_file(apt_pkg.__file__))

    def test_sha1(self):
        """hashes: Test apt_pkg.HashString().sha1"""
        self.assertIn("SHA1:", str(self.sha1))
        self.assertTrue(self.sha1.verify_file(apt_pkg.__file__))

    def test_sha256(self):
        """hashes: Test apt_pkg.HashString().sha256"""
        self.assertIn("SHA256:", str(self.sha256))
        self.assertTrue(self.sha256.verify_file(apt_pkg.__file__))

    def test_wrong(self):
        """hashes: Test apt_pkg.HashString(wrong_type)."""
        self.assertRaises(TypeError, apt_pkg.HashString, 0)
        self.assertRaises(TypeError, apt_pkg.HashString, b"")


class TestHashStringList(testcommon.TestCase):
    """Test apt_pkg.HashStringList()"""

    def test_file_size(self):
        hsl = apt_pkg.HashStringList()
        self.assertEqual(hsl.file_size, 0)
        hsl.file_size = 42
        self.assertEqual(hsl.file_size, 42)
        self.assertEqual(len(hsl), 1)

        # Verify that I can re-assign value (this handles the long case on
        # Python 2).
        hsl.file_size = hsl.file_size

        with self.assertRaises(OverflowError):
            hsl.file_size = -1

        hsl.file_size = 0

    def test_append(self):
        """Testing whether append works correctly."""
        hs1 = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed")
        hs2 = apt_pkg.HashString("SHA1", "ef113338e654b1ada807a939ad47b3a67633391b")

        hsl = apt_pkg.HashStringList()
        hsl.append(hs1)
        hsl.append(hs2)
        self.assertEqual(len(hsl), 2)
        self.assertEqual(hsl[0].hashtype, "MD5Sum")
        self.assertEqual(hsl[1].hashtype, "SHA1")
        self.assertEqual(str(hsl[0]), str(hs1))
        self.assertEqual(str(hsl[1]), str(hs2))

    def test_find(self):
        """Testing whether append works correctly."""
        hs1 = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed")
        hs2 = apt_pkg.HashString("SHA1", "ef113338e654b1ada807a939ad47b3a67633391b")

        hsl = apt_pkg.HashStringList()
        hsl.append(hs1)
        hsl.append(hs2)

        self.assertEqual(hsl.find("MD5Sum").hashtype, "MD5Sum")
        self.assertEqual(hsl.find("SHA1").hashtype, "SHA1")
        self.assertEqual(hsl.find().hashtype, "SHA1")

    def test_verify_file(self):
        with open(apt_pkg.__file__) as fobj:
            hashes = apt_pkg.Hashes(fobj)
            with warnings.catch_warnings(record=True):
                warnings.simplefilter("always")
                sha1 = hashes.hashes.find("sha1")
                sha256 = hashes.hashes.find("sha256")

        hsl = apt_pkg.HashStringList()
        hsl.append(sha1)
        hsl.append(sha256)

        self.assertTrue(hsl.verify_file(apt_pkg.__file__))

        md5sum = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed")
        hsl.append(md5sum)

        self.assertFalse(hsl.verify_file(apt_pkg.__file__))

        hsl2 = hashes.hashes
        self.assertIsInstance(hsl2, apt_pkg.HashStringList)
        self.assertGreater(len(hsl2), 0)
        self.assertTrue(hsl2.verify_file(apt_pkg.__file__))


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