|
| 1 | +import pytest |
| 2 | +from pytest_mock import mocker |
| 3 | +from src.cisco_gnmi.proto import gnmi_pb2 |
| 4 | +from src.cisco_gnmi import util |
| 5 | +from src.cisco_gnmi.util import urlparse, ssl, x509, default_backend |
| 6 | + |
| 7 | + |
| 8 | +def test_gen_target_netloc_valueerror(): |
| 9 | + with pytest.raises(ValueError): |
| 10 | + util.gen_target_netloc("http://www.test.com", "test_prefix://", 8000) |
| 11 | + |
| 12 | + |
| 13 | +def test_gen_target_netloc_parsed_none(): |
| 14 | + |
| 15 | + mock_netloc = "//www.testing.com:8080" |
| 16 | + mock_parsed_netloc = urlparse(mock_netloc) |
| 17 | + |
| 18 | + result = util.gen_target_netloc("www.testing.com", "http://", 8080) |
| 19 | + assert mock_parsed_netloc == result |
| 20 | + |
| 21 | + |
| 22 | +def test_validate_proto_enum_exception_one(): |
| 23 | + |
| 24 | + enum = gnmi_pb2.SubscriptionMode |
| 25 | + |
| 26 | + with pytest.raises(Exception): |
| 27 | + util.validate_proto_enum("test", "INVALID_VALUE", "test", enum) |
| 28 | + |
| 29 | + |
| 30 | +def test_validate_proto_enum_exception_two(): |
| 31 | + |
| 32 | + enum = gnmi_pb2.SubscriptionMode |
| 33 | + fake_subset = [3] |
| 34 | + |
| 35 | + with pytest.raises(Exception): |
| 36 | + util.validate_proto_enum("test", 2, "test", enum, subset=fake_subset) |
| 37 | + |
| 38 | + |
| 39 | +def test_validate_proto_enum_exception_three(): |
| 40 | + |
| 41 | + enum = gnmi_pb2.SubscriptionMode |
| 42 | + fake_subset = ["ON_CHANGE", "SAMPLE"] |
| 43 | + |
| 44 | + with pytest.raises(Exception): |
| 45 | + util.validate_proto_enum( |
| 46 | + "test", "TARGET_DEFINED", "test", enum, subset=fake_subset |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_validate_proto_enum_element_in_subset_one(): |
| 51 | + |
| 52 | + enum = gnmi_pb2.SubscriptionMode |
| 53 | + fake_subset = ["ON_CHANGE", "SAMPLE"] |
| 54 | + |
| 55 | + result = util.validate_proto_enum("test", 2, "test", enum, subset=fake_subset) |
| 56 | + assert 2 == result |
| 57 | + |
| 58 | + |
| 59 | +def test_validate_proto_enum_element_in_subset_two(): |
| 60 | + |
| 61 | + enum = gnmi_pb2.SubscriptionMode |
| 62 | + fake_subset = [2, 0] |
| 63 | + |
| 64 | + result = util.validate_proto_enum( |
| 65 | + "test", "TARGET_DEFINED", "test", enum, subset=fake_subset |
| 66 | + ) |
| 67 | + assert 0 == result |
| 68 | + |
| 69 | + |
| 70 | +def test_validate_proto_enum_value_returned_one(): |
| 71 | + |
| 72 | + enum = gnmi_pb2.SubscriptionMode |
| 73 | + |
| 74 | + result = util.validate_proto_enum("test", "ON_CHANGE", "test", enum) |
| 75 | + assert 1 == result |
| 76 | + |
| 77 | + |
| 78 | +def test_validate_proto_enum_value_returned_two(): |
| 79 | + |
| 80 | + enum = gnmi_pb2.SubscriptionMode |
| 81 | + |
| 82 | + result = util.validate_proto_enum("test", 1, "test", enum) |
| 83 | + assert 1 == result |
| 84 | + |
| 85 | + |
| 86 | +def test_get_cert_from_target(): |
| 87 | + |
| 88 | + target_netloc = {"hostname": "cisco.com", "port": 443} |
| 89 | + |
| 90 | + expected_ssl_cert = ssl.get_server_certificate( |
| 91 | + (target_netloc.get("hostname"), target_netloc.get("port")) |
| 92 | + ) |
| 93 | + |
| 94 | + expected_ssl_cert.encode("utf-8") |
| 95 | + |
| 96 | + target = util.gen_target_netloc("cisco.com:443") |
| 97 | + result = util.get_cert_from_target((target)).decode("utf-8") |
| 98 | + |
| 99 | + assert expected_ssl_cert == result |
| 100 | + |
| 101 | + |
| 102 | +def test_get_cn_from_cert_returned_value_invalid_entry(mocker): |
| 103 | + |
| 104 | + mock_cert_parsed = mocker.patch.object(x509, "load_pem_x509_certificate") |
| 105 | + result = util.get_cn_from_cert("INVALID_ENTRY") |
| 106 | + |
| 107 | + assert None == result |
| 108 | + |
| 109 | + |
| 110 | +def test_get_cn_from_cert_returned_value(mocker): |
| 111 | + pass |
0 commit comments