broker/ServerIntegrationSSLTest: code formatter used
This commit is contained in:
@@ -13,10 +13,10 @@
|
|||||||
*
|
*
|
||||||
* You may elect to redistribute this code under either of these licenses.
|
* You may elect to redistribute this code under either of these licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.moquette.server;
|
package io.moquette.server;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -27,13 +27,11 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.security.UnrecoverableKeyException;
|
import java.security.UnrecoverableKeyException;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.net.ssl.KeyManagerFactory;
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.TrustManagerFactory;
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
|
|
||||||
import io.moquette.BrokerConstants;
|
import io.moquette.BrokerConstants;
|
||||||
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||||
@@ -51,10 +49,11 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check that Moquette could also handle SSL.
|
* Check that Moquette could also handle SSL.
|
||||||
*
|
*
|
||||||
* @author andrea
|
* @author andrea
|
||||||
*/
|
*/
|
||||||
public class ServerIntegrationSSLTest {
|
public class ServerIntegrationSSLTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationSSLTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationSSLTest.class);
|
||||||
|
|
||||||
Server m_server;
|
Server m_server;
|
||||||
@@ -92,8 +91,8 @@ public class ServerIntegrationSSLTest {
|
|||||||
startServer();
|
startServer();
|
||||||
|
|
||||||
m_client = new MqttClient("ssl://localhost:8883", "TestClient", s_dataStore);
|
m_client = new MqttClient("ssl://localhost:8883", "TestClient", s_dataStore);
|
||||||
// m_client = new MqttClient("ssl://test.mosquitto.org:8883", "TestClient", s_dataStore);
|
// m_client = new MqttClient("ssl://test.mosquitto.org:8883", "TestClient", s_dataStore);
|
||||||
|
|
||||||
m_callback = new MessageCollector();
|
m_callback = new MessageCollector();
|
||||||
m_client.setCallback(m_callback);
|
m_client.setCallback(m_callback);
|
||||||
}
|
}
|
||||||
@@ -114,12 +113,12 @@ public class ServerIntegrationSSLTest {
|
|||||||
}
|
}
|
||||||
assertFalse(dbFile.exists());
|
assertFalse(dbFile.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkSupportSSL() throws Exception {
|
public void checkSupportSSL() throws Exception {
|
||||||
LOG.info("*** checkSupportSSL ***");
|
LOG.info("*** checkSupportSSL ***");
|
||||||
SSLSocketFactory ssf = configureSSLSocketFactory();
|
SSLSocketFactory ssf = configureSSLSocketFactory();
|
||||||
|
|
||||||
MqttConnectOptions options = new MqttConnectOptions();
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
options.setSocketFactory(ssf);
|
options.setSocketFactory(ssf);
|
||||||
m_client.connect(options);
|
m_client.connect(options);
|
||||||
@@ -131,38 +130,38 @@ public class ServerIntegrationSSLTest {
|
|||||||
public void checkSupportSSLForMultipleClient() throws Exception {
|
public void checkSupportSSLForMultipleClient() throws Exception {
|
||||||
LOG.info("*** checkSupportSSLForMultipleClient ***");
|
LOG.info("*** checkSupportSSLForMultipleClient ***");
|
||||||
SSLSocketFactory ssf = configureSSLSocketFactory();
|
SSLSocketFactory ssf = configureSSLSocketFactory();
|
||||||
|
|
||||||
MqttConnectOptions options = new MqttConnectOptions();
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
options.setSocketFactory(ssf);
|
options.setSocketFactory(ssf);
|
||||||
m_client.connect(options);
|
m_client.connect(options);
|
||||||
m_client.subscribe("/topic", 0);
|
m_client.subscribe("/topic", 0);
|
||||||
|
|
||||||
MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence());
|
MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence());
|
||||||
MqttConnectOptions secondClientOptions = new MqttConnectOptions();
|
MqttConnectOptions secondClientOptions = new MqttConnectOptions();
|
||||||
secondClientOptions.setSocketFactory(ssf);
|
secondClientOptions.setSocketFactory(ssf);
|
||||||
secondClient.connect(secondClientOptions);
|
secondClient.connect(secondClientOptions);
|
||||||
secondClient.publish("/topic", new MqttMessage("message".getBytes()));
|
secondClient.publish("/topic", new MqttMessage("message".getBytes()));
|
||||||
secondClient.disconnect();
|
secondClient.disconnect();
|
||||||
|
|
||||||
m_client.disconnect();
|
m_client.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* keystore generated into test/resources with command:
|
* keystore generated into test/resources with command:
|
||||||
*
|
*
|
||||||
* keytool -keystore clientkeystore.jks -alias testclient -genkey -keyalg RSA
|
* keytool -keystore clientkeystore.jks -alias testclient -genkey -keyalg RSA -> mandatory to
|
||||||
* -> mandatory to put the name surname
|
* put the name surname -> password is passw0rd -> type yes at the end
|
||||||
* -> password is passw0rd
|
*
|
||||||
* -> type yes at the end
|
* to generate the crt file from the keystore -- keytool -certreq -alias testclient -keystore
|
||||||
*
|
* clientkeystore.jks -file testclient.csr
|
||||||
* to generate the crt file from the keystore
|
*
|
||||||
* -- keytool -certreq -alias testclient -keystore clientkeystore.jks -file testclient.csr
|
|
||||||
*
|
|
||||||
* keytool -export -alias testclient -keystore clientkeystore.jks -file testclient.crt
|
* keytool -export -alias testclient -keystore clientkeystore.jks -file testclient.crt
|
||||||
*
|
*
|
||||||
* to import an existing certificate:
|
* to import an existing certificate: keytool -keystore clientkeystore.jks -import -alias
|
||||||
* keytool -keystore clientkeystore.jks -import -alias testclient -file testclient.crt -trustcacerts
|
* testclient -file testclient.crt -trustcacerts
|
||||||
*/
|
*/
|
||||||
private SSLSocketFactory configureSSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException, IOException, CertificateException, KeyStoreException {
|
private SSLSocketFactory configureSSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException,
|
||||||
|
UnrecoverableKeyException, IOException, CertificateException, KeyStoreException {
|
||||||
KeyStore ks = KeyStore.getInstance("JKS");
|
KeyStore ks = KeyStore.getInstance("JKS");
|
||||||
InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream("clientkeystore.jks");
|
InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream("clientkeystore.jks");
|
||||||
ks.load(jksInputStream, "passw0rd".toCharArray());
|
ks.load(jksInputStream, "passw0rd".toCharArray());
|
||||||
@@ -173,9 +172,9 @@ public class ServerIntegrationSSLTest {
|
|||||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||||
tmf.init(ks);
|
tmf.init(ks);
|
||||||
|
|
||||||
SSLContext sc = SSLContext.getInstance("TLS");
|
SSLContext sc = SSLContext.getInstance("TLS");
|
||||||
TrustManager[] trustManagers = tmf.getTrustManagers();
|
TrustManager[] trustManagers = tmf.getTrustManagers();
|
||||||
sc.init(kmf.getKeyManagers(), trustManagers, null);
|
sc.init(kmf.getKeyManagers(), trustManagers, null);
|
||||||
|
|
||||||
SSLSocketFactory ssf = sc.getSocketFactory();
|
SSLSocketFactory ssf = sc.getSocketFactory();
|
||||||
return ssf;
|
return ssf;
|
||||||
|
|||||||
Reference in New Issue
Block a user