Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.utils.security_unittest.py @ ab4b1cf2

History | View | Annotate | Download (4.1 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Script for unittesting the ganeti.utils.storage module"""
23

    
24
import mock
25
import os
26
import shutil
27
import tempfile
28
import unittest
29

    
30
from ganeti import constants
31
from ganeti.utils import security
32

    
33
import testutils
34

    
35

    
36
class TestUuidConversion(unittest.TestCase):
37

    
38
  def testUuidConversion(self):
39
    uuid_as_int = security.UuidToInt("5cd037f4-9587-49c4-a23e-142f8b7e909d")
40
    self.assertEqual(uuid_as_int, int(uuid_as_int))
41

    
42

    
43
class TestCandidateCerts(unittest.TestCase):
44

    
45
  def setUp(self):
46
    self._warn_fn = mock.Mock()
47
    self._info_fn = mock.Mock()
48
    self._candidate_certs = {}
49

    
50
  def testAddAndRemoveCerts(self):
51
    self.assertEqual(0, len(self._candidate_certs))
52

    
53
    node_uuid = "1234"
54
    cert_digest = "foobar"
55
    security.AddNodeToCandidateCerts(node_uuid, cert_digest,
56
      self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn)
57
    self.assertEqual(1, len(self._candidate_certs))
58

    
59
    # Try adding the same cert again
60
    security.AddNodeToCandidateCerts(node_uuid, cert_digest,
61
      self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn)
62
    self.assertEqual(1, len(self._candidate_certs))
63
    self.assertTrue(self._candidate_certs[node_uuid] == cert_digest)
64

    
65
    # Overriding cert
66
    other_digest = "barfoo"
67
    security.AddNodeToCandidateCerts(node_uuid, other_digest,
68
      self._candidate_certs, warn_fn=self._warn_fn, info_fn=self._info_fn)
69
    self.assertEqual(1, len(self._candidate_certs))
70
    self.assertTrue(self._candidate_certs[node_uuid] == other_digest)
71

    
72
    # Try removing a certificate from a node that is not in the list
73
    other_node_uuid = "5678"
74
    security.RemoveNodeFromCandidateCerts(
75
      other_node_uuid, self._candidate_certs, warn_fn=self._warn_fn)
76
    self.assertEqual(1, len(self._candidate_certs))
77

    
78
    # Remove a certificate from a node that is in the list
79
    security.RemoveNodeFromCandidateCerts(
80
      node_uuid, self._candidate_certs, warn_fn=self._warn_fn)
81
    self.assertEqual(0, len(self._candidate_certs))
82

    
83

    
84
class TestGetCertificateDigest(testutils.GanetiTestCase):
85

    
86
  def setUp(self):
87
    testutils.GanetiTestCase.setUp(self)
88
    # certificate file that contains the certificate only
89
    self._certfilename1 = testutils.TestDataFilename("cert1.pem")
90
    # (different) certificate file that contains both, certificate
91
    # and private key
92
    self._certfilename2 = testutils.TestDataFilename("cert2.pem")
93

    
94
  def testGetCertificateDigest(self):
95
    digest1 = security.GetCertificateDigest(
96
      cert_filename=self._certfilename1)
97
    digest2 = security.GetCertificateDigest(
98
      cert_filename=self._certfilename2)
99
    self.assertFalse(digest1 == digest2)
100

    
101

    
102
class TestCertVerification(testutils.GanetiTestCase):
103
  def setUp(self):
104
    testutils.GanetiTestCase.setUp(self)
105

    
106
    self.tmpdir = tempfile.mkdtemp()
107

    
108
  def tearDown(self):
109
    shutil.rmtree(self.tmpdir)
110

    
111
  def testVerifyCertificate(self):
112
    security.VerifyCertificate(testutils.TestDataFilename("cert1.pem"))
113

    
114
    nonexist_filename = os.path.join(self.tmpdir, "does-not-exist")
115

    
116
    (errcode, msg) = security.VerifyCertificate(nonexist_filename)
117
    self.assertEqual(errcode, constants.CV_ERROR)
118

    
119
    # Try to load non-certificate file
120
    invalid_cert = testutils.TestDataFilename("bdev-net.txt")
121
    (errcode, msg) = security.VerifyCertificate(invalid_cert)
122
    self.assertEqual(errcode, constants.CV_ERROR)
123

    
124

    
125
if __name__ == "__main__":
126
  testutils.GanetiTestProgram()