2 * Copyright (c) 2015 IBM Corporation 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
9 package org.opendaylight.neutron.spi;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
16 public class NeutronSubnetTest {
19 public void isValidCIDRTest() {
20 NeutronSubnet objectUT = new NeutronSubnet();
21 objectUT.setIpVersion(4);
22 objectUT.setCidr("10.18.0.0/24");
23 Assert.assertTrue("isValidCIDR Test 1: Testing valid CIDR (10.18.0.0/24) failed", objectUT.isValidCIDR());
25 objectUT.setCidr("10.18.0.0/16");
26 Assert.assertTrue("isValidCIDR Test 2: Testing valid CIDR (10.18.0.0/16) failed", objectUT.isValidCIDR());
28 objectUT.setCidr("10.18.0.0/8");
29 Assert.assertFalse("isValidCIDR Negative Test 1: Testing invalid CIDR (10.18.0.0/8) failed",
30 objectUT.isValidCIDR());
32 objectUT.setIpVersion(6);
33 objectUT.setCidr("2015:0:0:0:0:0:0:0/24");
34 Assert.assertTrue("isValidCIDR Test 1: Testing valid V6 CIDR (2015:0:0:0:0:0:0:0/24) failed",
35 objectUT.isValidCIDR());
36 objectUT.setCidr("2015:0:0:0:0:0:0:1/24");
37 Assert.assertFalse("isValidCIDR Negative Test 1: Testing invalid CIDR (2015:0:0:0:0:0:0:1) failed",
38 objectUT.isValidCIDR());
42 public void gatewayIp_PoolOverlapTest() {
43 NeutronSubnet objectUT = new NeutronSubnet();
44 objectUT.setIpVersion(4);
45 objectUT.setCidr("10.18.0.0/16");
47 NeutronSubnetIpAllocationPool allocationPool = new NeutronSubnetIpAllocationPool();
48 allocationPool.setPoolStart("10.18.0.2");
49 allocationPool.setPoolEnd("10.18.0.6");
50 List<NeutronSubnetIpAllocationPool> pools = new ArrayList<>();
51 pools.add(allocationPool);
52 objectUT.setAllocationPools(pools);
54 objectUT.setGatewayIp("10.18.0.1");
55 Assert.assertFalse("gatewayIp_Pool_overlap Test 1: test with address below allocation pool failed",
56 objectUT.gatewayIp_Pool_overlap());
58 objectUT.setGatewayIp("10.18.0.4");
59 Assert.assertTrue("gatewayIp_Pool_overlap Test 2: test with address in allocation pool failed",
60 objectUT.gatewayIp_Pool_overlap());
62 objectUT.setGatewayIp("10.18.0.7");
63 Assert.assertFalse("gatewayIp_Pool_overlap Test 3: test with address above allocation pool failed",
64 objectUT.gatewayIp_Pool_overlap());
66 objectUT.setIpVersion(6);
67 objectUT.setCidr("2015::0/64");
69 allocationPool = new NeutronSubnetIpAllocationPool();
70 allocationPool.setPoolStart("2015::2");
71 allocationPool.setPoolEnd("2015::6");
72 pools = new ArrayList<>();
73 pools.add(allocationPool);
74 objectUT.setAllocationPools(pools);
76 objectUT.setGatewayIp("2015::1");
77 Assert.assertFalse("gatewayIp_Pool_overlap v6 Test 1: test with address below allocation pool failed",
78 objectUT.gatewayIp_Pool_overlap());
80 objectUT.setGatewayIp("2015::4");
81 Assert.assertTrue("gatewayIp_Pool_overlap v6 Test 2: test with address in allocation pool failed",
82 objectUT.gatewayIp_Pool_overlap());
84 objectUT.setGatewayIp("2015::7");
85 Assert.assertFalse("gatewayIp_Pool_overlap v6 Test 3: test with address above allocation pool failed",
86 objectUT.gatewayIp_Pool_overlap());
90 public void isValidIpTest() {
91 NeutronSubnet objectUT = new NeutronSubnet();
92 objectUT.setIpVersion(4);
93 objectUT.setCidr("10.18.0.0/24");
95 Assert.assertFalse("isValidIp Negative Test 1: test of IP address outside of CIDR block failed",
96 objectUT.isValidIp("10.18.1.1"));
98 Assert.assertTrue("isValidIp Test 1: test of IP address within CIDR block failed",
99 objectUT.isValidIp("10.18.0.1"));
101 objectUT.setIpVersion(6);
102 objectUT.setCidr("2015::0/64");
104 Assert.assertFalse("isValidIp v6 Negative Test 1: test of IP address outside of CIDR block failed",
105 objectUT.isValidIp("2015:0:0:1:0:0:0:1"));
107 Assert.assertTrue("isValidIp v6 Test 1: test of IP address within CIDR block failed",
108 objectUT.isValidIp("2015:0:0:0:1:0:0:1"));
112 public void getLowAddrTest() {
113 NeutronSubnet objectUT = new NeutronSubnet();
114 objectUT.setIpVersion(4);
115 objectUT.setCidr("10.18.0.0/24");
117 NeutronSubnetIpAllocationPool allocationPool = new NeutronSubnetIpAllocationPool();
118 allocationPool.setPoolStart("10.18.0.2");
119 allocationPool.setPoolEnd("10.18.0.6");
120 List<NeutronSubnetIpAllocationPool> pools = new ArrayList<>();
121 pools.add(allocationPool);
122 allocationPool = new NeutronSubnetIpAllocationPool();
123 allocationPool.setPoolStart("10.18.0.10");
124 allocationPool.setPoolEnd("10.18.0.15");
125 objectUT.setAllocationPools(pools);
127 Assert.assertEquals("getLowAddr Test 1: test of returned address", "10.18.0.2", objectUT.getLowAddr());
129 objectUT.setIpVersion(6);
130 objectUT.setCidr("2015::0/24");
132 allocationPool = new NeutronSubnetIpAllocationPool();
133 allocationPool.setPoolStart("2015::2");
134 allocationPool.setPoolEnd("2015::6");
135 pools = new ArrayList<>();
136 pools.add(allocationPool);
137 allocationPool = new NeutronSubnetIpAllocationPool();
138 allocationPool.setPoolStart("2015::10");
139 allocationPool.setPoolEnd("2015::15");
140 objectUT.setAllocationPools(pools);
142 Assert.assertEquals("getLowAddr v6 Test 1: test of returned address", "2015::2", objectUT.getLowAddr());