blob: 00885ae05b6a88dfbc4fe50f2d7c74b10fe1a936 [file] [log] [blame]
Klement Sekerab4d30532018-11-08 13:00:02 +01001import unittest
2
3from framework import VppTestCase, VppTestRunner
Neale Ranns17dcec02019-01-09 21:22:20 -08004from template_ipsec import TemplateIpsec, IPsecIPv4Params
5from vpp_papi import VppEnum
Klement Sekerab4d30532018-11-08 13:00:02 +01006
7
8class IpsecApiTestCase(VppTestCase):
9 """ IPSec API tests """
10
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070011 @classmethod
12 def setUpClass(cls):
13 super(IpsecApiTestCase, cls).setUpClass()
14
15 @classmethod
16 def tearDownClass(cls):
17 super(IpsecApiTestCase, cls).tearDownClass()
18
Neale Ranns8e4a89b2019-01-23 08:16:17 -080019 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
Neale Ranns17dcec02019-01-09 21:22:20 -080025 self.vpp_esp_protocol = (VppEnum.vl_api_ipsec_proto_t.
26 IPSEC_API_PROTO_ESP)
27 self.vpp_ah_protocol = (VppEnum.vl_api_ipsec_proto_t.
28 IPSEC_API_PROTO_AH)
Neale Ranns8e4a89b2019-01-23 08:16:17 -080029 self.ipv4_params = IPsecIPv4Params()
30
31 def tearDown(self):
32 self.pg0.unconfig_ip4()
33 self.pg0.admin_down()
34 super(IpsecApiTestCase, self).tearDown()
Klement Sekerab4d30532018-11-08 13:00:02 +010035
36 def test_backend_dump(self):
37 """ backend dump """
38 d = self.vapi.ipsec_backend_dump()
39 self.assert_equal(len(d), 2, "number of ipsec backends in dump")
Neale Ranns8e4a89b2019-01-23 08:16:17 -080040 self.assert_equal(d[0].protocol, self.vpp_ah_protocol,
Klement Sekerab4d30532018-11-08 13:00:02 +010041 "ipsec protocol in dump entry")
42 self.assert_equal(d[0].index, 0, "index in dump entry")
43 self.assert_equal(d[0].active, 1, "active flag in dump entry")
Neale Ranns8e4a89b2019-01-23 08:16:17 -080044 self.assert_equal(d[1].protocol, self.vpp_esp_protocol,
Klement Sekerab4d30532018-11-08 13:00:02 +010045 "ipsec protocol in dump entry")
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):
50 """ select valid backend """
Neale Ranns8e4a89b2019-01-23 08:16:17 -080051 self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 0)
52 self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 0)
Klement Sekerab4d30532018-11-08 13:00:02 +010053
54 def test_select_invalid_backend(self):
55 """ select invalid backend """
56 with self.vapi.assert_negative_api_retval():
Neale Ranns8e4a89b2019-01-23 08:16:17 -080057 self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 200)
Klement Sekerab4d30532018-11-08 13:00:02 +010058 with self.vapi.assert_negative_api_retval():
Neale Ranns8e4a89b2019-01-23 08:16:17 -080059 self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 200)
Klement Sekerab4d30532018-11-08 13:00:02 +010060
61 def test_select_backend_in_use(self):
62 """ attempt to change backend while sad configured """
Neale Ranns8e4a89b2019-01-23 08:16:17 -080063 params = self.ipv4_params
Klement Sekerab4d30532018-11-08 13:00:02 +010064 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 Ranns17dcec02019-01-09 21:22:20 -080073 self.vapi.ipsec_sad_entry_add_del(scapy_tun_sa_id, scapy_tun_spi,
Klement Sekerab4d30532018-11-08 13:00:02 +010074 auth_algo_vpp_id, auth_key,
75 crypt_algo_vpp_id, crypt_key,
Neale Ranns8e4a89b2019-01-23 08:16:17 -080076 self.vpp_ah_protocol,
Neale Ranns17dcec02019-01-09 21:22:20 -080077 self.pg0.local_addr[addr_type],
78 self.pg0.remote_addr[addr_type])
Klement Sekerab4d30532018-11-08 13:00:02 +010079 with self.vapi.assert_negative_api_retval():
80 self.vapi.ipsec_select_backend(
Neale Ranns8e4a89b2019-01-23 08:16:17 -080081 protocol=self.vpp_ah_protocol, index=0)
Klement Sekerab4d30532018-11-08 13:00:02 +010082
Neale Ranns17dcec02019-01-09 21:22:20 -080083 self.vapi.ipsec_sad_entry_add_del(scapy_tun_sa_id, scapy_tun_spi,
Klement Sekerab4d30532018-11-08 13:00:02 +010084 auth_algo_vpp_id, auth_key,
85 crypt_algo_vpp_id, crypt_key,
Neale Ranns8e4a89b2019-01-23 08:16:17 -080086 self.vpp_ah_protocol,
Neale Ranns17dcec02019-01-09 21:22:20 -080087 self.pg0.local_addr[addr_type],
88 self.pg0.remote_addr[addr_type],
Klement Sekerab4d30532018-11-08 13:00:02 +010089 is_add=0)
90 self.vapi.ipsec_select_backend(
Neale Ranns8e4a89b2019-01-23 08:16:17 -080091 protocol=self.vpp_ah_protocol, index=0)
Klement Sekerab4d30532018-11-08 13:00:02 +010092
93
94if __name__ == '__main__':
95 unittest.main(testRunner=VppTestRunner)