| chojoyce | b081703 | 2022-01-12 18:29:36 +0800 | [diff] [blame] | 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.""" |
| 15 | import filecmp |
| 16 | import os |
| 17 | import unittest |
| 18 | |
| 19 | from acloud.internal.lib import driver_test_lib |
| 20 | from acloud.internal.lib import utils |
| 21 | from acloud.setup import mkcert |
| 22 | |
| 23 | |
| 24 | class MkcertTest(driver_test_lib.BaseDriverTest): |
| 25 | """Test Mkcert.""" |
| 26 | |
| 27 | # pylint: disable=no-member |
| 28 | def testInstall(self): |
| 29 | """Test Install.""" |
| 30 | self.Patch(os.path, "isdir", return_value=False) |
| 31 | self.Patch(os.path, "exists", return_value=False) |
| 32 | self.Patch(os, "mkdir") |
| 33 | self.Patch(mkcert, "IsRootCAReady") |
| 34 | self.Patch(mkcert, "UnInstall") |
| 35 | self.Patch(utils, "Popen") |
| 36 | mkcert.Install() |
| 37 | os.mkdir.assert_called_once() |
| 38 | self.assertEqual(4, utils.Popen.call_count) |
| 39 | utils.Popen.reset_mock() |
| 40 | |
| 41 | self.Patch(os.path, "isdir", return_value=True) |
| 42 | self.Patch(os.path, "exists", return_value=True) |
| 43 | mkcert.Install() |
| 44 | mkcert.UnInstall.assert_called_once() |
| 45 | self.assertEqual(0, utils.Popen.call_count) |
| 46 | |
| 47 | |
| 48 | def testAllocateLocalHostCert(self): |
| 49 | """Test AllocateLocalHostCert.""" |
| 50 | self.Patch(mkcert, "IsRootCAReady", return_value=False) |
| 51 | self.assertFalse(mkcert.AllocateLocalHostCert()) |
| 52 | |
| 53 | self.Patch(mkcert, "IsRootCAReady", return_value=True) |
| 54 | self.Patch(os.path, "exists", return_value=True) |
| 55 | self.Patch(utils, "Popen") |
| 56 | self.Patch(mkcert, "IsCertificateReady") |
| 57 | mkcert.AllocateLocalHostCert() |
| 58 | self.assertEqual(0, utils.Popen.call_count) |
| 59 | |
| 60 | self.Patch(os.path, "exists", return_value=False) |
| 61 | mkcert.AllocateLocalHostCert() |
| 62 | self.assertEqual(3, utils.Popen.call_count) |
| 63 | |
| 64 | |
| 65 | def testIsRootCAReady(self): |
| 66 | """Test IsRootCAReady.""" |
| 67 | self.Patch(os.path, "exists", return_value=True) |
| 68 | self.Patch(filecmp, "cmp", return_value=True) |
| 69 | self.assertTrue(mkcert.IsRootCAReady()) |
| 70 | |
| 71 | self.Patch(filecmp, "cmp", return_value=False) |
| 72 | self.assertFalse(mkcert.IsRootCAReady()) |
| 73 | |
| 74 | self.Patch(os.path, "exists", return_value=False) |
| 75 | self.assertFalse(mkcert.IsRootCAReady()) |
| 76 | |
| 77 | |
| 78 | def testIsCertificateReady(self): |
| 79 | """Test IsCertificateReady.""" |
| 80 | self.Patch(os.path, "exists", return_value=False) |
| 81 | self.assertFalse(mkcert.IsCertificateReady()) |
| 82 | |
| 83 | self.Patch(os.path, "exists", return_value=True) |
| 84 | self.assertTrue(mkcert.IsCertificateReady()) |
| 85 | |
| 86 | |
| 87 | def testUnInstall(self): |
| 88 | """Test UnInstall.""" |
| 89 | self.Patch(utils, "Popen") |
| 90 | mkcert.UnInstall() |
| 91 | self.assertEqual(3, utils.Popen.call_count) |
| 92 | |
| 93 | |
| 94 | if __name__ == '__main__': |
| 95 | unittest.main() |