2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.util.test;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.net.InetAddress;
29 import org.junit.Test;
30 import org.onap.aaf.cadi.util.MaskFormatException;
31 import org.onap.aaf.cadi.util.NetMask;
33 import junit.framework.Assert;
35 public class JU_Mask {
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()));
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"));
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"));
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"));
60 mask = new NetMask("/0");
61 assertTrue(mask.isInNet("193.168.1.1"));
63 String msg = "Should throw " + MaskFormatException.class.getSimpleName();
65 mask = new NetMask("256.256.256.256");
66 Assert.assertTrue(msg,false);
67 } catch (MaskFormatException e) {
68 Assert.assertTrue(msg,true);