Fix test identities
[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         Assert.assertEquals(expectedName, ci.getName().getValue());
158         List<ParameterValue> parameterValues = ci.getParameterValue();
159         Assert.assertNotNull(parameterValues);
160         Assert.assertEquals(3, parameterValues.size());
161         boolean containsEthertypeParam = false;
162         boolean containsProtoParam = false;
163         boolean containsDstPortParam = false;
164         for (ParameterValue parameter : parameterValues) {
165             ParameterName parameterName = parameter.getName();
166             Assert.assertNotNull(parameterName);
167             if (EtherTypeClassifierDefinition.ETHERTYPE_PARAM.equals(parameterName.getValue())) {
168                 containsEthertypeParam = true;
169                 assertClassifierParameterValue(parameter, EtherTypeClassifierDefinition.IPv4_VALUE, null, null);
170             } else if (IpProtoClassifierDefinition.PROTO_PARAM.equals(parameterName.getValue())) {
171                 containsProtoParam = true;
172                 assertClassifierParameterValue(parameter, IpProtoClassifierDefinition.TCP_VALUE, null, null);
173             } else if (L4ClassifierDefinition.DST_PORT_PARAM.equals(parameterName.getValue())) {
174                 containsDstPortParam = true;
175                 assertClassifierParameterValue(parameter, 5L, null, null);
176             } else {
177                 fail("This parameter is not expected: " + parameter);
178             }
179         }
180         Assert.assertTrue("Classifier-instance does not contain ethertype parameter", containsEthertypeParam);
181         Assert.assertTrue("Classifier-instance does not contain protocol parameter", containsProtoParam);
182         Assert.assertTrue("Classifier-instance does not contain destination port parameter", containsDstPortParam);
183     }
184
185     private void assertClassifierParameterValue(ParameterValue parameter, Long expectedIntValue,
186             String expectedStringValue, RangeValue expectedRangeValue) {
187         Assert.assertEquals(expectedIntValue, parameter.getIntValue());
188         Assert.assertEquals(expectedStringValue, parameter.getStringValue());
189         Assert.assertEquals(expectedRangeValue, parameter.getRangeValue());
190     }
191
192     @Test
193     public final void testGetClassifierInstance_EthertypeAndProtocolAndPorts() {
194         secRuleBuilder.setEthertype(EthertypeV4.class);
195         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
196         secRuleBuilder.setProtocol(protocolTcp);
197         secRuleBuilder.setPortRangeMin(5);
198         secRuleBuilder.setPortRangeMax(10);
199         ClassifierInstance ci = SecRuleEntityDecoder.getClassifierInstance(secRuleBuilder.build());
200         Assert.assertEquals(L4ClassifierDefinition.DEFINITION.getId(), ci.getClassifierDefinitionId());
201         // name is l4_destport_range_min-5_max-10__ip_proto-tcp__ether_type-IPv4
202         String expectedName = new StringBuilder().append(L4ClassifierDefinition.DEFINITION.getName().getValue())
203             .append(MappingUtils.NAME_DELIMETER)
204             .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
205             .append(SecRuleNameDecoder.MIN_PORT)
206             .append(MappingUtils.NAME_VALUE_DELIMETER)
207             .append(secRuleBuilder.getPortRangeMin())
208             .append(SecRuleNameDecoder.MAX_PORT)
209             .append(MappingUtils.NAME_VALUE_DELIMETER)
210             .append(secRuleBuilder.getPortRangeMax())
211             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
212             .append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
213             .append(MappingUtils.NAME_VALUE_DELIMETER)
214             .append(ProtocolTcp.class.getSimpleName())
215             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
216             .append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
217             .append(MappingUtils.NAME_VALUE_DELIMETER)
218             .append(EthertypeV4.class.getSimpleName())
219             .toString();
220         Assert.assertEquals(expectedName, ci.getName().getValue());
221     }
222
223     @Test
224     public final void testGetClassifierRef() {
225         secRuleBuilder.setDirection(DirectionIngress.class);
226         secRuleBuilder.setEthertype(EthertypeV4.class);
227         ClassifierName expectedName = SecRuleNameDecoder.getClassifierRefName(secRuleBuilder.build());
228         ClassifierRef cr = SecRuleEntityDecoder.getClassifierRef(secRuleBuilder.build());
229         Assert.assertEquals(expectedName, cr.getName());
230     }
231
232     @Test
233     public final void testGetDirection_directionIngress() {
234         secRuleBuilder.setDirection(DirectionIngress.class);
235         Assert.assertEquals(Direction.In, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
236     }
237
238     @Test
239     public final void testGetDirection_directionEgress() {
240         secRuleBuilder.setDirection(DirectionEgress.class);
241         Assert.assertEquals(Direction.Out, SecRuleEntityDecoder.getDirection(secRuleBuilder.build()));
242     }
243
244     @Test
245     public final void testGetDirection_directionNull() {
246         secRuleBuilder.setDirection(null);
247         thrown.expect(IllegalArgumentException.class);
248         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
249     }
250
251     @Test
252     public final void testGetDirection_directionUnknown() {
253         secRuleBuilder.setDirection(UnknownDirection.class);
254         thrown.expect(IllegalArgumentException.class);
255         SecRuleEntityDecoder.getDirection(secRuleBuilder.build());
256     }
257
258     private interface UnknownDirection extends DirectionBase {
259
260     }
261
262     @Test
263     public final void testGetClause_noRemoteIpPrefix() {
264         secRuleBuilder.setDirection(DirectionIngress.class);
265         secRuleBuilder.setEthertype(EthertypeV4.class);
266         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
267         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
268         Assert.assertEquals(expectedClauseName, clause.getName());
269         List<SubjectName> subjectRefs = clause.getSubjectRefs();
270         Assert.assertNotNull(subjectRefs);
271         Assert.assertEquals(1, subjectRefs.size());
272         SubjectName subjectNameFromClause = subjectRefs.get(0);
273         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
274         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
275         Assert.assertNull(clause.getConsumerMatchers());
276         Assert.assertNull(clause.getProviderMatchers());
277     }
278
279     @Test
280     public final void testGetClause_remoteIpPrefix() {
281         secRuleBuilder.setDirection(DirectionIngress.class);
282         secRuleBuilder.setEthertype(EthertypeV4.class);
283         secRuleBuilder.setRemoteIpPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8")));
284         Clause clause = SecRuleEntityDecoder.getClause(secRuleBuilder.build());
285         ClauseName expectedClauseName = SecRuleNameDecoder.getClauseName(secRuleBuilder.build());
286         Assert.assertEquals(expectedClauseName, clause.getName());
287         List<SubjectName> subjectRefs = clause.getSubjectRefs();
288         Assert.assertNotNull(subjectRefs);
289         Assert.assertEquals(1, subjectRefs.size());
290         SubjectName subjectNameFromClause = subjectRefs.get(0);
291         SubjectName expectedSubjectName = SecRuleNameDecoder.getSubjectName(secRuleBuilder.build());
292         Assert.assertEquals(expectedSubjectName, subjectNameFromClause);
293         Assert.assertNull(clause.getProviderMatchers());
294         ConsumerMatchers consumerMatchers = clause.getConsumerMatchers();
295         Assert.assertNotNull(consumerMatchers);
296         Assert.assertNull(consumerMatchers.getConditionMatcher());
297         Assert.assertNull(consumerMatchers.getGroupIdentificationConstraints());
298         EndpointIdentificationConstraints endpointIdentificationConstraints =
299                 consumerMatchers.getEndpointIdentificationConstraints();
300         Assert.assertNotNull(endpointIdentificationConstraints);
301         L3EndpointIdentificationConstraints l3EndpointIdentificationConstraints =
302                 endpointIdentificationConstraints.getL3EndpointIdentificationConstraints();
303         Assert.assertNotNull(l3EndpointIdentificationConstraints);
304         List<PrefixConstraint> prefixConstraints = l3EndpointIdentificationConstraints.getPrefixConstraint();
305         Assert.assertNotNull(prefixConstraints);
306         Assert.assertEquals(1, prefixConstraints.size());
307         PrefixConstraint prefixConstraint = prefixConstraints.get(0);
308         Assert.assertEquals(new Ipv4Prefix("10.0.0.0/8"), prefixConstraint.getIpPrefix().getIpv4Prefix());
309     }
310
311     @Test
312     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv4() {
313         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
314         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
315         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
316     }
317
318     @Test
319     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeNull() {
320         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
321         SecurityRule two = secRuleBuilder.setEthertype(null).build();
322         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
323     }
324
325     @Test
326     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeNull() {
327         SecurityRule one = secRuleBuilder.setEthertype(null).build();
328         SecurityRule two = secRuleBuilder.setEthertype(null).build();
329         assertTrue(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
330     }
331
332     @Test
333     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeIPv4TwoEthTypeIPv6() {
334         SecurityRule one = secRuleBuilder.setEthertype(EthertypeV4.class).build();
335         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV6.class).build();
336         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
337     }
338
339     @Test
340     public final void testIsEtherTypeOfOneWithinTwo_oneEthTypeNullTwoEthTypeIPv4() {
341         SecurityRule one = secRuleBuilder.setEthertype(null).build();
342         SecurityRule two = secRuleBuilder.setEthertype(EthertypeV4.class).build();
343         assertFalse(SecRuleEntityDecoder.isEtherTypeOfOneWithinTwo(one, two));
344     }
345
346     @Test
347     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolTcp() {
348         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
349         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
350         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
351         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
352     }
353
354     @Test
355     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolNull() {
356         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
357         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
358         SecurityRule two = secRuleBuilder.setProtocol(null).build();
359         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
360     }
361
362     @Test
363     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolNull() {
364         SecurityRule one = secRuleBuilder.setProtocol(null).build();
365         SecurityRule two = secRuleBuilder.setProtocol(null).build();
366         assertTrue(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
367     }
368
369     @Test
370     public void testIsProtocolOfOneWithinTwo_oneProtocolTcpTwoProtocolUdp() {
371         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
372         SecurityRule one = secRuleBuilder.setProtocol(protocolTcp).build();
373         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
374         SecurityRule two = secRuleBuilder.setProtocol(protocolUdp).build();
375         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
376     }
377
378     @Test
379     public void testIsProtocolOfOneWithinTwo_oneProtocolNullTwoProtocolTcp() {
380         SecurityRule one = secRuleBuilder.setProtocol(null).build();
381         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
382         SecurityRule two = secRuleBuilder.setProtocol(protocolTcp).build();
383         assertFalse(SecRuleEntityDecoder.isProtocolOfOneWithinTwo(one, two));
384     }
385
386     @Test
387     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxLtTwoPortMax() {
388         SecurityRule one = createSecRuleWithMinMaxPort(6, 9);
389         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
390         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
391     }
392
393     @Test
394     public final void testIsPortsOfOneWithinTwo_onePortMinEqTwoPortMinOnePortMaxEqTwoPortMax() {
395         SecurityRule one = createSecRuleWithMinMaxPort(5, 10);
396         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
397         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
398     }
399
400     @Test
401     public final void testIsPortsOfOneWithinTwo_onePortMinTwoPortMinNullOnePortMaxTwoPortMaxNull() {
402         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
403         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
404         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
405     }
406
407     @Test
408     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinNullOnePortMaxNullTwoPortMaxNull() {
409         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
410         SecurityRule two = createSecRuleWithMinMaxPort(null, null);
411         assertTrue(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
412     }
413
414     @Test
415     public final void testIsPortsOfOneWithinTwo_onePortMinNullTwoPortMinOnePortMaxNullTwoPortMax() {
416         SecurityRule one = createSecRuleWithMinMaxPort(null, null);
417         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
418         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
419     }
420
421     @Test
422     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxLtTwoPortMax() {
423         SecurityRule one = createSecRuleWithMinMaxPort(4, 9);
424         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
425         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
426     }
427
428     @Test
429     public final void testIsPortsOfOneWithinTwo_onePortMinGtTwoPortMinOnePortMaxGtTwoPortMax() {
430         SecurityRule one = createSecRuleWithMinMaxPort(6, 11);
431         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
432         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
433     }
434
435     @Test
436     public final void testIsPortsOfOneWithinTwo_onePortMinLtTwoPortMinOnePortMaxGtTwoPortMax() {
437         SecurityRule one = createSecRuleWithMinMaxPort(4, 11);
438         SecurityRule two = createSecRuleWithMinMaxPort(5, 10);
439         assertFalse(SecRuleEntityDecoder.isPortsOfOneWithinTwo(one, two));
440     }
441
442     private SecurityRule createSecRuleWithMinMaxPort(Integer portMin, Integer portMax) {
443         return secRuleBuilder.setPortRangeMin(portMin).setPortRangeMax(portMax).build();
444     }
445
446     @Test
447     public final void testGetEtherType_ethertypeIPv4() {
448         secRuleBuilder.setEthertype(EthertypeV4.class);
449         Assert.assertEquals(EtherTypeClassifierDefinition.IPv4_VALUE,
450             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,
457             SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
458     }
459
460     @Test
461     public final void testGetEtherType_ethertypeNull() {
462         secRuleBuilder.setEthertype(null);
463         Assert.assertNull(SecRuleEntityDecoder.getEtherType(secRuleBuilder.build()));
464     }
465
466     @Test
467     public final void testGetEtherType_ethertypeUnknown() {
468         secRuleBuilder.setEthertype(UnknownEthertype.class);
469         thrown.expect(IllegalArgumentException.class);
470         SecRuleEntityDecoder.getEtherType(secRuleBuilder.build());
471     }
472
473     private interface UnknownEthertype extends EthertypeBase {
474
475     }
476
477     @Test
478     public final void testGetProtocol_protoTcp() {
479         SecurityRuleAttributes.Protocol protocolTcp = new SecurityRuleAttributes.Protocol(ProtocolTcp.class);
480         secRuleBuilder.setProtocol(protocolTcp);
481         Assert.assertEquals(IpProtoClassifierDefinition.TCP_VALUE,
482             SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
483     }
484
485     @Test
486     public final void testGetProtocol_protoUdp() {
487         SecurityRuleAttributes.Protocol protocolUdp = new SecurityRuleAttributes.Protocol(ProtocolUdp.class);
488         secRuleBuilder.setProtocol(protocolUdp);
489         Assert.assertEquals(IpProtoClassifierDefinition.UDP_VALUE,
490             SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
491     }
492
493     @Test
494     public final void testGetProtocol_protoIcmp() {
495         SecurityRuleAttributes.Protocol protocolIcmp = new SecurityRuleAttributes.Protocol(ProtocolIcmp.class);
496         secRuleBuilder.setProtocol(protocolIcmp);
497         Assert.assertEquals(IpProtoClassifierDefinition.ICMP_VALUE,
498             SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
499     }
500
501     @Test
502     public final void testGetProtocol_protoNull() {
503         secRuleBuilder.setProtocol(null);
504         Assert.assertNull(SecRuleEntityDecoder.getProtocol(secRuleBuilder.build()));
505     }
506
507     @Test
508     public final void testGetProtocol_protoUnknown() {
509         SecurityRuleAttributes.Protocol protocolUnknown = new SecurityRuleAttributes.Protocol(UnknownProtocol.class);
510         secRuleBuilder.setProtocol(protocolUnknown);
511         thrown.expect(IllegalArgumentException.class);
512         SecRuleEntityDecoder.getProtocol(secRuleBuilder.build());
513     }
514
515     private interface UnknownProtocol extends ProtocolBase {
516
517     }
518 }