blob: d4edc8d0819e4a81286c6d0f3a6d4a434e08b7ab [file] [log] [blame]
chojoyceb0817032022-01-12 18:29:36 +08001# 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
chojoyce32ee70e2022-02-11 12:39:53 +080017import shutil
chojoyceb0817032022-01-12 18:29:36 +080018import 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")
chojoyce32ee70e2022-02-11 12:39:53 +080037 self.Patch(shutil, "rmtree")
chojoyce8c7f7ad2022-02-18 19:55:13 +080038 self.Patch(os, "stat", return_value=33188)
39 self.Patch(os, "chmod")
chojoyceb0817032022-01-12 18:29:36 +080040 mkcert.Install()
chojoyce8c7f7ad2022-02-18 19:55:13 +080041 os.chmod.assert_not_called()
chojoyce32ee70e2022-02-11 12:39:53 +080042 shutil.rmtree.assert_not_called()
43 mkcert.UnInstall.assert_not_called()
chojoyceb0817032022-01-12 18:29:36 +080044 self.assertEqual(4, utils.Popen.call_count)
45 utils.Popen.reset_mock()
46
47 self.Patch(os.path, "isdir", return_value=True)
48 self.Patch(os.path, "exists", return_value=True)
chojoyce8c7f7ad2022-02-18 19:55:13 +080049 self.Patch(os, "stat", return_value=33184)
chojoyceb0817032022-01-12 18:29:36 +080050 mkcert.Install()
chojoyce8c7f7ad2022-02-18 19:55:13 +080051 os.chmod.assert_called_once()
chojoyce32ee70e2022-02-11 12:39:53 +080052 shutil.rmtree.assert_called_once()
chojoyceb0817032022-01-12 18:29:36 +080053 mkcert.UnInstall.assert_called_once()
chojoyce32ee70e2022-02-11 12:39:53 +080054 self.assertEqual(4, utils.Popen.call_count)
chojoyceb0817032022-01-12 18:29:36 +080055
56
57 def testAllocateLocalHostCert(self):
58 """Test AllocateLocalHostCert."""
59 self.Patch(mkcert, "IsRootCAReady", return_value=False)
60 self.assertFalse(mkcert.AllocateLocalHostCert())
61
62 self.Patch(mkcert, "IsRootCAReady", return_value=True)
63 self.Patch(os.path, "exists", return_value=True)
64 self.Patch(utils, "Popen")
65 self.Patch(mkcert, "IsCertificateReady")
66 mkcert.AllocateLocalHostCert()
67 self.assertEqual(0, utils.Popen.call_count)
68
69 self.Patch(os.path, "exists", return_value=False)
70 mkcert.AllocateLocalHostCert()
71 self.assertEqual(3, utils.Popen.call_count)
72
73
74 def testIsRootCAReady(self):
75 """Test IsRootCAReady."""
76 self.Patch(os.path, "exists", return_value=True)
77 self.Patch(filecmp, "cmp", return_value=True)
78 self.assertTrue(mkcert.IsRootCAReady())
79
80 self.Patch(filecmp, "cmp", return_value=False)
81 self.assertFalse(mkcert.IsRootCAReady())
82
83 self.Patch(os.path, "exists", return_value=False)
84 self.assertFalse(mkcert.IsRootCAReady())
85
86
87 def testIsCertificateReady(self):
88 """Test IsCertificateReady."""
89 self.Patch(os.path, "exists", return_value=False)
90 self.assertFalse(mkcert.IsCertificateReady())
91
92 self.Patch(os.path, "exists", return_value=True)
93 self.assertTrue(mkcert.IsCertificateReady())
94
95
96 def testUnInstall(self):
97 """Test UnInstall."""
98 self.Patch(utils, "Popen")
99 mkcert.UnInstall()
100 self.assertEqual(3, utils.Popen.call_count)
101
102
103if __name__ == '__main__':
104 unittest.main()