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:
Feri73 2018-12-12 01:29:02 -08:00 committed by Norman Maurer
parent 1dacd37989
commit d17bd5e160
7 changed files with 96 additions and 51 deletions

View File

@ -31,6 +31,7 @@
*/ */
package io.netty.handler.codec.http2; package io.netty.handler.codec.http2;
import io.netty.util.internal.ResourcesUtil;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -38,7 +39,6 @@ import org.junit.runners.Parameterized.Parameters;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -56,8 +56,7 @@ public class HpackTest {
@Parameters(name = "{0}") @Parameters(name = "{0}")
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
URL url = HpackTest.class.getResource(TEST_DIR); File[] files = ResourcesUtil.getFile(HpackTest.class, TEST_DIR).listFiles();
File[] files = new File(url.getFile()).listFiles();
if (files == null) { if (files == null) {
throw new NullPointerException("files"); throw new NullPointerException("files");
} }

View File

@ -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() { }
}

View File

@ -33,6 +33,7 @@ import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate; import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.ssl.util.SimpleTrustManagerFactory; import io.netty.handler.ssl.util.SimpleTrustManagerFactory;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ResourcesUtil;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener; import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
@ -46,7 +47,6 @@ import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
@ -302,8 +302,8 @@ public class ParameterizedSslHandlerTest {
final SslContext sslClientCtx = SslContextBuilder.forClient() final SslContext sslClientCtx = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE) .trustManager(InsecureTrustManagerFactory.INSTANCE)
.keyManager(new File(getClass().getResource("test.crt").getFile()), .keyManager(ResourcesUtil.getFile(getClass(), "test.crt"),
new File(getClass().getResource("test_unencrypted.pem").getFile())) ResourcesUtil.getFile(getClass(), "test_unencrypted.pem"))
.sslProvider(clientProvider).build(); .sslProvider(clientProvider).build();
NioEventLoopGroup group = new NioEventLoopGroup(); NioEventLoopGroup group = new NioEventLoopGroup();

View File

