!943 修复MaskBIt中根据掩码位获取long值的bug

Merge pull request !943 from emptypoint/fix-MaskBit
This commit is contained in:
Looly 2023-02-20 07:37:45 +00:00 committed by Gitee
commit 3ab4a9c4d2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 10 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package cn.hutool.core.net;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.BiMap;
import java.util.HashMap;
/**
@ -83,6 +84,6 @@ public class MaskBit {
*/
public static long getMaskIpLong(final int maskBit) {
Assert.isTrue(MASK_BIT_MAP.containsKey(maskBit), "非法的掩码位数:{}", maskBit);
return -1L << (32 - maskBit);
return (0xffffffffL << (32 - maskBit)) & 0xffffffffL;
}
}

View File

@ -132,6 +132,13 @@ public class Ipv4UtilTest {
Assert.assertEquals(4294967295L, l);
}
@Test
public void getMaskIpLongTest() {
for (int i = 1; i <= 32; i++) {
Assert.assertEquals(Ipv4Util.ipv4ToLong(MaskBit.get(i)), MaskBit.getMaskIpLong(i));
}
}
@SuppressWarnings("SameParameterValue")
private void testGenerateIpList(final String fromIp, final String toIp) {
Assert.assertEquals(