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