summaryrefslogtreecommitdiffstats
path: root/ipsec/ipsec-impl/src/test/java/io/fd/hc2vpp/ipsec/write/Ikev2PolicyIdentityCustomizerTest.java
blob: 2a62b9494a1c5a830c372f56d3304c58e07ab1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.ipsec.write;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.Ipv4Translator;
import io.fd.hc2vpp.common.translate.util.Ipv6Translator;
import io.fd.hc2vpp.ipsec.helpers.SchemaContextTestHelper;
import io.fd.honeycomb.test.tools.HoneycombTestRunner;
import io.fd.honeycomb.test.tools.annotations.InjectTestData;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.ikev2.dto.Ikev2ProfileSetId;
import io.fd.vpp.jvpp.ikev2.dto.Ikev2ProfileSetIdReply;
import io.fd.vpp.jvpp.ikev2.future.FutureJVppIkev2Facade;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipsec.rev181214.Ikev2;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipsec.rev181214.ike.general.policy.profile.grouping.Identity;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipsec.rev181214.ikev2.Policy;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipsec.rev181214.ikev2.PolicyKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

@RunWith(HoneycombTestRunner.class)
public class Ikev2PolicyIdentityCustomizerTest extends WriterCustomizerTest implements SchemaContextTestHelper,
        ByteDataTranslator, Ipv4Translator, Ipv6Translator {

    private static final String POLICY_NAME = "testPolicy";
    private static final String IPV4_TYPE_DATA = "192.168.123.22";
    private static final String IPV6_TYPE_DATA = "2001:DB8:0:0:8:800:200C:417A";
    private static final String FQDN_TYPE_DATA = "vpp.home";
    private static final String RFC822_TYPE_DATA = "rfc822@example.com";
    private static final String IDENTITY_PATH =
            "/hc2vpp-ietf-ipsec:ikev2/hc2vpp-ietf-ipsec:policy[hc2vpp-ietf-ipsec:name='" + POLICY_NAME +
                    "']/hc2vpp-ietf-ipsec:identity";
    private Ikev2PolicyIdentityCustomizer customizer;
    @Mock
    protected FutureJVppIkev2Facade ikev2api;

    @Override
    protected void setUpTest() throws Exception {
        customizer = new Ikev2PolicyIdentityCustomizer(ikev2api);
        when(ikev2api.ikev2ProfileSetId(any())).thenReturn(future(new Ikev2ProfileSetIdReply()));
    }

    @Test
    public void testWriteLocalIpv4(
            @InjectTestData(resourcePath = "/ikev2/identity/identity_local_ipv4.json", id = IDENTITY_PATH) Identity identity)
            throws WriteFailedException {
        customizer.writeCurrentAttributes(getId(), identity, writeContext);
        Ikev2ProfileSetId request = new Ikev2ProfileSetId();
        request.name = POLICY_NAME.getBytes();
        request.idType = (byte) 1;
        request.isLocal = BYTE_TRUE;
        request.data = ipv4AddressNoZoneToArray(IPV4_TYPE_DATA);
        request.dataLen = request.data.length;
        verify(ikev2api).ikev2ProfileSetId(request);
    }

    @Test
    public void testWriteRemoteFqdn(
            @InjectTestData(resourcePath = "/ikev2/identity/identity_remote_fqdn.json", id = IDENTITY_PATH) Identity identity)
            throws WriteFailedException {
        customizer.writeCurrentAttributes(getId(), identity, writeContext);
        Ikev2ProfileSetId request = new Ikev2ProfileSetId();
        request.name = POLICY_NAME.getBytes();
        request.idType = (byte) 2;
        request.isLocal = BYTE_FALSE;
        request.data = FQDN_TYPE_DATA.getBytes();
        request.dataLen = request.data.length;
        verify(ikev2api).ikev2ProfileSetId(request);
    }

    @Test
    public void testWriteLocalIpv6(
            @InjectTestData(resourcePath = "/ikev2/identity/identity_remote_ipv6.json", id = IDENTITY_PATH) Identity identity)
            throws WriteFailedException {
        customizer.writeCurrentAttributes(getId(), identity, writeContext);
        Ikev2ProfileSetId request = new Ikev2ProfileSetId();
        request.name = POLICY_NAME.getBytes();
        request.idType = (byte) 5;
        request.isLocal = BYTE_FALSE;
        request.data = ipv6AddressNoZoneToArray(new Ipv6Address(IPV6_TYPE_DATA));
        request.dataLen = request.data.length;
        verify(ikev2api).ikev2ProfileSetId(request);
    }

    @Test
    public void testUpdate(
            @InjectTestData(resourcePath = "/ikev2/identity/identity_local_ipv4.json", id = IDENTITY_PATH) Identity before,
            @InjectTestData(resourcePath = "/ikev2/identity/identity_local_rfc822.json", id = IDENTITY_PATH) Identity after)
            throws WriteFailedException {
        customizer.updateCurrentAttributes(getId(), before, after, writeContext);
        Ikev2ProfileSetId request = new Ikev2ProfileSetId();
        request.name = POLICY_NAME.getBytes();
        request.idType = (byte) 3;
        request.isLocal = BYTE_TRUE;
        request.data = RFC822_TYPE_DATA.getBytes();
        request.dataLen = request.data.length;
        verify(ikev2api).ikev2ProfileSetId(request);
    }

    private InstanceIdentifier<Identity> getId() {
        return InstanceIdentifier.create(Ikev2.class).child(Policy.class, new PolicyKey(POLICY_NAME))
                .child(Identity.class);
    }
}