JUnit tests for V6Match
[controller.git] / opendaylight / protocol_plugins / openflow / src / test / java / org / opendaylight / controller / protocol_plugin / openflow / vendorextension / v6extension / V6ExtensionTest.java
1 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
2
3 import static org.junit.Assert.fail;
4
5 import java.net.InetAddress;
6 import java.net.UnknownHostException;
7 import java.nio.ByteBuffer;
8 import java.util.Arrays;
9 import org.junit.Assert;
10 import org.junit.Test;
11 import org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match;
12 import org.openflow.protocol.OFMatch;
13
14 public class V6ExtensionTest {
15
16     @Test
17     public void testFromString() throws UnknownHostException {
18         // This tests creating V6Match using fromString and OFMatch by comparing
19         // the results to each other
20         V6Match match = new V6Match();
21         V6Match match2 = new V6Match();
22
23         OFMatch ofm = new OFMatch();
24         V6Match match4 = new V6Match(ofm);
25
26         match.fromString("");
27         Assert.assertTrue(match.equals(match2));
28         match.fromString("any");
29         Assert.assertTrue(match.equals(match2));
30         Assert.assertTrue(match.equals(match4));
31         try {
32             match.fromString("invalidArgument");
33
34             fail("Did not throw IllegalArgumentException");
35         } catch (IllegalArgumentException e) {
36             // passed test for throwing exception.
37         }
38         try {
39             match.fromString("invalidParameter=abcdefg");
40             fail("Did not throw IllegalArgumentException");
41         } catch (IllegalArgumentException e) {
42             // passed test for throwing exception.
43         }
44
45         match.fromString("input_port=1");
46         match.fromString("dl_dst=20:A0:11:10:00:99");
47         match.fromString("dl_src=00:10:08:22:12:75");
48
49         match.fromString("ip_src=10.1.1.1");
50         match.fromString("ip_dst=1.2.3.4");
51         match.fromString("eth_type=0x800");
52         match.fromString("dl_vlan=10");
53         match.fromString("dl_vpcp=1");
54         match.fromString("nw_proto=6");
55         match.fromString("nw_tos=100");
56         match.fromString("tp_dst=8080");
57         match.fromString("tp_src=60");
58
59         Assert.assertTrue(match.getInputPort() == 1);
60         // Assert.assertTrue(match.getIPv6MatchLen()==6);
61
62         ofm.setInputPort((short) 1);
63         // V6Match is meant for IPv6, but if using OFMatch, it will be set to
64         // IPv4 values, as OF1.0 doesn't support IPv6.
65         InetAddress addr = InetAddress.getByName("10.1.1.1");
66         int ipsrc = ByteBuffer.wrap(addr.getAddress()).getInt();
67         ofm.setNetworkSource(ipsrc);
68
69         addr = InetAddress.getByName("1.2.3.4");
70         int ipdst = ByteBuffer.wrap(addr.getAddress()).getInt();
71         ofm.setNetworkDestination(ipdst);
72
73         byte[] macSrc = { 0x00, 0x10, 0x08, 0x22, 0x12, 0x75 };
74         ofm.setDataLayerSource(macSrc);
75         byte[] macDst = { 0x20, (byte) 0xA0, 0x11, 0x10, 0x00, (byte) 0x99 };
76         ofm.setDataLayerDestination(macDst);
77         ofm.setDataLayerType((short) 0x800);
78         ofm.setDataLayerVirtualLan((short) 10);
79         ofm.setDataLayerVirtualLanPriorityCodePoint((byte) 1);
80         ofm.setNetworkProtocol((byte) 6);
81         ofm.setNetworkTypeOfService((byte) 100);
82         ofm.setTransportSource((short) 60);
83         ofm.setTransportDestination((short) 8080);
84
85         V6Match match3 = new V6Match(ofm);
86
87         Assert.assertTrue(match.getInputPort() == match3.getInputPort());
88         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
89                 match3.getDataLayerSource()));
90         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
91                 match3.getDataLayerDestination()));
92         Assert.assertTrue(match.getNetworkSrc().equals(match3.getNetworkSrc()));
93         Assert.assertTrue(match.getNetworkDest()
94                 .equals(match3.getNetworkDest()));
95         Assert.assertTrue(match.getDataLayerVirtualLan() == match3
96                 .getDataLayerVirtualLan());
97         Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match3
98                 .getDataLayerVirtualLanPriorityCodePoint());
99         Assert.assertTrue(match.getNetworkProtocol() == match3
100                 .getNetworkProtocol());
101         Assert.assertTrue(match.getNetworkTypeOfService() == match3
102                 .getNetworkTypeOfService());
103         Assert.assertTrue(match.getTransportSource() == match3
104                 .getTransportSource());
105         Assert.assertTrue(match.getTransportDestination() == match3
106                 .getTransportDestination());
107         Assert.assertTrue(match.getWildcards() == match3.getWildcards());
108
109     }
110
111     @Test
112     public void testReadWriteBuffer() {
113         V6Match match = new V6Match();
114         match.fromString("input_port=1");
115         match.fromString("dl_dst=20:A0:11:10:00:99");
116         match.fromString("dl_src=00:10:08:22:12:75");
117         // writeTo(ByteBuffer) will only write IPv6
118         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
119         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
120         match.fromString("dl_vlan=10");
121         match.fromString("nw_proto=6");
122         match.fromString("nw_tos=100");
123         match.fromString("tp_dst=8080");
124         match.fromString("tp_src=60");
125         match.fromString("dl_type=0x800");
126
127         ByteBuffer data = ByteBuffer.allocateDirect(10000);
128         match.writeTo(data);
129         data.flip();
130         V6Match match2 = new V6Match();
131         match2.readFrom(data);
132         Assert.assertTrue(match.getInputPort() == match2.getInputPort());
133         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
134                 match2.getDataLayerSource()));
135         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
136                 match2.getDataLayerDestination()));
137
138         Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
139         Assert.assertTrue(match.getNetworkDest()
140                 .equals(match2.getNetworkDest()));
141
142         Assert.assertTrue(match.getDataLayerVirtualLan() == match2
143                 .getDataLayerVirtualLan());
144         // vlan pcp isn't part of write/read buffer
145         Assert.assertTrue(match.getNetworkProtocol() == match2
146                 .getNetworkProtocol());
147         Assert.assertTrue(match.getNetworkTypeOfService() == match2
148                 .getNetworkTypeOfService());
149         Assert.assertTrue(match.getTransportSource() == match2
150                 .getTransportSource());
151         Assert.assertTrue(match.getTransportDestination() == match2
152                 .getTransportDestination());
153
154     }
155
156     @Test
157     public void testClone() {
158         V6Match match = new V6Match();
159         match.fromString("input_port=1");
160         match.fromString("dl_dst=20:A0:11:10:00:99");
161         match.fromString("dl_src=00:10:08:22:12:75");
162         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
163         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
164         match.fromString("dl_vlan=10");
165         match.fromString("dl_vpcp=1");
166         match.fromString("nw_proto=6");
167         match.fromString("nw_tos=100");
168         match.fromString("tp_dst=8080");
169         match.fromString("tp_src=60");
170         match.fromString("dl_type=0x800");
171
172         V6Match match2 = match.clone();
173         Assert.assertTrue(match.getInputPort() == match2.getInputPort());
174         Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
175                 match2.getDataLayerSource()));
176         Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
177                 match2.getDataLayerDestination()));
178         Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
179         Assert.assertTrue(match.getNetworkDest()
180                 .equals(match2.getNetworkDest()));
181         Assert.assertTrue(match.getDataLayerVirtualLan() == match2
182                 .getDataLayerVirtualLan());
183         Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match2
184                 .getDataLayerVirtualLanPriorityCodePoint());
185         Assert.assertTrue(match.getNetworkProtocol() == match2
186                 .getNetworkProtocol());
187         Assert.assertTrue(match.getNetworkTypeOfService() == match2
188                 .getNetworkTypeOfService());
189         Assert.assertTrue(match.getTransportSource() == match2
190                 .getTransportSource());
191         Assert.assertTrue(match.getTransportDestination() == match2
192                 .getTransportDestination());
193         Assert.assertTrue(match.getWildcards() == match2.getWildcards());
194     }
195
196     @Test
197     public void testPadding() {
198         // testing that matchlen+pad keeps the 8byte alignment
199         V6Match match = new V6Match();
200
201         match.fromString("input_port=1");
202         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
203         match.fromString("dl_dst=20:A0:11:10:00:99");
204         match.fromString("dl_src=00:10:08:22:12:75");
205         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
206         match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333");
207         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
208         match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222");
209         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
210         match.fromString("dl_vlan=10");
211         match.fromString("dl_vpcp=1");
212         match.fromString("nw_proto=6");
213         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
214         match.fromString("nw_tos=100");
215         match.fromString("tp_dst=8080");
216         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
217         match.fromString("tp_src=60");
218         Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
219     }
220 }