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