Skip to content

Commit cd08301

Browse files
authored
Don't rely on localhost4 and localhost6 hostnames (#15)
Use directly the IPs for localhost4 and localhost6 - so the test named "testMultipleEndpoints" will run correctly host, which do not define these names. Signed-off-by: Tino Reichardt <tino.reichardt@desy.de>
1 parent ae394f2 commit cd08301

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/test/java/org/dcache/nearline/cta/CtaNearlineStorageTest.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import diskCacheV111.vehicles.GenericStorageInfo;
2727
import java.io.File;
2828
import java.io.IOException;
29+
import java.net.Inet4Address;
30+
import java.net.Inet6Address;
2931
import java.net.NetworkInterface;
3032
import java.net.SocketException;
3133
import java.net.URI;
@@ -671,27 +673,30 @@ public void testFailOnLostDeleteMessage() {
671673
verify(request, times(1)).failed(any(CacheException.class));
672674
}
673675

674-
675676
@Test
676677
public void testMultipleEndpoints() throws SocketException {
677678

678-
assumeTrue("localhost has only one IP address",
679-
NetworkInterface.getByName("lo").getInterfaceAddresses().size() > 1);
679+
var lo = NetworkInterface.getByName("lo");
680+
boolean hasIPv4 = lo.getInterfaceAddresses().stream()
681+
.anyMatch(a -> a.getAddress() instanceof Inet4Address);
682+
boolean hasIPv6 = lo.getInterfaceAddresses().stream()
683+
.anyMatch(a -> a.getAddress() instanceof Inet6Address);
684+
assumeTrue("localhost needs both IPv4 and IPv6 for this test",
685+
hasIPv4 && hasIPv6);
680686

681-
var request = mockedStageRequest();
682687
driver = new CtaNearlineStorage("foo", "bar");
683688

684689
var servicePort = cta.getServicePort();
685-
drvConfig.put(CTA_ENDPOINT, "localhost4:" + servicePort + "," + "localhost6:" + servicePort);
690+
drvConfig.put(CTA_ENDPOINT, "127.0.0.1:" + servicePort + ",[::1]:" + servicePort);
686691

687692
driver.configure(drvConfig);
688693
driver.start();
689694

690695
// invoice some to trigger load balancing
691-
driver.stage(Set.of(request));
692-
driver.stage(Set.of(request));
693-
driver.stage(Set.of(request));
694-
driver.stage(Set.of(request));
696+
driver.stage(Set.of(mockedStageRequest()));
697+
driver.stage(Set.of(mockedStageRequest()));
698+
driver.stage(Set.of(mockedStageRequest()));
699+
driver.stage(Set.of(mockedStageRequest()));
695700

696701
assertTrue("Load balancing is not used", cta.getRequestsEndpoints().size() > 1);
697702
}
@@ -800,4 +805,4 @@ private RemoveRequest mockedRemoveRequest() {
800805
return request;
801806
}
802807

803-
}
808+
}

src/test/java/org/dcache/nearline/cta/TestUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.dcache.nearline.cta;
22

33
import eu.emi.security.authn.x509.impl.CertificateUtils;
4+
import org.bouncycastle.asn1.DEROctetString;
45
import org.bouncycastle.asn1.x500.X500Name;
56
import org.bouncycastle.asn1.x509.BasicConstraints;
67
import org.bouncycastle.asn1.x509.ExtendedKeyUsage;
@@ -62,16 +63,16 @@ public static void generateSelfSignedCert(File certFile, File keyFile)
6263
.addExtension(Extension.subjectAlternativeName, true, new GeneralNames(new GeneralName[] {
6364
new GeneralName(GeneralName.dNSName, "localhost"),
6465
new GeneralName(GeneralName.dNSName, "localhost4"),
65-
new GeneralName(GeneralName.dNSName, "localhost6")}))
66+
new GeneralName(GeneralName.dNSName, "localhost6"),
67+
new GeneralName(GeneralName.iPAddress, new DEROctetString(new byte[]{127, 0, 0, 1})),
68+
new GeneralName(GeneralName.iPAddress, new DEROctetString(new byte[]{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1}))}))
6669
.addExtension(Extension.basicConstraints, true, new BasicConstraints(true))
6770
.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment))
6871
.addExtension(Extension.extendedKeyUsage, true, new ExtendedKeyUsage(
6972
new KeyPurposeId[] {KeyPurposeId.id_kp_clientAuth, KeyPurposeId.id_kp_serverAuth}
7073
)
7174
);
7275

73-
74-
7576
String signatureAlgorithm = "SHA256WithRSA";
7677

7778
// sign with own key

0 commit comments

Comments
 (0)