Copilot commented on code in PR #8167: URL: https://github.com/apache/incubator-seata/pull/8167#discussion_r3649981293
########## server/src/test/java/org/apache/seata/server/ServerXidHostTest.java: ########## @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.server; + +import org.apache.seata.common.util.NetUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * Test the host selection that feeds {@link org.apache.seata.common.XID}. + */ +public class ServerXidHostTest { + + /** + * An ip literal is stored as is, no resolution happens. + */ + @Test + public void testResolveXidHostKeepsIpLiteral() { + Assertions.assertEquals("8.210.212.91", Server.resolveXidHost("8.210.212.91")); + } + + /** + * Guards the regression reported in #8158: a configured host name must be resolved to its ip literal + * before it is stored, otherwise the xid carries the host name and XIDLoadBalance cannot match the tc. + */ + @Test + public void testResolveXidHostConvertsHostNameToIpLiteral() throws UnknownHostException { + // the local host name is the only name guaranteed to resolve without external dns + String hostName = InetAddress.getLocalHost().getHostName(); + // in some environments the host name is already an ip literal, then there is nothing to convert + Assumptions.assumeFalse(NetUtil.isValidIp(hostName, true), "host name is already an ip literal"); + + String resolved = Server.resolveXidHost(hostName); + + // the value stored in the xid must be the resolved ip literal, never the host name + Assertions.assertNotEquals(hostName, resolved); + Assertions.assertTrue(NetUtil.isValidIp(resolved, true), "resolved host is not an ip literal: " + resolved); Review Comment: In this test, `Assumptions.assumeFalse(NetUtil.isValidIp(hostName, true), ...)` is incorrect for detecting whether `hostName` is already an IP literal. `isValidIp` resolves host names via `convertIpIfNecessary`, so it will often return `true` for a *non-literal* host name and skip the test, leaving the regression untested in typical environments. Consider instead computing the resolved address via `InetAddress.getByName(hostName)` and assuming it resolves to a non-forbidden literal, then asserting `resolveXidHost(hostName)` equals that literal. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
