bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test15.java
bloba5ac5d0ac75ebeac1cafbe821408b63a70c49b6e
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.storages;
21 import com.sun.star.uno.XInterface;
22 import com.sun.star.lang.XMultiServiceFactory;
23 import com.sun.star.lang.XSingleServiceFactory;
25 import com.sun.star.bridge.XUnoUrlResolver;
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.uno.XInterface;
29 import com.sun.star.embed.*;
31 import share.LogWriter;
32 import complex.storages.TestHelper;
33 import complex.storages.StorageTest;
35 public class Test15 implements StorageTest {
37 XMultiServiceFactory m_xMSF;
38 XSingleServiceFactory m_xStorageFactory;
39 TestHelper m_aTestHelper;
41 public Test15( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
43 m_xMSF = xMSF;
44 m_xStorageFactory = xStorageFactory;
45 m_aTestHelper = new TestHelper( aLogWriter, "Test15: " );
48 public boolean test()
50 String aStreamPrefix = "";
51 for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
52 if ( !testForPath( aStreamPrefix ) )
53 return false;
55 return true;
58 public boolean testForPath( String aStreamPrefix )
60 try
62 String aSubStream1Path = aStreamPrefix + "SubStream1";
63 String aSubStream2Path = aStreamPrefix + "SubStream2";
64 String aSubStream3Path = aStreamPrefix + "SubStream3";
65 String aSubStream4Path = aStreamPrefix + "SubStream4";
67 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
68 if ( sTempFileURL == null || sTempFileURL == "" )
70 m_aTestHelper.Error( "No valid temporary file was created!" );
71 return false;
74 // create temporary storage based on a previously created temporary file
75 Object pArgs[] = new Object[2];
76 pArgs[0] = (Object) sTempFileURL;
77 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
79 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
80 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
81 if ( xTempFileStorage == null )
83 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
84 return false;
87 // set the global password for the root storage
88 XEncryptionProtectedSource xTempStorageEncryption =
89 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
91 if ( xTempStorageEncryption == null )
93 m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
94 return true;
97 String sPass1 = "12345";
98 String sPass2 = "54321";
100 try {
101 xTempStorageEncryption.setEncryptionPassword( sPass1 );
103 catch( Exception e )
105 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
106 return false;
110 byte pBytes1[] = { 1, 1, 1, 1, 1 };
111 byte pBytes2[] = { 2, 2, 2, 2, 2 };
113 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
114 // and commit
115 if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true, true ) )
116 return false;
118 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
119 // and commit
120 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
121 return false;
123 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
124 // and commit
125 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", false, pBytes2, sPass2, true ) )
126 return false;
128 // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
129 // and don't commit
130 if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream4Path, "MediaType2", true, pBytes1, true, false ) )
131 return false;
134 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
135 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
136 "MediaType3",
137 true,
138 ElementModes.WRITE ) )
139 return false;
141 // commit the root storage so the contents must be stored now
142 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
143 return false;
145 // dispose used storages to free resources
146 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
147 return false;
150 // now reopen the storage,
151 // check all the written and copied information
152 // and change it
155 // the temporary file must not be locked any more after storage disposing
156 oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
157 xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
158 if ( xTempFileStorage == null )
160 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
161 return false;
164 // set the global password for the root storage
165 xTempStorageEncryption =
166 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
168 if ( xTempStorageEncryption == null )
170 m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
171 return false;
174 try {
175 xTempStorageEncryption.setEncryptionPassword( sPass2 );
177 catch( Exception e )
179 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
180 return false;
184 if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
185 return false;
187 if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
188 return false;
190 if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", true, pBytes2 ) )
191 return false;
193 if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", true, pBytes2 ) )
194 return false;
196 if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream4Path, ElementModes.READ, sPass1 ) )
197 return false;
199 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
200 // and commit
201 if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType4", true, pBytes2, sPass1, true ) )
202 return false;
204 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
205 // and don't commit
206 if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType5", true, pBytes1, true ) )
207 return false;
209 // change the password of the existing stream
210 if ( m_aTestHelper.ChangeStreamPassH( xTempFileStorage, aSubStream2Path, sPass2, sPass1, true ) != 1 )
211 return false;
213 // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
214 // and don't commit
215 if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType5", true, pBytes1, false ) )
216 return false;
218 // commit the root storage so the contents must be stored now
219 if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
220 return false;
222 // dispose used storages to free resources
223 if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
224 return false;
227 // now reopen the storage,
228 // check all the written information
231 // the temporary file must not be locked any more after storage disposing
232 pArgs[1] = Integer.valueOf( ElementModes.READ );
233 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
234 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
235 if ( xResultStorage == null )
237 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
238 return false;
241 // set the global password for the root storage
242 xTempStorageEncryption =
243 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
245 if ( xTempStorageEncryption == null )
247 m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
248 return false;
251 try {
252 xTempStorageEncryption.setEncryptionPassword( sPass1 );
254 catch( Exception e )
256 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
257 return false;
260 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
261 return false;
263 if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType4", true, pBytes2 ) )
264 return false;
266 if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream2Path, "MediaType5", true, pBytes1 ) )
267 return false;
269 if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream3Path, "MediaType3", pBytes2, sPass2 ) )
270 return false;
272 // dispose used storages to free resources
273 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
274 return false;
276 return true;
278 catch( Exception e )
280 m_aTestHelper.Error( "Exception: " + e );
281 return false;