tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.protocol; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNull; |
| 6 | import static org.junit.Assert.assertThat; |
| 7 | import static org.junit.Assert.assertTrue; |
| 8 | |
| 9 | import org.hamcrest.CoreMatchers; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 13 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 14 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 15 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4Src; |
| 16 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4SrcMasked; |
| 17 | import org.projectfloodlight.openflow.protocol.oxm.OFOxms; |
| 18 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 19 | import org.projectfloodlight.openflow.types.IPv4AddressWithMask; |
| 20 | |
| 21 | public class OFOxmTest { |
| 22 | private OFOxms oxms; |
| 23 | |
| 24 | @Before |
| 25 | public void setup() { |
| 26 | oxms = OFFactories.getFactory(OFVersion.OF_13).oxms(); |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | public void testGetCanonicalFullMask() { |
| 31 | IPv4AddressWithMask empty = IPv4AddressWithMask.of("0.0.0.0/0"); |
| 32 | assertEquals(IPv4Address.FULL_MASK, empty.getMask()); |
| 33 | OFOxmIpv4SrcMasked ipv4SrcMasked = oxms.ipv4SrcMasked(empty.getValue(), empty.getMask()); |
| 34 | // canonicalize should remove /0 |
| 35 | assertNull(ipv4SrcMasked.getCanonical()); |
| 36 | } |
| 37 | |
| 38 | @Test |
| 39 | public void testGetCanonicalNoMask() { |
| 40 | IPv4AddressWithMask fullIp = IPv4AddressWithMask.of("1.2.3.4/32"); |
| 41 | assertEquals(IPv4Address.NO_MASK, fullIp.getMask()); |
| 42 | OFOxmIpv4SrcMasked ipv4SrcMasked = oxms.ipv4SrcMasked(fullIp.getValue(), fullIp.getMask()); |
| 43 | assertTrue(ipv4SrcMasked.isMasked()); |
| 44 | assertEquals(IPv4Address.NO_MASK, ipv4SrcMasked.getMask()); |
| 45 | |
| 46 | // canonicalize should convert the masked oxm to the non-masked one |
| 47 | OFOxm<IPv4Address> canonical = ipv4SrcMasked.getCanonical(); |
| 48 | assertThat(canonical, CoreMatchers.instanceOf(OFOxmIpv4Src.class)); |
| 49 | assertFalse(canonical.isMasked()); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testGetCanonicalNormalMask() { |
| 54 | IPv4AddressWithMask ip = IPv4AddressWithMask.of("1.2.3.0/24"); |
| 55 | OFOxmIpv4SrcMasked ipv4SrcMasked = oxms.ipv4SrcMasked(ip.getValue(), ip.getMask()); |
| 56 | assertTrue(ipv4SrcMasked.isMasked()); |
| 57 | |
| 58 | // canonicalize should convert the masked oxm to the non-masked one |
| 59 | OFOxm<IPv4Address> canonical = ipv4SrcMasked.getCanonical(); |
| 60 | assertEquals(ipv4SrcMasked, canonical); |
| 61 | } |
| 62 | } |