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>
<muc>conference</muc>
<!-- LoginTest parameters -->
<testAnonymousLogin>false</testAnonymousLogin>
<!-- Account creation parameters -->
<!--
<accountCreationParameters email="test@xcp.localhost" name="test"/>
-->
</testcase>

View File

@ -67,6 +67,8 @@ public class LoginTest extends SmackTestCase {
* Check that the server handles anonymous users correctly.
*/
public void testSASLAnonymousLogin() {
if (!isTestAnonymousLogin()) return;
try {
XMPPConnection conn1 = createConnection();
XMPPConnection conn2 = createConnection();
@ -84,8 +86,7 @@ public class LoginTest extends SmackTestCase {
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
}
catch (XMPPException e) {
e.printStackTrace();
//fail(e.getMessage());
fail(e.getMessage());
}
finally {
// Close the connection
@ -103,6 +104,8 @@ public class LoginTest extends SmackTestCase {
* Check that the server handles anonymous users correctly.
*/
public void testNonSASLAnonymousLogin() {
if (!isTestAnonymousLogin()) return;
try {
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setSASLAuthenticationEnabled(false);
@ -126,7 +129,6 @@ public class LoginTest extends SmackTestCase {
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
// Close the connection
@ -147,12 +149,16 @@ public class LoginTest extends SmackTestCase {
XMPPConnection conn = createConnection();
conn.connect();
try {
conn.getAccountManager().createAccount("user_1", "user_1");
conn.getAccountManager().createAccount("user_1", "user_1", getAccountCreationParameters());
} catch (XMPPException e) {
// Do nothing if the accout already exists
// Do nothing if the account already exists
if (e.getXMPPError().getCode() != 409) {
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);
if (conn.getSASLAuthentication().isAuthenticated()) {
@ -163,11 +169,15 @@ public class LoginTest extends SmackTestCase {
conn.disconnect();
}
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) {
assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode());
if (e.getXMPPError() != null) {
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.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.SocketFactory;
@ -58,6 +60,8 @@ public abstract class SmackTestCase extends TestCase {
private int port = 5222;
private String usernamePrefix = "user";
private String passwordPrefix;
private boolean testAnonymousLogin = false;
private Map<String, String> accountCreationParameters = new HashMap<String, String>();
private boolean samePassword;
private List<Integer> createdUserIdx = new ArrayList<Integer>();
@ -435,9 +439,23 @@ public abstract class SmackTestCase extends TestCase {
usernamePrefix = parser.nextText();
}
else if (parser.getName().equals("password")) {
samePassword = "true".equals(parser.getAttributeValue(0));
samePassword = "true".equals(parser.getAttributeValue(0));
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();
}
@ -498,4 +516,11 @@ public abstract class SmackTestCase extends TestCase {
}
}
public boolean isTestAnonymousLogin() {
return testAnonymousLogin;
}
public Map<String, String> getAccountCreationParameters() {
return accountCreationParameters;
}
}