2004-12-29 03:17:27 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
2007-11-14 17:27:47 +01:00
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
2004-12-29 03:17:27 +01:00
|
|
|
*
|
2007-02-02 19:30:24 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-12-29 03:17:27 +01:00
|
|
|
*
|
2007-02-02 19:30:24 +01:00
|
|
|
* All rights reserved. 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.
|
2004-12-29 03:17:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2005-09-13 22:05:29 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2004-12-29 03:17:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes set of login tests.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class LoginTest extends SmackTestCase {
|
|
|
|
|
|
|
|
public LoginTest(String arg0) {
|
|
|
|
super(arg0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the server is returning the correct error when trying to login using an invalid
|
|
|
|
* (i.e. non-existent) user.
|
|
|
|
*/
|
|
|
|
public void testInvalidLogin() {
|
|
|
|
try {
|
2007-01-05 00:00:58 +01:00
|
|
|
XMPPConnection connection = createConnection();
|
2006-09-14 21:16:40 +02:00
|
|
|
connection.connect();
|
2004-12-29 03:17:27 +01:00
|
|
|
try {
|
|
|
|
// Login with an invalid user
|
|
|
|
connection.login("invaliduser" , "invalidpass");
|
2006-09-14 21:16:40 +02:00
|
|
|
connection.disconnect();
|
2004-12-29 03:17:27 +01:00
|
|
|
fail("Invalid user was able to log into the server");
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
2005-09-13 22:05:29 +02:00
|
|
|
if (e.getXMPPError() != null) {
|
|
|
|
assertEquals("Incorrect error code while login with an invalid user", 401,
|
|
|
|
e.getXMPPError().getCode());
|
|
|
|
}
|
2004-12-29 03:17:27 +01:00
|
|
|
}
|
|
|
|
// Wait here while trying tests with exodus
|
|
|
|
//Thread.sleep(300);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the server handles anonymous users correctly.
|
|
|
|
*/
|
2006-02-27 21:01:30 +01:00
|
|
|
public void testSASLAnonymousLogin() {
|
2004-12-29 03:17:27 +01:00
|
|
|
try {
|
2007-01-05 00:00:58 +01:00
|
|
|
XMPPConnection conn1 = createConnection();
|
|
|
|
XMPPConnection conn2 = createConnection();
|
2006-09-14 21:16:40 +02:00
|
|
|
conn1.connect();
|
|
|
|
conn2.connect();
|
2004-12-29 03:17:27 +01:00
|
|
|
try {
|
|
|
|
// Try to login anonymously
|
2004-12-31 05:34:24 +01:00
|
|
|
conn1.loginAnonymously();
|
|
|
|
conn2.loginAnonymously();
|
2006-02-27 21:01:30 +01:00
|
|
|
|
|
|
|
assertNotNull("Resource is null", StringUtils.parseResource(conn1.getUser()));
|
|
|
|
assertNotNull("Resource is null", StringUtils.parseResource(conn2.getUser()));
|
|
|
|
|
|
|
|
assertNotNull("Username is null", StringUtils.parseName(conn1.getUser()));
|
|
|
|
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
e.printStackTrace();
|
2007-02-16 01:13:18 +01:00
|
|
|
//fail(e.getMessage());
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Close the connection
|
|
|
|
conn1.disconnect();
|
|
|
|
conn2.disconnect();
|
2006-02-27 21:01:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the server handles anonymous users correctly.
|
|
|
|
*/
|
|
|
|
public void testNonSASLAnonymousLogin() {
|
|
|
|
try {
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
|
|
|
|
config.setSASLAuthenticationEnabled(false);
|
|
|
|
XMPPConnection conn1 = new XMPPConnection(config);
|
2006-09-14 21:16:40 +02:00
|
|
|
conn1.connect();
|
2006-02-27 21:01:30 +01:00
|
|
|
|
|
|
|
config = new ConnectionConfiguration(getHost(), getPort());
|
|
|
|
config.setSASLAuthenticationEnabled(false);
|
|
|
|
XMPPConnection conn2 = new XMPPConnection(config);
|
2006-09-14 21:16:40 +02:00
|
|
|
conn2.connect();
|
|
|
|
|
2006-02-27 21:01:30 +01:00
|
|
|
try {
|
|
|
|
// Try to login anonymously
|
|
|
|
conn1.loginAnonymously();
|
|
|
|
conn2.loginAnonymously();
|
|
|
|
|
|
|
|
assertNotNull("Resource is null", StringUtils.parseResource(conn1.getUser()));
|
|
|
|
assertNotNull("Resource is null", StringUtils.parseResource(conn2.getUser()));
|
|
|
|
|
|
|
|
assertNotNull("Username is null", StringUtils.parseName(conn1.getUser()));
|
|
|
|
assertNotNull("Username is null", StringUtils.parseName(conn2.getUser()));
|
2004-12-29 03:17:27 +01:00
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
// Close the connection
|
2006-09-14 21:16:40 +02:00
|
|
|
conn1.disconnect();
|
|
|
|
conn2.disconnect();
|
2004-12-29 03:17:27 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-14 05:24:04 +02:00
|
|
|
/**
|
|
|
|
* Check that the server does not allow to log in without specifying a resource.
|
|
|
|
*/
|
|
|
|
public void testLoginWithNoResource() {
|
|
|
|
try {
|
2007-01-05 00:00:58 +01:00
|
|
|
XMPPConnection conn = createConnection();
|
2006-09-14 21:16:40 +02:00
|
|
|
conn.connect();
|
2005-04-14 05:24:04 +02:00
|
|
|
try {
|
|
|
|
conn.getAccountManager().createAccount("user_1", "user_1");
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
// Do nothing if the accout already exists
|
|
|
|
if (e.getXMPPError().getCode() != 409) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
conn.login("user_1", "user_1", (String) null);
|
2005-09-13 22:05:29 +02:00
|
|
|
if (conn.getSASLAuthentication().isAuthenticated()) {
|
|
|
|
// Check that the server assigned a resource
|
|
|
|
assertNotNull("JID assigned by server is missing", conn.getUser());
|
|
|
|
assertNotNull("JID assigned by server does not have a resource",
|
|
|
|
StringUtils.parseResource(conn.getUser()));
|
2006-09-14 21:16:40 +02:00
|
|
|
conn.disconnect();
|
2005-09-13 22:05:29 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fail("User with no resource was able to log into the server");
|
|
|
|
}
|
2005-04-14 05:24:04 +02:00
|
|
|
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode());
|
|
|
|
}
|
|
|
|
}
|
2004-12-29 03:17:27 +01:00
|
|
|
|
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|