bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / Test08.java
blob9113ee385491e4216a2115f0829ad77bc403c155
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 Test08 implements StorageTest {
37 XMultiServiceFactory m_xMSF;
38 XSingleServiceFactory m_xStorageFactory;
39 TestHelper m_aTestHelper;
41 public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
43 m_xMSF = xMSF;
44 m_xStorageFactory = xStorageFactory;
45 m_aTestHelper = new TestHelper( aLogWriter, "Test08: " );
48 public boolean test()
50 try
53 // create temporary storage based on arbitrary medium
54 // after such a storage is closed it is lost
55 Object oTempStorage = m_xStorageFactory.createInstance();
56 XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
57 if ( xTempStorage == null )
59 m_aTestHelper.Error( "Can't create temporary storage representation!" );
60 return false;
63 // set the global password for the root storage
64 XEncryptionProtectedSource xTempStorageEncryption =
65 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage );
67 if ( xTempStorageEncryption == null )
69 m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
70 return true;
73 String sPass1 = "123";
74 String sPass2 = "321";
76 try {
77 xTempStorageEncryption.setEncryptionPassword( sPass1 );
79 catch( Exception e )
81 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
82 return false;
85 // open a new substorage
86 XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
87 "SubStorage1",
88 ElementModes.WRITE );
89 if ( xTempSubStorage == null )
91 m_aTestHelper.Error( "Can't create substorage!" );
92 return false;
95 byte pBigBytes[] = new byte[33000];
96 for ( int nInd = 0; nInd < 33000; nInd++ )
97 pBigBytes[nInd] = (byte)( nInd % 128 );
99 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
100 // the stream will be encrypted with common password
101 byte pBytes1[] = { 1, 1, 1, 1, 1 };
102 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) )
103 return false;
104 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
105 return false;
107 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
108 // the stream will not be encrypted
109 byte pBytes2[] = { 2, 2, 2, 2, 2 };
110 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) )
111 return false;
112 if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes, false ) )
113 return false;
115 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
116 // the stream will be compressed with own password
117 byte pBytes3[] = { 3, 3, 3, 3, 3 };
119 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
120 // the stream will not be encrypted
121 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, sPass2 ) )
122 return false;
123 if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream3", "MediaType3", false, pBigBytes, sPass2 ) )
124 return false;
126 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
127 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
128 "MediaType4",
129 true,
130 ElementModes.WRITE ) )
131 return false;
133 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
134 if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
135 "MediaType5",
136 false,
137 ElementModes.WRITE ) )
138 return false;
140 // create temporary file
141 String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
142 if ( sTempFileURL == null || sTempFileURL == "" )
144 m_aTestHelper.Error( "No valid temporary file was created!" );
145 return false;
148 // create temporary storage based on a previously created temporary file
149 Object pArgs[] = new Object[2];
150 pArgs[0] = (Object) sTempFileURL;
151 pArgs[1] = Integer.valueOf( ElementModes.WRITE );
153 Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
154 XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
155 if ( xTempFileStorage == null )
157 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
158 return false;
161 // copy xTempStorage to xTempFileStorage
162 // xTempFileStorage will be automatically committed
163 if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
164 return false;
166 // dispose used storages to free resources
167 if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
168 return false;
171 // now check all the written and copied information
174 // the temporary file must not be locked any more after storage disposing
175 pArgs[1] = Integer.valueOf( ElementModes.READ );
176 Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
177 XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
178 if ( xResultStorage == null )
180 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
181 return false;
184 if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
185 return false;
187 // open existing substorage
188 XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
189 "SubStorage1",
190 ElementModes.READ );
191 if ( xResultSubStorage == null )
193 m_aTestHelper.Error( "Can't open existing substorage!" );
194 return false;
197 if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
198 return false;
200 // set the global password for the root storage
201 XEncryptionProtectedSource xResultStorageEncryption =
202 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
204 if ( xResultStorageEncryption == null )
206 m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
207 return false;
210 try {
211 xResultStorageEncryption.setEncryptionPassword( sPass2 );
213 catch( Exception e )
215 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
216 return false;
219 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
220 return false;
221 if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
222 return false;
224 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
225 return false;
226 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
227 return false;
229 // the common root storage password should allow to open this stream
230 if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", true, pBytes3 ) )
231 return false;
232 if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream3", "MediaType3", true, pBigBytes ) )
233 return false;
235 // dispose used storages to free resources
236 if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
237 return false;
239 return true;
241 catch( Exception e )
243 m_aTestHelper.Error( "Exception: " + e );
244 return false;