mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-09-09 10:19:41 +02:00
Add support for XEP-0133: Service Administration
also extend Smack's integration test framework to use XEP-0133 as a means for of throw away account creation. Fixes SMACK-742
This commit is contained in:
parent
1f1bc236fd
commit
274e5630c4
12 changed files with 414 additions and 56 deletions
|
@ -60,7 +60,7 @@ public abstract class AbstractSmackLowLevelIntegrationTest extends AbstractSmack
|
|||
try {
|
||||
callback.connectionCallback(connection);
|
||||
} finally {
|
||||
IntTestUtil.disconnectAndMaybeDelete(connection, true);
|
||||
IntTestUtil.disconnectAndMaybeDelete(connection, configuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
* Copyright 2015-2016 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -35,6 +35,12 @@ import org.jxmpp.stringprep.XmppStringprepException;
|
|||
|
||||
public final class Configuration {
|
||||
|
||||
public enum AccountRegistration {
|
||||
disabled,
|
||||
inBandRegistration,
|
||||
serviceAdministration,
|
||||
}
|
||||
|
||||
public final DomainBareJid service;
|
||||
|
||||
public final String serviceTlsPin;
|
||||
|
@ -43,7 +49,11 @@ public final class Configuration {
|
|||
|
||||
public final int replyTimeout;
|
||||
|
||||
public final boolean registerAccounts;
|
||||
public final AccountRegistration accountRegistration;
|
||||
|
||||
public final String adminAccountUsername;
|
||||
|
||||
public final String adminAccountPassword;
|
||||
|
||||
public final String accountOneUsername;
|
||||
|
||||
|
@ -68,21 +78,27 @@ public final class Configuration {
|
|||
private Configuration(DomainBareJid service, String serviceTlsPin, SecurityMode securityMode, int replyTimeout,
|
||||
boolean debug, String accountOneUsername, String accountOnePassword, String accountTwoUsername,
|
||||
String accountTwoPassword, String accountThreeUsername, String accountThreePassword, Set<String> enabledTests, Set<String> disabledTests,
|
||||
Set<String> testPackages) {
|
||||
Set<String> testPackages, String adminAccountUsername, String adminAccountPassword) {
|
||||
this.service = Objects.requireNonNull(service,
|
||||
"'service' must be set. Either via 'properties' files or via system property 'sinttest.service'.");
|
||||
this.serviceTlsPin = serviceTlsPin;
|
||||
this.securityMode = securityMode;
|
||||
this.replyTimeout = replyTimeout;
|
||||
this.debug = debug;
|
||||
if (StringUtils.isNullOrEmpty(accountOneUsername) || StringUtils.isNullOrEmpty(accountOnePassword)
|
||||
|| StringUtils.isNullOrEmpty(accountTwoUsername)
|
||||
|| StringUtils.isNullOrEmpty(accountTwoPassword)) {
|
||||
registerAccounts = true;
|
||||
if (StringUtils.isNotEmpty(adminAccountUsername, adminAccountPassword)) {
|
||||
accountRegistration = AccountRegistration.serviceAdministration;
|
||||
}
|
||||
else if (StringUtils.isNotEmpty(accountOneUsername, accountOnePassword, accountTwoUsername, accountTwoPassword,
|
||||
accountThreeUsername, accountThreePassword)) {
|
||||
accountRegistration = AccountRegistration.disabled;
|
||||
}
|
||||
else {
|
||||
registerAccounts = false;
|
||||
accountRegistration = AccountRegistration.inBandRegistration;
|
||||
}
|
||||
|
||||
this.adminAccountUsername = adminAccountUsername;
|
||||
this.adminAccountPassword = adminAccountPassword;
|
||||
|
||||
this.accountOneUsername = accountOneUsername;
|
||||
this.accountOnePassword = accountOnePassword;
|
||||
this.accountTwoUsername = accountTwoUsername;
|
||||
|
@ -94,6 +110,10 @@ public final class Configuration {
|
|||
this.testPackages = testPackages;
|
||||
}
|
||||
|
||||
public boolean isAccountRegistrationPossible() {
|
||||
return accountRegistration != AccountRegistration.disabled;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
@ -108,6 +128,10 @@ public final class Configuration {
|
|||
|
||||
private int replyTimeout;
|
||||
|
||||
private String adminAccountUsername;
|
||||
|
||||
private String adminAccountPassword;
|
||||
|
||||
private String accountOneUsername;
|
||||
|
||||
private String accountOnePassword;
|
||||
|
@ -162,6 +186,12 @@ public final class Configuration {
|
|||
return this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Builder setUsernamesAndPassword(String accountOneUsername, String accountOnePassword,
|
||||
String accountTwoUsername, String accountTwoPassword, String accountThreeUsername, String accountThreePassword) {
|
||||
this.accountOneUsername = StringUtils.requireNotNullOrEmpty(accountOneUsername, "accountOneUsername must not be null or empty");
|
||||
|
@ -226,7 +256,7 @@ public final class Configuration {
|
|||
public Configuration build() {
|
||||
return new Configuration(service, serviceTlsPin, securityMode, replyTimeout, debug, accountOneUsername,
|
||||
accountOnePassword, accountTwoUsername, accountTwoPassword, accountThreeUsername, accountThreePassword, enabledTests, disabledTests,
|
||||
testPackages);
|
||||
testPackages, adminAccountUsername, adminAccountPassword);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -260,6 +290,12 @@ public final class Configuration {
|
|||
builder.setSecurityMode(properties.getProperty("securityMode"));
|
||||
builder.setReplyTimeout(properties.getProperty("replyTimeout", "60000"));
|
||||
|
||||
String adminAccountUsername = properties.getProperty("adminAccountUsername");
|
||||
String adminAccountPassword = properties.getProperty("adminAccountPassword");
|
||||
if (StringUtils.isNotEmpty(adminAccountUsername, adminAccountPassword)) {
|
||||
builder.setAdminAccountUsernameAndPassword(adminAccountUsername, adminAccountPassword);
|
||||
}
|
||||
|
||||
String accountOneUsername = properties.getProperty("accountOneUsername");
|
||||
String accountOnePassword = properties.getProperty("accountOnePassword");
|
||||
String accountTwoUsername = properties.getProperty("accountTwoUsername");
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015 Florian Schmaus
|
||||
* Copyright 2015-2016 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -25,13 +25,17 @@ import java.util.logging.Logger;
|
|||
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.igniterealtime.smack.inttest.Configuration.AccountRegistration;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smackx.admin.ServiceAdministrationManager;
|
||||
import org.jivesoftware.smackx.iqregister.AccountManager;
|
||||
import org.jxmpp.jid.EntityBareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.jid.parts.Localpart;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
|
||||
|
@ -39,14 +43,52 @@ public class IntTestUtil {
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(IntTestUtil.class.getName());
|
||||
|
||||
public static UsernameAndPassword registerAccount(XMPPConnection connection)
|
||||
|
||||
public static UsernameAndPassword registerAccount(XMPPTCPConnection connection, Configuration config) throws InterruptedException, XMPPException, SmackException, IOException {
|
||||
return registerAccount(connection, StringUtils.insecureRandomString(12), StringUtils.insecureRandomString(12), config);
|
||||
}
|
||||
|
||||
public static UsernameAndPassword registerAccount(XMPPTCPConnection connection, String accountUsername, String accountPassword,
|
||||
Configuration config) throws InterruptedException, XMPPException, SmackException, IOException {
|
||||
switch (config.accountRegistration) {
|
||||
case inBandRegistration:
|
||||
return registerAccountViaIbr(connection, accountUsername, accountPassword);
|
||||
case serviceAdministration:
|
||||
return registerAccountViaAdmin(connection, accountUsername, accountPassword, config.adminAccountUsername, config.adminAccountPassword);
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
// public static UsernameAndPassword registerAccountViaAdmin(XMPPTCPConnection connection) throws XmppStringprepException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
||||
// return registerAccountViaAdmin(connection, StringUtils.insecureRandomString(12),
|
||||
// StringUtils.insecureRandomString(12));
|
||||
// }
|
||||
|
||||
public static UsernameAndPassword registerAccountViaAdmin(XMPPTCPConnection connection, String username,
|
||||
String password, String adminAccountUsername, String adminAccountPassword) throws InterruptedException, XMPPException, SmackException, IOException {
|
||||
connection.login(adminAccountUsername, adminAccountPassword);
|
||||
|
||||
ServiceAdministrationManager adminManager = ServiceAdministrationManager.getInstanceFor(connection);
|
||||
|
||||
EntityBareJid userJid = JidCreate.entityBareFrom(Localpart.from(username), connection.getServiceName());
|
||||
adminManager.addUser(userJid, password);
|
||||
|
||||
connection.disconnect();
|
||||
connection.connect();
|
||||
|
||||
return new UsernameAndPassword(username, password);
|
||||
|
||||
}
|
||||
|
||||
public static UsernameAndPassword registerAccountViaIbr(XMPPConnection connection)
|
||||
throws NoResponseException, XMPPErrorException, NotConnectedException,
|
||||
InterruptedException {
|
||||
return registerAccount(connection, StringUtils.insecureRandomString(12),
|
||||
return registerAccountViaIbr(connection, StringUtils.insecureRandomString(12),
|
||||
StringUtils.insecureRandomString(12));
|
||||
}
|
||||
|
||||
public static UsernameAndPassword registerAccount(XMPPConnection connection, String username,
|
||||
public static UsernameAndPassword registerAccountViaIbr(XMPPConnection connection, String username,
|
||||
String password) throws NoResponseException, XMPPErrorException,
|
||||
NotConnectedException, InterruptedException {
|
||||
AccountManager accountManager = AccountManager.getInstance(connection);
|
||||
|
@ -82,7 +124,73 @@ public class IntTestUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static void disconnectAndMaybeDelete(XMPPTCPConnection connection, boolean delete)
|
||||
|
||||
public static void disconnectAndMaybeDelete(XMPPTCPConnection connection, Configuration config) throws InterruptedException {
|
||||
try {
|
||||
if (!config.isAccountRegistrationPossible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Configuration.AccountRegistration accountDeletionMethod = config.accountRegistration;
|
||||
|
||||
AccountManager accountManager = AccountManager.getInstance(connection);
|
||||
try {
|
||||
if (accountManager.isSupported()) {
|
||||
accountDeletionMethod = AccountRegistration.inBandRegistration;
|
||||
}
|
||||
}
|
||||
catch (NoResponseException | XMPPErrorException | NotConnectedException e) {
|
||||
LOGGER.log(Level.WARNING, "Could not test if XEP-0077 account deletion is possible", e);
|
||||
}
|
||||
|
||||
switch (accountDeletionMethod) {
|
||||
case inBandRegistration:
|
||||
deleteViaIbr(connection);
|
||||
break;
|
||||
case serviceAdministration:
|
||||
deleteViaServiceAdministration(connection, config);
|
||||
break;
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteViaServiceAdministration(XMPPTCPConnection connection, Configuration config) {
|
||||
EntityBareJid accountToDelete = connection.getUser().asEntityBareJid();
|
||||
|
||||
final int maxAttempts = 3;
|
||||
|
||||
int attempts;
|
||||
for (attempts = 0; attempts < maxAttempts; attempts++) {
|
||||
connection.disconnect();
|
||||
|
||||
try {
|
||||
connection.connect().login(config.adminAccountUsername, config.adminAccountPassword);
|
||||
}
|
||||
catch (XMPPException | SmackException | IOException | InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
ServiceAdministrationManager adminManager = ServiceAdministrationManager.getInstanceFor(connection);
|
||||
try {
|
||||
adminManager.deleteUser(accountToDelete);
|
||||
}
|
||||
catch (NoResponseException | XMPPErrorException | NotConnectedException | InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (attempts > maxAttempts) {
|
||||
LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection);
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteViaIbr(XMPPTCPConnection connection)
|
||||
throws InterruptedException {
|
||||
// If the connection is disconnected, then re-reconnect and login. This could happen when
|
||||
// (low-level) integration tests disconnect the connection, e.g. to test disconnection
|
||||
|
@ -95,39 +203,35 @@ public class IntTestUtil {
|
|||
LOGGER.log(Level.WARNING, "Exception reconnection account for deletion", e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (delete) {
|
||||
final int maxAttempts = 3;
|
||||
AccountManager am = AccountManager.getInstance(connection);
|
||||
int attempts;
|
||||
for (attempts = 0; attempts < maxAttempts; attempts++) {
|
||||
try {
|
||||
am.deleteAccount();
|
||||
}
|
||||
catch (XMPPErrorException | NoResponseException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
continue;
|
||||
}
|
||||
catch (NotConnectedException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
try {
|
||||
connection.connect().login();
|
||||
}
|
||||
catch (XMPPException | SmackException | IOException e2) {
|
||||
LOGGER.log(Level.WARNING, "Exception while trying to re-connect " + connection, e);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
LOGGER.info("Successfully deleted account of " + connection);
|
||||
break;
|
||||
}
|
||||
if (attempts > maxAttempts) {
|
||||
LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection);
|
||||
}
|
||||
|
||||
final int maxAttempts = 3;
|
||||
AccountManager am = AccountManager.getInstance(connection);
|
||||
int attempts;
|
||||
for (attempts = 0; attempts < maxAttempts; attempts++) {
|
||||
try {
|
||||
am.deleteAccount();
|
||||
}
|
||||
catch (XMPPErrorException | NoResponseException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
continue;
|
||||
}
|
||||
catch (NotConnectedException e) {
|
||||
LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e);
|
||||
try {
|
||||
connection.connect().login();
|
||||
}
|
||||
catch (XMPPException | SmackException | IOException e2) {
|
||||
LOGGER.log(Level.WARNING, "Exception while trying to re-connect " + connection, e);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
LOGGER.info("Successfully deleted account of " + connection);
|
||||
break;
|
||||
}
|
||||
finally {
|
||||
connection.disconnect();
|
||||
if (attempts > maxAttempts) {
|
||||
LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -436,7 +436,7 @@ public class SmackIntegrationTestFramework {
|
|||
final int numberOfConnections = testMethod.getParameterTypes().length;
|
||||
XMPPTCPConnection[] connections = null;
|
||||
try {
|
||||
if (numberOfConnections > 0 && !config.registerAccounts) {
|
||||
if (numberOfConnections > 0 && !config.isAccountRegistrationPossible()) {
|
||||
throw new TestNotPossibleException(
|
||||
"Must create accounts for this test, but it's not enabled");
|
||||
}
|
||||
|
@ -457,13 +457,13 @@ public class SmackIntegrationTestFramework {
|
|||
}
|
||||
finally {
|
||||
for (int i = 0; i < numberOfConnections; ++i) {
|
||||
IntTestUtil.disconnectAndMaybeDelete(connections[i], true);
|
||||
IntTestUtil.disconnectAndMaybeDelete(connections[i], config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) throws InterruptedException {
|
||||
IntTestUtil.disconnectAndMaybeDelete(connection, config.registerAccounts);
|
||||
IntTestUtil.disconnectAndMaybeDelete(connection, config);
|
||||
}
|
||||
|
||||
protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException,
|
||||
|
@ -546,8 +546,8 @@ public class SmackIntegrationTestFramework {
|
|||
}
|
||||
XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
|
||||
connection.connect();
|
||||
if (config.registerAccounts) {
|
||||
IntTestUtil.registerAccount(connection, accountUsername, accountPassword);
|
||||
if (config.isAccountRegistrationPossible()) {
|
||||
IntTestUtil.registerAccount(connection, accountUsername, accountPassword, config);
|
||||
|
||||
// TODO is this still required?
|
||||
// Some servers, e.g. Openfire, do not support a login right after the account was
|
||||
|
@ -573,7 +573,7 @@ public class SmackIntegrationTestFramework {
|
|||
builder.setXmppDomain(config.service);
|
||||
XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
|
||||
connection.connect();
|
||||
UsernameAndPassword uap = IntTestUtil.registerAccount(connection);
|
||||
UsernameAndPassword uap = IntTestUtil.registerAccount(connection, config);
|
||||
connection.login(uap.username, uap.password);
|
||||
return connection;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue