@ -10,6 +10,9 @@
import net.java.sip.communicator.service.protocol.* ;
import java.util.* ;
import org.osgi.framework.* ;
import net.java.sip.communicator.service.protocol.event.RegistrationStateChangeListener ;
import net.java.sip.communicator.service.protocol.event.RegistrationStateChangeEvent ;
import net.java.sip.communicator.util.Logger ;
/ * *
* Tests basic account manager functionalitites
@ -17,6 +20,13 @@
* /
public class TestAccountInstallation extends TestCase
{
private static final Logger logger =
Logger . getLogger ( TestAccountInstallation . class ) ;
/ * *
* The lock that we wait on until registration is finalized .
* /
private Object registrationLock = new Object ( ) ;
AccountManager icqAccountManager = null ;
public TestAccountInstallation ( String name )
@ -35,6 +45,262 @@ protected void tearDown() throws Exception
super . tearDown ( ) ;
}
/ * *
* We try to register with wrong uin which must fire event with status
* AUTHENTICATION_FAILED . As the uin is new ( not existing and not registered )
* we first install this account , then try to register and wait for the
* supposed event . After all we unregister this account
* /
public void testRegisterUrongUsername ( )
{
ServiceReference [ ] serRefs = null ;
String osgiFilter = "(" + AccountManager . PROTOCOL_PROPERTY_NAME
+ "=" + ProtocolNames . ICQ + ")" ;
try {
serRefs = IcqSlickFixture . bc . getServiceReferences (
AccountManager . class . getName ( ) , osgiFilter ) ;
}
catch ( InvalidSyntaxException ex ) {
//this really shouldhn't occur as the filter expression is static.
fail ( osgiFilter + " is not a valid osgi filter" ) ;
}
assertTrue (
"Failed to find an account manager service for protocol ICQ" ,
serRefs ! = null | | serRefs . length > 0 ) ;
//Keep the reference for later usage.
AccountManager icqAccountManager =
( AccountManager ) IcqSlickFixture . bc . getService ( serRefs [ 0 ] ) ;
//Prepare the properties of the icq account.
String passwd = System . getProperty ( IcqProtocolProviderSlick
. TESTED_IMPL_PWD_PROP_NAME , null ) ;
String uin = System . getProperty ( IcqProtocolProviderSlick
. TESTED_IMPL_ACCOUNT_ID_PROP_NAME , null ) ;
// make this uin an invalid one
uin = uin + "1234" ;
Hashtable icqAccountProperties = new Hashtable ( ) ;
icqAccountProperties . put ( AccountProperties . PASSWORD , passwd ) ;
AccountID icqAccountID = icqAccountManager . installAccount (
IcqSlickFixture . bc , uin , icqAccountProperties ) ;
//find the protocol provider service
ServiceReference [ ] icqProviderRefs = null ;
try
{
icqProviderRefs
= IcqSlickFixture . bc . getServiceReferences (
ProtocolProviderService . class . getName ( ) ,
"(&"
+ "(" + AccountManager . PROTOCOL_PROPERTY_NAME + "=" +
ProtocolNames . ICQ + ")"
+ "(" + AccountManager . ACCOUNT_ID_PROPERTY_NAME + "="
+ uin + ")"
+ ")" ) ;
}
catch ( InvalidSyntaxException ex1 )
{
}
//make sure we found a service
assertNotNull ( "No Protocol Provider was found for ICQ UIN:" + icqAccountID ,
icqProviderRefs ) ;
assertTrue ( "No Protocol Provider was found for ICQ UIN:" + icqAccountID ,
icqProviderRefs . length > 0 ) ;
//save the service for other tests to use.
ServiceReference icqServiceRef = icqProviderRefs [ 0 ] ;
ProtocolProviderService provider = ( ProtocolProviderService ) IcqSlickFixture . bc . getService ( icqServiceRef ) ;
RegistrationFailedEventCollector regFailedEvtCollector = new RegistrationFailedEventCollector ( ) ;
logger . debug ( "install " + regFailedEvtCollector ) ;
provider . addRegistrationStateChangeListener ( regFailedEvtCollector ) ;
provider . register ( null ) ;
//give it enough time to register. We won't really have to wait all this
//time since the registration event collector would notify us the moment
//we get signed on.
try {
synchronized ( registrationLock ) {
logger . debug ( "Waiting for registration to complete ..." ) ;
registrationLock . wait ( 40000 ) ;
logger . debug ( "Registration was completed or we lost patience." ) ;
}
}
catch ( InterruptedException ex ) {
logger . debug ( "Interrupted while waiting for registration" , ex ) ;
}
catch ( Throwable t )
{
logger . debug ( "We got thrown out while waiting for registration" , t ) ;
}
assertTrue (
"No registration event notifying of registration has failed. "
+ "All events were: " + regFailedEvtCollector . collectedNewStates
, regFailedEvtCollector . collectedNewStates
. contains ( RegistrationState . AUTHENTICATION_FAILED ) ) ;
assertTrue ( "Registration status must be auth failed as we are logging in with wrong uin" , regFailedEvtCollector . failedCode = = RegistrationStateChangeEvent . REASON_AUTHENTICATION_FAILED ) ;
assertNotNull ( "We must have reason for auth failed" , regFailedEvtCollector . failedReason ) ;
provider . removeRegistrationStateChangeListener ( regFailedEvtCollector ) ;
icqAccountManager . uninstallAccount ( icqAccountID ) ;
}
/ * *
* Will try to register with wrong password and wait for triggered event
* with status AUTHENTICATION_FAILED .
* We get the already installed account . Change the password and try to register
* After all tests we must return the original password so we don ' t break the other tests
* /
public void testRegisterUrongPassword ( )
{
ServiceReference [ ] serRefs = null ;
String osgiFilter = "(" + AccountManager . PROTOCOL_PROPERTY_NAME
+ "=" + ProtocolNames . ICQ + ")" ;
try {
serRefs = IcqSlickFixture . bc . getServiceReferences (
AccountManager . class . getName ( ) , osgiFilter ) ;
}
catch ( InvalidSyntaxException ex ) {
//this really shouldhn't occur as the filter expression is static.
fail ( osgiFilter + " is not a valid osgi filter" ) ;
}
assertTrue (
"Failed to find an account manager service for protocol ICQ" ,
serRefs ! = null | | serRefs . length > 0 ) ;
//Keep the reference for later usage.
AccountManager icqAccountManager =
( AccountManager ) IcqSlickFixture . bc . getService ( serRefs [ 0 ] ) ;
//Prepare the properties of the icq account.
String passwd = System . getProperty ( IcqProtocolProviderSlick
. TESTED_IMPL_PWD_PROP_NAME , null ) ;
String uin = System . getProperty ( IcqProtocolProviderSlick
. TESTED_IMPL_ACCOUNT_ID_PROP_NAME , null ) ;
passwd + = "1234" ;
Hashtable icqAccountProperties = new Hashtable ( ) ;
icqAccountProperties . put ( AccountProperties . PASSWORD , passwd ) ;
AccountID icqAccountID = icqAccountManager . installAccount (
IcqSlickFixture . bc , uin , icqAccountProperties ) ;
//find the protocol provider service
ServiceReference [ ] icqProviderRefs = null ;
try
{
icqProviderRefs
= IcqSlickFixture . bc . getServiceReferences (
ProtocolProviderService . class . getName ( ) ,
"(&"
+ "(" + AccountManager . PROTOCOL_PROPERTY_NAME + "=" +
ProtocolNames . ICQ + ")"
+ "(" + AccountManager . ACCOUNT_ID_PROPERTY_NAME + "="
+ icqAccountID . getAccountUserID ( ) + ")"
+ ")" ) ;
}
catch ( InvalidSyntaxException ex1 )
{
logger . error ( "" , ex1 ) ;
}
//make sure we found a service
assertNotNull ( "No Protocol Provider was found for ICQ UIN:" + icqAccountID ,
icqProviderRefs ) ;
assertTrue ( "No Protocol Provider was found for ICQ UIN:" + icqAccountID ,
icqProviderRefs . length > 0 ) ;
//save the service for other tests to use.
ServiceReference icqServiceRef = icqProviderRefs [ 0 ] ;
ProtocolProviderService provider = ( ProtocolProviderService ) IcqSlickFixture .
bc . getService ( icqServiceRef ) ;
RegistrationFailedEventCollector regFailedEvtCollector = new RegistrationFailedEventCollector ( ) ;
logger . debug ( "install " + regFailedEvtCollector ) ;
provider . addRegistrationStateChangeListener ( regFailedEvtCollector ) ;
provider . register ( null ) ;
//give it enough time to register. We won't really have to wait all this
//time since the registration event collector would notify us the moment
//we get signed on.
try {
synchronized ( registrationLock ) {
logger . debug ( "Waiting for registration to complete ..." ) ;
registrationLock . wait ( 40000 ) ;
logger . debug ( "Registration was completed or we lost patience." ) ;
}
}
catch ( InterruptedException ex ) {
logger . debug ( "Interrupted while waiting for registration" , ex ) ;
}
catch ( Throwable t )
{
logger . debug ( "We got thrown out while waiting for registration" , t ) ;
}
assertTrue (
"No registration event notifying of registration has failed. "
+ "All events were: " + regFailedEvtCollector . collectedNewStates
, regFailedEvtCollector . collectedNewStates
. contains ( RegistrationState . AUTHENTICATION_FAILED ) ) ;
assertTrue ( "Registration status must be auth failed as we are logging in with wrong pass" , regFailedEvtCollector . failedCode = = RegistrationStateChangeEvent . REASON_AUTHENTICATION_FAILED ) ;
assertNotNull ( "We must have reason for auth failed" , regFailedEvtCollector . failedReason ) ;
provider . removeRegistrationStateChangeListener ( regFailedEvtCollector ) ;
icqAccountManager . uninstallAccount ( icqAccountID ) ;
}
public class RegistrationFailedEventCollector
implements RegistrationStateChangeListener
{
public List collectedNewStates = new LinkedList ( ) ;
public int failedCode ;
public String failedReason = null ;
public void registrationStateChanged ( RegistrationStateChangeEvent evt )
{
collectedNewStates . add ( evt . getNewState ( ) ) ;
logger . debug ( "damencho col event - " + evt . getNewState ( ) ) ;
if ( evt . getNewState ( ) . equals ( RegistrationState . AUTHENTICATION_FAILED ) )
{
failedCode = evt . getReasonCode ( ) ;
failedReason = evt . getReason ( ) ;
logger . debug ( "Our registration failed - " + failedCode + " = " + failedReason ) ;
synchronized ( registrationLock ) {
logger . debug ( "." ) ;
registrationLock . notifyAll ( ) ;
logger . debug ( "." ) ;
}
}
}
}
/ * *
* Installs an account and verifies whether the installation has gone well .
* /