2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension;
10 import static org.junit.Assert.fail;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.nio.ByteBuffer;
15 import java.util.Arrays;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.openflow.protocol.OFMatch;
20 import org.openflow.util.U32;
22 public class V6ExtensionTest {
25 public void testFromString() throws UnknownHostException {
26 // This tests creating V6Match using fromString and OFMatch by comparing
27 // the results to each other
28 V6Match match = new V6Match();
29 V6Match match2 = new V6Match();
31 OFMatch ofm = new OFMatch();
32 V6Match match4 = new V6Match(ofm);
35 Assert.assertTrue(match.equals(match2));
36 match.fromString("any");
37 Assert.assertTrue(match.equals(match2));
38 Assert.assertTrue(match.equals(match4));
40 match.fromString("invalidArgument");
42 fail("Did not throw IllegalArgumentException");
43 } catch (IllegalArgumentException e) {
44 // passed test for throwing exception.
47 match.fromString("invalidParameter=abcdefg");
48 fail("Did not throw IllegalArgumentException");
49 } catch (IllegalArgumentException e) {
50 // passed test for throwing exception.
53 match.fromString("input_port=1");
54 match.fromString("dl_dst=20:A0:11:10:00:99");
55 match.fromString("dl_src=00:10:08:22:12:75");
56 match.fromString("ip_src=10.1.1.1");
57 match.fromString("ip_dst=1.2.3.4");
58 match.fromString("eth_type=0x800");
59 match.fromString("dl_vlan=10");
60 match.fromString("nw_proto=6");
61 match.fromString("nw_tos=100");
62 match.fromString("tp_dst=8080");
63 match.fromString("tp_src=60");
64 match.fromString("dl_vpcp=1");
66 Assert.assertTrue(match.getInputPort() == 1);
67 // Assert.assertTrue(match.getIPv6MatchLen()==6);
68 ofm.setInputPort((short) 1);
69 // V6Match is meant for IPv6, but if using OFMatch, it will be set to
70 // IPv4 values, as OF1.0 doesn't support IPv6.
71 InetAddress addr = InetAddress.getByName("10.1.1.1");
72 int ipsrc = ByteBuffer.wrap(addr.getAddress()).getInt();
73 ofm.setNetworkSource(ipsrc);
75 addr = InetAddress.getByName("1.2.3.4");
76 int ipdst = ByteBuffer.wrap(addr.getAddress()).getInt();
77 ofm.setNetworkDestination(ipdst);
79 byte[] macSrc = { 0x00, 0x10, 0x08, 0x22, 0x12, 0x75 };
80 ofm.setDataLayerSource(macSrc);
81 byte[] macDst = { 0x20, (byte) 0xA0, 0x11, 0x10, 0x00, (byte) 0x99 };
82 ofm.setDataLayerDestination(macDst);
83 ofm.setDataLayerType((short) 0x800);
84 ofm.setDataLayerVirtualLan((short) 10);
85 ofm.setDataLayerVirtualLanPriorityCodePoint((byte) 1);
86 ofm.setNetworkProtocol((byte) 6);
87 ofm.setNetworkTypeOfService((byte) 100);
88 ofm.setTransportSource((short) 60);
89 ofm.setTransportDestination((short) 8080);
91 // this v6match ctor now looks at the wildcard field to
92 // determine if vlan pcp has been set
93 // so set the wildcards appropriately to reflect that vlan pcp
95 int wildcards = OFMatch.OFPFW_ALL;
96 wildcards &= ~OFMatch.OFPFW_DL_VLAN_PCP;
97 ofm.setWildcards(U32.t(Long.valueOf(wildcards)));
98 V6Match match3 = new V6Match(ofm);
100 Assert.assertTrue(match.getInputPort() == match3.getInputPort());
101 Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
102 match3.getDataLayerSource()));
103 Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
104 match3.getDataLayerDestination()));
105 Assert.assertNull(match.getNetworkSrc());
106 Assert.assertNull(match3.getNetworkSrc());
107 Assert.assertNull(match.getNetworkDest());
108 Assert.assertNull(match3.getNetworkDest());
109 Assert.assertTrue(match.getDataLayerVirtualLan() == match3
110 .getDataLayerVirtualLan());
111 Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match3
112 .getDataLayerVirtualLanPriorityCodePoint());
113 Assert.assertTrue(match.getNetworkProtocol() == match3
114 .getNetworkProtocol());
115 Assert.assertTrue(match.getNetworkTypeOfService() == match3
116 .getNetworkTypeOfService());
117 Assert.assertTrue(match.getTransportSource() == match3
118 .getTransportSource());
119 Assert.assertTrue(match.getTransportDestination() == match3
120 .getTransportDestination());
121 Assert.assertTrue(match.getWildcards() == match3.getWildcards());
126 public void testReadWriteBuffer() {
127 V6Match match = new V6Match();
128 match.fromString("input_port=1");
129 match.fromString("dl_dst=20:A0:11:10:00:99");
130 match.fromString("dl_src=00:10:08:22:12:75");
131 // writeTo(ByteBuffer) will only write IPv6
132 match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
133 match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
134 match.fromString("dl_vlan=10");
135 match.fromString("dl_vpcp=1");
136 match.fromString("nw_proto=6");
137 match.fromString("nw_tos=100");
138 match.fromString("tp_dst=8080");
139 match.fromString("tp_src=60");
140 match.fromString("dl_type=0x800");
142 ByteBuffer data = ByteBuffer.allocateDirect(10000);
145 V6Match match2 = new V6Match();
146 match2.readFrom(data);
147 Assert.assertTrue(match.getInputPort() == match2.getInputPort());
148 Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
149 match2.getDataLayerSource()));
150 Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
151 match2.getDataLayerDestination()));
153 Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
154 Assert.assertTrue(match.getNetworkDest()
155 .equals(match2.getNetworkDest()));
157 Assert.assertTrue(match.getDataLayerVirtualLan() == match2
158 .getDataLayerVirtualLan());
159 Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match2
160 .getDataLayerVirtualLanPriorityCodePoint());
161 Assert.assertTrue(match.getNetworkProtocol() == match2
162 .getNetworkProtocol());
163 Assert.assertTrue(match.getNetworkTypeOfService() == match2
164 .getNetworkTypeOfService());
165 Assert.assertTrue(match.getTransportSource() == match2
166 .getTransportSource());
167 Assert.assertTrue(match.getTransportDestination() == match2
168 .getTransportDestination());
173 public void testClone() {
174 V6Match match = new V6Match();
175 match.fromString("input_port=1");
176 match.fromString("dl_dst=20:A0:11:10:00:99");
177 match.fromString("dl_src=00:10:08:22:12:75");
178 match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333/64");
179 match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222/64");
180 match.fromString("dl_vlan=10");
181 match.fromString("dl_vpcp=1");
182 match.fromString("nw_proto=6");
183 match.fromString("nw_tos=100");
184 match.fromString("tp_dst=8080");
185 match.fromString("tp_src=60");
186 match.fromString("dl_type=0x800");
188 V6Match match2 = match.clone();
189 Assert.assertTrue(match.getInputPort() == match2.getInputPort());
190 Assert.assertTrue(Arrays.equals(match.getDataLayerSource(),
191 match2.getDataLayerSource()));
192 Assert.assertTrue(Arrays.equals(match.getDataLayerDestination(),
193 match2.getDataLayerDestination()));
194 Assert.assertTrue(match.getNetworkSrc().equals(match2.getNetworkSrc()));
195 Assert.assertTrue(match.getNetworkDest()
196 .equals(match2.getNetworkDest()));
197 Assert.assertTrue(match.getDataLayerVirtualLan() == match2
198 .getDataLayerVirtualLan());
199 Assert.assertTrue(match.getDataLayerVirtualLanPriorityCodePoint() == match2
200 .getDataLayerVirtualLanPriorityCodePoint());
201 Assert.assertTrue(match.getNetworkProtocol() == match2
202 .getNetworkProtocol());
203 Assert.assertTrue(match.getNetworkTypeOfService() == match2
204 .getNetworkTypeOfService());
205 Assert.assertTrue(match.getTransportSource() == match2
206 .getTransportSource());
207 Assert.assertTrue(match.getTransportDestination() == match2
208 .getTransportDestination());
209 Assert.assertTrue(match.getWildcards() == match2.getWildcards());
213 public void testPadding() {
214 // testing that matchlen+pad keeps the 8byte alignment
215 V6Match match = new V6Match();
217 match.fromString("input_port=1");
218 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
219 match.fromString("dl_dst=20:A0:11:10:00:99");
220 match.fromString("dl_src=00:10:08:22:12:75");
221 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
222 match.fromString("ip_src=2001:ddd:3e1:1234:0000:1111:2222:3333");
223 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
224 match.fromString("ip_dst=2001:123:222:abc:111:aaa:1111:2222");
225 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
226 match.fromString("dl_vlan=10");
227 match.fromString("dl_vpcp=1");
228 match.fromString("nw_proto=6");
229 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
230 match.fromString("nw_tos=100");
231 match.fromString("tp_dst=8080");
232 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);
233 match.fromString("tp_src=60");
234 Assert.assertTrue((match.getPadSize() + match.getIPv6MatchLen()) % 8 == 0);