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): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 9 | """IPSec API tests""" |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 10 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 11 | @classmethod |
| 12 | def setUpClass(cls): |
| 13 | super(IpsecApiTestCase, cls).setUpClass() |
| 14 | |
| 15 | @classmethod |
| 16 | def tearDownClass(cls): |
| 17 | super(IpsecApiTestCase, cls).tearDownClass() |
| 18 | |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 19 | def setUp(self): |
| 20 | super(IpsecApiTestCase, self).setUp() |
| 21 | self.create_pg_interfaces([0]) |
| 22 | self.pg0.config_ip4() |
| 23 | self.pg0.admin_up() |
| 24 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 25 | self.vpp_esp_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_ESP |
| 26 | self.vpp_ah_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_AH |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 27 | self.ipv4_params = IPsecIPv4Params() |
| 28 | |
| 29 | def tearDown(self): |
| 30 | self.pg0.unconfig_ip4() |
| 31 | self.pg0.admin_down() |
| 32 | super(IpsecApiTestCase, self).tearDown() |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 33 | |
| 34 | def test_backend_dump(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 35 | """backend dump""" |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 36 | d = self.vapi.ipsec_backend_dump() |
| 37 | self.assert_equal(len(d), 2, "number of ipsec backends in dump") |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | self.assert_equal( |
| 39 | d[0].protocol, self.vpp_ah_protocol, "ipsec protocol in dump entry" |
| 40 | ) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 41 | self.assert_equal(d[0].index, 0, "index in dump entry") |
| 42 | self.assert_equal(d[0].active, 1, "active flag in dump entry") |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 43 | self.assert_equal( |
| 44 | d[1].protocol, self.vpp_esp_protocol, "ipsec protocol in dump entry" |
| 45 | ) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 46 | self.assert_equal(d[1].index, 0, "index in dump entry") |
| 47 | self.assert_equal(d[1].active, 1, "active flag in dump entry") |
| 48 | |
| 49 | def test_select_valid_backend(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 50 | """select valid backend""" |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 51 | self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 0) |
| 52 | self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 53 | |
| 54 | def test_select_invalid_backend(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 55 | """select invalid backend""" |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 56 | with self.vapi.assert_negative_api_retval(): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 57 | self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 200) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 58 | with self.vapi.assert_negative_api_retval(): |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 59 | self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 200) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 60 | |
| 61 | def test_select_backend_in_use(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 62 | """attempt to change backend while sad configured""" |
Neale Ranns | 8e4a89b | 2019-01-23 08:16:17 -0800 | [diff] [blame] | 63 | params = self.ipv4_params |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 64 | addr_type = params.addr_type |
| 65 | is_ipv6 = params.is_ipv6 |
| 66 | scapy_tun_sa_id = params.scapy_tun_sa_id |
| 67 | scapy_tun_spi = params.scapy_tun_spi |
| 68 | auth_algo_vpp_id = params.auth_algo_vpp_id |
| 69 | auth_key = params.auth_key |
| 70 | crypt_algo_vpp_id = params.crypt_algo_vpp_id |
| 71 | crypt_key = params.crypt_key |
| 72 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 73 | self.vapi.ipsec_sad_entry_add_del( |
| 74 | is_add=1, |
| 75 | entry={ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 76 | "sad_id": scapy_tun_sa_id, |
| 77 | "spi": scapy_tun_spi, |
| 78 | "integrity_algorithm": auth_algo_vpp_id, |
| 79 | "integrity_key": { |
| 80 | "data": auth_key, |
| 81 | "length": len(auth_key), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 82 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 83 | "crypto_algorithm": crypt_algo_vpp_id, |
| 84 | "crypto_key": { |
| 85 | "data": crypt_key, |
| 86 | "length": len(crypt_key), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 87 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 88 | "protocol": self.vpp_ah_protocol, |
| 89 | "tunnel_src": self.pg0.local_addr[addr_type], |
| 90 | "tunnel_dst": self.pg0.remote_addr[addr_type], |
| 91 | }, |
| 92 | ) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 93 | with self.vapi.assert_negative_api_retval(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 94 | self.vapi.ipsec_select_backend(protocol=self.vpp_ah_protocol, index=0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 95 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 96 | self.vapi.ipsec_sad_entry_add_del( |
| 97 | is_add=0, |
| 98 | entry={ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 99 | "sad_id": scapy_tun_sa_id, |
| 100 | "spi": scapy_tun_spi, |
| 101 | "integrity_algorithm": auth_algo_vpp_id, |
| 102 | "integrity_key": { |
| 103 | "data": auth_key, |
| 104 | "length": len(auth_key), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 105 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 106 | "crypto_algorithm": crypt_algo_vpp_id, |
| 107 | "crypto_key": { |
| 108 | "data": crypt_key, |
| 109 | "length": len(crypt_key), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 110 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | "protocol": self.vpp_ah_protocol, |
| 112 | "tunnel_src": self.pg0.local_addr[addr_type], |
| 113 | "tunnel_dst": self.pg0.remote_addr[addr_type], |
| 114 | }, |
| 115 | ) |
| 116 | self.vapi.ipsec_select_backend(protocol=self.vpp_ah_protocol, index=0) |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 117 | |
| 118 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | if __name__ == "__main__": |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 120 | unittest.main(testRunner=VppTestRunner) |