Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .asf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# specific language governing permissions and limitations
# under the License.

notifications:
issues: [email protected]

github:
features:
issues: true
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ jobs:
esac
- name: Set up toolchain JDK ${{ matrix.java }}
id: setup-java-toolchain
uses: actions/setup-java@v5.6.0
uses: actions/setup-java@v6.3.0
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Set up JDK 17 for Maven
uses: actions/setup-java@v5.6.0
uses: actions/setup-java@v6.3.0
with:
distribution: 'temurin'
java-version: '17'
Expand Down
4 changes: 2 additions & 2 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wrapperVersion=3.3.4
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.10.0/apache-maven-3.10.0-bin.zip
distributionSha256Sum=1f6d9909266510f039f59aa0e13dcd2c66da85f043e56276e41f2918f8bddaff
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.10.2/apache-maven-3.10.2-bin.zip
distributionSha256Sum=7d69999d218e025cf1c14691ba748a09706e47999bd505009403cef9d757c2df
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
/*
* ====================================================================
* 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.client5.testing;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.hc.client5.http.AddressSelectingDnsResolver;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
import org.apache.hc.client5.http.config.ProtocolFamilyPreference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

@EnabledIfSystemProperty(named = "httpclient.rfc6724.it", matches = "true")
class AddressSelectingDnsResolverIT {

private static final String PROP_HOST = "httpclient.rfc6724.host";

@Test
void interleave_isStableInterleavingOfDefault_forSameResolution() throws Exception {
final String host = System.getProperty(PROP_HOST, "localhost");

final InetAddress[] resolvedOnce = SystemDefaultDnsResolver.INSTANCE.resolve(host);

final DnsResolver captured = new DnsResolver() {
@Override
public InetAddress[] resolve(final String h) throws UnknownHostException {
if (!host.equals(h)) {
throw new UnknownHostException(h);
}
return resolvedOnce.clone();
}

@Override
public String resolveCanonicalHostname(final String h) throws UnknownHostException {
return SystemDefaultDnsResolver.INSTANCE.resolveCanonicalHostname(h);
}
};

final AddressSelectingDnsResolver rDefault =
new AddressSelectingDnsResolver(captured, ProtocolFamilyPreference.DEFAULT);

final AddressSelectingDnsResolver rInterleave =
new AddressSelectingDnsResolver(captured, ProtocolFamilyPreference.INTERLEAVE);

final InetAddress[] outDefault = rDefault.resolve(host);
final InetAddress[] outInterleave = rInterleave.resolve(host);

// 0) Both outputs must be permutations of the captured, single resolution.
Assertions.assertNotNull(outDefault);
assertSameElements(Arrays.asList(resolvedOnce), Arrays.asList(outDefault));
Assertions.assertNotNull(outInterleave);
assertSameElements(Arrays.asList(resolvedOnce), Arrays.asList(outInterleave));

// 1) Same elements between DEFAULT and INTERLEAVE (no drops, no additions).
assertSameElements(Arrays.asList(outDefault), Arrays.asList(outInterleave));

// 2) Family counts must match between DEFAULT and INTERLEAVE.
final Counts cDefault = countFamilies(outDefault);
final Counts cInterleave = countFamilies(outInterleave);
assertEquals(cDefault.v4, cInterleave.v4);
assertEquals(cDefault.v6, cInterleave.v6);

// 3) INTERLEAVE must be the stable interleaving of the DEFAULT-ordered list.
final List<InetAddress> expected = expectedInterleaveFromBaseline(Arrays.asList(outDefault));
assertEquals(expected, Arrays.asList(outInterleave));

// 4) If both families are present, the first 2*min(v4,v6) addresses must alternate by family.
final int pairs = Math.min(cInterleave.v4, cInterleave.v6);
if (pairs > 0) {
assertAlternatingPrefix(outInterleave, 2 * pairs);
}

// 5) Relative order within each family is preserved from DEFAULT.
assertFamilyRelativeOrderPreserved(outDefault, outInterleave);

// Diagnostics for manual runs.
System.out.println("Host: " + host);
dump("DEFAULT", outDefault);
dump("INTERLEAVE", outInterleave);
}

private static void assertSameElements(final List<InetAddress> a, final List<InetAddress> b) {
assertEquals(a.size(), b.size());

final List<InetAddress> remaining = new ArrayList<>(b);
for (final InetAddress x : a) {
assertTrue(remaining.remove(x), "Missing address: " + x);
}
assertTrue(remaining.isEmpty(), "Extra addresses: " + remaining);
}

private static final class Counts {
final int v4;
final int v6;

Counts(final int v4, final int v6) {
this.v4 = v4;
this.v6 = v6;
}
}

private static Counts countFamilies(final InetAddress[] out) {
int v4 = 0;
int v6 = 0;
for (final InetAddress a : out) {
if (a instanceof Inet6Address) {
v6++;
} else {
v4++;
}
}
return new Counts(v4, v6);
}

private static void assertAlternatingPrefix(final InetAddress[] out, final int length) {
if (length <= 1) {
return;
}
final boolean firstV6 = out[0] instanceof Inet6Address;
for (int i = 0; i < length; i++) {
final boolean expectV6 = firstV6 == (i % 2 == 0);
final boolean isV6 = out[i] instanceof Inet6Address;
assertEquals(expectV6, isV6, "Not alternating at index " + i);
}
}

private static void assertFamilyRelativeOrderPreserved(
final InetAddress[] base,
final InetAddress[] interleaved) {

final List<InetAddress> baseV6 = new ArrayList<>();
final List<InetAddress> baseV4 = new ArrayList<>();
for (final InetAddress a : base) {
if (a instanceof Inet6Address) {
baseV6.add(a);
} else {
baseV4.add(a);
}
}

final List<InetAddress> gotV6 = new ArrayList<>();
final List<InetAddress> gotV4 = new ArrayList<>();
for (final InetAddress a : interleaved) {
if (a instanceof Inet6Address) {
gotV6.add(a);
} else {
gotV4.add(a);
}
}

assertEquals(baseV6, gotV6, "IPv6 relative order changed");
assertEquals(baseV4, gotV4, "IPv4 relative order changed");
}

private static List<InetAddress> expectedInterleaveFromBaseline(final List<InetAddress> baseline) {
if (baseline.size() <= 1) {
return baseline;
}

final List<InetAddress> v6 = new ArrayList<>();
final List<InetAddress> v4 = new ArrayList<>();

for (final InetAddress a : baseline) {
if (a instanceof Inet6Address) {
v6.add(a);
} else {
v4.add(a);
}
}

if (v6.isEmpty() || v4.isEmpty()) {
return baseline;
}

final boolean startV6 = baseline.get(0) instanceof Inet6Address;

final List<InetAddress> out = new ArrayList<>(baseline.size());
int i6 = 0;
int i4 = 0;

while (i6 < v6.size() || i4 < v4.size()) {
if (startV6) {
if (i6 < v6.size()) {
out.add(v6.get(i6++));
}
if (i4 < v4.size()) {
out.add(v4.get(i4++));
}
} else {
if (i4 < v4.size()) {
out.add(v4.get(i4++));
}
if (i6 < v6.size()) {
out.add(v6.get(i6++));
}
}
}

return out;
}

private static void dump(final String label, final InetAddress[] out) {
int v4 = 0;
int v6 = 0;

System.out.println("Preference: " + label);
for (final InetAddress a : out) {
if (a instanceof Inet6Address) {
v6++;
System.out.println(" IPv6 " + a.getHostAddress());
} else {
v4++;
System.out.println(" IPv4 " + a.getHostAddress());
}
}
System.out.println("Counts: IPv4=" + v4 + " IPv6=" + v6);
System.out.println();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* ====================================================================
* 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.testing.compatibility;

import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.testing.compatibility.async.H2OverH2TunnelCompatibilityTest;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.URIScheme;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers(disabledWithoutDocker = true)
class ApacheHTTPDCaddyH2CompatibilityIT {

private static final Network NETWORK = Network.newNetwork();
static final GenericContainer<?> HTTPD_CONTAINER = ContainerImages.apacheHttpD(NETWORK);
static final GenericContainer<?> CADDY = ContainerImages.caddyH2Proxy(NETWORK);

@BeforeAll
static void startContainers() {
HTTPD_CONTAINER.start();
CADDY.start();
}

static HttpHost targetInternalTlsHost() {
return new HttpHost(URIScheme.HTTPS.id, ContainerImages.WEB_SERVER, ContainerImages.HTTPS_PORT);
}

static HttpHost h2ProxyContainerHost() {
return new HttpHost(URIScheme.HTTPS.id, CADDY.getHost(), CADDY.getMappedPort(ContainerImages.H2_PROXY_PORT));
}

static HttpHost h2ProxyPwProtectedContainerHost() {
return new HttpHost(URIScheme.HTTPS.id, CADDY.getHost(), CADDY.getMappedPort(ContainerImages.H2_PROXY_PW_PROTECTED_PORT));
}

@AfterAll
static void cleanup() {
CADDY.close();
HTTPD_CONTAINER.close();
NETWORK.close();
}

@Nested
@DisplayName("H2 client: TLS, connection via H2 proxy (H2-over-H2 tunnel)")
class AsyncViaH2Proxy extends H2OverH2TunnelCompatibilityTest {

public AsyncViaH2Proxy() throws Exception {
super(targetInternalTlsHost(), h2ProxyContainerHost(), null);
}

}

@Nested
@DisplayName("H2 client: TLS, connection via password protected H2 proxy (H2-over-H2 tunnel)")
class AsyncViaPwProtectedH2Proxy extends H2OverH2TunnelCompatibilityTest {

public AsyncViaPwProtectedH2Proxy() throws Exception {
super(
targetInternalTlsHost(),
h2ProxyPwProtectedContainerHost(),
new UsernamePasswordCredentials("caddy", "nopassword".toCharArray()));
}

}

}
Loading
Loading