summaryrefslogtreecommitdiff
path: root/obnam/gpgTests.py
blob: 68d286ba05de1dcedfe56b62ecaf74b304dd7232 (plain)
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
# Copyright (C) 2006  Lars Wirzenius <liw@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Unit tests for obnam.gpg."""


import os
import shutil
import tempfile
import unittest


import obnam


class GpgEncryptionFailureTests(unittest.TestCase):

    def testIncludesExitCodeInMessage(self):
        e = obnam.gpg.GpgEncryptionFailure(42, "")
        self.failUnless("42" in str(e))

    def testIncludesStderrInMessage(self):
        e = obnam.gpg.GpgEncryptionFailure(42, "pink")
        self.failUnless("pink" in str(e))


class GpgDecryptionFailureTests(unittest.TestCase):

    def testIncludesExitCodeInMessage(self):
        e = obnam.gpg.GpgDecryptionFailure(42, "")
        self.failUnless("42" in str(e))

    def testIncludesStderrInMessage(self):
        e = obnam.gpg.GpgDecryptionFailure(42, "pink")
        self.failUnless("pink" in str(e))


class GpgTests(unittest.TestCase):

    def test(self):
        block = "pink"
        config = obnam.config.default_config()
        config.set("backup", "gpg-home", "sample-gpg-home")
        config.set("backup", "gpg-encrypt-to", "490C9ED1")
        config.set("backup", "gpg-sign-with", "490C9ED1")
        
        encrypted = obnam.gpg.encrypt(config, block)
        self.failIf(block in encrypted)
        
        decrypted = obnam.gpg.decrypt(config, encrypted)
        self.failUnlessEqual(block, decrypted)

    def testEncryptionWithInvalidKey(self):
        block = "pink"
        config = obnam.config.default_config()
        config.set("backup", "gpg-home", "sample-gpg-home")
        config.set("backup", "gpg-encrypt-to", "pretty")
        
        self.failUnlessRaises(obnam.gpg.GpgEncryptionFailure,
                              obnam.gpg.encrypt, config, block)

    def testDecryptionWithInvalidData(self):
        encrypted = "pink"
        config = obnam.config.default_config()
        config.set("backup", "gpg-home", "sample-gpg-home")
        
        self.failUnlessRaises(obnam.gpg.GpgDecryptionFailure,
                              obnam.gpg.decrypt, config, encrypted)