| 1 |
# Copyright (c) 2015 Cisco Systems, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
from neutron.agent.linux import ip_lib |
| 19 |
from neutron.agent.linux import utils as linux_utils |
| 20 |
|
| 21 |
from neutron_vpnaas.services.vpn.device_drivers import ipsec |
| 22 |
from neutron_vpnaas.tests.functional.common import test_scenario |
| 23 |
from oslo_config import cfg |
| 24 |
|
| 25 |
|
| 26 |
class TestOpenSwanDeviceDriver(test_scenario.TestIPSecBase): |
| 27 |
|
| 28 |
"""Test the OpenSwan reference implementation of the device driver.""" |
| 29 |
|
| 30 |
# NOTE: Tests may be added/removed/changed, when this is fleshed out |
| 31 |
# in future commits. |
| 32 |
|
| 33 |
def _ping_mtu(self, from_site, to_site, size, instance=0): |
| 34 |
"""Pings ip address using packets of given size and with DF=1. |
| 35 |
|
| 36 |
In order to ping it uses following cli command: |
| 37 |
ip netns exec <namespace> ping -c 4 -M do -s <size> <ip> |
| 38 |
""" |
| 39 |
namespace = from_site.vm[instance].namespace |
| 40 |
ip = to_site.vm[instance].port_ip |
| 41 |
try: |
| 42 |
cmd = ['ping', '-c', 4, '-M', 'do', '-s', size, ip] |
| 43 |
cmd = ip_lib.add_namespace_to_cmd(cmd, namespace) |
| 44 |
linux_utils.execute(cmd, run_as_root=True) |
| 45 |
return True |
| 46 |
except RuntimeError: |
| 47 |
return False |
| 48 |
|
| 49 |
def test_process_created_on_ipsec_connection_create(self): |
| 50 |
"""Check that pluto process is running.""" |
| 51 |
pass |
| 52 |
|
| 53 |
def test_connection_status_with_one_side_of_ipsec_connection(self): |
| 54 |
"""Check status of connection, with only one end created. |
| 55 |
|
| 56 |
Expect that the status will indicate that the connection is down. |
| 57 |
""" |
| 58 |
pass |
| 59 |
|
| 60 |
def test_process_gone_on_ipsec_connection_delete(self): |
| 61 |
"""Verify that there is no longer a process, upon deletion.""" |
| 62 |
pass |
| 63 |
|
| 64 |
def test_cached_status_on_create_and_delete(self): |
| 65 |
"""Test that the status is cached.""" |
| 66 |
pass |
| 67 |
|
| 68 |
def test_status_reporting(self): |
| 69 |
"""Test status reported correctly to agent.""" |
| 70 |
pass |
| 71 |
|
| 72 |
def _override_mtu_for_site(self, site, mtu): |
| 73 |
ipsec_connection = site.vpn_service['ipsec_site_connections'][0] |
| 74 |
ipsec_connection['mtu'] = mtu |
| 75 |
|
| 76 |
def test_ipsec_site_connections_mtu_enforcement(self): |
| 77 |
"""Test that mtu of ipsec site connections is enforced.""" |
| 78 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 79 |
[self.private_nets[1]]) |
| 80 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 81 |
[self.private_nets[2]]) |
| 82 |
|
| 83 |
self.check_ping(site1, site2, success=False) |
| 84 |
self.check_ping(site2, site1, success=False) |
| 85 |
|
| 86 |
self.prepare_ipsec_site_connections(site1, site2) |
| 87 |
# Set up non-default mtu value |
| 88 |
self._override_mtu_for_site(site1, 1200) |
| 89 |
self._override_mtu_for_site(site2, 1200) |
| 90 |
|
| 91 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 92 |
|
| 93 |
# Validate that ip packets with 1172 (1200) bytes of data pass |
| 94 |
self.assertTrue(self._ping_mtu(site1, site2, 1172)) |
| 95 |
self.assertTrue(self._ping_mtu(site2, site1, 1172)) |
| 96 |
|
| 97 |
# Validate that ip packets with 1173 (1201) bytes of data are dropped |
| 98 |
self.assertFalse(self._ping_mtu(site1, site2, 1173)) |
| 99 |
self.assertFalse(self._ping_mtu(site2, site1, 1173)) |
| 100 |
|
| 101 |
def test_no_config_change_skip_restart(self): |
| 102 |
"""Test when config is not changed, then restart should be skipped""" |
| 103 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 104 |
[self.private_nets[1]]) |
| 105 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 106 |
[self.private_nets[2]]) |
| 107 |
|
| 108 |
self.prepare_ipsec_site_connections(site1, site2) |
| 109 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 110 |
|
| 111 |
self.check_ping(site1, site2) |
| 112 |
self.check_ping(site2, site1) |
| 113 |
|
| 114 |
with mock.patch.object(ipsec.OpenSwanProcess, 'start') as my_start: |
| 115 |
|
| 116 |
ipsec.OpenSwanProcess.active = mock.patch.object( |
| 117 |
ipsec.OpenSwanProcess, 'active', return_value=True).start() |
| 118 |
ipsec.OpenSwanProcess._config_changed = mock.patch.object( |
| 119 |
ipsec.OpenSwanProcess, |
| 120 |
'_config_changed', return_value=False).start() |
| 121 |
|
| 122 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 123 |
# when restart_check_config is not set, start will be |
| 124 |
# called in first sync |
| 125 |
self.assertEqual(1, my_start.call_count) |
| 126 |
my_start.reset_mock() |
| 127 |
|
| 128 |
cfg.CONF.set_override('restart_check_config', True, group='pluto') |
| 129 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 130 |
# after restart_check_config enabled, then start will |
| 131 |
# not be called, since no config changes |
| 132 |
my_start.assert_not_called() |
| 133 |
|
| 134 |
ipsec.OpenSwanProcess.active.stop() |
| 135 |
ipsec.OpenSwanProcess._config_changed.stop() |
| 136 |
cfg.CONF.set_override('restart_check_config', False, group='pluto') |
| 137 |
|
| 138 |
def test_openswan_connection_with_non_ascii_vpnservice_name(self): |
| 139 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 140 |
[self.private_nets[1]]) |
| 141 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 142 |
[self.private_nets[2]]) |
| 143 |
site1.vpn_service.update( |
| 144 |
{'name': test_scenario.NON_ASCII_VPNSERVICE_NAME}) |
| 145 |
|
| 146 |
self.check_ping(site1, site2, success=False) |
| 147 |
self.check_ping(site2, site1, success=False) |
| 148 |
|
| 149 |
self.prepare_ipsec_site_connections(site1, site2) |
| 150 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 151 |
|
| 152 |
self.check_ping(site1, site2) |
| 153 |
self.check_ping(site2, site1) |
| 154 |
|
| 155 |
def test_openswan_connection_with_non_ascii_psk(self): |
| 156 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 157 |
[self.private_nets[1]]) |
| 158 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 159 |
[self.private_nets[2]]) |
| 160 |
|
| 161 |
self.check_ping(site1, site2, success=False) |
| 162 |
self.check_ping(site2, site1, success=False) |
| 163 |
|
| 164 |
self.prepare_ipsec_site_connections(site1, site2) |
| 165 |
self._update_ipsec_connection(site1, psk=test_scenario.NON_ASCII_PSK) |
| 166 |
self._update_ipsec_connection(site2, psk=test_scenario.NON_ASCII_PSK) |
| 167 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 168 |
|
| 169 |
self.check_ping(site1, site2) |
| 170 |
self.check_ping(site2, site1) |
| 171 |
|
| 172 |
def test_openswan_connection_with_wrong_non_ascii_psk(self): |
| 173 |
site1 = self.create_site(test_scenario.PUBLIC_NET[4], |
| 174 |
[self.private_nets[1]]) |
| 175 |
site2 = self.create_site(test_scenario.PUBLIC_NET[5], |
| 176 |
[self.private_nets[2]]) |
| 177 |
|
| 178 |
self.check_ping(site1, site2, success=False) |
| 179 |
self.check_ping(site2, site1, success=False) |
| 180 |
|
| 181 |
self.prepare_ipsec_site_connections(site1, site2) |
| 182 |
self._update_ipsec_connection(site1, psk=test_scenario.NON_ASCII_PSK) |
| 183 |
self._update_ipsec_connection(site2, |
| 184 |
psk=test_scenario.NON_ASCII_PSK[:-1]) |
| 185 |
self.sync_to_create_ipsec_connections(site1, site2) |
| 186 |
|
| 187 |
self.check_ping(site1, site2, success=False) |
| 188 |
self.check_ping(site2, site1, success=False) |