1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package complex.passwordcontainer;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
31*cdf0e10cSrcweir import com.sun.star.task.UrlRecord;
32*cdf0e10cSrcweir import com.sun.star.task.UserRecord;
33*cdf0e10cSrcweir import com.sun.star.task.XPasswordContainer;
34*cdf0e10cSrcweir import com.sun.star.task.XMasterPasswordHandling;
35*cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir public class Test03 implements PasswordContainerTest {
42*cdf0e10cSrcweir     XMultiServiceFactory m_xMSF = null;
43*cdf0e10cSrcweir     XPasswordContainer m_xPasswordContainer = null;
44*cdf0e10cSrcweir     TestHelper m_aTestHelper = null;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     public Test03 ( XMultiServiceFactory xMSF )
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         m_xMSF = xMSF;
49*cdf0e10cSrcweir         m_aTestHelper = new TestHelper ( "Test03: ");
50*cdf0e10cSrcweir     }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     public boolean test() {
53*cdf0e10cSrcweir         final String sURL = "http://www.openoffice.org";
54*cdf0e10cSrcweir         final String sUserPre = "OOoUser";
55*cdf0e10cSrcweir         final String sPwdPre = "Password";
56*cdf0e10cSrcweir         final int iPersistentUserNum = 10;
57*cdf0e10cSrcweir         final int iRuntimeUserNum = 5;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         UserRecord aInputUserList[] = new UserRecord[iPersistentUserNum+iRuntimeUserNum];
60*cdf0e10cSrcweir         for(int i = 0; i < iPersistentUserNum; i++) {
61*cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_1_" + i};     // currently one password for one user
62*cdf0e10cSrcweir             aInputUserList[i] = new UserRecord(sUserPre + "_1_" + i, sTemp);
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir         for(int i = 0; i < iRuntimeUserNum; i++) {
65*cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_2_" + i};
66*cdf0e10cSrcweir             aInputUserList[i+iPersistentUserNum] = new UserRecord(sUserPre + "_2_" + i, sTemp);
67*cdf0e10cSrcweir         }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir         try {
70*cdf0e10cSrcweir             Object oPasswordContainer = m_xMSF.createInstance("com.sun.star.task.PasswordContainer");
71*cdf0e10cSrcweir             XPasswordContainer xContainer = UnoRuntime.queryInterface(XPasswordContainer.class, oPasswordContainer);
72*cdf0e10cSrcweir             Object oHandler = m_xMSF.createInstance("com.sun.star.task.InteractionHandler");
73*cdf0e10cSrcweir             XInteractionHandler xHandler = UnoRuntime.queryInterface(XInteractionHandler.class, oHandler);
74*cdf0e10cSrcweir             MasterPasswdHandler aMHandler = new MasterPasswdHandler(xHandler);
75*cdf0e10cSrcweir             XMasterPasswordHandling xMHandling = UnoRuntime.queryInterface(XMasterPasswordHandling.class, oPasswordContainer);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir             // allow the storing of the passwords
78*cdf0e10cSrcweir             xMHandling.allowPersistentStoring(true);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir             // add a set of users and passwords for the same URL persistently
81*cdf0e10cSrcweir             for(int i = 0; i < iPersistentUserNum; i++) {
82*cdf0e10cSrcweir                 xContainer.addPersistent(sURL, aInputUserList[i].UserName, aInputUserList[i].Passwords, aMHandler);
83*cdf0e10cSrcweir             }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir             // add a set of users and passwords for the same URL for runtime
86*cdf0e10cSrcweir             for(int i = 0; i < iRuntimeUserNum; i++) {
87*cdf0e10cSrcweir                 xContainer.add(sURL, aInputUserList[i+iPersistentUserNum].UserName, aInputUserList[i+iPersistentUserNum].Passwords, aMHandler);
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir             // get the result for the URL and check that it contains persistent and runtime passwords
91*cdf0e10cSrcweir             UrlRecord aRecord = xContainer.find(sURL, aMHandler);
92*cdf0e10cSrcweir             if(!aRecord.Url.equals(sURL)) {
93*cdf0e10cSrcweir                 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL);
94*cdf0e10cSrcweir                 return false;
95*cdf0e10cSrcweir             }
96*cdf0e10cSrcweir             if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList)) {
97*cdf0e10cSrcweir                 m_aTestHelper.Error("User list is not the expected");
98*cdf0e10cSrcweir                 return false;
99*cdf0e10cSrcweir             }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             // remove all the persistent passwords
102*cdf0e10cSrcweir             xContainer.removeAllPersistent();
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir              // remove the runtime passwords
105*cdf0e10cSrcweir             aRecord = xContainer.find(sURL, aMHandler);
106*cdf0e10cSrcweir             for(int i = 0; i < aRecord.UserList.length; i++) {
107*cdf0e10cSrcweir                 xContainer.remove(sURL, aRecord.UserList[i].UserName);
108*cdf0e10cSrcweir             }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir             // disallow the storing of the passwords
111*cdf0e10cSrcweir             xMHandling.allowPersistentStoring(false);
112*cdf0e10cSrcweir         }catch(Exception e){
113*cdf0e10cSrcweir             m_aTestHelper.Error("Exception: " + e);
114*cdf0e10cSrcweir             return false;
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir         return true;
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir }
119