~nataliabidart/ubuntuone-storage-protocol/stable-3-0-update-2.99.92

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
# -*- coding: utf-8 -*-
#
# Author: Facundo Batista <facundo@canonical.com>
#
# Copyright (C) 2011-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL.  If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so.  If you
# do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source
# files in the program, then also delete it here.
"""Tests for the protocol hashing methods."""

import hashlib
import os
import pickle
import unittest

from ubuntuone.storageprotocol.content_hash import (
    MagicContentHash,
    SHA1ContentHash,
    content_hash_factory,
    magic_hash_factory,
)

# let's not get picky about aatributes outside __init__ in tests
# pylint: disable=W0201
# it's ok to access internals in the test suite
# pylint: disable=W0212


class FactoriesTest(unittest.TestCase):
    """Test the hasher factories."""

    def test_content_hash_factory(self):
        """Check the factory for the normal content hash."""
        o = content_hash_factory()
        self.assertTrue(isinstance(o, SHA1ContentHash))

    def test_content_hash_method(self):
        """Test the method that the normal content hash uses."""
        self.assertEqual(SHA1ContentHash.method, hashlib.sha1)

    def test_content_hash_method_name(self):
        """Test the method name for the normal content hash."""
        self.assertEqual(SHA1ContentHash.method_name, 'sha1')

    def test_magic_hash_factory(self):
        """Check the factory for the magic content hash."""
        o = magic_hash_factory()
        self.assertTrue(isinstance(o, MagicContentHash))

    def test_magic_hash_method(self):
        """Test the method that the magic content hash uses."""
        self.assertEqual(MagicContentHash.method, hashlib.sha1)

    def test_magic_hash_method_name(self):
        """Test the method name for the magic content hash."""
        self.assertEqual(MagicContentHash.method_name, 'magic_hash')


class ContentHashingTests(unittest.TestCase):
    """Test normal content hashing."""

    def setUp(self):
        """Set up."""
        self.hasher = SHA1ContentHash()

    def test_hashing_empty(self):
        """Test the hashing for no data."""
        r = self.hasher.hexdigest()
        s = hashlib.sha1().hexdigest()
        self.assertEqual(r, s)

    def test_hashing_content_once(self):
        """Test the hashing for some content sent once."""
        self.hasher.update("foobar")
        r = self.hasher.hexdigest()
        s = hashlib.sha1("foobar").hexdigest()
        self.assertEqual(r, s)

    def test_hashing_content_upadting(self):
        """Test the hashing for some content sent more than once."""
        c1 = os.urandom(1000)
        c2 = os.urandom(1000)
        self.hasher.update(c1)
        self.hasher.update(c2)
        r = self.hasher.hexdigest()
        s = hashlib.sha1(c1 + c2).hexdigest()
        self.assertEqual(r, s)

    def test_content_hash(self):
        """The hexdigest with the prefix."""
        self.hasher.update("foobar")
        hexdigest = self.hasher.hexdigest()
        ch = self.hasher.content_hash()
        self.assertEqual("sha1:" + hexdigest, ch)


class MagicHashingTests(unittest.TestCase):
    """Test magic content hashing."""

    def setUp(self):
        """Set up."""
        self.hasher = MagicContentHash()

    def test_hashing_empty(self):
        """Test the hashing for no data."""
        r = self.hasher.hash_object.hexdigest()
        s = hashlib.sha1("Ubuntu One").hexdigest()
        self.assertEqual(r, s)

    def test_hashing_content_once(self):
        """Test the hashing for some content sent once."""
        self.hasher.update("foobar")
        r = self.hasher.hash_object.hexdigest()
        s = hashlib.sha1("Ubuntu Onefoobar").hexdigest()
        self.assertEqual(r, s)

    def test_hashing_content_upadting(self):
        """Test the hashing for some content sent more than once."""
        c1 = os.urandom(1000)
        c2 = os.urandom(1000)
        self.hasher.update(c1)
        self.hasher.update(c2)
        r = self.hasher.hash_object.hexdigest()
        s = hashlib.sha1("Ubuntu One" + c1 + c2).hexdigest()
        self.assertEqual(r, s)

    def test_hexdigest_hiding(self):
        """Can not access the hex digest."""
        self.assertRaises(NotImplementedError, self.hasher.hexdigest)

    def test_digest_hiding(self):
        """Can not access the digest."""
        self.assertRaises(NotImplementedError, self.hasher.digest)

    def test_content_hash_hiding(self):
        """The content hash is not the content hash!"""
        self.hasher.update("foobar")
        hexdigest = self.hasher.hash_object.hexdigest()
        ch = self.hasher.content_hash()

        # not a string, and never show the content
        self.assertFalse(isinstance(ch, basestring))
        self.assertFalse(hexdigest in str(ch))
        self.assertFalse(hexdigest in repr(ch))

        # we have the real value hidden in the object
        self.assertEqual('magic_hash:' + hexdigest, ch._magic_hash)

    def test_not_pickable(self):
        """The magic hasher and value can not be pickled"""
        # the hasher
        self.assertRaises(NotImplementedError, pickle.dumps, self.hasher)

        # the value
        ch = self.hasher.content_hash()
        self.assertRaises(NotImplementedError, pickle.dumps, ch)


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