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