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 import com.sun.star.io.XStream;
32 import com.sun.star.io.XInputStream;
33 
34 import com.sun.star.embed.*;
35 
36 import share.LogWriter;
37 import complex.storages.TestHelper;
38 import complex.storages.StorageTest;
39 
40 public class RegressionTest_i59886 implements StorageTest {
41 
42 	XMultiServiceFactory m_xMSF;
43 	XSingleServiceFactory m_xStorageFactory;
44 	TestHelper m_aTestHelper;
45 
RegressionTest_i59886( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )46 	public RegressionTest_i59886( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
47 	{
48 		m_xMSF = xMSF;
49 		m_xStorageFactory = xStorageFactory;
50 		m_aTestHelper = new TestHelper( aLogWriter, "RegressionTest_i59886: " );
51 	}
52 
test()53     public boolean test()
54 	{
55 		try
56 		{
57 			XStream xTempFileStream = m_aTestHelper.CreateTempFileStream( m_xMSF );
58 			if ( xTempFileStream == null )
59 				return false;
60 
61 			// create storage based on the temporary stream
62 			Object pArgs[] = new Object[2];
63 			pArgs[0] = (Object) xTempFileStream;
64 			pArgs[1] = new Integer( ElementModes.WRITE );
65 
66 			Object oTempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
67 			XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
68 			if ( xTempStorage == null )
69 			{
70 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
71 				return false;
72 			}
73 
74 			byte pBytes[] = new byte[36000];
75 			for ( int nInd = 0; nInd < 36000; nInd++ )
76 				pBytes[nInd] = (byte)( nInd % 128 );
77 
78 			String sPass = "12345";
79 
80 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
81 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempStorage, "SubStream1", "MediaType1", true, pBytes, sPass ) )
82 				return false;
83 
84 			// open a new substorage
85 			XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
86 																		"SubStorage1",
87 																		ElementModes.WRITE );
88 			if ( xTempSubStorage == null )
89 			{
90 				m_aTestHelper.Error( "Can't create substorage!" );
91 				return false;
92 			}
93 
94 			// open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
95 			if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes, sPass ) )
96 				return false;
97 
98 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
99 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
100 															"MediaType3",
101 															true,
102 															ElementModes.WRITE ) )
103 				return false;
104 
105 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
106 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
107 															"MediaType4",
108 															false,
109 															ElementModes.WRITE ) )
110 				return false;
111 
112 			// commit substorage first
113 			if ( !m_aTestHelper.commitStorage( xTempSubStorage ) )
114 				return false;
115 
116 			// commit the root storage so the contents must be stored now
117 			if ( !m_aTestHelper.commitStorage( xTempStorage ) )
118 				return false;
119 
120 			// dispose used storage to free resources
121 			if ( !m_aTestHelper.disposeStorage( xTempStorage ) )
122 				return false;
123 
124 			// ================================================
125 			// now reopen the storage, set the common storage key
126 			// and copy the storage
127 			// ================================================
128 
129 			Object oStep2TempStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
130 			XStorage xStep2TempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oStep2TempStorage );
131 			if ( xStep2TempStorage == null )
132 			{
133 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
134 				return false;
135 			}
136 
137 
138 			XStorage xStep2TempSubStorage = m_aTestHelper.openSubStorage( xStep2TempStorage,
139 																		"SubStorage1",
140 																		ElementModes.WRITE );
141 			if ( xStep2TempSubStorage == null )
142 			{
143 				m_aTestHelper.Error( "Can't create substorage!" );
144 				return false;
145 			}
146 
147 			// set the common storage password
148 			XEncryptionProtectedSource xEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xStep2TempStorage );
149 			if ( xEncr == null )
150 			{
151 				m_aTestHelper.Error( "The storage does not support encryption access!" );
152 				return false;
153 			}
154 			try
155 			{
156 				xEncr.setEncryptionPassword( sPass );
157 			}
158 			catch( Exception e )
159 			{
160 				m_aTestHelper.Error( "Can not set the common storage password!" );
161 				return false;
162 			}
163 
164 			// open the stream for writing and read them so that the cache is created, but do not change
165 			byte pDummyData[][] = new byte[1][3];
166 			XStream xTempStream1 = m_aTestHelper.OpenStream( xStep2TempStorage, "SubStream1", ElementModes.WRITE );
167 			XStream xTempStream2 = m_aTestHelper.OpenStream( xStep2TempSubStorage, "SubStream2", ElementModes.WRITE );
168 			if ( xTempStream1 == null || xTempStream2 == null )
169 				return false;
170 
171 			XInputStream xTempInStream1 = xTempStream1.getInputStream();
172 			XInputStream xTempInStream2 = xTempStream2.getInputStream();
173 			if ( xTempInStream1 == null || xTempInStream2 == null )
174 			{
175 				m_aTestHelper.Error( "No input stream is available!" );
176 				return false;
177 			}
178 
179 			xTempInStream1.readBytes( pDummyData, 3 );
180 			xTempInStream2.readBytes( pDummyData, 3 );
181 
182 
183 			// create temporary storage, it will be checked later
184 			Object oTargetStorage = m_xStorageFactory.createInstance();
185 			XStorage xTargetStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTargetStorage );
186 			if ( xTargetStorage == null )
187 			{
188 				m_aTestHelper.Error( "Can't create temporary storage representation!" );
189 				return false;
190 			}
191 
192 			// copy the current storage to the target
193 			try
194 			{
195 				xStep2TempStorage.copyToStorage( xTargetStorage );
196 			}
197 			catch( Exception e )
198 			{
199 				m_aTestHelper.Error( "Can not copy the storage with common storage password!" );
200 				return false;
201 			}
202 
203 			// dispose used storage to free resources
204 			if ( !m_aTestHelper.disposeStorage( xStep2TempStorage ) )
205 				return false;
206 
207 			// ================================================
208 			// now check all the information in the copy
209 			// ================================================
210 
211 			if ( !m_aTestHelper.checkStorageProperties( xTargetStorage, "MediaType3", true, ElementModes.WRITE ) )
212 				return false;
213 
214 			// open existing substorage
215 			XStorage xTargetSubStorage = m_aTestHelper.openSubStorage( xTargetStorage,
216 																		"SubStorage1",
217 																		ElementModes.WRITE );
218 			if ( xTargetSubStorage == null )
219 			{
220 				m_aTestHelper.Error( "Can't open existing substorage!" );
221 				return false;
222 			}
223 
224 			if ( !m_aTestHelper.checkStorageProperties( xTargetSubStorage, "MediaType4", false, ElementModes.WRITE ) )
225 				return false;
226 
227 			// set the common storage password
228 			XEncryptionProtectedSource xTargetEncr = (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTargetStorage );
229 			if ( xTargetEncr == null )
230 			{
231 				m_aTestHelper.Error( "The storage does not support encryption access!" );
232 				return false;
233 			}
234 			try
235 			{
236 				xTargetEncr.setEncryptionPassword( sPass );
237 			}
238 			catch( Exception e )
239 			{
240 				m_aTestHelper.Error( "Can not set the common storage password!" );
241 				return false;
242 			}
243 
244 			// check the streams
245 			if ( !m_aTestHelper.checkStream( xTargetStorage, "SubStream1", "MediaType1", true, pBytes ) )
246 				return false;
247 			if ( !m_aTestHelper.checkStream( xTargetSubStorage, "SubStream2", "MediaType2", true, pBytes ) )
248 				return false;
249 
250 
251 			// dispose used storages to free resources
252 			if ( !m_aTestHelper.disposeStorage( xTargetStorage ) )
253 				return false;
254 
255 			return true;
256 		}
257 		catch( Exception e )
258 		{
259 			m_aTestHelper.Error( "Exception: " + e );
260 			return false;
261 		}
262     }
263 }
264 
265