2 * Copyright (c) 2013 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.openflowplugin.openflow.md.core.sal.convertor.match;
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.openflowjava.util.ByteBufUtils;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import java.lang.reflect.Constructor;
17 import java.math.BigInteger;
18 import java.util.Arrays;
23 public class MatchConvertorUtilTest {
25 private static Logger LOG = LoggerFactory
26 .getLogger(MatchConvertorUtilTest.class);
29 * Test method for {@link MatchConvertorUtil#ipv6ExthdrFlagsToInt(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags)}.
35 public void testIpv6ExthdrFlagsToInt() throws Exception {
36 Ipv6ExthdrFlags pField;
37 Constructor<Ipv6ExthdrFlags> ctor = Ipv6ExthdrFlags.class.getConstructor(
38 Boolean.class, Boolean.class, Boolean.class, Boolean.class,
39 Boolean.class, Boolean.class, Boolean.class, Boolean.class, Boolean.class);
41 int[] expectedFlagCumulants = new int[]{
42 4, 8, 2, 16, 64, 1, 32, 128, 256
45 for (int i = 0; i < 9; i++) {
46 pField = ctor.newInstance(createIpv6ExthdrFlagsCtorParams(i));
47 int intResult = MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField);
48 LOG.debug("{}:Ipv6ExthdrFlags[{}] as int = {}", i, pField, intResult);
49 Assert.assertEquals(expectedFlagCumulants[i], intResult);
52 pField = new Ipv6ExthdrFlags(
53 false, false, false, false, false, false, false, false, false);
54 Assert.assertEquals(0, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
56 pField = new Ipv6ExthdrFlags(
57 true, true, true, true, true, true, true, true, true);
58 Assert.assertEquals(511, MatchConvertorUtil.ipv6ExthdrFlagsToInt(pField).intValue());
65 private static Object[] createIpv6ExthdrFlagsCtorParams(int trueIndex) {
66 Boolean[] flags = new Boolean[]{false, false, false, false, false, false, false, false, false};
67 flags[trueIndex] = true;
72 * Test method for {@link MatchConvertorUtil#ipv6NetmaskArrayToCIDRValue(byte[])} (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MatchEntry)}.
78 public void testIpv6NetmaskArrayToCIDRValue() throws Exception {
79 BigInteger maskSeed = new BigInteger("1ffffffffffffffffffffffffffffffff", 16);
80 byte[] maskArray = new byte[16];
81 LOG.debug("maskSeed= {}", ByteBufUtils.bytesToHexString(maskSeed.toByteArray()));
83 for (int i = 0; i <= 128; i++) {
84 System.arraycopy(maskSeed.toByteArray(), 1, maskArray, 0, 16);
85 LOG.debug("maskHex[{}] = {}", i, ByteBufUtils.bytesToHexString(maskArray));
86 int cidr = MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskArray);
87 LOG.debug("cidr = {}", cidr);
88 Assert.assertEquals(128 - i, cidr);
90 maskSeed = maskSeed.clearBit(i);
95 * Test method for {@link MatchConvertorUtil#getIpv4Mask(byte[])}.
101 public void testGetIpv4Mask() {
102 byte[][] maskInputs = new byte[][]{
103 {(byte) 255, (byte) 255, (byte) 255, (byte) 255},
104 {(byte) 255, (byte) 255, (byte) 254, 0},
105 {(byte) 128, 0, 0, 0},
109 String[] maskOutputs = new String[]{
110 "/32", "/23", "/1", "/0"
113 for (int i = 0; i < maskInputs.length; i++) {
114 String mask = MatchConvertorUtil.getIpv4Mask(maskInputs[i]);
115 Assert.assertEquals(maskOutputs[i], mask);
120 * Test method for {@link MatchConvertorUtil#getIpv4Mask(byte[])}.
126 public void testGetIpv4MaskNegative() {
127 byte[][] maskInputs = new byte[][]{
128 {(byte) 127, 0, 0, 0},
132 for (int i = 0; i < maskInputs.length; i++) {
134 String mask = MatchConvertorUtil.getIpv4Mask(maskInputs[i]);
135 Assert.fail("invalid mask should not have passed: " + Arrays.toString(maskInputs[i]));
136 } catch (Exception e) {
138 LOG.info("ipv4 mask excepted exception: {}", e.getMessage(), e);