| 1 |
# Copyright (c) Siemens AG, 2022 |
| 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 |
|
| 15 |
import ssl |
| 16 |
import sys |
| 17 |
import unittest |
| 18 |
import socket |
| 19 |
from unittest.mock import MagicMock, patch, call |
| 20 |
|
| 21 |
try: |
| 22 |
from ssl import PROTOCOL_TLS_CLIENT |
| 23 |
tls_proto = PROTOCOL_TLS_CLIENT |
| 24 |
except ImportError: |
| 25 |
from ssl import PROTOCOL_TLS |
| 26 |
tls_proto = PROTOCOL_TLS |
| 27 |
|
| 28 |
from ncclient.transport.errors import TLSError |
| 29 |
from ncclient.transport.tls import TLSSession, DEFAULT_TLS_NETCONF_PORT |
| 30 |
|
| 31 |
|
| 32 |
HOST = '10.10.10.10' |
| 33 |
PORT = DEFAULT_TLS_NETCONF_PORT |
| 34 |
KEYFILE = 'test/unit/transport/certs/test.key' |
| 35 |
CERTFILE_WO_KEY = 'test/unit/transport/certs/test.crt' |
| 36 |
CERTFILE_WITH_KEY = 'test/unit/transport/certs/test.pem' |
| 37 |
|
| 38 |
|
| 39 |
class TestTLS(unittest.TestCase): |
| 40 |
|
| 41 |
@patch('ssl.SSLContext.wrap_socket') |
| 42 |
@patch('ncclient.transport.Session._post_connect') |
| 43 |
def test_connect_tls_certfile_with_key(self, mock_post_connect, mock_wrap_socket_fn): |
| 44 |
session = TLSSession(MagicMock()) |
| 45 |
mock_wrap_socket = MagicMock() |
| 46 |
mock_wrap_socket_fn.return_value = mock_wrap_socket |
| 47 |
session.connect(host=HOST, certfile=CERTFILE_WITH_KEY, |
| 48 |
protocol=tls_proto) |
| 49 |
mock_wrap_socket.connect.assert_called_once_with( |
| 50 |
(HOST, DEFAULT_TLS_NETCONF_PORT)) |
| 51 |
self.assertTrue(session.connected) |
| 52 |
|
| 53 |
@patch('ssl.SSLContext.wrap_socket') |
| 54 |
@patch('ncclient.transport.Session._post_connect') |
| 55 |
def test_connect_tls_certfile_without_key(self, mock_post_connect, mock_wrap_socket_fn): |
| 56 |
session = TLSSession(MagicMock()) |
| 57 |
mock_wrap_socket = MagicMock() |
| 58 |
mock_wrap_socket_fn.return_value = mock_wrap_socket |
| 59 |
session.connect(host=HOST, certfile=CERTFILE_WO_KEY, keyfile=KEYFILE, |
| 60 |
protocol=tls_proto) |
| 61 |
mock_wrap_socket.connect.assert_called_once_with( |
| 62 |
(HOST, DEFAULT_TLS_NETCONF_PORT)) |
| 63 |
self.assertTrue(session.connected) |
| 64 |
|
| 65 |
@unittest.skipIf(sys.version_info < (3, 6), "test not supported < Python3.6") |
| 66 |
@patch('ssl.SSLContext.wrap_socket') |
| 67 |
@patch('ncclient.transport.Session._post_connect') |
| 68 |
def test_tls_handshake(self, mock_post_connect, mock_wrap_socket_fn): |
| 69 |
session = TLSSession(MagicMock()) |
| 70 |
mock_wrap_socket = MagicMock() |
| 71 |
mock_wrap_socket_fn.return_value = mock_wrap_socket |
| 72 |
session.connect(host=HOST, certfile=CERTFILE_WITH_KEY, |
| 73 |
protocol=tls_proto) |
| 74 |
mock_wrap_socket.do_handshake.assert_called_once() |
| 75 |
|
| 76 |
@patch('ssl.SSLContext.wrap_socket') |
| 77 |
def test_tls_unsuccessul_handshake(self, mock_wrap_socket_fn): |
| 78 |
session = TLSSession(MagicMock()) |
| 79 |
mock_wrap_socket = MagicMock() |
| 80 |
mock_wrap_socket.do_handshake = MagicMock(side_effect=ssl.SSLError) |
| 81 |
mock_wrap_socket_fn.return_value = mock_wrap_socket |
| 82 |
with self.assertRaises(TLSError): |
| 83 |
session.connect(host=HOST, certfile=CERTFILE_WITH_KEY, |
| 84 |
protocol=tls_proto) |
| 85 |
|
| 86 |
def test_connect_tls_missing_hostname(self): |
| 87 |
session = TLSSession(MagicMock()) |
| 88 |
with self.assertRaises(TLSError): |
| 89 |
session.connect(host=None, certfile=CERTFILE_WITH_KEY, |
| 90 |
protocol=tls_proto) |
| 91 |
|
| 92 |
def test_connect_tls_missing_certfile(self): |
| 93 |
session = TLSSession(MagicMock()) |
| 94 |
with self.assertRaises(TLSError): |
| 95 |
session.connect(host=HOST, certfile=None, |
| 96 |
protocol=tls_proto) |
| 97 |
|
| 98 |
def test_connect_tls_missing_private_key(self): |
| 99 |
session = TLSSession(MagicMock()) |
| 100 |
with self.assertRaises(TLSError): |
| 101 |
session.connect(host=HOST, certfile=CERTFILE_WO_KEY, |
| 102 |
protocol=tls_proto) |
| 103 |
|
| 104 |
def test_connect_tls_missing_ca_file(self): |
| 105 |
session = TLSSession(MagicMock()) |
| 106 |
with self.assertRaises(TLSError): |
| 107 |
# Use arbitrary string to point to non-existing key location. |
| 108 |
session.connect(host=HOST, certfile=CERTFILE_WITH_KEY, |
| 109 |
ca_certs='-', protocol=tls_proto) |
| 110 |
|
| 111 |
@patch('socket.socket.close') |
| 112 |
def test_close_tls(self, mock_sock_close_fn): |
| 113 |
session = TLSSession(MagicMock()) |
| 114 |
session._socket = socket.socket() |
| 115 |
session._connected = True |
| 116 |
session.close() |
| 117 |
mock_sock_close_fn.assert_called_once_with() |
| 118 |
self.assertFalse(session._connected) |