4510aee6c4623d195d4397d3a491966a18bbbd50
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleEntityDecoderTest.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.ExpectedException;
21 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
22 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
23 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
24 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.EndpointIdentificationConstraints;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.L3EndpointIdentificationConstraints;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.parameter.value.RangeValue;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
55
56 public class SecRuleEntityDecoderTest {
57
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     private SecurityRuleBuilder secRuleBuilder;
62
63     @Before
64     public void setUp() throws Exception {
65         secRuleBuilder = new SecurityRuleBuilder().setUuid(new Uuid("01234567-abcd-ef01-0123-0123456789ab"));
66     }
67
68     @Test
69     public final void testGetContractId() {
70         Assert.assertEquals("01234567-abcd-ef01-0123-0123456789ab",
71                 SecRuleEntityDecoder.getContractId(secRuleBuilder.build()).getValue());
72     }
73
74     @Test
75     public final void testGetClassifierInstance_onlyEthertype() {
76         secRuleBuilder.setEthertype(EthertypeV4.class);
77         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
78         Assert.assertEquals(EtherTypeClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
79         // name is ether_type_IPv4
80         String expectedName = new StringBuilder().append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
81             .append(MappingUtils.NAME_VALUE_DELIMETER)
82             .append(EthertypeV4.class.getSimpleName())
83             .toString();
84         Assert.assertEquals(expectedName, ci.getName().getValue());
85         Assert.assertEquals(expectedName, ci.getName().getValue());
86         List<ParameterValue> parameterValues = ci.getParameterValue();
87         Assert.assertNotNull(parameterValues);
88         Assert.assertEquals(1, parameterValues.size());
89         ParameterValue parameter = parameterValues.get(0);
90         Assert.assertEquals(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, parameter.getName().getValue());
91         assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
92     }
93
94     @Test
95     public final void testGetClassifierInstance_EthertypeAndProtocol() {
96         secRuleBuilder.setEthertype(EthertypeV4.class);
97         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
98         secRuleBuilder.setProtocol(protocolTcp);
99         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
100         Assert.assertEquals(IpProtoClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
101         // name is ip_proto_tcp__ether_type_IPv4
102         String expectedName = new StringBuilder().append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
103             .append(MappingUtils.NAME_VALUE_DELIMETER)
104             .append(ProtocolTcp.class.getSimpleName())
105             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
106             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
107             .append(MappingUtils.NAME_VALUE_DELIMETER)
108             .append(EthertypeV4.class.getSimpleName())
109             .toString();
110         Assert.assertEquals(expectedName, ci.getName().getValue());
111         List<ParameterValue> parameterValues = ci.getParameterValue();
112         Assert.assertNotNull(parameterValues);
113         Assert.assertEquals(2, parameterValues.size());
114         boolean containsEthertypeParam = false;
115         boolean containsProtoParam = false;
116         for (ParameterValue parameter : parameterValues) {
117             ParameterName parameterName = parameter.getName();
118             Assert.assertNotNull(parameterName);
119             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
120                 containsEthertypeParam = true;
121                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
122             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
123                 containsProtoParam = true;
124                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
125             } else {
126                 fail("This parameter is not expected: " + parameter);
127             }
128         }
129         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
130         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
131     }
132
133     @Test
134     public final void testGetClassifierInstance_EthertypeAndProtocolAndPort() {
135         secRuleBuilder.setEthertype(EthertypeV4.class);
136         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
137         secRuleBuilder.setProtocol(protocolTcp);
138         secRuleBuilder.setPortRangeMin(5);
139         secRuleBuilder.setPortRangeMax(5);
140         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
141         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
142         // name is l4_destport-5__ip_proto-tcp__ether_type-IPv4
143         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
144             .append(MappingUtils.NAME_DELIMETER)
145             .append(L4ClassifierDefinition.DST_PORT_PARAM)
146             .append(MappingUtils.NAME_VALUE_DELIMETER)
147             .append(secRuleBuilder.getPortRangeMin())
148             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
149             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
150             .append(MappingUtils.NAME_VALUE_DELIMETER)
151             .append(ProtocolTcp.class.getSimpleName())
152             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
153             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
154             .append(MappingUtils.NAME_VALUE_DELIMETER)
155             .append(EthertypeV4.class.getSimpleName())
156             .toString();
157         System.out.println(expectedName);
158         System.out.println(ci.getName().getValue());
159         Assert.assertEquals(expectedName, ci.getName().getValue());
160         List<ParameterValue> parameterValues = ci.getParameterValue();
161         Assert.assertNotNull(parameterValues);
162         Assert.assertEquals(3, parameterValues.size());
163         boolean containsEthertypeParam = false;
164         boolean containsProtoParam = false;
165         boolean containsDstPortParam = false;
166         for (ParameterValue parameter : parameterValues) {
167             ParameterName parameterName = parameter.getName();
168             Assert.assertNotNull(parameterName);
169             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
170                 containsEthertypeParam = true;
171                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
172             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
173                 containsProtoParam = true;
174                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
175             } else if (L4ClassifierDefinition.DST_PORT_PARAM.equals(parameterName.getValue())) {
176                 containsDstPortParam = true;
177                 assertClassifierParameterValue(parameter, 5L, null, null);
178             } else {
179                 fail("This parameter is not expected: " + parameter);
180             }
181         }
182         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
183         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
184         Assert.assertTrue("Classifier-instance does not contain destination port parameter", containsDstPortParam);
185     }
186
187     private final void assertClassifierParameterValue(ParameterValue parameter, Long expectedIntValue,
188             String expectedStringValue, RangeValue expectedRangeValue) {
189         Assert.assertEquals(expectedIntValue, parameter.getIntValue());
190         Assert.assertEquals(expectedStringValue, parameter.getStringValue());
191         Assert.assertEquals(expectedRangeValue, parameter.getRangeValue());
192     }
193
194     @Test
195     public final void testGetClassifierInstance_EthertypeAndProtocolAndPorts() {
196         secRuleBuilder.setEthertype(EthertypeV4.class);
197         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
198         secRuleBuilder.setProtocol(protocolTcp);
199         secRuleBuilder.setPortRangeMin(5);
200         secRuleBuilder.setPortRangeMax(10);
201         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
202         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
203         // name is l4_destport_range_min-5_max-10__ip_proto-tcp__ether_type-IPv4
204         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
205             .append(MappingUtils.NAME_DELIMETER)
206             .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
207             .append(SecRuleNameDecoder.MIN_PORT)
208             .append(MappingUtils.NAME_VALUE_DELIMETER)
209             .append(secRuleBuilder.getPortRangeMin())
210             .append(SecRuleNameDecoder.MAX_PORT)
211             .append(MappingUtils.NAME_VALUE_DELIMETER)
212             .append(secRuleBuilder.getPortRangeMax())
213             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
214             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
215             .append(MappingUtils.NAME_VALUE_DELIMETER)
216             .append(ProtocolTcp.class.getSimpleName())
217             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
218             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
219             .append(MappingUtils.NAME_VALUE_DELIMETER)
220             .append(EthertypeV4.class.getSimpleName())
221             .toString();
222         Assert.assertEquals(expectedName, ci.getName().getValue());
223     }
224
225     @Test
226     public final void testGetClassifierRef() {
227         secRuleBuilder.setDirection(DirectionIngress.class);
228         secRuleBuilder.setEthertype(EthertypeV4.class);
229         ClassifierName expectedName = SecRuleNameDecoder.getClassifierRefName(secRuleBuilder.build());
230         ClassifierRef cr = SecRuleEntityDecoder.getClassifierRef(secRuleBuilder.build());
231         Assert.assertEquals(expectedName, cr.getName());
232     }
233
234     @Test
235     public final void testGetDirection_directionIngress() {
236         secRuleBuilder.setDirection(DirectionIngress.class);
237         Assert.assertEquals(Direction.In, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
238     }
239
240     @Test
241     public final void testGetDirection_directionEgress() {
242         secRuleBuilder.setDirection(DirectionEgress.class);
243         Assert.assertEquals(Direction.Out, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
244     }
245
246     @Test
247     public final void testGetDirection_directionNull() {
248         secRuleBuilder.setDirection(null);
249         thrown.expect(IllegalArgumentException.class);
250         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
251     }
252
253     @Test
254     public final void testGetDirection_directionUnknown() {
255         secRuleBuilder.setDirection(UnknownDirection.class);
256         thrown.expect(IllegalArgumentException.class);
257         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
258     }
259
260     private static class UnknownDirection extends DirectionBase {
261     }
262
263     @Test
264     public final void testGetClause_noRemoteIpPrefix() {
265         secRuleBuilder.setDirection(DirectionIngress.class);
266         secRuleBuilder.setEthertype(EthertypeV4.class);
267         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
268         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
269         Assert.assertEquals(expectedClauseName, clause.getName());
270         List<SubjectName> subjectRefs = clause.getSubjectRefs();
271         Assert.assertNotNull(subjectRefs);
272         Assert.assertEquals(1, subjectRefs.size());
273         SubjectName subjectNameFromClause = subjectRefs.get(0);
274         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
275         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
276         Assert.assertNull(clause.getConsumerMatchers());
277         Assert.assertNull(clause.getProviderMatchers());
278     }
279
280     @Test
281     public final void testGetClause_remoteIpPrefix() {
282         secRuleBuilder.setDirection(DirectionIngress.class);
283         secRuleBuilder.setEthertype(EthertypeV4.class);
284         secRuleBuilder.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
285         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
286         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
287         Assert.assertEquals(expectedClauseName, clause.getName());
288         List<SubjectName> subjectRefs = clause.getSubjectRefs();
289         Assert.assertNotNull(subjectRefs);
290         Assert.assertEquals(1, subjectRefs.size());
291         SubjectName subjectNameFromClause = subjectRefs.get(0);
292         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
293         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
294         Assert.assertNull(clause.getProviderMatchers());
295         ConsumerMatchers consumerMatchers = clause.getConsumerMatchers();
296         Assert.assertNotNull(consumerMatchers);
297         Assert.assertNull(consumerMatchers.getConditionMatcher());
298         Assert.assertNull(consumerMatchers.getGroupIdentificationConstraints());
299         EndpointIdentificationConstraints endpointIdentificationConstraints =
300                 consumerMatchers.getEndpointIdentificationConstraints();
301         Assert.assertNotNull(endpointIdentificationConstraints);
302         L3EndpointIdentificationConstraints l3EndpointIdentificationConstraints =
303                 endpointIdentificationConstraints.getL3EndpointIdentificationConstraints();
304         Assert.assertNotNull(l3EndpointIdentificationConstraints);
305         List<PrefixConstraint> prefixConstraints = l3EndpointIdentificationConstraints.getPrefixConstraint();
306         Assert.assertNotNull(prefixConstraints);
307         Assert.assertEquals(1, prefixConstraints.size());
308         PrefixConstraint prefixConstraint = prefixConstraints.get(0);
309         Assert.assertEquals(new Ipv4Prefix("10.0.0.0/8"), prefixConstraint.getIpPrefix().getIpv4Prefix());
310     }
311
312     @Test
313     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv4() {
314         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
315         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
316         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
317     }
318
319     @Test
320     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeNull() {
321         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
322         SecurityRule two = secRuleBuilder.setEthertype(null).build();
323         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
324     }
325
326     @Test
327     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeNull() {
328         SecurityRule one = secRuleBuilder.setEthertype(null).build();
329         SecurityRule two = secRuleBuilder.setEthertype(null).build();
330         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
331     }
332
333     @Test
334     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv6() {
335         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
336         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV6.class).build();
337         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
338     }
339
340     @Test
341     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeIPv4() {
342         SecurityRule one = secRuleBuilder.setEthertype(null).build();
343         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
344         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
345     }
346
347     @Test
348     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolTcp() {
349         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
350         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
351         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
352         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
353     }
354
355     @Test
356     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolNull() {
357         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
358         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
359         SecurityRule two = secRuleBuilder.setProtocol(null).build();
360         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
361     }
362
363     @Test
364     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolNull() {
365         SecurityRule one = secRuleBuilder.setProtocol(null).build();
366         SecurityRule two = secRuleBuilder.setProtocol(null).build();
367         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
368     }
369
370     @Test
371     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolUdp() {
372         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
373         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
374         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
375         SecurityRule two = secRuleBuilder.setProtocol(protocolUdp).build();
376         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
377     }
378
379     @Test
380     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolTcp() {
381         SecurityRule one = secRuleBuilder.setProtocol(null).build();
382         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
383         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
384         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
385     }
386
387     @Test
388     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxLtTwoPortMax() {
389         SecurityRule one = createSecRuleWithMinMaxPort(6, 9);
390         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
391         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
392     }
393
394     @Test
395     public final void testIsPortsOfOneWithinTwo_onePortMinEqTwoPortMinOnePortMaxEqTwoPortMax() {
396         SecurityRule one = createSecRuleWithMinMaxPort(5, 10);
397         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
398         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
399     }
400
401     @Test
402     public final void testIsPortsOfOneWithinTwo_onePortMinTwoPortMinNullOnePortMaxTwoPortMaxNull() {
403         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
404         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
405         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
406     }
407
408     @Test
409     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinNullOnePortMaxNullTwoPortMaxNull() {
410         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
411         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
412         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
413     }
414
415     @Test
416     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinOnePortMaxNullTwoPortMax() {
417         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
418         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
419         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
420     }
421
422     @Test
423     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxLtTwoPortMax() {
424         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
425         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
426         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
427     }
428
429     @Test
430     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxGtTwoPortMax() {
431         SecurityRule one = createSecRuleWithMinMaxPort(6, 11);
432         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
433         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
434     }
435
436     @Test
437     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxGtTwoPortMax() {
438         SecurityRule one = createSecRuleWithMinMaxPort(4, 11);
439         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
440         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
441     }
442
443     private SecurityRule createSecRuleWithMinMaxPort(Integer portMin, Integer portMax) {
444         return secRuleBuilder.setPortRangeMin(portMin).setPortRangeMax(portMax).build();
445     }
446
447     @Test
448     public final void testGetEtherType_ethertypeIPv4() {
449         secRuleBuilder.setEthertype(EthertypeV4.class);
450         Assert.assertEquals(EtherTypeClassifierDefinition.IPv4_VALUE, SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
451     }
452
453     @Test
454     public final void testGetEtherType_ethertypeIPv6() {
455         secRuleBuilder.setEthertype(EthertypeV6.class);
456         Assert.assertEquals(EtherTypeClassifierDefinition.IPv6_VALUE, SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
457     }
458
459     @Test
460     public final void testGetEtherType_ethertypeNull() {
461         secRuleBuilder.setEthertype(null);
462         Assert.assertNull(SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
463     }
464
465     @Test
466     public final void testGetEtherType_ethertypeUnknown() {
467         secRuleBuilder.setEthertype(UnknownEthertype.class);
468         thrown.expect(IllegalArgumentException.class);
469         SecRuleEntityDecoder.getEtherType(secRuleBuilder.build());
470     }
471
472     private static class UnknownEthertype extends EthertypeBase {
473     }
474
475     @Test
476     public final void testGetProtocol_protoTcp() {
477         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
478         secRuleBuilder.setProtocol(protocolTcp);
479         Assert.assertEquals(IpProtoClassifierDefinition.TCP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
480     }
481
482     @Test
483     public final void testGetProtocol_protoUdp() {
484         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
485         secRuleBuilder.setProtocol(protocolUdp);
486         Assert.assertEquals(IpProtoClassifierDefinition.UDP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
487     }
488
489     @Test
490     public final void testGetProtocol_protoIcmp() {
491         SecurityRuleAttributes.Protocol protocolIcmp = new SecurityRuleAttributes.Protocol(ProtocolIcmp.class);
492         secRuleBuilder.setProtocol(protocolIcmp);
493         Assert.assertEquals(IpProtoClassifierDefinition.ICMP_VALUE, SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
494     }
495
496     @Test
497     public final void testGetProtocol_protoNull() {
498         secRuleBuilder.setProtocol(null);
499         Assert.assertNull(SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
500     }
501
502     @Test
503     public final void testGetProtocol_protoUnknown() {
504         SecurityRuleAttributes.Protocol protocolUnknown = new SecurityRuleAttributes.Protocol(UnknownProtocol.class);
505         secRuleBuilder.setProtocol(protocolUnknown);
506         thrown.expect(IllegalArgumentException.class);
507         SecRuleEntityDecoder.getProtocol(secRuleBuilder.build());
508     }
509
510     private static class UnknownProtocol extends ProtocolBase {
511     }
512 }