• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for mkcert."""
15import filecmp
16import os
17import shutil
18import unittest
19
20from acloud.internal.lib import driver_test_lib
21from acloud.internal.lib import utils
22from acloud.setup import mkcert
23
24
25class MkcertTest(driver_test_lib.BaseDriverTest):
26    """Test Mkcert."""
27
28    # pylint: disable=no-member
29    def testInstall(self):
30        """Test Install."""
31        self.Patch(os.path, "isdir", return_value=False)
32        self.Patch(os.path, "exists", return_value=False)
33        self.Patch(os, "mkdir")
34        self.Patch(mkcert, "IsRootCAReady")
35        self.Patch(mkcert, "UnInstall")
36        self.Patch(utils, "Popen")
37        self.Patch(shutil, "rmtree")
38        self.Patch(os, "stat")
39        self.Patch(os, "chmod")
40        os.stat().st_mode = 33188
41        mkcert.Install()
42        os.chmod.assert_not_called()
43        shutil.rmtree.assert_not_called()
44        mkcert.UnInstall.assert_not_called()
45        self.assertEqual(4, utils.Popen.call_count)
46        utils.Popen.reset_mock()
47
48        self.Patch(os.path, "isdir", return_value=True)
49        self.Patch(os.path, "exists", return_value=True)
50        os.stat().st_mode = 33184
51        mkcert.Install()
52        os.chmod.assert_called_once()
53        shutil.rmtree.assert_called_once()
54        mkcert.UnInstall.assert_called_once()
55        self.assertEqual(4, utils.Popen.call_count)
56
57
58    def testAllocateLocalHostCert(self):
59        """Test AllocateLocalHostCert."""
60        self.Patch(mkcert, "IsRootCAReady", return_value=False)
61        self.assertFalse(mkcert.AllocateLocalHostCert())
62
63        self.Patch(mkcert, "IsRootCAReady", return_value=True)
64        self.Patch(os.path, "exists", return_value=True)
65        self.Patch(utils, "Popen")
66        self.Patch(mkcert, "IsCertificateReady")
67        mkcert.AllocateLocalHostCert()
68        self.assertEqual(0, utils.Popen.call_count)
69
70        self.Patch(os.path, "exists", return_value=False)
71        mkcert.AllocateLocalHostCert()
72        self.assertEqual(3, utils.Popen.call_count)
73
74
75    def testIsRootCAReady(self):
76        """Test IsRootCAReady."""
77        self.Patch(os.path, "exists", return_value=True)
78        self.Patch(filecmp, "cmp", return_value=True)
79        self.assertTrue(mkcert.IsRootCAReady())
80
81        self.Patch(filecmp, "cmp", return_value=False)
82        self.assertFalse(mkcert.IsRootCAReady())
83
84        self.Patch(os.path, "exists", return_value=False)
85        self.assertFalse(mkcert.IsRootCAReady())
86
87
88    def testIsCertificateReady(self):
89        """Test IsCertificateReady."""
90        self.Patch(os.path, "exists", return_value=False)
91        self.assertFalse(mkcert.IsCertificateReady())
92
93        self.Patch(os.path, "exists", return_value=True)
94        self.assertTrue(mkcert.IsCertificateReady())
95
96
97    def testUnInstall(self):
98        """Test UnInstall."""
99        self.Patch(utils, "Popen")
100        mkcert.UnInstall()
101        self.assertEqual(3, utils.Popen.call_count)
102
103
104if __name__ == '__main__':
105    unittest.main()
106