1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package complex.passwordcontainer;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.task.UrlRecord;
28 import com.sun.star.task.UserRecord;
29 import com.sun.star.task.XPasswordContainer;
30 import com.sun.star.task.XMasterPasswordHandling;
31 import com.sun.star.task.XInteractionHandler;
32 
33 
34 import com.sun.star.uno.UnoRuntime;
35 
36 
37 public class Test03 implements PasswordContainerTest {
38     XMultiServiceFactory m_xMSF = null;
39     XPasswordContainer m_xPasswordContainer = null;
40     TestHelper m_aTestHelper = null;
41 
Test03( XMultiServiceFactory xMSF )42     public Test03 ( XMultiServiceFactory xMSF )
43     {
44         m_xMSF = xMSF;
45         m_aTestHelper = new TestHelper ( "Test03: ");
46     }
47 
test()48     public boolean test() {
49         final String sURL = "http://www.openoffice.org";
50         final String sUserPre = "OOoUser";
51         final String sPwdPre = "Password";
52         final int iPersistentUserNum = 10;
53         final int iRuntimeUserNum = 5;
54 
55         UserRecord aInputUserList[] = new UserRecord[iPersistentUserNum+iRuntimeUserNum];
56         for(int i = 0; i < iPersistentUserNum; i++) {
57             String sTemp[] = {sPwdPre + "_1_" + i};     // currently one password for one user
58             aInputUserList[i] = new UserRecord(sUserPre + "_1_" + i, sTemp);
59         }
60         for(int i = 0; i < iRuntimeUserNum; i++) {
61             String sTemp[] = {sPwdPre + "_2_" + i};
62             aInputUserList[i+iPersistentUserNum] = new UserRecord(sUserPre + "_2_" + i, sTemp);
63         }
64 
65         try {
66             Object oPasswordContainer = m_xMSF.createInstance("com.sun.star.task.PasswordContainer");
67             XPasswordContainer xContainer = UnoRuntime.queryInterface(XPasswordContainer.class, oPasswordContainer);
68             Object oHandler = m_xMSF.createInstance("com.sun.star.task.InteractionHandler");
69             XInteractionHandler xHandler = UnoRuntime.queryInterface(XInteractionHandler.class, oHandler);
70             MasterPasswdHandler aMHandler = new MasterPasswdHandler(xHandler);
71             XMasterPasswordHandling xMHandling = UnoRuntime.queryInterface(XMasterPasswordHandling.class, oPasswordContainer);
72 
73             // allow the storing of the passwords
74             xMHandling.allowPersistentStoring(true);
75 
76             // add a set of users and passwords for the same URL persistently
77             for(int i = 0; i < iPersistentUserNum; i++) {
78                 xContainer.addPersistent(sURL, aInputUserList[i].UserName, aInputUserList[i].Passwords, aMHandler);
79             }
80 
81             // add a set of users and passwords for the same URL for runtime
82             for(int i = 0; i < iRuntimeUserNum; i++) {
83                 xContainer.add(sURL, aInputUserList[i+iPersistentUserNum].UserName, aInputUserList[i+iPersistentUserNum].Passwords, aMHandler);
84             }
85 
86             // get the result for the URL and check that it contains persistent and runtime passwords
87             UrlRecord aRecord = xContainer.find(sURL, aMHandler);
88             if(!aRecord.Url.equals(sURL)) {
89                 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL);
90                 return false;
91             }
92             if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList)) {
93                 m_aTestHelper.Error("User list is not the expected");
94                 return false;
95             }
96 
97             // remove all the persistent passwords
98             xContainer.removeAllPersistent();
99 
100              // remove the runtime passwords
101             aRecord = xContainer.find(sURL, aMHandler);
102             for(int i = 0; i < aRecord.UserList.length; i++) {
103                 xContainer.remove(sURL, aRecord.UserList[i].UserName);
104             }
105 
106             // disallow the storing of the passwords
107             xMHandling.allowPersistentStoring(false);
108         }catch(Exception e){
109             m_aTestHelper.Error("Exception: " + e);
110             return false;
111         }
112         return true;
113     }
114 }
115