Skip to content

Commit

Permalink
Fixed test command exit codes and stderr.
Browse files Browse the repository at this point in the history
Added basic test command testsuite.
  • Loading branch information
LukasBrand committed Dec 23, 2022
1 parent 54394bb commit cf3daa9
Show file tree
Hide file tree
Showing 5 changed files with 572 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/main/java/com/hivemq/cli/commands/cli/TestBrokerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,27 @@ public TestBrokerCommand(final @NotNull DefaultCLIProperties defaultCLIPropertie
return 1;
}

int mqtt3ExitCode = 0;
int mqtt5ExitCode = 0;
if (version != null) {
if (version == MqttVersion.MQTT_3_1_1) {
testMqtt3Features();
mqtt3ExitCode = testMqtt3Features();
} else if (version == MqttVersion.MQTT_5_0) {
testMqtt5Features();
mqtt5ExitCode = testMqtt5Features();
}
} else {
testMqtt3Features();
testMqtt5Features();
mqtt3ExitCode = testMqtt3Features();
mqtt5ExitCode = testMqtt5Features();
}

return 0;
if (mqtt3ExitCode != 0 || mqtt5ExitCode != 0) {
return 1;
} else {
return 0;
}
}

public void testMqtt5Features() {
public int testMqtt5Features() {
final Mqtt5FeatureTester mqtt5Tester = new Mqtt5FeatureTester(Objects.requireNonNull(host),
Objects.requireNonNull(port),
authenticationOptions.getUser(),
Expand All @@ -168,16 +174,16 @@ public void testMqtt5Features() {
connAck = mqtt5Tester.testConnect();
} catch (final Exception e) {
Logger.error(e, "Could not connect MQTT 5 client");
System.out.println("Could not connect MQTT 5 client - " + Throwables.getRootCause(e).getMessage());
return;
System.err.println("Could not connect MQTT 5 client - " + Throwables.getRootCause(e).getMessage());
return 1;
}

if (connAck == null) {
System.out.println("NO");
return;
return 1;
} else if (connAck.getReasonCode() != Mqtt5ConnAckReasonCode.SUCCESS) {
System.out.println(connAck.getReasonCode());
return;
return 1;
} else {
System.out.println("OK");
}
Expand Down Expand Up @@ -299,9 +305,10 @@ public void testMqtt5Features() {
}

Logger.info("Finished testing MQTT 5");
return 0;
}

public void testMqtt3Features() {
public int testMqtt3Features() {
final Mqtt3FeatureTester mqtt3Tester = new Mqtt3FeatureTester(Objects.requireNonNull(host),
Objects.requireNonNull(port),
authenticationOptions.getUser(),
Expand All @@ -319,15 +326,15 @@ public void testMqtt3Features() {
connAck = mqtt3Tester.testConnect();
} catch (final Exception e) {
Logger.error(e, "Could not connect MQTT 3 client");
System.out.println("Could not connect MQTT 3 client - " + Throwables.getRootCause(e).getMessage());
return;
System.err.println("Could not connect MQTT 3 client - " + Throwables.getRootCause(e).getMessage());
return 1;
}
if (connAck == null) {
System.out.println("NO");
return;
return 1;
} else if (connAck.getReturnCode() != Mqtt3ConnAckReturnCode.SUCCESS) {
System.out.println(connAck.getReturnCode());
return;
return 1;
} else {
System.out.println("OK");
}
Expand Down Expand Up @@ -407,6 +414,7 @@ public void testMqtt3Features() {
}

Logger.info("Finished testing MQTT 3");
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
* Copyright 2019-present HiveMQ and the HiveMQ Community
*
* Licensed 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 com.hivemq.cli.commands.cli.test_broker;

import com.hivemq.cli.utils.MqttVersionConverter;
import com.hivemq.cli.utils.broker.HiveMQ;
import com.hivemq.cli.utils.cli.MqttCli;
import com.hivemq.cli.utils.cli.results.ExecutionResult;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.hivemq.cli.utils.broker.assertions.TestConnectAssertion.assertTestConnectPacket;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TestBrokerConnectST {

@RegisterExtension
private static final @NotNull HiveMQ HIVEMQ = HiveMQ.builder().build();

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectWrongHost(final char mqttVersion) throws Exception {
final List<String> testCommand = List.of("test",
"-h",
"wrong-host",
"-p",
String.valueOf(HIVEMQ.getMqttPort()),
"-V",
String.valueOf(mqttVersion));

final ExecutionResult executionResult = MqttCli.execute(testCommand);
assertEquals(1, executionResult.getExitCode());
assertTrue(executionResult.getErrorOutput().contains("Could not connect"));
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectWrongPort(final char mqttVersion) throws Exception {
final List<String> testCommand =
List.of("test", "-h", HIVEMQ.getHost(), "-p", "22", "-V", String.valueOf(mqttVersion));

final ExecutionResult executionResult = MqttCli.execute(testCommand);
assertEquals(1, executionResult.getExitCode());
assertTrue(executionResult.getErrorOutput().contains("Could not connect"));
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectInvalidTimeOut(final char mqttVersion) throws Exception {
final List<String> testCommand = List.of("test",
"-h",
HIVEMQ.getHost(),
"-p",
String.valueOf(HIVEMQ.getMqttPort()),
"-V",
String.valueOf(mqttVersion),
"-t",
"random");

final ExecutionResult executionResult = MqttCli.execute(testCommand);
assertEquals(2, executionResult.getExitCode());
assertTrue(executionResult.getErrorOutput()
.contains("Invalid value for option '--timeOut': 'random' is not an int"));
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectUserName(final char mqttVersion) throws Exception {
final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-u");
testCommand.add("username");

final ExecutionResult executionResult = MqttCli.execute(testCommand);
assertTestOutput(executionResult, mqttVersion);

assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setUserName("username");
});
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectPassword(final char mqttVersion) throws Exception {
final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-pw");
testCommand.add("password");

final ExecutionResult executionResult = MqttCli.execute(testCommand);

if (mqttVersion == '3') {
assertTrue(executionResult.getErrorOutput()
.contains("Password-Only Authentication is not allowed in MQTT 3"));
assertEquals(1, executionResult.getExitCode());
} else {
assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectPasswordEnv(final char mqttVersion) throws Exception {
final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-pw:env");
testCommand.add("PASSWORD");

final ExecutionResult executionResult = MqttCli.execute(testCommand, Map.of("PASSWORD", "password"));

if (mqttVersion == '3') {
assertTrue(executionResult.getErrorOutput()
.contains("Password-Only Authentication is not allowed in MQTT 3"));
assertEquals(1, executionResult.getExitCode());
} else {
assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectPasswordFile(final char mqttVersion) throws Exception {
final Path passwordFile = Files.createTempFile("password-file", ".txt");
passwordFile.toFile().deleteOnExit();
Files.writeString(passwordFile, "password");

final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-pw:file");
testCommand.add(passwordFile.toString());

final ExecutionResult executionResult = MqttCli.execute(testCommand);

if (mqttVersion == '3') {
assertTrue(executionResult.getErrorOutput()
.contains("Password-Only Authentication is not allowed in MQTT 3"));
assertEquals(1, executionResult.getExitCode());
} else {
assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectUserNameAndPassword(final char mqttVersion) throws Exception {
final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-u");
testCommand.add("username");
testCommand.add("-pw");
testCommand.add("password");

final ExecutionResult executionResult = MqttCli.execute(testCommand);
assertTestOutput(executionResult, mqttVersion);

assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setUserName("username");
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectUserNameAndPasswordEnv(final char mqttVersion) throws Exception {
final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-u");
testCommand.add("username");
testCommand.add("-pw:env");
testCommand.add("PASSWORD");

final ExecutionResult executionResult = MqttCli.execute(testCommand, Map.of("PASSWORD", "password"));

assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setUserName("username");
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}

@ParameterizedTest
@Timeout(value = 3, unit = TimeUnit.MINUTES)
@ValueSource(chars = {'3', '5'})
void test_connectUserNamePasswordFile(final char mqttVersion) throws Exception {
final Path passwordFile = Files.createTempFile("password-file", ".txt");
passwordFile.toFile().deleteOnExit();
Files.writeString(passwordFile, "password");

final List<String> testCommand = defaultTestCommand(mqttVersion);
testCommand.add("-u");
testCommand.add("username");
testCommand.add("-pw:file");
testCommand.add(passwordFile.toString());

final ExecutionResult executionResult = MqttCli.execute(testCommand);

assertTestOutput(executionResult, mqttVersion);
assertTestConnectPacket(HIVEMQ.getConnectPackets().get(0), connectAssertion -> {
connectAssertion.setMqttVersion(MqttVersionConverter.toExtensionSdkVersion(mqttVersion));
connectAssertion.setUserName("username");
connectAssertion.setPassword(ByteBuffer.wrap("password".getBytes(StandardCharsets.UTF_8)));
});
}

private void assertTestOutput(final @NotNull ExecutionResult executionResult, final char mqttVersion) {
assertEquals(0, executionResult.getExitCode());
assertTrue(executionResult.getStandardOutput().contains("MQTT " + mqttVersion + ": OK"));
}

private @NotNull List<String> defaultTestCommand(final char mqttVersion) {
final ArrayList<String> testCommand = new ArrayList<>();
testCommand.add("test");
testCommand.add("-h");
testCommand.add(HIVEMQ.getHost());
testCommand.add("-p");
testCommand.add(String.valueOf(HIVEMQ.getMqttPort()));
testCommand.add("-V");
testCommand.add(String.valueOf(mqttVersion));
return testCommand;
}
}
Loading

0 comments on commit cf3daa9

Please sign in to comment.