Fix #3940: 修复未正确解析 IPv6 服务器地址的问题 (#3942)

This commit is contained in:
Glavo
2025-05-29 19:34:53 +08:00
committed by GitHub
parent f223d2bc41
commit e0805fc25f
5 changed files with 215 additions and 71 deletions

View File

@@ -0,0 +1,62 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author Glavo
*/
public final class ServerAddressTest {
@Test
public void testParse() {
assertEquals(new ServerAddress("example.com"), ServerAddress.parse("example.com"));
assertEquals(new ServerAddress("example.com", 25565), ServerAddress.parse("example.com:25565"));
assertEquals(new ServerAddress("127.0.0.0"), ServerAddress.parse("127.0.0.0"));
assertEquals(new ServerAddress("127.0.0.0", 0), ServerAddress.parse("127.0.0.0:0"));
assertEquals(new ServerAddress("127.0.0.0", 12345), ServerAddress.parse("127.0.0.0:12345"));
assertEquals(new ServerAddress("::1"), ServerAddress.parse("[::1]"));
assertEquals(new ServerAddress("::1", 0), ServerAddress.parse("[::1]:0"));
assertEquals(new ServerAddress("::1", 12345), ServerAddress.parse("[::1]:12345"));
assertEquals(new ServerAddress("2001:db8::1"), ServerAddress.parse("[2001:db8::1]"));
assertEquals(new ServerAddress("2001:db8::1", 0), ServerAddress.parse("[2001:db8::1]:0"));
assertEquals(new ServerAddress("2001:db8::1", 12345), ServerAddress.parse("[2001:db8::1]:12345"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("["));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[]]"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[]:0"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]:"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]|"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]|0"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]:a"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]:65536"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[::1]:-1"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[ ]:-1"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("[-]:-1"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("example.com:"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("example.com:a"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("example.com:65536"));
assertThrows(IllegalArgumentException.class, () -> ServerAddress.parse("example.com:-1"));
}
}