xref: /aoo42x/main/package/qa/storages/Test08.java (revision a893be29)
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 package complex.storages;
23 
24 import com.sun.star.uno.XInterface;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.lang.XSingleServiceFactory;
27 
28 import com.sun.star.bridge.XUnoUrlResolver;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 
32 import com.sun.star.embed.*;
33 
34 import share.LogWriter;
35 import complex.storages.TestHelper;
36 import complex.storages.StorageTest;
37 
38 public class Test08 implements StorageTest {
39 
40 	XMultiServiceFactory m_xMSF;
41 	XSingleServiceFactory m_xStorageFactory;
42 	TestHelper m_aTestHelper;
43 
Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44 	public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
45 	{
46 		m_xMSF = xMSF;
47 		m_xStorageFactory = xStorageFactory;
48 		m_aTestHelper = new TestHelper( aLogWriter, "Test08: " );
49 	}
50 
test()51     public boolean test()
52 	{
53 		try
54 		{
55 
56 			// create temporary storage based on arbitrary medium
57 			// after such a storage is closed it is lost
58 			Object oTempStorage = m_xStorageFactory.createInstance();
59 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
60 			if ( xTempStorage == null )
61 			{
62 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
63 				return false;
64 			}
65 
66 			// set the global password for the root storage
67 			XEncryptionProtectedSource xTempStorageEncryption =
68 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage );
69 
70 			if ( xTempStorageEncryption == null )
71 			{
72 				m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
73 				return true;
74 			}
75 
76 			String sPass1 = "123";
77 			String sPass2 = "321";
78 
79 			try {
80 				xTempStorageEncryption.setEncryptionPassword( sPass1 );
81 			}
82 			catch( Exception e )
83 			{
84 				m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
85 				return false;
86 			}
87 
88 			// open a new substorage
89 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
90 																		"SubStorage1",
91 																		ElementModes.WRITE );
92 			if ( xTempSubStorage == null )
93 			{
94 				m_aTestHelper.Error( "Can't create substorage!" );
95 				return false;
96 			}
97 
98             byte pBigBytes[] = new byte[33000];
99 			for ( int nInd = 0; nInd < 33000; nInd++ )
100 				pBigBytes[nInd] = (byte)( nInd % 128 );
101 
102 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
103 			// the stream will be encrypted with common password
104 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
105 			if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) )
106 				return false;
107 			if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes, true ) )
108 				return false;
109 
110 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
111 			// the stream will not be encrypted
112 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
113 			if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) )
114 				return false;
115 			if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes, false ) )
116 				return false;
117 
118 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
119 			// the stream will be compressed with own password
120 			byte pBytes3[] = { 3, 3, 3, 3, 3 };
121 
122 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
123 			// the stream will not be encrypted
124 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, sPass2 ) )
125 				return false;
126             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream3", "MediaType3", false, pBigBytes, sPass2 ) )
127 				return false;
128 
129 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
130 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
131 															"MediaType4",
132 															true,
133 															ElementModes.WRITE ) )
134 				return false;
135 
136 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
137 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
138 															"MediaType5",
139 															false,
140 															ElementModes.WRITE ) )
141 				return false;
142 
143 			// create temporary file
144 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
145 			if ( sTempFileURL == null || sTempFileURL == "" )
146 			{
147 				m_aTestHelper.Error( "No valid temporary file was created!" );
148 				return false;
149 			}
150 
151 			// create temporary storage based on a previously created temporary file
152 			Object pArgs[] = new Object[2];
153 			pArgs[0] = (Object) sTempFileURL;
154 			pArgs[1] = new Integer( ElementModes.WRITE );
155 
156 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
157 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
158 			if ( xTempFileStorage == null )
159 			{
160 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
161 				return false;
162 			}
163 
164 			// copy xTempStorage to xTempFileStorage
165 			// xTempFileStorage will be automatically committed
166 			if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
167 				return false;
168 
169 			// dispose used storages to free resources
170 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
171 				return false;
172 
173 			// ================================================
174 			// now check all the written and copied information
175 			// ================================================
176 
177 			// the temporary file must not be locked any more after storage disposing
178 			pArgs[1] = new Integer( ElementModes.READ );
179 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
180 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
181 			if ( xResultStorage == null )
182 			{
183 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
184 				return false;
185 			}
186 
187 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) )
188 				return false;
189 
190 			// open existing substorage
191 			XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
192 																		"SubStorage1",
193 																		ElementModes.READ );
194 			if ( xResultSubStorage == null )
195 			{
196 				m_aTestHelper.Error( "Can't open existing substorage!" );
197 				return false;
198 			}
199 
200 			if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) )
201 				return false;
202 
203 			// set the global password for the root storage
204 			XEncryptionProtectedSource xResultStorageEncryption =
205 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
206 
207 			if ( xResultStorageEncryption == null )
208 			{
209 				m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
210 				return false;
211 			}
212 
213 			try {
214 				xResultStorageEncryption.setEncryptionPassword( sPass2 );
215 			}
216 			catch( Exception e )
217 			{
218 				m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
219 				return false;
220 			}
221 
222 			if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) )
223 				return false;
224 			if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) )
225 				return false;
226 
227 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) )
228 				return false;
229 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) )
230 				return false;
231 
232 			// the common root storage password should allow to open this stream
233 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", true, pBytes3 ) )
234 				return false;
235 			if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream3", "MediaType3", true, pBigBytes ) )
236 				return false;
237 
238 			// dispose used storages to free resources
239 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
240 				return false;
241 
242 			return true;
243 		}
244 		catch( Exception e )
245 		{
246 			m_aTestHelper.Error( "Exception: " + e );
247 			return false;
248 		}
249     }
250 }
251 
252