Add missing license headers
[packetcable.git] / packetcable-driver / src / test / java / org / pcmm / gates / impl / ExtendedClassifierTest.java
1 /*
2  * Copyright (c) 2015 Cable Television Laboratories, Inc 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.pcmm.gates.impl;
9
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.pcmm.gates.IClassifier.Protocol;
13 import org.pcmm.gates.IExtendedClassifier;
14 import org.pcmm.gates.IExtendedClassifier.ActivationState;
15
16 import java.net.Inet4Address;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19
20 /**
21  * Tests the data holder class ExtendedClassifier to ensure both construction and byte parsing result in correct object
22  * creation.
23  */
24 public class ExtendedClassifierTest {
25
26     @Test(expected = IllegalArgumentException.class)
27     public void nullProtocol() throws UnknownHostException {
28         new ExtendedClassifier(null, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
29                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
30                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
31                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
32     }
33
34     @Test(expected = IllegalArgumentException.class)
35     public void nullSrcAddr() throws UnknownHostException {
36         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, null,
37                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
38                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
39                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
40     }
41
42     @Test(expected = IllegalArgumentException.class)
43     public void nullDstAddr() throws UnknownHostException {
44         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
45                 null, (short)1, (short)2, (byte)4,
46                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
47                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
48     }
49
50     @Test(expected = IllegalArgumentException.class)
51     public void nullSrcMask() throws UnknownHostException {
52         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
53                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
54                 null, (Inet4Address) InetAddress.getByName("localhost"),
55                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
56     }
57
58     @Test(expected = IllegalArgumentException.class)
59     public void nullDstMask() throws UnknownHostException {
60         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
61                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
62                 (Inet4Address) InetAddress.getByName("localhost"), null,
63                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
64     }
65
66     @Test(expected = IllegalArgumentException.class)
67       public void nullActivationState() throws UnknownHostException {
68         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
69                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
70                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
71                 (short)5, (short)6, (short)7, null, IExtendedClassifier.Action.ADD);
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void nullAction() throws UnknownHostException {
76         new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)1, (Inet4Address) InetAddress.getByName("localhost"),
77                 (Inet4Address)InetAddress.getByName("localhost"), (short)1, (short)2, (byte)4,
78                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
79                 (short)5, (short)6, (short)7, ActivationState.ACTIVE, null);
80     }
81
82     @Test
83     public void constructionActive() throws UnknownHostException {
84         final ExtendedClassifier classifier = new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)10,
85                 (Inet4Address) InetAddress.getByName("localhost"),
86                 (Inet4Address)InetAddress.getByName("localhost"), (short)11, (short)12, (byte)14,
87                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
88                 (short)15, (short)16, (short)17, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
89         Assert.assertEquals(Protocol.NONE, classifier.getProtocol());
90         Assert.assertEquals((byte)1, classifier.getDSCPTOS());
91         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getSourceIPAddress());
92         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getDestinationIPAddress());
93         Assert.assertEquals((short) 11, classifier.getSourcePort());
94         Assert.assertEquals((short) 12, classifier.getDestinationPort());
95         Assert.assertEquals((byte) 14, classifier.getPriority());
96         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getIPSourceMask());
97         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getIPDestinationMask());
98         Assert.assertEquals((short) 15, classifier.getSourcePortEnd());
99         Assert.assertEquals((short) 16, classifier.getDestinationPortEnd());
100         Assert.assertEquals((short) 17, classifier.getClassifierID());
101         Assert.assertEquals(ActivationState.ACTIVE, classifier.getActivationState());
102         Assert.assertEquals(IExtendedClassifier.Action.ADD, classifier.getAction());
103     }
104
105     @Test
106     public void constructionInactive() throws UnknownHostException {
107         final ExtendedClassifier classifier = new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)10,
108                 (Inet4Address) InetAddress.getByName("localhost"),
109                 (Inet4Address)InetAddress.getByName("localhost"), (short)11, (short)12, (byte)14,
110                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
111                 (short)15, (short)16, (short)17, ActivationState.INACTIVE, IExtendedClassifier.Action.ADD);
112         Assert.assertEquals(Protocol.NONE, classifier.getProtocol());
113         Assert.assertEquals((byte)1, classifier.getDSCPTOS());
114         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getSourceIPAddress());
115         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getDestinationIPAddress());
116         Assert.assertEquals((short) 11, classifier.getSourcePort());
117         Assert.assertEquals((short) 12, classifier.getDestinationPort());
118         Assert.assertEquals((byte) 14, classifier.getPriority());
119         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getIPSourceMask());
120         Assert.assertEquals(InetAddress.getByName("localhost"), classifier.getIPDestinationMask());
121         Assert.assertEquals((short) 15, classifier.getSourcePortEnd());
122         Assert.assertEquals((short) 16, classifier.getDestinationPortEnd());
123         Assert.assertEquals((short) 17, classifier.getClassifierID());
124         Assert.assertEquals(ActivationState.INACTIVE, classifier.getActivationState());
125         Assert.assertEquals(IExtendedClassifier.Action.ADD, classifier.getAction());
126     }
127
128     @Test
129     public void byteParsingActive() throws UnknownHostException {
130         final ExtendedClassifier classifier = new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)20,
131                 (Inet4Address) InetAddress.getByName("localhost"),
132                 (Inet4Address)InetAddress.getByName("localhost"), (short)21, (short)22, (byte)24,
133                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
134                 (short)25, (short)26, (short)27, ActivationState.ACTIVE, IExtendedClassifier.Action.ADD);
135         final ExtendedClassifier parsed = ExtendedClassifier.parse(classifier.getBytes());
136         Assert.assertEquals(classifier, parsed);
137     }
138
139     @Test
140     public void byteParsingInactive() throws UnknownHostException {
141         final ExtendedClassifier classifier = new ExtendedClassifier(Protocol.NONE, (byte)1, (byte)20,
142                 (Inet4Address) InetAddress.getByName("localhost"),
143                 (Inet4Address)InetAddress.getByName("localhost"), (short)21, (short)22, (byte)24,
144                 (Inet4Address) InetAddress.getByName("localhost"), (Inet4Address) InetAddress.getByName("localhost"),
145                 (short)25, (short)26, (short)27, ActivationState.INACTIVE, IExtendedClassifier.Action.ADD);
146         final ExtendedClassifier parsed = ExtendedClassifier.parse(classifier.getBytes());
147         Assert.assertEquals(classifier, parsed);
148     }
149
150 }