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