~hadware/magicicada-server/trusty-support

« back to all changes in this revision

Viewing changes to lib/ubuntuone/storage/tests/test_rhashlib.py

  • Committer: Facundo Batista
  • Date: 2015-08-05 13:10:02 UTC
  • Revision ID: facundo@taniquetil.com.ar-20150805131002-he7b7k704d8o7js6
First released version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008-2015 Canonical
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Affero General Public License as
 
5
# published by the Free Software Foundation, either version 3 of the
 
6
# License, or (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU Affero General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU Affero General Public License
 
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# For further info, check  http://launchpad.net/filesync-server
 
17
 
 
18
"""Tests for rhashlib."""
 
19
 
 
20
import os
 
21
import hashlib
 
22
import unittest
 
23
 
 
24
from ubuntuone.storage import rhashlib
 
25
from ubuntuone.storageprotocol.content_hash import magic_hash_factory
 
26
 
 
27
 
 
28
class TestRHashLib(unittest.TestCase):
 
29
    """Tests for resumable hashing."""
 
30
 
 
31
    def test_context(self):
 
32
        """get/set context work."""
 
33
        ctx = rhashlib.sha1()
 
34
        h0 = ctx.h0
 
35
 
 
36
        # get content works
 
37
        old = ctx.get_context()
 
38
 
 
39
        # change it
 
40
        ctx.h0 = 1
 
41
        self.assertEqual(ctx.h0, 1)
 
42
 
 
43
        # set works
 
44
        ctx.set_context(old)
 
45
        self.assertEquals(old, ctx.get_context())
 
46
        self.assertEqual(ctx.h0, h0)
 
47
 
 
48
    def test_hashing(self):
 
49
        """rhashlib produces the same results as hashlib."""
 
50
        first_results = []
 
51
        results = []
 
52
        hexresults = []
 
53
        data = "War is Peace" * 1000
 
54
 
 
55
        for hl in (hashlib, rhashlib):
 
56
            sha = hl.sha1()
 
57
            sha.update(data)
 
58
            first_results.append(sha.digest())
 
59
            sha.update(data)
 
60
            results.append(sha.digest())
 
61
            hexresults.append(sha.hexdigest())
 
62
 
 
63
        self.assertEqual(*first_results)
 
64
        self.assertEqual(*results)
 
65
        self.assertEqual(*hexresults)
 
66
 
 
67
    def test_resumable_hashing(self):
 
68
        """Resuming hash has the same result as hashlib."""
 
69
        results = []
 
70
        hexresults = []
 
71
        data = "Freedom is slavery" * 1000
 
72
 
 
73
        sha = hashlib.sha1()
 
74
        sha.update(data)
 
75
        results.append(sha.digest())
 
76
        hexresults.append(sha.hexdigest())
 
77
 
 
78
        sha = rhashlib.sha1()
 
79
        sha.update(data[:500])
 
80
 
 
81
        sha2 = rhashlib.sha1()
 
82
        sha2.set_context(sha.get_context())
 
83
        sha2.update(data[500:])
 
84
 
 
85
        results.append(sha2.digest())
 
86
        hexresults.append(sha2.hexdigest())
 
87
 
 
88
        self.assertEqual(*results)
 
89
        self.assertEqual(*hexresults)
 
90
 
 
91
    def test_hashcreate(self):
 
92
        """Data can be provided as an argument to the sha1 factory."""
 
93
        data = "Ignorance is strength" * 1000
 
94
        results = [hl.sha1(data).digest() for hl in (hashlib, rhashlib)]
 
95
        self.assertEqual(*results)
 
96
 
 
97
    def test_copy(self):
 
98
        """copied hashes have the same context."""
 
99
        s = rhashlib.sha1("Thoughtcrime does not entail death: "
 
100
                          "thoughtcrime is death.")
 
101
        self.assertEqual(s.get_context(), s.copy().get_context())
 
102
 
 
103
    def test_hashing_random(self):
 
