/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/

#if DEBUG
using System;
using System.Text;
using System.Collections;
//using System.InvalidOperationException;

using NUnit.Framework;

using sscs.common;
using sscs.cache;

namespace sscs.common
{

[TestFixture]
public class TestSessionManager
{	

 	User theuser = null;
 	UnixUserIdentifier UserId = null;
 	UnixUserIdentifier root = null;
      SecretStore mysec = null;
      SecretStore anothersec  = null;
      KeyChain mykc1 = null;
      KeyChain mykc2 = null;
      byte[] secbyte1 = null;
      byte[] secbyte2 = null;
      Secret mysecret1 = null;
      Secret mysecret2 = null;
      SessionManager sesman ;

     [SetUp]
     public void Init()
     {

          sesman =  SessionManager.GetSessionManager;
      		    	
          mykc1 = new KeyChain("k1");
          mykc2 = new KeyChain("k2");
                   
 	   
          mysecret1 = new Secret(); 
          mysecret2 = new Secret(); 
          
          mysecret1.SetKey("key1"); 
          mysecret2.SetKey("key2"); 
          secbyte1 = Encoding.ASCII.GetBytes("NOVELL");
          secbyte2 = Encoding.ASCII.GetBytes("IBM");
          	
          mysecret1.SetValue(secbyte1);     
          mysecret2.SetValue(secbyte2);     

	    mykc1.AddSecret(mysecret1);
          mykc2.AddSecret(mysecret2);
          
       
          UserId = new UnixUserIdentifier(420);
          root = new UnixUserIdentifier(0);
          
          //theuser = new UnixUser(UserId);

       

     	 
     }
     
 	
	[Test]	
	public void TestCreateUserSession()
	{

	      anothersec =  SessionManager.CreateUserSession(root);
	      	mysec = SessionManager.CreateUserSession(UserId);
		
		//Assert.AreEqual(1, mysec.getRefCount());
		//Assert.AreEqual(1, anothersec.getRefCount());

	      Assert.AreEqual(true, SessionManager.CheckIfUserSessionExists(UserId));
	      Assert.AreEqual(true, SessionManager.CheckIfUserSessionExists(root));

	     
	
	}

	
	[Test]
	public void TestAddtoSession()
	{
	       SecretStore s1  =  SessionManager.GetUserSecretStore(UserId);
	       SecretStore s2  =  SessionManager.GetUserSecretStore(root);

	       s1.AddKeyChain(mykc1);
	       s2.AddKeyChain(mykc2);

	       s1  =  SessionManager.GetUserSecretStore(UserId);
	       s2  =  SessionManager.GetUserSecretStore(root);

	       KeyChain returnK1 = s1.GetKeyChain("k1");
	       Secret returnS1 = returnK1.GetSecret("key1");
	       
	       KeyChain returnK2 = s2.GetKeyChain("k2");
	       Secret returnS2 = returnK2.GetSecret("key2");

             Assert.AreEqual("NOVELL", Encoding.ASCII.GetString(returnS1.GetValue()));
             Assert.AreEqual("IBM",Encoding.ASCII.GetString(returnS2.GetValue()) );

	     
	
	}


	[Test] 
      [ExpectedException(typeof(KeyChainDoesNotExistException))]
	public void TestInvalidAccess()
	{
	       SecretStore s1  =  SessionManager.GetUserSecretStore(UserId);
	       //SecretStore s2  =  SessionManager.GetUserSecretStore(root);

	       s1.GetKeyChain("k2");
	       
	
	}

	

	[Test] 
	public void TestRemoveUserSession()
	{
	     SessionManager.RemoveUserSession(UserId, true);
             Assert.AreEqual(false, SessionManager.CheckIfUserSessionExists(UserId)); 
	     
	     //TBD :Make the ref count more than one and delete call remove sesison once.
	     //The call it once more.. only second time it should remove the session entry.
	
	}



     

      
		
	
}

}
#endif