|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# test_crypto_functions.py |
| 5 | +# |
| 6 | +# Copyright Sensors & Signals LLC https://www.snstac.com |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +"""Tests for PyTAK Crypto Functions.""" |
| 20 | + |
| 21 | +import os |
| 22 | +import pytest |
| 23 | + |
| 24 | +from unittest import mock |
| 25 | +from tempfile import NamedTemporaryFile |
| 26 | + |
| 27 | +from pytak.crypto_functions import convert_cert, INSTALL_MSG |
| 28 | + |
| 29 | + |
| 30 | +# @pytest.mark.skipif(not os.getenv("USE_CRYPTOGRAPHY"), reason=INSTALL_MSG) |
| 31 | +def test_convert_cert(): |
| 32 | + cert_path = "test_cert.p12" |
| 33 | + cert_pass = "test_pass" |
| 34 | + |
| 35 | + # Mock the load_cert function |
| 36 | + with mock.patch("pytak.crypto_functions.load_cert") as mock_load_cert: |
| 37 | + mock_private_key = mock.Mock() |
| 38 | + mock_cert = mock.Mock() |
| 39 | + mock_additional_certificates = [mock.Mock()] |
| 40 | + |
| 41 | + mock_load_cert.return_value = ( |
| 42 | + mock_private_key, |
| 43 | + mock_cert, |
| 44 | + mock_additional_certificates, |
| 45 | + ) |
| 46 | + |
| 47 | + # Mock the save_pem function |
| 48 | + with mock.patch("pytak.crypto_functions.save_pem") as mock_save_pem: |
| 49 | + mock_save_pem.side_effect = lambda pem: NamedTemporaryFile( |
| 50 | + delete=False |
| 51 | + ).name |
| 52 | + |
| 53 | + cert_paths = convert_cert(cert_path, cert_pass) |
| 54 | + |
| 55 | + assert "pk_pem_path" in cert_paths |
| 56 | + assert "cert_pem_path" in cert_paths |
| 57 | + assert "ca_pem_path" in cert_paths |
| 58 | + |
| 59 | + assert os.path.exists(cert_paths["pk_pem_path"]) |
| 60 | + assert os.path.exists(cert_paths["cert_pem_path"]) |
| 61 | + assert os.path.exists(cert_paths["ca_pem_path"]) |
| 62 | + |
| 63 | + # Clean up temporary files |
| 64 | + os.remove(cert_paths["pk_pem_path"]) |
| 65 | + os.remove(cert_paths["cert_pem_path"]) |
| 66 | + os.remove(cert_paths["ca_pem_path"]) |
0 commit comments