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 NetMask mask = new NetMask(ia.getAddress());
41 assertTrue(mask.isInNet(ia.getAddress()));
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"));
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"));
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"));
59 mask = new NetMask("/0");
60 assertTrue(mask.isInNet("193.168.1.1"));
62 String msg = "Should throw " + MaskFormatException.class.getSimpleName();
64 mask = new NetMask("256.256.256.256");
65 Assert.assertTrue(msg,false);
66 } catch (MaskFormatException e) {
67 Assert.assertTrue(msg,true);