Debian Codegraph @

test/py/legacy/ganeti.utils.security_unittest.py

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3
4 # Copyright (C) 2013 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 """Script for unittesting the ganeti.utils.storage module"""
32
33 import os
34 import shutil
35 import tempfile
36 import unittest
37 from unittest import mock
38
39 from ganeti import constants
40 from ganeti.utils import security
41
42 import testutils
43
44
45 class TestUuidConversion(unittest.TestCase):
46
47 def testUuidConversion(self):
48 uuid_as_int = security.UuidToInt("5cd037f4-9587-49c4-a23e-142f8b7e909d")
49 self.assertEqual(uuid_as_int, int(uuid_as_int))
50
51
52 class TestGetCertificateDigest(testutils.GanetiTestCase):
53
54 def setUp(self):
55 testutils.GanetiTestCase.setUp(self)
56 # certificate file that contains the certificate only
57 self._certfilename1 = testutils.TestDataFilename("cert1.pem")
58 # (different) certificate file that contains both, certificate
59 # and private key
60 self._certfilename2 = testutils.TestDataFilename("cert2.pem")
61
62 def testGetCertificateDigest(self):
63 digest1 = security.GetCertificateDigest(
64 cert_filename=self._certfilename1)
65 digest2 = security.GetCertificateDigest(
66 cert_filename=self._certfilename2)
67 self.assertFalse(digest1 == digest2)
68
69
70 class TestCertVerification(testutils.GanetiTestCase):
71 def setUp(self):
72 testutils.GanetiTestCase.setUp(self)
73
74 self.tmpdir = tempfile.mkdtemp()
75
76 def tearDown(self):
77 shutil.rmtree(self.tmpdir)
78
79 def testVerifyCertificate(self):
80 security.VerifyCertificate(testutils.TestDataFilename("cert1.pem"))
81
82 nonexist_filename = os.path.join(self.tmpdir, "does-not-exist")
83
84 (errcode, msg) = security.VerifyCertificate(nonexist_filename)
85 self.assertEqual(errcode, constants.CV_ERROR)
86
87 # Try to load non-certificate file
88 invalid_cert = testutils.TestDataFilename("bdev-net.txt")
89 (errcode, msg) = security.VerifyCertificate(invalid_cert)
90 self.assertEqual(errcode, constants.CV_ERROR)
91
92
93 if __name__ == "__main__":
94 testutils.GanetiTestProgram()