1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-27 00:32:07 +01:00

SMACK-358: Support account creation parameters for LoginTest.testLoginWithNoResource()

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13479 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Florian Schmaus 2013-02-14 11:43:41 +00:00 committed by flow
parent b2a0a2154b
commit 091015aa4b
3 changed files with 51 additions and 8 deletions

View file

@ -13,4 +13,12 @@
<chat>chat</chat> <chat>chat</chat>
<muc>conference</muc> <muc>conference</muc>
<!-- LoginTest parameters -->
<testAnonymousLogin>false</testAnonymousLogin>
<!-- Account creation parameters -->
<!--
<accountCreationParameters email="test@xcp.localhost" name="test"/>
-->
</testcase> </testcase>

View file

@ -67,6 +67,8 @@ public class LoginTest extends SmackTestCase {
* Check that the server handles anonymous users correctly. * Check that the server handles anonymous users correctly.
*/ */
public void testSASLAnonymousLogin() { public void testSASLAnonymousLogin() {
if (!isTestAnonymousLogin()) return;
try { try {
XMPPConnection conn1 = createConnection(); XMPPConnection conn1 = createConnection();
XMPPConnection conn2 = createConnection(); XMPPConnection conn2 = createConnection();
@ -84,8 +86,7 @@ public class LoginTest extends SmackTestCase {
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser())); assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
} }
catch (XMPPException e) { catch (XMPPException e) {
e.printStackTrace(); fail(e.getMessage());
//fail(e.getMessage());
} }
finally { finally {
// Close the connection // Close the connection
@ -103,6 +104,8 @@ public class LoginTest extends SmackTestCase {
* Check that the server handles anonymous users correctly. * Check that the server handles anonymous users correctly.
*/ */
public void testNonSASLAnonymousLogin() { public void testNonSASLAnonymousLogin() {
if (!isTestAnonymousLogin()) return;
try { try {
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort()); ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setSASLAuthenticationEnabled(false); config.setSASLAuthenticationEnabled(false);
@ -126,7 +129,6 @@ public class LoginTest extends SmackTestCase {
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser())); assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
} }
catch (XMPPException e) { catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
} }
// Close the connection // Close the connection
@ -147,12 +149,16 @@ public class LoginTest extends SmackTestCase {
XMPPConnection conn = createConnection(); XMPPConnection conn = createConnection();
conn.connect(); conn.connect();
try { try {
conn.getAccountManager().createAccount("user_1", "user_1"); conn.getAccountManager().createAccount("user_1", "user_1", getAccountCreationParameters());
} catch (XMPPException e) { } catch (XMPPException e) {
// Do nothing if the accout already exists // Do nothing if the account already exists
if (e.getXMPPError().getCode() != 409) { if (e.getXMPPError().getCode() != 409) {
throw e; throw e;
} }
// Else recreate the connection, ins case the server closed it as
// a result of the error, so we can login.
conn = createConnection();
conn.connect();
} }
conn.login("user_1", "user_1", (String) null); conn.login("user_1", "user_1", (String) null);
if (conn.getSASLAuthentication().isAuthenticated()) { if (conn.getSASLAuthentication().isAuthenticated()) {
@ -163,11 +169,15 @@ public class LoginTest extends SmackTestCase {
conn.disconnect(); conn.disconnect();
} }
else { else {
fail("User with no resource was able to log into the server"); fail("User with no resource was not able to log into the server");
} }
} catch (XMPPException e) { } catch (XMPPException e) {
if (e.getXMPPError() != null) {
assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode()); assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode());
} else {
fail(e.getMessage());
}
} }
} }

View file

@ -23,7 +23,9 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import javax.net.SocketFactory; import javax.net.SocketFactory;
@ -58,6 +60,8 @@ public abstract class SmackTestCase extends TestCase {
private int port = 5222; private int port = 5222;
private String usernamePrefix = "user"; private String usernamePrefix = "user";
private String passwordPrefix; private String passwordPrefix;
private boolean testAnonymousLogin = false;
private Map<String, String> accountCreationParameters = new HashMap<String, String>();
private boolean samePassword; private boolean samePassword;
private List<Integer> createdUserIdx = new ArrayList<Integer>(); private List<Integer> createdUserIdx = new ArrayList<Integer>();
@ -438,6 +442,20 @@ public abstract class SmackTestCase extends TestCase {
samePassword = "true".equals(parser.getAttributeValue(0)); samePassword = "true".equals(parser.getAttributeValue(0));
passwordPrefix = parser.nextText(); passwordPrefix = parser.nextText();
} }
else if (parser.getName().equals("testAnonymousLogin")) {
testAnonymousLogin = "true".equals(parser.nextText());
}
else if (parser.getName().equals("accountCreationParameters")) {
int numAttributes = parser.getAttributeCount();
String key = null;
String value = null;
for (int i = 0; i < numAttributes; i++) {
key = parser.getAttributeName(i);
value = parser.getAttributeValue(i);
accountCreationParameters.put(key, value);
}
}
} }
eventType = parser.next(); eventType = parser.next();
} }
@ -498,4 +516,11 @@ public abstract class SmackTestCase extends TestCase {
} }
} }
public boolean isTestAnonymousLogin() {
return testAnonymousLogin;
}
public Map<String, String> getAccountCreationParameters() {
return accountCreationParameters;
}
} }