Duplicate java SSL test for mqtt v5

This commit is contained in:
Chunyi Lyu 2023-06-21 14:35:48 +01:00 committed by Chunyi Lyu
parent 147e2d6676
commit 17ad067259
3 changed files with 169 additions and 3 deletions

View File

@ -49,8 +49,7 @@ init_per_suite(Config) ->
rabbit_ct_helpers:log_environment(),
Config.
end_per_suite(Config) ->
rabbit_ct_helpers:run_teardown_steps(Config).
end_per_suite(Config) -> Config.
init_per_group(Group, Config0) ->
Suffix = rabbit_ct_helpers:testcase_absname(Config0, "", "-"),
@ -103,7 +102,7 @@ java(Config) ->
run_test(Config, ["tests", "ssltests"]).
java_v5(Config) ->
run_test(Config, ["v5tests"]).
run_test(Config, ["v5tests", "v5ssltests"]).
run_test(Config, Target) ->
CertsDir = rabbit_ct_helpers:get_config(Config, rmq_certsdir),

View File

@ -28,6 +28,9 @@ v5tests:
# @mvnw -q $(MVN_FLAGS) -Dtest=MqttV5Test#subscribeMultiple test
@mvnw -q $(MVN_FLAGS) -Dtest=MqttV5Test test
v5ssltests:
@mvnw -q $(MVN_FLAGS) -Dtest=MqttV5SSLTest test
clean:
@mvnw clean

View File

@ -0,0 +1,164 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
//
package com.rabbitmq.mqtt.test.tls;
import org.eclipse.paho.mqttv5.client.*;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.eclipse.paho.mqttv5.common.packet.MqttReturnCode.RETURN_CODE_BAD_USERNAME_OR_PASSWORD;
/**
* MQTT v5 TLS test
*/
public class MqttV5SSLTest implements MqttCallback {
private final String brokerUrl = "ssl://" + getHost() + ":" + getPort();
private String clientId;
private String clientId2;
private MqttClient client;
private MqttClient client2;
private MqttConnectionOptions conOpt;
private volatile List<MqttMessage> receivedMessages;
private static String getPort() {
Object port = System.getProperty("mqtt.ssl.port");
assertNotNull(port);
return port.toString();
}
private static String getHost() {
Object host = System.getProperty("hostname");
assertNotNull(host);
return host.toString();
}
// override 10s limit
private class MyConnOpts extends MqttConnectionOptions {
private int keepAliveInterval = 60;
@Override
public void setKeepAliveInterval(int keepAliveInterval) {
this.keepAliveInterval = keepAliveInterval;
}
@Override
public int getKeepAliveInterval() {
return keepAliveInterval;
}
}
@BeforeEach
public void setUp() throws MqttException, IOException {
clientId = getClass().getSimpleName() + ((int) (10000 * Math.random()));
clientId2 = clientId + "-2";
client = new MqttClient(brokerUrl, clientId, null);
client2 = new MqttClient(brokerUrl, clientId2, null);
conOpt = new MyConnOpts();
conOpt.setSocketFactory(MutualAuth.getSSLContextWithoutCert().getSocketFactory());
setConOpts(conOpt);
receivedMessages = Collections.synchronizedList(new ArrayList<MqttMessage>());
}
@AfterEach
public void tearDown() throws MqttException {
// clean any sticky sessions
setConOpts(conOpt);
client = new MqttClient(brokerUrl, clientId, null);
try {
client.connect(conOpt);
client.disconnect();
} catch (Exception ignored) {
}
client2 = new MqttClient(brokerUrl, clientId2, null);
try {
client2.connect(conOpt);
client2.disconnect();
} catch (Exception ignored) {
}
}
private void setConOpts(MqttConnectionOptions conOpts) {
conOpts.setCleanStart(true);
conOpts.setKeepAliveInterval(60);
}
@Test
public void certLogin() throws MqttException {
try {
conOpt.setSocketFactory(MutualAuth.getSSLContextWithClientCert().getSocketFactory());
client.connect(conOpt);
} catch (Exception e) {
e.printStackTrace();
fail("Exception: " + e.getMessage());
}
}
@Test public void invalidUser() throws MqttException {
conOpt.setUserName("invalid-user");
try {
client.connect(conOpt);
fail("Authentication failure expected");
} catch (MqttException ex) {
assertEquals(RETURN_CODE_BAD_USERNAME_OR_PASSWORD, ex.getReasonCode());
} catch (Exception e) {
e.printStackTrace();
fail("Exception: " + e.getMessage());
}
}
@Test public void invalidPassword() throws MqttException {
conOpt.setUserName("invalid-user");
conOpt.setPassword("invalid-password".getBytes());
try {
client.connect(conOpt);
fail("Authentication failure expected");
} catch (MqttException ex) {
assertEquals(RETURN_CODE_BAD_USERNAME_OR_PASSWORD, ex.getReasonCode());
} catch (Exception e) {
e.printStackTrace();
fail("Exception: " + e.getMessage());
}
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
receivedMessages.add(message);
}
public void disconnected(MqttDisconnectResponse mqttDisconnectResponse) {
}
public void mqttErrorOccurred(MqttException e) {
}
public void deliveryComplete(IMqttToken iMqttToken) {
}
public void connectComplete(boolean b, String s) {
}
public void authPacketArrived(int i, MqttProperties mqttProperties) {
}
}