Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 1 | import unittest |
| 2 | |
| 3 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 4 | from template_ipsec import TemplateIpsec, IPsecIPv4Params |
| 5 | from vpp_papi import VppEnum |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class IpsecApiTestCase(VppTestCase): |
| 9 | """ IPSec API tests """ |
| 10 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 11 | def setUp(self): |
| 12 | super(IpsecApiTestCase, self).setUp() |
| 13 | self.create_pg_interfaces([0]) |
| 14 | self.pg0.config_ip4() |
| 15 | self.pg0.admin_up() |
| 16 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 17 | self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 18 | IPSEC_API_PROTO_ESP) |
| 19 | self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t. |
| 20 | IPSEC_API_PROTO_AH) |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 21 | self.ipv4_params = IPsecIPv4Params() |
| 22 | |
| 23 | def tearDown(self): |
| 24 | self.pg0.unconfig_ip4() |
| 25 | self.pg0.admin_down() |
| 26 | super(IpsecApiTestCase, self).tearDown() |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 27 | |
| 28 | def test_backend_dump(self): |
| 29 | """ backend dump """ |
| 30 | d = self.vapi.ipsec_backend_dump() |
| 31 | self.assert_equal(len(d), 2, "number of ipsec backends in dump") |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 32 | self.assert_equal(d[0].protocol, self.vpp_ah_protocol, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 33 | "ipsec protocol in dump entry") |
| 34 | self.assert_equal(d[0].index, 0, "index in dump entry") |
| 35 | self.assert_equal(d[0].active, 1, "active flag in dump entry") |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 36 | self.assert_equal(d[1].protocol, self.vpp_esp_protocol, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 37 | "ipsec protocol in dump entry") |
| 38 | self.assert_equal(d[1].index, 0, "index in dump entry") |
| 39 | self.assert_equal(d[1].active, 1, "active flag in dump entry") |
| 40 | |
| 41 | def test_select_valid_backend(self): |
| 42 | """ select valid backend """ |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 43 | self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 0) |
| 44 | self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 45 | |
| 46 | def test_select_invalid_backend(self): |
| 47 | """ select invalid backend """ |
| 48 | with self.vapi.assert_negative_api_retval(): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 49 | self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 200) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 50 | with self.vapi.assert_negative_api_retval(): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 51 | self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 200) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 52 | |
| 53 | def test_select_backend_in_use(self): |
| 54 | """ attempt to change backend while sad configured """ |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 55 | params = self.ipv4_params |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 56 | addr_type = params.addr_type |
| 57 | is_ipv6 = params.is_ipv6 |
| 58 | scapy_tun_sa_id = params.scapy_tun_sa_id |
| 59 | scapy_tun_spi = params.scapy_tun_spi |
| 60 | auth_algo_vpp_id = params.auth_algo_vpp_id |
| 61 | auth_key = params.auth_key |
| 62 | crypt_algo_vpp_id = params.crypt_algo_vpp_id |
| 63 | crypt_key = params.crypt_key |
| 64 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 65 | self.vapi.ipsec_sad_entry_add_del(scapy_tun_sa_id, scapy_tun_spi, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 66 | auth_algo_vpp_id, auth_key, |
| 67 | crypt_algo_vpp_id, crypt_key, |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 68 | self.vpp_ah_protocol, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 69 | self.pg0.local_addr[addr_type], |
| 70 | self.pg0.remote_addr[addr_type]) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 71 | with self.vapi.assert_negative_api_retval(): |
| 72 | self.vapi.ipsec_select_backend( |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 73 | protocol=self.vpp_ah_protocol, index=0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 74 | |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 75 | self.vapi.ipsec_sad_entry_add_del(scapy_tun_sa_id, scapy_tun_spi, |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 76 | auth_algo_vpp_id, auth_key, |
| 77 | crypt_algo_vpp_id, crypt_key, |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 78 | self.vpp_ah_protocol, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 79 | self.pg0.local_addr[addr_type], |
| 80 | self.pg0.remote_addr[addr_type], |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 81 | is_add=0) |
| 82 | self.vapi.ipsec_select_backend( |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 83 | protocol=self.vpp_ah_protocol, index=0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | unittest.main(testRunner=VppTestRunner) |