104
        """rhashlib produces the same results as hashlib."""
 
105
        first_results = []
 
106
        results = []
 
107
        hexresults = []
 
108
        data = os.urandom(1024 * 1024 * 4)
 
109
 
 
110
        for hl in (hashlib, rhashlib):
 
111
            sha = hl.sha1()
 
112
            sha.update(data)
 
113
            first_results.append(sha.digest())
 
114
            sha.update(data)
 
115
            results.append(sha.digest())
 
116
            hexresults.append(sha.hexdigest())
 
117
 
 
118
        self.assertEqual(*first_results)
 
119
        self.assertEqual(*results)
 
120
        self.assertEqual(*hexresults)
 
121
 
 
122
 
 
123
class TestResumableMagicContentHash(unittest.TestCase):
 
124
    """Tests for resumable magic hashing."""
 
125
 
 
126
    def test_context(self):
 
127
        """get/set context work."""
 
128
        ctx = rhashlib.resumable_magic_hash_factory()
 
129
        h0 = ctx.hash_object.h0
 
130
 
 
131
        # get content works
 
132
        old = ctx.get_context()
 
133
 
 
134
        # change it
 
135
        ctx.hash_object.h0 = 1
 
136
        self.assertEqual(ctx.hash_object.h0, 1)
 
137
 
 
138
        # set works
 
139
        ctx.set_context(old)
 
140
        self.assertEquals(old, ctx.get_context())
 
141
        self.assertEqual(ctx.hash_object.h0, h0)
 
142
 
 
143
    def test_hashing(self):
 
144
        """rhashlib produces the same results as hashlib."""
 
145
        first_results = []
 
146
        results = []
 
147
        data = "War is Peace" * 1000
 
148
 
 
149
        for hf in (magic_hash_factory, rhashlib.resumable_magic_hash_factory):
 
150
            sha = hf()
 
151
            sha.update(data)
 
152
            first_results.append(sha.content_hash()._magic_hash)
 
153
            sha.update(data)
 
154
            results.append(sha.content_hash()._magic_hash)
 
155
 
 
156
        self.assertEqual(*first_results)
 
157
        self.assertEqual(*results)
 
158
 
 
159
    def test_resumable_hashing(self):
 
160
        """Resuming hash has the same result as hashlib."""
 
161
        results = []
 
162
        data = "Freedom is slavery" * 1000
 
163
 
 
164
        sha = magic_hash_factory()
 
165
        sha.update(data)
 
166
        results.append(sha.content_hash()._magic_hash)
 
167
 
 
168
        sha = rhashlib.resumable_magic_hash_factory()
 
169
        sha.update(data[:500])
 
170
 
 
171
        sha2 = rhashlib.resumable_magic_hash_factory()
 
172
        sha2.set_context(sha.get_context())
 
173
        sha2.update(data[500:])
 
174
 
 
175
        results.append(sha2.content_hash()._magic_hash)
 
176
 
 
177
        self.assertEqual(*results)
 
178
 
 
179
    def test_copy(self):
 
180
        """copied hashes have the same context."""
 
181
        s = rhashlib.resumable_magic_hash_factory()
 
182
        s.update("Thoughtcrime does not entail death: thoughtcrime is death.")
 
183
        self.assertEqual(s.get_context(), s.copy().get_context())
 
184
 
 
185
    def test_hashing_random(self):
 
186
        """rhashlib produces the same results as hashlib."""
 
187
        first_results = []
 
188
        results = []
 
189
        data = os.urandom(1024 * 1024 * 4)
 
190
 
 
191
        for hf in (magic_hash_factory, rhashlib.resumable_magic_hash_factory):
 
192
            sha = hf()
 
193
            sha.update(data)
 
194
            first_results.append(sha.content_hash()._magic_hash)
 
195
            sha.update(data)
 
196
            results.append(sha.content_hash()._magic_hash)
 
197
 
 
198
        self.assertEqual(*first_results)
 
199
        self.assertEqual(*results)