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    *      https://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.util;
18  
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests https://issues.apache.org/jira/browse/NET-728
26   */
27  public class SubnetUtilsNet728Test {
28  
29      private static final String CIDR_SUFFIX_30 = "30";
30      private static final String CIDR_SUFFIX_32 = "32";
31      private static final String cidr1 = "192.168.0.151";
32      private static final String cidr2 = "192.168.0.50";
33  
34      private static final SubnetUtils snu1s30;
35      private static final SubnetUtils snu1s32;
36      private static final SubnetUtils snu2s32;
37  
38      static {
39          snu1s32 = new SubnetUtils(StringUtils.joinWith("/", cidr1, CIDR_SUFFIX_32));
40          snu1s32.setInclusiveHostCount(true);
41          snu1s30 = new SubnetUtils(StringUtils.joinWith("/", cidr1, CIDR_SUFFIX_30));
42          snu1s30.setInclusiveHostCount(true);
43          snu2s32 = new SubnetUtils(StringUtils.joinWith("/", cidr2, CIDR_SUFFIX_32));
44          snu2s32.setInclusiveHostCount(true);
45      }
46  
47      @Test
48      void test() {
49          final SubnetUtils s = new SubnetUtils("192.168.1.1/32");
50          s.setInclusiveHostCount(true);
51          final SubnetUtils ss = new SubnetUtils("10.65.0.151/32");
52          ss.setInclusiveHostCount(true);
53          assertTrue(ss.getInfo().isInRange("10.65.0.151"));
54          assertTrue(s.getInfo().isInRange("192.168.1.1"));
55      }
56  
57      @Test
58      void testCidr1InRange2() {
59          assertTrue(snu1s30.getInfo().isInRange(cidr1), snu1s30::toString);
60      }
61  
62      @Test
63      void testCidr1NotInRange1() {
64          assertTrue(snu1s32.getInfo().isInRange(cidr1), snu1s32::toString);
65      }
66  
67      @Test
68      void testCidr2InRange3() {
69          assertTrue(snu2s32.getInfo().isInRange(cidr2), snu2s32::toString);
70      }
71  
72      @Test
73      void testCidr2NotInRange3() {
74          assertTrue(snu2s32.getInfo().isInRange(cidr2), snu2s32::toString);
75      }
76  
77  }