@ -39,6 +39,7 @@ import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil; import io.netty.util.NetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ResourcesUtil;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.EmptyArrays;
@ -501,8 +502,8 @@ public abstract class SSLEngineTest {
@Test @Test
public void testMutualAuthSameCerts() throws Throwable { public void testMutualAuthSameCerts() throws Throwable {
mySetupMutualAuth(new File(getClass().getResource("test_unencrypted.pem").getFile()), mySetupMutualAuth(ResourcesUtil.getFile(getClass(), "test_unencrypted.pem"),
new File(getClass().getResource("test.crt").getFile()), ResourcesUtil.getFile(getClass(), "test.crt"),
null); null);
runTest(null); runTest(null);
assertTrue(serverLatch.await(2, TimeUnit.SECONDS)); assertTrue(serverLatch.await(2, TimeUnit.SECONDS));
@ -514,11 +515,11 @@ public abstract class SSLEngineTest {
@Test @Test
public void testMutualAuthDiffCerts() throws Exception { public void testMutualAuthDiffCerts() throws Exception {
File serverKeyFile = new File(getClass().getResource("test_encrypted.pem").getFile()); File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
File serverCrtFile = new File(getClass().getResource("test.crt").getFile()); File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
String serverKeyPassword = "12345"; String serverKeyPassword = "12345";
File clientKeyFile = new File(getClass().getResource("test2_encrypted.pem").getFile()); File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile()); File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
String clientKeyPassword = "12345"; String clientKeyPassword = "12345";
mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword, mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
serverCrtFile, clientKeyFile, clientCrtFile, clientKeyPassword); serverCrtFile, clientKeyFile, clientCrtFile, clientKeyPassword);
@ -528,11 +529,11 @@ public abstract class SSLEngineTest {
@Test @Test
public void testMutualAuthDiffCertsServerFailure() throws Exception { public void testMutualAuthDiffCertsServerFailure() throws Exception {
File serverKeyFile = new File(getClass().getResource("test_encrypted.pem").getFile()); File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
File serverCrtFile = new File(getClass().getResource("test.crt").getFile()); File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
String serverKeyPassword = "12345"; String serverKeyPassword = "12345";
File clientKeyFile = new File(getClass().getResource("test2_encrypted.pem").getFile()); File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile()); File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
String clientKeyPassword = "12345"; String clientKeyPassword = "12345";
// Client trusts server but server only trusts itself // Client trusts server but server only trusts itself
mySetupMutualAuth(serverCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword, mySetupMutualAuth(serverCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
@ -543,11 +544,11 @@ public abstract class SSLEngineTest {
@Test @Test
public void testMutualAuthDiffCertsClientFailure() throws Exception { public void testMutualAuthDiffCertsClientFailure() throws Exception {
File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile()); File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
File serverCrtFile = new File(getClass().getResource("test.crt").getFile()); File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
String serverKeyPassword = null; String serverKeyPassword = null;
File clientKeyFile = new File(getClass().getResource("test2_unencrypted.pem").getFile()); File clientKeyFile = ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem");
File clientCrtFile = new File(getClass().getResource("test2.crt").getFile()); File clientCrtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
String clientKeyPassword = null; String clientKeyPassword = null;
// Server trusts client but client only trusts itself // Server trusts client but client only trusts itself
mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword, mySetupMutualAuth(clientCrtFile, serverKeyFile, serverCrtFile, serverKeyPassword,
@ -593,7 +594,7 @@ public abstract class SSLEngineTest {
final KeyManagerFactory clientKeyManagerFactory = final KeyManagerFactory clientKeyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
clientKeyManagerFactory.init(clientKeyStore, password); 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, mySetupMutualAuth(serverKeyManagerFactory, commonCertChain, clientKeyManagerFactory, commonCertChain,
auth, false, false); auth, false, false);
@ -620,7 +621,7 @@ public abstract class SSLEngineTest {
final KeyManagerFactory clientKeyManagerFactory = final KeyManagerFactory clientKeyManagerFactory =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
clientKeyManagerFactory.init(clientKeyStore, password); 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, mySetupMutualAuth(serverKeyManagerFactory, commonCertChain, clientKeyManagerFactory, commonCertChain,
auth, true, serverInitEngine); auth, true, serverInitEngine);
@ -784,9 +785,9 @@ public abstract class SSLEngineTest {
@Test @Test
public void testClientHostnameValidationSuccess() throws InterruptedException, SSLException { public void testClientHostnameValidationSuccess() throws InterruptedException, SSLException {
mySetupClientHostnameValidation(new File(getClass().getResource("localhost_server.pem").getFile()), mySetupClientHostnameValidation(ResourcesUtil.getFile(getClass(), "localhost_server.pem"),
new File(getClass().getResource("localhost_server.key").getFile()), ResourcesUtil.getFile(getClass(), "localhost_server.key"),
new File(getClass().getResource("mutual_auth_ca.pem").getFile()), ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem"),
false); false);
assertTrue(clientLatch.await(5, TimeUnit.SECONDS)); assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
assertNull(clientException); assertNull(clientException);
@ -796,9 +797,9 @@ public abstract class SSLEngineTest {
@Test @Test
public void testClientHostnameValidationFail() throws InterruptedException, SSLException { public void testClientHostnameValidationFail() throws InterruptedException, SSLException {
mySetupClientHostnameValidation(new File(getClass().getResource("notlocalhost_server.pem").getFile()), mySetupClientHostnameValidation(ResourcesUtil.getFile(getClass(), "notlocalhost_server.pem"),
new File(getClass().getResource("notlocalhost_server.key").getFile()), ResourcesUtil.getFile(getClass(), "notlocalhost_server.key"),
new File(getClass().getResource("mutual_auth_ca.pem").getFile()), ResourcesUtil.getFile(getClass(), "mutual_auth_ca.pem"),
true); true);
assertTrue(clientLatch.await(5, TimeUnit.SECONDS)); assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
assertTrue("unexpected exception: " + clientException, assertTrue("unexpected exception: " + clientException,
@ -1347,8 +1348,8 @@ public abstract class SSLEngineTest {
protected void testEnablingAnAlreadyDisabledSslProtocol(String[] protocols1, String[] protocols2) throws Exception { protected void testEnablingAnAlreadyDisabledSslProtocol(String[] protocols1, String[] protocols2) throws Exception {
SSLEngine sslEngine = null; SSLEngine sslEngine = null;
try { try {
File serverKeyFile = new File(getClass().getResource("test_unencrypted.pem").getFile()); File serverKeyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
File serverCrtFile = new File(getClass().getResource("test.crt").getFile()); File serverCrtFile = ResourcesUtil.getFile(getClass(), "test.crt");
serverSslCtx = SslContextBuilder.forServer(serverCrtFile, serverKeyFile) serverSslCtx = SslContextBuilder.forServer(serverCrtFile, serverKeyFile)
.sslProvider(sslServerProvider()) .sslProvider(sslServerProvider())
.sslContextProvider(serverSslContextProvider()) .sslContextProvider(serverSslContextProvider())

View File

@ -44,6 +44,7 @@ import io.netty.util.DomainNameMappingBuilder;
import io.netty.util.Mapping; import io.netty.util.Mapping;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted; import io.netty.util.ReferenceCounted;
import io.netty.util.internal.ResourcesUtil;
import io.netty.util.concurrent.Promise; import io.netty.util.concurrent.Promise;
import io.netty.util.internal.ObjectUtil; import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
@ -98,8 +99,8 @@ public class SniHandlerTest {
assumeApnSupported(provider); assumeApnSupported(provider);
} }
File keyFile = new File(SniHandlerTest.class.getResource("test_encrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(SniHandlerTest.class, "test_encrypted.pem");
File crtFile = new File(SniHandlerTest.class.getResource("test.crt").getFile()); File crtFile = ResourcesUtil.getFile(SniHandlerTest.class, "test.crt");
SslContextBuilder sslCtxBuilder = SslContextBuilder.forServer(crtFile, keyFile, "12345") SslContextBuilder sslCtxBuilder = SslContextBuilder.forServer(crtFile, keyFile, "12345")
.sslProvider(provider); .sslProvider(provider);
@ -114,7 +115,7 @@ public class SniHandlerTest {
assumeApnSupported(provider); 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); SslContextBuilder sslCtxBuilder = SslContextBuilder.forClient().trustManager(crtFile).sslProvider(provider);
if (apn) { if (apn) {

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.handler.ssl; package io.netty.handler.ssl;
import io.netty.util.internal.ResourcesUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -38,58 +39,58 @@ public abstract class SslContextTest {
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void testUnencryptedEmptyPassword() throws Exception { public void testUnencryptedEmptyPassword() throws Exception {
PrivateKey key = SslContext.toPrivateKey( PrivateKey key = SslContext.toPrivateKey(
new File(getClass().getResource("test2_unencrypted.pem").getFile()), ""); ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem"), "");
Assert.assertNotNull(key); Assert.assertNotNull(key);
} }
@Test @Test
public void testUnEncryptedNullPassword() throws Exception { public void testUnEncryptedNullPassword() throws Exception {
PrivateKey key = SslContext.toPrivateKey( PrivateKey key = SslContext.toPrivateKey(
new File(getClass().getResource("test2_unencrypted.pem").getFile()), null); ResourcesUtil.getFile(getClass(), "test2_unencrypted.pem"), null);
Assert.assertNotNull(key); Assert.assertNotNull(key);
} }
@Test @Test
public void testEncryptedEmptyPassword() throws Exception { public void testEncryptedEmptyPassword() throws Exception {
PrivateKey key = SslContext.toPrivateKey( 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); Assert.assertNotNull(key);
} }
@Test(expected = InvalidKeySpecException.class) @Test(expected = InvalidKeySpecException.class)
public void testEncryptedNullPassword() throws Exception { public void testEncryptedNullPassword() throws Exception {
SslContext.toPrivateKey( SslContext.toPrivateKey(
new File(getClass().getResource("test_encrypted_empty_pass.pem").getFile()), null); ResourcesUtil.getFile(getClass(), "test_encrypted_empty_pass.pem"), null);
} }
@Test @Test
public void testSslServerWithEncryptedPrivateKey() throws SSLException { public void testSslServerWithEncryptedPrivateKey() throws SSLException {
File keyFile = new File(getClass().getResource("test_encrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(getClass(), "test_encrypted.pem");
File crtFile = new File(getClass().getResource("test.crt").getFile()); File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
newServerContext(crtFile, keyFile, "12345"); newServerContext(crtFile, keyFile, "12345");
} }
@Test @Test
public void testSslServerWithEncryptedPrivateKey2() throws SSLException { public void testSslServerWithEncryptedPrivateKey2() throws SSLException {
File keyFile = new File(getClass().getResource("test2_encrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(getClass(), "test2_encrypted.pem");
File crtFile = new File(getClass().getResource("test2.crt").getFile()); File crtFile = ResourcesUtil.getFile(getClass(), "test2.crt");
newServerContext(crtFile, keyFile, "12345"); newServerContext(crtFile, keyFile, "12345");
} }
@Test @Test
public void testSslServerWithUnencryptedPrivateKey() throws SSLException { public void testSslServerWithUnencryptedPrivateKey() throws SSLException {
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
File crtFile = new File(getClass().getResource("test.crt").getFile()); File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
newServerContext(crtFile, keyFile, null); newServerContext(crtFile, keyFile, null);
} }
@Test(expected = SSLException.class) @Test(expected = SSLException.class)
public void testSslServerWithUnencryptedPrivateKeyEmptyPass() throws SSLException { public void testSslServerWithUnencryptedPrivateKeyEmptyPass() throws SSLException {
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
File crtFile = new File(getClass().getResource("test.crt").getFile()); File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
newServerContext(crtFile, keyFile, ""); newServerContext(crtFile, keyFile, "");
} }
@ -108,8 +109,8 @@ public abstract class SslContextTest {
exception = e; exception = e;
} }
assumeNotNull(exception); assumeNotNull(exception);
File keyFile = new File(getClass().getResource("test_unencrypted.pem").getFile()); File keyFile = ResourcesUtil.getFile(getClass(), "test_unencrypted.pem");
File crtFile = new File(getClass().getResource("test.crt").getFile()); File crtFile = ResourcesUtil.getFile(getClass(), "test.crt");
SslContext sslContext = newServerContext(crtFile, keyFile, null); SslContext sslContext = newServerContext(crtFile, keyFile, null);
assertFalse(sslContext.cipherSuites().contains(unsupportedCipher)); assertFalse(sslContext.cipherSuites().contains(unsupportedCipher));

View File

@ -16,11 +16,11 @@
package io.netty.resolver; package io.netty.resolver;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.internal.ResourcesUtil;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.net.Inet4Address; import java.net.Inet4Address;
@ -76,7 +76,7 @@ public class HostsFileParserTest {
return; return;
} }
testParseFile(HostsFileParser.parse( testParseFile(HostsFileParser.parse(
new File(getClass().getResource("hosts-unicode").getFile()), unicodeCharset)); ResourcesUtil.getFile(getClass(), "hosts-unicode"), unicodeCharset));
} }
@Test @Test
@ -88,7 +88,7 @@ public class HostsFileParserTest {
Assume.assumeNoException(e); Assume.assumeNoException(e);
return; 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)); CharsetUtil.UTF_8, CharsetUtil.ISO_8859_1, unicodeCharset));
} }