Project

Profile

Help

Revision 659cf1e0

ID659cf1e05b94dc4ff34d59435c9ad1cfee0cb0e9
Parent 17b858a0
Child 738db9e9

Added by Francisco Del Roio over 1 year ago

Fixed some issues with `Safe` class when storing/retrieving data.

This fixes the bug #141 and character creating/loading issues.

View differences:

src/safe.py
96 96
            self.passphrase = base64.b64encode(os.urandom(
97 97
                    self.passphrase_size))
98 98
            if file:
99
                with open(file, "w") as pass_file:
99
                with open(file, "wb") as pass_file:
100 100
                    pass_file.write(self.passphrase)
101 101

  
102 102
        # Load the secret file
......
182 182
            raise KeyError(key)
183 183

  
184 184
        value = self.data[key]
185
        if isinstance(value, str):
185
        if isinstance(value, tuple) and value[0] == "str":
186 186
            salt = self.get_salt_from_key(key)
187
            return self.decrypt(value, salt)
187
            return self.decrypt(value[1], salt)
188 188

  
189 189
        return value
190 190

  
......
199 199
        if isinstance(value, str):
200 200
            salt = self.get_salt_from_key(key)
201 201
            crypted = self.encrypt(value, salt)
202
            self.data[key] = crypted
202
            self.data[key] = ("str", crypted)
203 203
        else:
204 204
            self.data[key] = value
205 205

  
src/tests/test_safe.py
1
# Copyright (c) 2016, LE GOFF Vincent
2
# All rights reserved.
3

  
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
6

  
7
# * Redistributions of source code must retain the above copyright notice, this
8
#   list of conditions and the following disclaimer.
9

  
10
# * Redistributions in binary form must reproduce the above copyright notice,
11
#   this list of conditions and the following disclaimer in the documentation
12
#   and/or other materials provided with the distribution.
13

  
14
# * Neither the name of ytranslate nor the names of its
15
#   contributors may be used to endorse or promote products derived from
16
#   this software without specific prior written permission.
17

  
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

  
29
from unittest import TestCase
30
import os, shutil
31

  
32
from safe import Safe
33

  
34
class SafeTests(TestCase):
35
    "Test for the `Safe` class"
36

  
37
    # Files used by the safe class
38
    base_dir = os.path.join(os.path.abspath("."), "test_safe_data")
39
    pass_file = os.path.join(base_dir, ".passphrase")
40
    secret_file = os.path.join(base_dir, "data.secret")
41

  
42
    def setUp(self):
43
        "Sets up the test case"
44

  
45
        # The `Safe` object needs two files and we put these inside a folder.
46
        os.makedirs(self.base_dir)
47

  
48
        # We cannot ensure that all data will be removed.
49
        self.addCleanup(shutil.rmtree, self.base_dir, ignore_errors=True)
50
        self.safe = Safe(file=self.pass_file, secret=self.secret_file)
51

  
52
    def tearDown(self):
53
        "Destroys all data produced by the tests"
54
        if self.safe:
55
            self.safe = None
56

  
57
    def test_encrypt(self):
58
        "Tests if encrypt/decrypt works correctly"
59

  
60
        salt = self.safe.get_salt_from_key("mykey")
61
        value = "Decrypted value"
62
        encrypted = self.safe.encrypt(value, salt)
63

  
64
        self.assertEqual(self.safe.decrypt(encrypted, salt), value)
65

  
66
    def test_store(self):
67
        "Checks if the store/retrieve functions work correctly"
68

  
69
        key, value = ("mykey", "myvalue")
70
        self.safe.store(key, value)
71
        self.assertEqual(self.safe.retrieve(key), value)
72

  

Also available in: Unified diff