AT&T 2.0.19 Code drop, stage 3
[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                 NetMask mask = new NetMask(ia.getAddress());
41                 assertTrue(mask.isInNet(ia.getAddress()));
42                 
43                 mask = new NetMask("192.168.1/24");
44                 assertTrue(mask.isInNet("192.168.1.20"));
45                 assertTrue(mask.isInNet("192.168.1.255"));
46                 assertFalse(mask.isInNet("192.168.2.20"));
47                 
48                 mask = new NetMask("192.168.1/31");
49                 assertFalse(mask.isInNet("192.168.2.20"));
50                 assertFalse(mask.isInNet("192.168.1.20"));
51                 assertTrue(mask.isInNet("192.168.1.1"));
52                 assertFalse(mask.isInNet("192.168.1.2"));
53
54                 mask = new NetMask("192/8");
55                 assertTrue(mask.isInNet("192.168.1.1"));
56                 assertTrue(mask.isInNet("192.1.1.1"));
57                 assertFalse(mask.isInNet("193.168.1.1"));
58                 
59                 mask = new NetMask("/0");
60                 assertTrue(mask.isInNet("193.168.1.1"));
61                 
62                 String msg = "Should throw " + MaskFormatException.class.getSimpleName();
63                 try {
64                         mask = new NetMask("256.256.256.256");
65                         Assert.assertTrue(msg,false);
66                 } catch (MaskFormatException e) {
67                         Assert.assertTrue(msg,true);
68                 }
69         }
70
71 }