View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net;
18  
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.commons.net.util.SubnetUtils;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests https://issues.apache.org/jira/browse/NET-728
27   */
28  public class SubnetUtilsNet728Test {
29  
30      private static final String CIDR_SUFFIX_30 = "30";
31      private static final String CIDR_SUFFIX_32 = "32";
32      private static final String cidr1 = "192.168.0.151";
33      private static final String cidr2 = "192.168.0.50";
34  
35      private static final SubnetUtils snu1s30;
36      private static final SubnetUtils snu1s32;
37      private static final SubnetUtils snu2s32;
38  
39      static {
40          snu1s32 = new SubnetUtils(StringUtils.joinWith("/", cidr1, CIDR_SUFFIX_32));
41          snu1s32.setInclusiveHostCount(true);
42          snu1s30 = new SubnetUtils(StringUtils.joinWith("/", cidr1, CIDR_SUFFIX_30));
43          snu1s30.setInclusiveHostCount(true);
44          snu2s32 = new SubnetUtils(StringUtils.joinWith("/", cidr2, CIDR_SUFFIX_32));
45          snu2s32.setInclusiveHostCount(true);
46      }
47  
48      @Test
49      void test() {
50          final SubnetUtils s = new SubnetUtils("192.168.1.1/32");
51          s.setInclusiveHostCount(true);
52          final SubnetUtils ss = new SubnetUtils("10.65.0.151/32");
53          ss.setInclusiveHostCount(true);
54          assertTrue(ss.getInfo().isInRange("10.65.0.151"));
55          assertTrue(s.getInfo().isInRange("192.168.1.1"));
56      }
57  
58      @Test
59      void testCidr1InRange2() {
60          assertTrue(snu1s30.getInfo().isInRange(cidr1), snu1s30::toString);
61      }
62  
63      @Test
64      void testCidr1NotInRange1() {
65          assertTrue(snu1s32.getInfo().isInRange(cidr1), snu1s32::toString);
66      }
67  
68      @Test
69      void testCidr2InRange3() {
70          assertTrue(snu2s32.getInfo().isInRange(cidr2), snu2s32::toString);
71      }
72  
73      @Test
74      void testCidr2NotInRange3() {
75          assertTrue(snu2s32.getInfo().isInRange(cidr2), snu2s32::toString);
76      }
77  
78  }