Skip to content

Commit 619d066

Browse files
committed
Fix URL to pathname conversion
Filename were taken from properties as URL. If a pathname contains characters that are not valid URL characters, they appear URL-encoded in the string (e.g. '@' -> '%40') causing the program to fail when trying to open the file. Signed-off-by: maxp <maxpag@gmail.com>
1 parent 7c911f0 commit 619d066

5 files changed

Lines changed: 40 additions & 19 deletions

File tree

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/MqttDataTypesTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package org.eclipse.paho.client.mqttv3.test;
22

3-
import java.io.BufferedReader;
4-
import java.io.ByteArrayInputStream;
5-
import java.io.ByteArrayOutputStream;
6-
import java.io.DataInputStream;
7-
import java.io.DataOutputStream;
8-
import java.io.File;
9-
import java.io.FileReader;
10-
import java.io.IOException;
3+
import java.io.*;
4+
import java.nio.charset.Charset;
5+
import java.nio.charset.StandardCharsets;
116

127
import org.eclipse.paho.client.mqttv3.MqttException;
138
import org.eclipse.paho.client.mqttv3.internal.wire.MqttWireMessage;
@@ -127,7 +122,18 @@ public void TestEncodeAndDecodeComplexUTF8String() throws MqttException {
127122
@Test
128123
public void testICanEatGlass() throws IOException, MqttException {
129124
ClassLoader classLoader = getClass().getClassLoader();
130-
File file = new File(classLoader.getResource("i_can_eat_glass.txt").getFile());
125+
String encodedFileName = classLoader.getResource("i_can_eat_glass.txt").getFile();
126+
String decodedFileName;
127+
try {
128+
decodedFileName = java.net.URLDecoder.decode( encodedFileName, StandardCharsets.UTF_8.name() );
129+
}
130+
catch ( UnsupportedEncodingException e ) {
131+
// can't decode the URL, passing on the encoded name hoping that it
132+
// actually contains something that exists in the filesystem with
133+
// that name.
134+
decodedFileName = encodedFileName;
135+
}
136+
File file = new File(decodedFileName);
131137

132138
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
133139
for (String line; (line = br.readLine()) != null;) {

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/SendReceiveAsyncTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public void testConnectTimeout() throws Exception {
640640
Assert.fail("Should throw an timeout exception.");
641641
}
642642
catch (Exception exception) {
643-
log.log(Level.INFO, "Connect action failed at expected.");
643+
log.log(Level.INFO, "Connect action failed as expected.");
644644
Assert.assertTrue(exception instanceof MqttException);
645645
Assert.assertEquals(MqttException.REASON_CODE_CLIENT_TIMEOUT, ((MqttException) exception).getReasonCode());
646646
}
@@ -658,7 +658,7 @@ public void testConnectTimeout() throws Exception {
658658
connectToken.waitForCompletion(5000);
659659
}
660660
catch (Exception exception) {
661-
log.log(Level.INFO, "Connect action failed at expected.");
661+
log.log(Level.INFO, "Connect action failed as expected.");
662662
Assert.assertTrue(exception instanceof MqttException);
663663
Assert.assertEquals(
664664
(MqttException.REASON_CODE_CLIENT_TIMEOUT == ((MqttException) exception).getReasonCode() ||

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/properties/TestProperties.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
package org.eclipse.paho.client.mqttv3.test.properties;
1616

17-
import java.io.File;
18-
import java.io.FileInputStream;
19-
import java.io.IOException;
20-
import java.io.InputStream;
17+
import java.io.*;
2118
import java.net.InetAddress;
2219
import java.net.URI;
2320
import java.net.URISyntaxException;
2421
import java.net.URL;
2522
import java.net.UnknownHostException;
23+
import java.nio.charset.StandardCharsets;
2624
import java.util.ArrayList;
2725
import java.util.HashMap;
2826
import java.util.List;
@@ -336,7 +334,15 @@ public static File getTemporaryDirectory() {
336334

337335
public static String getClientKeyStore() {
338336
URL keyStore = cclass.getClassLoader().getResource(getInstance().getProperty(KEY_CLIENT_KEY_STORE));
339-
return keyStore.getPath();
337+
String encodedPath=keyStore.getPath();
338+
try {
339+
return java.net.URLDecoder.decode( encodedPath, StandardCharsets.UTF_8.name());
340+
}
341+
catch (UnsupportedEncodingException e ) {
342+
// likely the property value is malformed. Return it as is, hoping that
343+
// the requester knows what to make of it.
344+
return encodedPath;
345+
}
340346
}
341347

342348
/**

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/security/SSLSocketFactoryFactory.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.io.FileInputStream;
1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.io.UnsupportedEncodingException;
22+
import java.nio.charset.StandardCharsets;
2123
import java.security.KeyManagementException;
2224
import java.security.KeyStore;
2325
import java.security.KeyStoreException;
@@ -1005,7 +1007,14 @@ public String getKeyManager(String configID) {
10051007
* @return The name of the file that contains the truststore.
10061008
*/
10071009
public String getTrustStore(String configID) {
1008-
return getProperty(configID, TRUSTSTORE, SYSTRUSTSTORE);
1010+
String encodedPath = getProperty(configID, TRUSTSTORE, SYSTRUSTSTORE);
1011+
try {
1012+
String decodedPath = java.net.URLDecoder.decode( encodedPath, StandardCharsets.UTF_8.name());
1013+
return decodedPath;
1014+
}
1015+
catch( UnsupportedEncodingException e ) {
1016+
return encodedPath;
1017+
}
10091018
}
10101019

10111020
/**

org.eclipse.paho.mqttv5.client/src/test/java/org/eclipse/paho/mqttv5/client/test/SendReceiveAsyncTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public void testConnectTimeout() throws Exception {
624624
Assert.fail("Should throw a timeout exception.");
625625
}
626626
catch (Exception exception) {
627-
log.log(Level.INFO, "Connect action failed at expected.");
627+
log.log(Level.INFO, "Connect action failed as expected.");
628628
Assert.assertTrue(exception instanceof MqttException);
629629
Assert.assertEquals(MqttException.REASON_CODE_MALFORMED_PACKET, ((MqttException) exception).getReasonCode());
630630
}
@@ -646,7 +646,7 @@ public void testConnectTimeout() throws Exception {
646646
connectToken.waitForCompletion(5000);
647647
}
648648
catch (Exception exception) {
649-
log.log(Level.INFO, "Connect action failed at expected.");
649+
log.log(Level.INFO, "Connect action failed as expected.");
650650
//Assert.assertTrue(exception instanceof MqttException);
651651
Assert.assertEquals(
652652
(MqttClientException.REASON_CODE_CLIENT_CLOSED == ((MqttException) exception).getReasonCode() ||

0 commit comments

Comments
 (0)