5bc1b68d132567f5715d3264870999138ed70b9d
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / util / test / JU_Mask.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.util.test;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.net.InetAddress;
28
29 import org.junit.Test;
30 import org.onap.aaf.cadi.util.MaskFormatException;
31 import org.onap.aaf.cadi.util.NetMask;
32
33 import junit.framework.Assert;
34
35 public class JU_Mask {
36
37     @Test
38     public void test() throws Exception {
39 //        InetAddress ia = InetAddress.getLocalHost();
40         InetAddress ia = InetAddress.getByName("192.168.0.0");
41         NetMask mask = new NetMask(ia.getAddress());
42         assertTrue(mask.isInNet(ia.getAddress()));
43         
44         mask = new NetMask("192.168.1/24");
45         assertTrue(mask.isInNet("192.168.1.20"));
46         assertTrue(mask.isInNet("192.168.1.255"));
47         assertFalse(mask.isInNet("192.168.2.20"));
48         
49         mask = new NetMask("192.168.1/31");
50         assertFalse(mask.isInNet("192.168.2.20"));
51         assertFalse(mask.isInNet("192.168.1.20"));
52         assertTrue(mask.isInNet("192.168.1.1"));
53         assertFalse(mask.isInNet("192.168.1.2"));
54
55         mask = new NetMask("192/8");
56         assertTrue(mask.isInNet("192.168.1.1"));
57         assertTrue(mask.isInNet("192.1.1.1"));
58         assertFalse(mask.isInNet("193.168.1.1"));
59         
60         mask = new NetMask("/0");
61         assertTrue(mask.isInNet("193.168.1.1"));
62         
63         String msg = "Should throw " + MaskFormatException.class.getSimpleName();
64         try {
65             mask = new NetMask("256.256.256.256");
66             Assert.assertTrue(msg,false);
67         } catch (MaskFormatException e) {
68             Assert.assertTrue(msg,true);
69         }
70     }
71
72 }