xref: /aoo41x/main/package/qa/storages/Test15.java (revision cdf0e10c)
1 package complex.storages;
2 
3 import com.sun.star.uno.XInterface;
4 import com.sun.star.lang.XMultiServiceFactory;
5 import com.sun.star.lang.XSingleServiceFactory;
6 
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.uno.UnoRuntime;
9 import com.sun.star.uno.XInterface;
10 
11 import com.sun.star.embed.*;
12 
13 import share.LogWriter;
14 import complex.storages.TestHelper;
15 import complex.storages.StorageTest;
16 
17 public class Test15 implements StorageTest {
18 
19 	XMultiServiceFactory m_xMSF;
20 	XSingleServiceFactory m_xStorageFactory;
21 	TestHelper m_aTestHelper;
22 
23 	public Test15( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )
24 	{
25 		m_xMSF = xMSF;
26 		m_xStorageFactory = xStorageFactory;
27 		m_aTestHelper = new TestHelper( aLogWriter, "Test15: " );
28 	}
29 
30     public boolean test()
31 	{
32 		String aStreamPrefix = "";
33 		for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd )
34 			if ( !testForPath( aStreamPrefix ) )
35 				return false;
36 
37 		return true;
38 	}
39 
40 	public boolean testForPath( String aStreamPrefix )
41 	{
42 		try
43 		{
44 			String aSubStream1Path = aStreamPrefix + "SubStream1";
45 			String aSubStream2Path = aStreamPrefix + "SubStream2";
46 			String aSubStream3Path = aStreamPrefix + "SubStream3";
47 			String aSubStream4Path = aStreamPrefix + "SubStream4";
48 
49 			String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
50 			if ( sTempFileURL == null || sTempFileURL == "" )
51 			{
52 				m_aTestHelper.Error( "No valid temporary file was created!" );
53 				return false;
54 			}
55 
56 			// create temporary storage based on a previously created temporary file
57 			Object pArgs[] = new Object[2];
58 			pArgs[0] = (Object) sTempFileURL;
59 			pArgs[1] = new Integer( ElementModes.WRITE );
60 
61 			Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
62 			XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
63 			if ( xTempFileStorage == null )
64 			{
65 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
66 				return false;
67 			}
68 
69 			// set the global password for the root storage
70 			XEncryptionProtectedSource xTempStorageEncryption =
71 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
72 
73 			if ( xTempStorageEncryption == null )
74 			{
75 				m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
76 				return true;
77 			}
78 
79 			String sPass1 = "12345";
80 			String sPass2 = "54321";
81 
82 			try {
83 				xTempStorageEncryption.setEncryptionPassword( sPass1 );
84 			}
85 			catch( Exception e )
86 			{
87 				m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
88 				return false;
89 			}
90 
91 
92 			byte pBytes1[] = { 1, 1, 1, 1, 1 };
93 			byte pBytes2[] = { 2, 2, 2, 2, 2 };
94 
95 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
96 			// and commit
97 			if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true, true ) )
98 				return false;
99 
100 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
101 			// and commit
102 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) )
103 				return false;
104 
105 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
106 			// and commit
107 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", false, pBytes2, sPass2, true ) )
108 				return false;
109 
110 			// open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
111 			// and dont commit
112 			if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream4Path, "MediaType2", true, pBytes1, true, false ) )
113 				return false;
114 
115 
116 			// set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
117 			if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage,
118 															"MediaType3",
119 															true,
120 															ElementModes.WRITE ) )
121 				return false;
122 
123 			// commit the root storage so the contents must be stored now
124 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
125 				return false;
126 
127 			// dispose used storages to free resources
128 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
129 				return false;
130 
131 			// ================================================
132 			// now reopen the storage,
133 			// check all the written and copied information
134 			// and change it
135 			// ================================================
136 
137 			// the temporary file must not be locked any more after storage disposing
138 			oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
139 			xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
140 			if ( xTempFileStorage == null )
141 			{
142 				m_aTestHelper.Error( "Can't create storage based on temporary file!" );
143 				return false;
144 			}
145 
146 			// set the global password for the root storage
147 			xTempStorageEncryption =
148 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage );
149 
150 			if ( xTempStorageEncryption == null )
151 			{
152 				m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
153 				return false;
154 			}
155 
156 			try {
157 				xTempStorageEncryption.setEncryptionPassword( sPass2 );
158 			}
159 			catch( Exception e )
160 			{
161 				m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
162 				return false;
163 			}
164 
165 
166 			if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) )
167 				return false;
168 
169 			if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) )
170 				return false;
171 
172 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", true, pBytes2 ) )
173 				return false;
174 
175 			if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", true, pBytes2 ) )
176 				return false;
177 
178 			if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream4Path, ElementModes.READ, sPass1 ) )
179 				return false;
180 
181 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
182 			// and commit
183 			if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType4", true, pBytes2, sPass1, true ) )
184 				return false;
185 
186 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
187 			// and don't commit
188 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType5", true, pBytes1, true ) )
189 				return false;
190 
191 			// change the password of the existing stream
192 			if ( m_aTestHelper.ChangeStreamPassH( xTempFileStorage, aSubStream2Path, sPass2, sPass1, true ) != 1 )
193 				return false;
194 
195 			// open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes
196 			// and don't commit
197 			if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType5", true, pBytes1, false ) )
198 				return false;
199 
200 			// commit the root storage so the contents must be stored now
201 			if ( !m_aTestHelper.commitStorage( xTempFileStorage ) )
202 				return false;
203 
204 			// dispose used storages to free resources
205 			if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) )
206 				return false;
207 
208 			// ================================================
209 			// now reopen the storage,
210 			// check all the written information
211 			// ================================================
212 
213 			// the temporary file must not be locked any more after storage disposing
214 			pArgs[1] = new Integer( ElementModes.READ );
215 			Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
216 			XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
217 			if ( xResultStorage == null )
218 			{
219 				m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
220 				return false;
221 			}
222 
223 			// set the global password for the root storage
224 			xTempStorageEncryption =
225 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
226 
227 			if ( xTempStorageEncryption == null )
228 			{
229 				m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" );
230 				return false;
231 			}
232 
233 			try {
234 				xTempStorageEncryption.setEncryptionPassword( sPass1 );
235 			}
236 			catch( Exception e )
237 			{
238 				m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
239 				return false;
240 			}
241 
242 			if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) )
243 				return false;
244 
245 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType4", true, pBytes2 ) )
246 				return false;
247 
248 			if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream2Path, "MediaType5", true, pBytes1 ) )
249 				return false;
250 
251 			if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream3Path, "MediaType3", pBytes2, sPass2 ) )
252 				return false;
253 
254 			// dispose used storages to free resources
255 			if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
256 				return false;
257 
258 			return true;
259 		}
260 		catch( Exception e )
261 		{
262 			m_aTestHelper.Error( "Exception: " + e );
263 			return false;
264 		}
265     }
266 
267 }
268 
269