1
# Copyright 2008-2015 Canonical
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.
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.
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/>.
16
# For further info, check http://launchpad.net/filesync-server
18
"""Tests for rhashlib."""
24
from ubuntuone.storage import rhashlib
25
from ubuntuone.storageprotocol.content_hash import magic_hash_factory
28
class TestRHashLib(unittest.TestCase):
29
"""Tests for resumable hashing."""
31
def test_context(self):
32
"""get/set context work."""
37
old = ctx.get_context()
41
self.assertEqual(ctx.h0, 1)
45
self.assertEquals(old, ctx.get_context())
46
self.assertEqual(ctx.h0, h0)
48
def test_hashing(self):
49
"""rhashlib produces the same results as hashlib."""
53
data = "War is Peace" * 1000
55
for hl in (hashlib, rhashlib):
58
first_results.append(sha.digest())
60
results.append(sha.digest())
61
hexresults.append(sha.hexdigest())
63
self.assertEqual(*first_results)
64
self.assertEqual(*results)
65
self.assertEqual(*hexresults)
67
def test_resumable_hashing(self):
68
"""Resuming hash has the same result as hashlib."""
71
data = "Freedom is slavery" * 1000
75
results.append(sha.digest())
76
hexresults.append(sha.hexdigest())
79
sha.update(data[:500])
81
sha2 = rhashlib.sha1()
82
sha2.set_context(sha.get_context())
83
sha2.update(data[500:])
85
results.append(sha2.digest())
86
hexresults.append(sha2.hexdigest())
88
self.assertEqual(*results)
89
self.assertEqual(*hexresults)
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)
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())
103
def test_hashing_random(self):
104
"""rhashlib produces the same results as hashlib."""
108
data = os.urandom(1024 * 1024 * 4)
110
for hl in (hashlib, rhashlib):
113
first_results.append(sha.digest())
115
results.append(sha.digest())
116
hexresults.append(sha.hexdigest())
118
self.assertEqual(*first_results)
119
self.assertEqual(*results)
120
self.assertEqual(*hexresults)
123
class TestResumableMagicContentHash(unittest.TestCase):
124
"""Tests for resumable magic hashing."""
126
def test_context(self):
127
"""get/set context work."""
128
ctx = rhashlib.resumable_magic_hash_factory()
129
h0 = ctx.hash_object.h0
132
old = ctx.get_context()
135
ctx.hash_object.h0 = 1
136
self.assertEqual(ctx.hash_object.h0, 1)
140
self.assertEquals(old, ctx.get_context())
141
self.assertEqual(ctx.hash_object.h0, h0)
143
def test_hashing(self):
144
"""rhashlib produces the same results as hashlib."""
147
data = "War is Peace" * 1000
149
for hf in (magic_hash_factory, rhashlib.resumable_magic_hash_factory):
152
first_results.append(sha.content_hash()._magic_hash)
154
results.append(sha.content_hash()._magic_hash)
156
self.assertEqual(*first_results)
157
self.assertEqual(*results)
159
def test_resumable_hashing(self):
160
"""Resuming hash has the same result as hashlib."""
162
data = "Freedom is slavery" * 1000
164
sha = magic_hash_factory()
166
results.append(sha.content_hash()._magic_hash)
168
sha = rhashlib.resumable_magic_hash_factory()
169
sha.update(data[:500])
171
sha2 = rhashlib.resumable_magic_hash_factory()
172
sha2.set_context(sha.get_context())
173
sha2.update(data[500:])
175
results.append(sha2.content_hash()._magic_hash)
177
self.assertEqual(*results)
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())
185
def test_hashing_random(self):
186
"""rhashlib produces the same results as hashlib."""
189
data = os.urandom(1024 * 1024 * 4)
191
for hf in (magic_hash_factory, rhashlib.resumable_magic_hash_factory):
194
first_results.append(sha.content_hash()._magic_hash)
196
results.append(sha.content_hash()._magic_hash)
198
self.assertEqual(*first_results)
199
self.assertEqual(*results)