Adding support for whitespace in resource path in tests (#8606)
Motivation: In windows if the project is in a path that contains whitespace, resources cannot be accessed and tests fail. Modifications: Adds ResourcesUtil.java in netty-common. Tests use ResourcesUtil.java to access a resource. Result: Being able to build netty in a path containing whitespace
This commit is contained in:
parent
1dacd37989
commit
d17bd5e160
@ -31,6 +31,7 @@
|
||||
*/
|
||||
package io.netty.handler.codec.http2;
|
||||
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
@ -38,7 +39,6 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@ -56,8 +56,7 @@ public class HpackTest {
|
||||
|
||||
@Parameters(name = "{0}")
|
||||
public static Collection<Object[]> data() {
|
||||
URL url = HpackTest.class.getResource(TEST_DIR);
|
||||
File[] files = new File(url.getFile()).listFiles();
|
||||
File[] files = ResourcesUtil.getFile(HpackTest.class, TEST_DIR).listFiles();
|
||||
if (files == null) {
|
||||
throw new NullPointerException("files");
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2018 The Netty Project
|
||||
*
|
||||
* The Netty Project 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 io.netty.util.internal;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
/**
|
||||
* A utility class that provides various common operations and constants
|
||||
* related to loading resources
|
||||
*/
|
||||
public final class ResourcesUtil {
|
||||
|
||||
/**
|
||||
* Returns a {@link File} named {@code fileName} associated with {@link Class} {@code resourceClass} .
|
||||
*
|
||||
* @param resourceClass The associated class
|
||||
* @param fileName The file name
|
||||
* @return The file named {@code fileName} associated with {@link Class} {@code resourceClass} .
|
||||
*/
|
||||
public static File getFile(Class resourceClass, String fileName) {
|
||||
try {
|
||||
return new File(URLDecoder.decode(resourceClass.getResource(fileName).getFile(), "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return new File(resourceClass.getResource(fileName).getFile());
|
||||
}
|
||||
}
|
||||
|
||||
private ResourcesUtil() { }
|
||||
}
|
@ -33,6 +33,7 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import io.netty.handler.ssl.util.SelfSignedCertificate;
|
||||
import io.netty.handler.ssl.util.SimpleTrustManagerFactory;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.FutureListener;
|
||||
import io.netty.util.concurrent.Promise;
|
||||
@ -46,7 +47,6 @@ import javax.net.ssl.ManagerFactoryParameters;
|
||||
import javax.net.ssl.SSLException;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.File;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.CertificateException;
|
||||
@ -302,8 +302,8 @@ public class ParameterizedSslHandlerTest {
|
||||
|
||||
final SslContext sslClientCtx = SslContextBuilder.forClient()
|
||||
.trustManager(InsecureTrustManagerFactory.INSTANCE)
|
||||
.keyManager(new File(getClass().getResource("test.crt").getFile()),
|
||||
new File(getClass().getResource("test_unencrypted.pem").getFile()))
|
||||
.keyManager(ResourcesUtil.getFile(getClass(), "test.crt"),
|
||||
ResourcesUtil.getFile(getClass(), "test_unencrypted.pem"))
|
||||
.sslProvider(clientProvider).build();
|
||||
|
||||
NioEventLoopGroup group = new NioEventLoopGroup();
|
||||
|
@ -39,6 +39,7 @@ import io.netty.handler.ssl.util.SelfSignedCertificate;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.NetUtil;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.Promise;
|
||||
import io.netty.util.internal.EmptyArrays;
|
||||
@ -501,9 +502,9 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testMutualAuthSameCerts() throws Throwable {
|
||||
mySetupMutualAuth(new File(getClass().getResource("test_unencrypted.pem").getFile()),
|
||||
new File(getClass().getResource("test.crt").getFile()),
|
||||
null);
|
||||
mySetupMutualAuth(ResourcesUtil.getFile(getClass(), "test_unencrypted.pem"),
|
||||
ResourcesUtil.getFile(getClass(), "test.crt"),
|
||||
null);
|
||||
runTest(null);
|
||||
assertTrue(serverLatch.await(2, TimeUnit.SECONDS));
|
||||
Throwable cause = serverException;
|
||||
@ -514,11 +515,11 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testMutualAuthDiffCerts() throws Exception {
|
||||
File serverKeyFile = new File(getClass().getResource("test_encrypted.pem").getFile());
|
||||
File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
|
||||
File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
String serverKeyPassword = "12345";
|
||||
File clientKeyFile = new File(getClass().getResource("test2_encrypted.pem").getFile());
|
||||
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile());
|
||||
File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
|
||||
File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
|
||||
String clientKeyPassword = "12345";
|
||||
mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
|
||||
serverCrtFile, clientKeyFile, clientCrtFile, clientKeyPassword);
|
||||
@ -528,11 +529,11 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testMutualAuthDiffCertsServerFailure() throws Exception {
|
||||
File serverKeyFile = new File(getClass().getResource("test_encrypted.pem").getFile());
|
||||
File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
|
||||
File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
String serverKeyPassword = "12345";
|
||||
File clientKeyFile = new File(getClass().getResource("test2_encrypted.pem").getFile());
|
||||
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile());
|
||||
File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
|
||||
File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
|
||||
String clientKeyPassword = "12345";
|
||||
// Client trusts server but server only trusts itself
|
||||
mySetupMutualAuth(serverCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
|
||||
@ -543,11 +544,11 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testMutualAuthDiffCertsClientFailure() throws Exception {
|
||||
File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
|
||||
File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
|
||||
File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
String serverKeyPassword = null;
|
||||
File clientKeyFile = new File(getClass().getResource("test2_unencrypted.pem").getFile());
|
||||
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile());
|
||||
File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem");
|
||||
File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
|
||||
String clientKeyPassword = null;
|
||||
// Server trusts client but client only trusts itself
|
||||
mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
|
||||
@ -593,7 +594,7 @@ public abstract class SSLEngineTest {
|
||||
final KeyManagerFactory clientKeyManagerFactory =
|
||||
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
clientKeyManagerFactory.init(clientKeyStore, password);
|
||||
File commonCertChain = new File(getClass().getResource("mutual_auth_ca.pem").getFile());
|
||||
File commonCertChain = ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem");
|
||||
|
||||
mySetupMutualAuth(serverKeyManagerFactory, commonCertChain, clientKeyManagerFactory, commonCertChain,
|
||||
auth, false, false);
|
||||
@ -620,7 +621,7 @@ public abstract class SSLEngineTest {
|
||||
final KeyManagerFactory clientKeyManagerFactory =
|
||||
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
clientKeyManagerFactory.init(clientKeyStore, password);
|
||||
File commonCertChain = new File(getClass().getResource("mutual_auth_ca.pem").getFile());
|
||||
File commonCertChain = ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem");
|
||||
|
||||
mySetupMutualAuth(serverKeyManagerFactory, commonCertChain, clientKeyManagerFactory, commonCertChain,
|
||||
auth, true, serverInitEngine);
|
||||
@ -784,9 +785,9 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testClientHostnameValidationSuccess() throws InterruptedException, SSLException {
|
||||
mySetupClientHostnameValidation(new File(getClass().getResource("localhost_server.pem").getFile()),
|
||||
new File(getClass().getResource("localhost_server.key").getFile()),
|
||||
new File(getClass().getResource("mutual_auth_ca.pem").getFile()),
|
||||
mySetupClientHostnameValidation(ResourcesUtil.getFile(getClass(), "localhost_server.pem"),
|
||||
ResourcesUtil.getFile(getClass(), "localhost_server.key"),
|
||||
ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem"),
|
||||
false);
|
||||
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
|
||||
assertNull(clientException);
|
||||
@ -796,9 +797,9 @@ public abstract class SSLEngineTest {
|
||||
|
||||
@Test
|
||||
public void testClientHostnameValidationFail() throws InterruptedException, SSLException {
|
||||
mySetupClientHostnameValidation(new File(getClass().getResource("notlocalhost_server.pem").getFile()),
|
||||
new File(getClass().getResource("notlocalhost_server.key").getFile()),
|
||||
new File(getClass().getResource("mutual_auth_ca.pem").getFile()),
|
||||
mySetupClientHostnameValidation(ResourcesUtil.getFile(getClass(), "notlocalhost_server.pem"),
|
||||
ResourcesUtil.getFile(getClass(), "notlocalhost_server.key"),
|
||||
ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem"),
|
||||
true);
|
||||
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
|
||||
assertTrue("unexpected exception: " + clientException,
|
||||
@ -1347,8 +1348,8 @@ public abstract class SSLEngineTest {
|
||||
protected void testEnablingAnAlreadyDisabledSslProtocol(String[] protocols1, String[] protocols2) throws Exception {
|
||||
SSLEngine sslEngine = null;
|
||||
try {
|
||||
File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
|
||||
File serverCrtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
|
||||
File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
serverSslCtx = SslContextBuilder.forServer(serverCrtFile, serverKeyFile)
|
||||
.sslProvider(sslServerProvider())
|
||||
.sslContextProvider(serverSslContextProvider())
|
||||
|
@ -44,6 +44,7 @@ import io.netty.util.DomainNameMappingBuilder;
|
||||
import io.netty.util.Mapping;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.util.ReferenceCounted;
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import io.netty.util.concurrent.Promise;
|
||||
import io.netty.util.internal.ObjectUtil;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
@ -98,8 +99,8 @@ public class SniHandlerTest {
|
||||
assumeApnSupported(provider);
|
||||
}
|
||||
|
||||
File keyFile = new File(SniHandlerTest.class.getResource("test_encrypted.pem").getFile());
|
||||
File crtFile = new File(SniHandlerTest.class.getResource("test.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(SniHandlerTest.class, "test_encrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(SniHandlerTest.class, "test.crt");
|
||||
|
||||
SslContextBuilder sslCtxBuilder = SslContextBuilder.forServer(crtFile, keyFile, "12345")
|
||||
.sslProvider(provider);
|
||||
@ -114,7 +115,7 @@ public class SniHandlerTest {
|
||||
assumeApnSupported(provider);
|
||||
}
|
||||
|
||||
File crtFile = new File(SniHandlerTest.class.getResource("test.crt").getFile());
|
||||
File crtFile = ResourcesUtil.getFile(SniHandlerTest.class, "test.crt");
|
||||
|
||||
SslContextBuilder sslCtxBuilder = SslContextBuilder.forClient().trustManager(crtFile).sslProvider(provider);
|
||||
if (apn) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.netty.handler.ssl;
|
||||
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -38,58 +39,58 @@ public abstract class SslContextTest {
|
||||
@Test(expected = IOException.class)
|
||||
public void testUnencryptedEmptyPassword() throws Exception {
|
||||
PrivateKey key = SslContext.toPrivateKey(
|
||||
new File(getClass().getResource("test2_unencrypted.pem").getFile()), "");
|
||||
ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem"), "");
|
||||
Assert.assertNotNull(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnEncryptedNullPassword() throws Exception {
|
||||
PrivateKey key = SslContext.toPrivateKey(
|
||||
new File(getClass().getResource("test2_unencrypted.pem").getFile()), null);
|
||||
ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem"), null);
|
||||
Assert.assertNotNull(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptedEmptyPassword() throws Exception {
|
||||
PrivateKey key = SslContext.toPrivateKey(
|
||||
new File(getClass().getResource("test_encrypted_empty_pass.pem").getFile()), "");
|
||||
ResourcesUtil.getFile(getClass(), "test_encrypted_empty_pass.pem"), "");
|
||||
Assert.assertNotNull(key);
|
||||
}
|
||||
|
||||
@Test(expected = InvalidKeySpecException.class)
|
||||
public void testEncryptedNullPassword() throws Exception {
|
||||
SslContext.toPrivateKey(
|
||||
new File(getClass().getResource("test_encrypted_empty_pass.pem").getFile()), null);
|
||||
ResourcesUtil.getFile(getClass(), "test_encrypted_empty_pass.pem"), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSslServerWithEncryptedPrivateKey() throws SSLException {
|
||||
File keyFile = new File(getClass().getResource("test_encrypted.pem").getFile());
|
||||
File crtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
|
||||
newServerContext(crtFile, keyFile, "12345");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSslServerWithEncryptedPrivateKey2() throws SSLException {
|
||||
File keyFile = new File(getClass().getResource("test2_encrypted.pem").getFile());
|
||||
File crtFile = new File(getClass().getResource("test2.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
|
||||
|
||||
newServerContext(crtFile, keyFile, "12345");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSslServerWithUnencryptedPrivateKey() throws SSLException {
|
||||
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
|
||||
File crtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
|
||||
newServerContext(crtFile, keyFile, null);
|
||||
}
|
||||
|
||||
@Test(expected = SSLException.class)
|
||||
public void testSslServerWithUnencryptedPrivateKeyEmptyPass() throws SSLException {
|
||||
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
|
||||
File crtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
|
||||
newServerContext(crtFile, keyFile, "");
|
||||
}
|
||||
@ -108,8 +109,8 @@ public abstract class SslContextTest {
|
||||
exception = e;
|
||||
}
|
||||
assumeNotNull(exception);
|
||||
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile());
|
||||
File crtFile = new File(getClass().getResource("test.crt").getFile());
|
||||
File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
|
||||
File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
|
||||
|
||||
SslContext sslContext = newServerContext(crtFile, keyFile, null);
|
||||
assertFalse(sslContext.cipherSuites().contains(unsupportedCipher));
|
||||
|
@ -16,11 +16,11 @@
|
||||
package io.netty.resolver;
|
||||
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.internal.ResourcesUtil;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.net.Inet4Address;
|
||||
@ -76,7 +76,7 @@ public class HostsFileParserTest {
|
||||
return;
|
||||
}
|
||||
testParseFile(HostsFileParser.parse(
|
||||
new File(getClass().getResource("hosts-unicode").getFile()), unicodeCharset));
|
||||
ResourcesUtil.getFile(getClass(), "hosts-unicode"), unicodeCharset));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -88,7 +88,7 @@ public class HostsFileParserTest {
|
||||
Assume.assumeNoException(e);
|
||||
return;
|
||||
}
|
||||
testParseFile(HostsFileParser.parse(new File(getClass().getResource("hosts-unicode").getFile()),
|
||||
testParseFile(HostsFileParser.parse(ResourcesUtil.getFile(getClass(), "hosts-unicode"),
|
||||
CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1, unicodeCharset));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user