2cdcd82714e4ee4fbe0867329b45fc7072b9c8dd
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / NeutronSubnetTest.java
1 /*
2  * Copyright (c) 2015 IBM Corporation and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
15
16 public class NeutronSubnetTest {
17
18     @Test
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());
24
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());
27
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());
31
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());
39     }
40
41     @Test
42     public void gatewayIp_PoolOverlapTest() {
43         NeutronSubnet objectUT = new NeutronSubnet();
44         objectUT.setIpVersion(4);
45         objectUT.setCidr("10.18.0.0/16");
46
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<NeutronSubnetIpAllocationPool>();
51         pools.add(allocationPool);
52         objectUT.setAllocationPools(pools);
53
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());
57
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());
61
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());
65
66         objectUT.setIpVersion(6);
67         objectUT.setCidr("2015::0/64");
68
69         allocationPool = new NeutronSubnetIpAllocationPool();
70         allocationPool.setPoolStart("2015::2");
71         allocationPool.setPoolEnd("2015::6");
72         pools = new ArrayList<NeutronSubnetIpAllocationPool>();
73         pools.add(allocationPool);
74         objectUT.setAllocationPools(pools);
75
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());
79
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());
83
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());
87     }
88
89     @Test
90     public void isValidIpTest() {
91         NeutronSubnet objectUT = new NeutronSubnet();
92         objectUT.setIpVersion(4);
93         objectUT.setCidr("10.18.0.0/24");
94
95         Assert.assertFalse("isValidIp Negative Test 1: test of IP address outside of CIDR block failed",
96                 objectUT.isValidIp("10.18.1.1"));
97
98         Assert.assertTrue("isValidIp Test 1: test of IP address within CIDR block failed",
99                 objectUT.isValidIp("10.18.0.1"));
100
101         objectUT.setIpVersion(6);
102         objectUT.setCidr("2015::0/64");
103
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"));
106
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"));
109     }
110
111     @Test
112     public void getLowAddrTest() {
113         NeutronSubnet objectUT = new NeutronSubnet();
114         objectUT.setIpVersion(4);
115         objectUT.setCidr("10.18.0.0/24");
116
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<NeutronSubnetIpAllocationPool>();
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);
126
127         Assert.assertEquals("getLowAddr Test 1: test of returned address", "10.18.0.2", objectUT.getLowAddr());
128
129         objectUT.setIpVersion(6);
130         objectUT.setCidr("2015::0/24");
131
132         allocationPool = new NeutronSubnetIpAllocationPool();
133         allocationPool.setPoolStart("2015::2");
134         allocationPool.setPoolEnd("2015::6");
135         pools = new ArrayList<NeutronSubnetIpAllocationPool>();
136         pools.add(allocationPool);
137         allocationPool = new NeutronSubnetIpAllocationPool();
138         allocationPool.setPoolStart("2015::10");
139         allocationPool.setPoolEnd("2015::15");
140         objectUT.setAllocationPools(pools);
141
142         Assert.assertEquals("getLowAddr v6 Test 1: test of returned address", "2015::2", objectUT.getLowAddr());
143     }
144 }