Debian Codegraph @

ansible_collections/community/aws/tests/unit/plugins/modules/test_acm_certificate.py

raw
1 # (c) 2019 Telstra Corporation Limited
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21
22 __metaclass__ = type
23
24 from pprint import pprint
25
26 from ansible.module_utils._text import to_text
27
28 from ansible_collections.community.aws.plugins.modules.acm_certificate import chain_compare
29 from ansible_collections.community.aws.plugins.modules.acm_certificate import pem_chain_split
30
31
32 def test_chain_compare():
33 # The functions we're testing take module as an argument
34 # Just so they can call module.fail_json
35 # Let's just use None for the unit tests,
36 # Because they shouldn't fail
37 # And if they do, fail_json is not applicable
38 module = None
39
40 fixture_suffix = "tests/unit/plugins/modules/fixtures/certs"
41
42 # Test chain split function on super simple (invalid) certs
43 expected = ["aaa", "bbb", "ccc"]
44
45 for fname in ["simple-chain-a.cert", "simple-chain-b.cert"]:
46 path = fixture_suffix + "/" + fname
47 with open(path, "r") as f:
48 pem = to_text(f.read())
49 actual = pem_chain_split(module, pem)
50 actual = [a.strip() for a in actual]
51 if actual != expected:
52 print("Expected:")
53 pprint(expected)
54 print("Actual:")
55 pprint(actual)
56 raise AssertionError(f"Failed to properly split {fname}")
57
58 # Now test real chains
59 # chains with same same_as should be considered equal
60 test_chains = [
61 {"path": fixture_suffix + "/chain-1.0.cert", "same_as": 1, "length": 3}, # Original Cert chain
62 {"path": fixture_suffix + "/chain-1.1.cert", "same_as": 1, "length": 3}, # Same as 1.0, but longer PEM lines
63 { # Same as 1.0, but without the stuff before each --------
64 "path": fixture_suffix + "/chain-1.2.cert",
65 "same_as": 1,
66 "length": 3,
67 },
68 { # Same as 1.0, but in a different order, so should be considered different
69 "path": fixture_suffix + "/chain-1.3.cert",
70 "same_as": 2,
71 "length": 3,
72 },
73 { # Same as 1.0, but with last link missing
74 "path": fixture_suffix + "/chain-1.4.cert",
75 "same_as": 3,
76 "length": 2,
77 },
78 { # Completely different cert chain to all the others
79 "path": fixture_suffix + "/chain-4.cert",
80 "same_as": 4,
81 "length": 3,
82 },
83 {"path": fixture_suffix + "/a.pem", "same_as": 5, "length": 1}, # Single cert
84 {"path": fixture_suffix + "/b.pem", "same_as": 6, "length": 1}, # a different, single cert
85 ]
86
87 for chain in test_chains:
88 with open(chain["path"], "r") as f:
89 chain["pem_text"] = to_text(f.read())
90
91 # Test to make sure our regex isn't too greedy
92 chain["split"] = pem_chain_split(module, chain["pem_text"])
93 if len(chain["split"]) != chain["length"]:
94 print("Cert before split")
95 print(chain["pem_text"])
96 print("Cert after split")
97 pprint(chain["split"])
98 print(f"path: {chain['path']}")
99 print(f"Expected chain length: {int(chain['length'])}")
100 print(f"Actual chain length: {len(chain['split'])}")
101 raise AssertionError(f"Chain {chain['path']} was not split properly")
102
103 for chain_a in test_chains:
104 for chain_b in test_chains:
105 expected = chain_a["same_as"] == chain_b["same_as"]
106
107 # Now test the comparison function
108 actual = chain_compare(module, chain_a["pem_text"], chain_b["pem_text"])
109 if expected != actual:
110 print(f"Error, unexpected comparison result between \n{chain_a['path']}\nand\n{chain_b['path']}")
111 print(f"Expected {str(expected)} got {str(actual)}")
112 assert expected == actual