~ubuntu-branches/ubuntu/precise/maas/precise-updates

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_filestorage.py

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for the FileStorage model."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
import codecs
 
16
from io import BytesIO
 
17
 
 
18
from maasserver.models import FileStorage
 
19
from maasserver.testing.factory import factory
 
20
from maasserver.testing.testcase import TestCase
 
21
 
 
22
 
 
23
class FileStorageTest(TestCase):
 
24
    """Testing of the :class:`FileStorage` model."""
 
25
 
 
26
    def make_data(self, including_text='data'):
 
27
        """Return arbitrary data.
 
28
 
 
29
        :param including_text: Text to include in the data.  Leave something
 
30
            here to make failure messages more recognizable.
 
31
        :type including_text: basestring
 
32
        :return: A string of bytes, including `including_text`.
 
33
        :rtype: bytes
 
34
        """
 
35
        # Note that this won't automatically insert any non-ASCII bytes.
 
36
        # Proper handling of real binary data is tested separately.
 
37
        text = "%s %s" % (including_text, factory.getRandomString())
 
38
        return text.encode('ascii')
 
39
 
 
40
    def test_save_file_creates_storage(self):
 
41
        filename = factory.getRandomString()
 
42
        content = self.make_data()
 
43
        user = factory.make_user()
 
44
        storage = FileStorage.objects.save_file(
 
45
            filename, BytesIO(content), user)
 
46
        self.assertEqual(
 
47
            (filename, content, user),
 
48
            (storage.filename, storage.content, storage.owner))
 
49
 
 
50
    def test_storage_can_be_retrieved(self):
 
51
        filename = factory.getRandomString()
 
52
        content = self.make_data()
 
53
        factory.make_file_storage(filename=filename, content=content)
 
54
        storage = FileStorage.objects.get(filename=filename)
 
55
        self.assertEqual(
 
56
            (filename, content),
 
57
            (storage.filename, storage.content))
 
58
 
 
59
    def test_stores_binary_data(self):
 
60
        # This horrible binary data could never, ever, under any
 
61
        # encoding known to man be interpreted as text(1).  Switch the
 
62
        # bytes of the byte-order mark around and by design you get an
 
63
        # invalid codepoint; put a byte with the high bit set between bytes
 
64
        # that have it cleared, and you have a guaranteed non-UTF-8
 
65
        # sequence.
 
66
        #
 
67
        # (1) Provided, of course, that man know only about ASCII and
 
68
        # UTF.
 
69
        binary_data = codecs.BOM64_LE + codecs.BOM64_BE + b'\x00\xff\x00'
 
70
 
 
71
        # And yet, because FileStorage supports binary data, it comes
 
72
        # out intact.
 
73
        storage = factory.make_file_storage(filename="x", content=binary_data)
 
74
        self.assertEqual(binary_data, storage.content)
 
75
 
 
76
    def test_overwrites_file(self):
 
77
        # If a file of the same name has already been stored, the
 
78
        # reference to the old data gets overwritten with one to the new
 
79
        # data.
 
80
        filename = factory.make_name('filename')
 
81
        old_storage = factory.make_file_storage(
 
82
            filename=filename, content=self.make_data('old data'))
 
83
        new_data = self.make_data('new-data')
 
84
        new_storage = factory.make_file_storage(
 
85
            filename=filename, content=new_data)
 
86
        self.assertEqual(old_storage.filename, new_storage.filename)
 
87
        self.assertEqual(
 
88
            new_data, FileStorage.objects.get(filename=filename).content)
 
89
 
 
90
    def test_key_gets_generated(self):
 
91
        # The generated system_id looks good.
 
92
        storage = factory.make_file_storage()
 
93
        self.assertEqual(len(storage.key), 36)
 
94
 
 
95
    def test_key_includes_random_part(self):
 
96
        storage1 = factory.make_file_storage()
 
97
        storage2 = factory.make_file_storage()
 
98
        self.assertNotEqual(storage1.key, storage2.key)