2015-03-18 09:52:33 +01:00
|
|
|
/**
|
|
|
|
*
|
2017-03-09 21:35:29 +01:00
|
|
|
* Copyright 2015-2017 Florian Schmaus
|
2015-03-18 09:52:33 +01:00
|
|
|
*
|
|
|
|
* 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 org.igniterealtime.smack.inttest;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
2017-03-09 21:35:29 +01:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2015-03-18 09:52:33 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2017-03-09 21:35:29 +01:00
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
|
2015-03-23 21:12:13 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2015-03-18 09:52:33 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
|
|
|
import eu.geekplace.javapinning.java7.Java7Pinning;
|
2015-03-18 09:52:33 +01:00
|
|
|
import org.jxmpp.jid.DomainBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
|
|
|
|
2015-04-06 10:45:12 +02:00
|
|
|
public final class Configuration {
|
2015-03-18 09:52:33 +01:00
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
public enum AccountRegistration {
|
|
|
|
disabled,
|
|
|
|
inBandRegistration,
|
|
|
|
serviceAdministration,
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
public final DomainBareJid service;
|
|
|
|
|
|
|
|
public final String serviceTlsPin;
|
|
|
|
|
2017-03-09 21:35:29 +01:00
|
|
|
public final SSLContext tlsContext;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
public final SecurityMode securityMode;
|
|
|
|
|
|
|
|
public final int replyTimeout;
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
public final AccountRegistration accountRegistration;
|
|
|
|
|
|
|
|
public final String adminAccountUsername;
|
|
|
|
|
|
|
|
public final String adminAccountPassword;
|
2015-03-18 09:52:33 +01:00
|
|
|
|
|
|
|
public final String accountOneUsername;
|
|
|
|
|
|
|
|
public final String accountOnePassword;
|
|
|
|
|
|
|
|
public final String accountTwoUsername;
|
|
|
|
|
|
|
|
public final String accountTwoPassword;
|
|
|
|
|
2016-07-20 20:57:04 +02:00
|
|
|
public final String accountThreeUsername;
|
|
|
|
|
|
|
|
public final String accountThreePassword;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
public final boolean debug;
|
|
|
|
|
|
|
|
public final Set<String> enabledTests;
|
|
|
|
|
|
|
|
public final Set<String> disabledTests;
|
|
|
|
|
|
|
|
public final Set<String> testPackages;
|
|
|
|
|
|
|
|
private Configuration(DomainBareJid service, String serviceTlsPin, SecurityMode securityMode, int replyTimeout,
|
|
|
|
boolean debug, String accountOneUsername, String accountOnePassword, String accountTwoUsername,
|
2016-07-20 20:57:04 +02:00
|
|
|
String accountTwoPassword, String accountThreeUsername, String accountThreePassword, Set<String> enabledTests, Set<String> disabledTests,
|
2017-03-09 21:35:29 +01:00
|
|
|
Set<String> testPackages, String adminAccountUsername, String adminAccountPassword)
|
|
|
|
throws KeyManagementException, NoSuchAlgorithmException {
|
2015-03-23 21:12:13 +01:00
|
|
|
this.service = Objects.requireNonNull(service,
|
|
|
|
"'service' must be set. Either via 'properties' files or via system property 'sinttest.service'.");
|
2015-03-18 09:52:33 +01:00
|
|
|
this.serviceTlsPin = serviceTlsPin;
|
2017-03-09 21:35:29 +01:00
|
|
|
if (serviceTlsPin != null) {
|
|
|
|
tlsContext = Java7Pinning.forPin(serviceTlsPin);
|
|
|
|
} else {
|
|
|
|
tlsContext = null;
|
|
|
|
}
|
2015-03-18 09:52:33 +01:00
|
|
|
this.securityMode = securityMode;
|
2016-12-19 14:35:09 +01:00
|
|
|
if (replyTimeout > 0) {
|
|
|
|
this.replyTimeout = replyTimeout;
|
|
|
|
} else {
|
|
|
|
this.replyTimeout = 60000;
|
|
|
|
}
|
2015-03-18 09:52:33 +01:00
|
|
|
this.debug = debug;
|
2016-11-27 21:14:44 +01:00
|
|
|
if (StringUtils.isNotEmpty(adminAccountUsername, adminAccountPassword)) {
|
|
|
|
accountRegistration = AccountRegistration.serviceAdministration;
|
|
|
|
}
|
|
|
|
else if (StringUtils.isNotEmpty(accountOneUsername, accountOnePassword, accountTwoUsername, accountTwoPassword,
|
|
|
|
accountThreeUsername, accountThreePassword)) {
|
|
|
|
accountRegistration = AccountRegistration.disabled;
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-11-27 21:14:44 +01:00
|
|
|
accountRegistration = AccountRegistration.inBandRegistration;
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
2016-11-27 21:14:44 +01:00
|
|
|
|
|
|
|
this.adminAccountUsername = adminAccountUsername;
|
|
|
|
this.adminAccountPassword = adminAccountPassword;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
this.accountOneUsername = accountOneUsername;
|
|
|
|
this.accountOnePassword = accountOnePassword;
|
|
|
|
this.accountTwoUsername = accountTwoUsername;
|
|
|
|
this.accountTwoPassword = accountTwoPassword;
|
2016-07-20 20:57:04 +02:00
|
|
|
this.accountThreeUsername = accountThreeUsername;
|
|
|
|
this.accountThreePassword = accountThreePassword;
|
2015-03-18 09:52:33 +01:00
|
|
|
this.enabledTests = enabledTests;
|
|
|
|
this.disabledTests = disabledTests;
|
|
|
|
this.testPackages = testPackages;
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
public boolean isAccountRegistrationPossible() {
|
|
|
|
return accountRegistration != AccountRegistration.disabled;
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
public static Builder builder() {
|
|
|
|
return new Builder();
|
|
|
|
}
|
|
|
|
|
2015-04-06 10:45:12 +02:00
|
|
|
public static final class Builder {
|
2015-03-18 09:52:33 +01:00
|
|
|
|
|
|
|
private DomainBareJid service;
|
|
|
|
|
|
|
|
private String serviceTlsPin;
|
|
|
|
|
|
|
|
private SecurityMode securityMode;
|
|
|
|
|
|
|
|
private int replyTimeout;
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
private String adminAccountUsername;
|
|
|
|
|
|
|
|
private String adminAccountPassword;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
private String accountOneUsername;
|
|
|
|
|
|
|
|
private String accountOnePassword;
|
|
|
|
|
|
|
|
private String accountTwoUsername;
|
|
|
|
|
|
|
|
private String accountTwoPassword;
|
|
|
|
|
2016-07-20 20:57:04 +02:00
|
|
|
public String accountThreeUsername;
|
|
|
|
|
|
|
|
public String accountThreePassword;
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
private boolean debug;
|
|
|
|
|
|
|
|
private Set<String> enabledTests;
|
|
|
|
|
|
|
|
private Set<String> disabledTests;
|
|
|
|
|
|
|
|
private Set<String> testPackages;
|
|
|
|
|
|
|
|
private Builder() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setService(String service) throws XmppStringprepException {
|
2016-06-08 23:02:45 +02:00
|
|
|
if (service == null) {
|
|
|
|
// Do nothing if user did not specify the XMPP service domain. When the builder
|
|
|
|
// builds a configuration using build() it will throw a meaningful exception.
|
|
|
|
return this;
|
|
|
|
}
|
2015-03-18 09:52:33 +01:00
|
|
|
return setService(JidCreate.domainBareFrom(service));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setService(DomainBareJid service) {
|
|
|
|
this.service = service;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder addEnabledTest(Class<? extends AbstractSmackIntTest> enabledTest) {
|
|
|
|
if (enabledTests == null) {
|
|
|
|
enabledTests = new HashSet<>();
|
|
|
|
}
|
|
|
|
enabledTests.add(enabledTest.getName());
|
|
|
|
// Also add the package of the test as test package
|
|
|
|
return addTestPackage(enabledTest.getPackage().getName());
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
private void ensureTestPackagesIsSet(int length) {
|
2015-03-18 09:52:33 +01:00
|
|
|
if (testPackages == null) {
|
2017-06-02 12:26:37 +02:00
|
|
|
testPackages = new HashSet<>(length);
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Builder addTestPackage(String testPackage) {
|
|
|
|
ensureTestPackagesIsSet(4);
|
2015-03-18 09:52:33 +01:00
|
|
|
testPackages.add(testPackage);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
public Builder setAdminAccountUsernameAndPassword(String adminAccountUsername, String adminAccountPassword) {
|
|
|
|
this.adminAccountUsername = StringUtils.requireNotNullOrEmpty(adminAccountUsername, "adminAccountUsername must not be null or empty");
|
|
|
|
this.adminAccountPassword = StringUtils.requireNotNullOrEmpty(adminAccountPassword, "adminAccountPassword must no be null or empty");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
public Builder setUsernamesAndPassword(String accountOneUsername, String accountOnePassword,
|
2016-07-20 20:57:04 +02:00
|
|
|
String accountTwoUsername, String accountTwoPassword, String accountThreeUsername, String accountThreePassword) {
|
|
|
|
this.accountOneUsername = StringUtils.requireNotNullOrEmpty(accountOneUsername, "accountOneUsername must not be null or empty");
|
|
|
|
this.accountOnePassword = StringUtils.requireNotNullOrEmpty(accountOnePassword, "accountOnePassword must not be null or empty");
|
|
|
|
this.accountTwoUsername = StringUtils.requireNotNullOrEmpty(accountTwoUsername, "accountTwoUsername must not be null or empty");
|
|
|
|
this.accountTwoPassword = StringUtils.requireNotNullOrEmpty(accountTwoPassword, "accountTwoPasswordmust not be null or empty");
|
|
|
|
this.accountThreeUsername = StringUtils.requireNotNullOrEmpty(accountThreeUsername, "accountThreeUsername must not be null or empty");
|
|
|
|
this.accountThreePassword = StringUtils.requireNotNullOrEmpty(accountThreePassword, "accountThreePassword must not be null or empty");
|
2015-03-18 09:52:33 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setServiceTlsPin(String tlsPin) {
|
|
|
|
this.serviceTlsPin = tlsPin;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setSecurityMode(String securityModeString) {
|
|
|
|
if (securityModeString != null) {
|
|
|
|
securityMode = SecurityMode.valueOf(securityModeString);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
securityMode = SecurityMode.required;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setReplyTimeout(String timeout) {
|
|
|
|
if (timeout != null) {
|
|
|
|
replyTimeout = Integer.valueOf(timeout);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setDebug(String debugString) {
|
|
|
|
if (debugString != null) {
|
|
|
|
debug = Boolean.valueOf(debugString);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setEnabledTests(String enabledTestsString) {
|
|
|
|
enabledTests = getTestSetFrom(enabledTestsString);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Builder setDisabledTests(String disabledTestsString) {
|
|
|
|
disabledTests = getTestSetFrom(disabledTestsString);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
public Builder addTestPackages(String testPackagesString) {
|
2015-03-18 09:52:33 +01:00
|
|
|
if (testPackagesString != null) {
|
|
|
|
String[] testPackagesArray = testPackagesString.split(",");
|
2017-06-02 12:26:37 +02:00
|
|
|
|
|
|
|
ensureTestPackagesIsSet(testPackagesArray.length);
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
for (String s : testPackagesArray) {
|
|
|
|
testPackages.add(s.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
public Builder addTestPackages(String[] testPackagesString) {
|
|
|
|
if (testPackagesString == null) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureTestPackagesIsSet(testPackagesString.length);
|
|
|
|
|
|
|
|
for (String testPackage : testPackagesString) {
|
|
|
|
testPackages.add(testPackage);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-09 21:35:29 +01:00
|
|
|
public Configuration build() throws KeyManagementException, NoSuchAlgorithmException {
|
2015-03-18 09:52:33 +01:00
|
|
|
return new Configuration(service, serviceTlsPin, securityMode, replyTimeout, debug, accountOneUsername,
|
2016-07-20 20:57:04 +02:00
|
|
|
accountOnePassword, accountTwoUsername, accountTwoPassword, accountThreeUsername, accountThreePassword, enabledTests, disabledTests,
|
2016-11-27 21:14:44 +01:00
|
|
|
testPackages, adminAccountUsername, adminAccountPassword);
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final String SINTTEST = "sinttest.";
|
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
public static Configuration newConfiguration(String[] testPackages)
|
2017-03-09 21:35:29 +01:00
|
|
|
throws IOException, KeyManagementException, NoSuchAlgorithmException {
|
2015-03-18 09:52:33 +01:00
|
|
|
Properties properties = new Properties();
|
|
|
|
|
2015-03-23 21:12:13 +01:00
|
|
|
File propertiesFile = findPropertiesFile();
|
|
|
|
if (propertiesFile != null) {
|
|
|
|
try (FileInputStream in = new FileInputStream(propertiesFile)) {
|
|
|
|
properties.load(in);
|
|
|
|
}
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Properties set via the system override the file properties
|
|
|
|
Properties systemProperties = System.getProperties();
|
|
|
|
for (Entry<Object, Object> entry : systemProperties.entrySet()) {
|
|
|
|
String key = (String) entry.getKey();
|
|
|
|
if (!key.startsWith(SINTTEST)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
key = key.substring(SINTTEST.length());
|
|
|
|
String value = (String) entry.getValue();
|
|
|
|
properties.put(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder builder = builder();
|
|
|
|
builder.setService(properties.getProperty("service"));
|
|
|
|
builder.setServiceTlsPin(properties.getProperty("serviceTlsPin"));
|
|
|
|
builder.setSecurityMode(properties.getProperty("securityMode"));
|
|
|
|
builder.setReplyTimeout(properties.getProperty("replyTimeout", "60000"));
|
|
|
|
|
2016-11-27 21:14:44 +01:00
|
|
|
String adminAccountUsername = properties.getProperty("adminAccountUsername");
|
|
|
|
String adminAccountPassword = properties.getProperty("adminAccountPassword");
|
|
|
|
if (StringUtils.isNotEmpty(adminAccountUsername, adminAccountPassword)) {
|
|
|
|
builder.setAdminAccountUsernameAndPassword(adminAccountUsername, adminAccountPassword);
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:52:33 +01:00
|
|
|
String accountOneUsername = properties.getProperty("accountOneUsername");
|
|
|
|
String accountOnePassword = properties.getProperty("accountOnePassword");
|
|
|
|
String accountTwoUsername = properties.getProperty("accountTwoUsername");
|
|
|
|
String accountTwoPassword = properties.getProperty("accountTwoPassword");
|
2016-07-20 20:57:04 +02:00
|
|
|
String accountThreeUsername = properties.getProperty("accountThreeUsername");
|
|
|
|
String accountThreePassword = properties.getProperty("accountThreePassword");
|
2016-07-31 12:09:06 +02:00
|
|
|
if (accountOneUsername != null || accountOnePassword != null || accountTwoUsername != null
|
|
|
|
|| accountTwoPassword != null || accountThreeUsername != null || accountThreePassword != null) {
|
|
|
|
builder.setUsernamesAndPassword(accountOneUsername, accountOnePassword, accountTwoUsername,
|
|
|
|
accountTwoPassword, accountThreeUsername, accountThreePassword);
|
|
|
|
}
|
2015-03-18 09:52:33 +01:00
|
|
|
|
|
|
|
builder.setDebug(properties.getProperty("debug"));
|
|
|
|
builder.setEnabledTests(properties.getProperty("enabledTests"));
|
|
|
|
builder.setDisabledTests(properties.getProperty("disabledTests"));
|
2017-06-02 12:26:37 +02:00
|
|
|
|
|
|
|
builder.addTestPackages(properties.getProperty("testPackages"));
|
|
|
|
builder.addTestPackages(testPackages);
|
2015-03-18 09:52:33 +01:00
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2016-07-20 20:57:04 +02:00
|
|
|
private static File findPropertiesFile() {
|
2015-03-18 09:52:33 +01:00
|
|
|
List<String> possibleLocations = new LinkedList<>();
|
|
|
|
possibleLocations.add("properties");
|
|
|
|
String userHome = System.getProperty("user.home");
|
|
|
|
if (userHome != null) {
|
|
|
|
possibleLocations.add(userHome + "/.config/smack-integration-test/properties");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String possibleLocation : possibleLocations) {
|
|
|
|
File res = new File(possibleLocation);
|
|
|
|
if (res.isFile())
|
|
|
|
return res;
|
|
|
|
}
|
2015-03-23 21:12:13 +01:00
|
|
|
return null;
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Set<String> getTestSetFrom(String string) {
|
|
|
|
if (string == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String[] stringArray = string.split(",");
|
|
|
|
Set<String> res = new HashSet<>(stringArray.length);
|
|
|
|
for (String s : stringArray) {
|
|
|
|
res.add(getFullTestStringFrom(s));
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getFullTestStringFrom(String string) {
|
|
|
|
string = string.trim();
|
|
|
|
if (string.startsWith("smackx.") || string.startsWith("smack.")) {
|
|
|
|
string = "org.jivesoftware." + string;
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|