xref: /trunk/main/odk/examples/java/Storage/Test08.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1ae15d43aSAndrew Rist /**************************************************************
2ae15d43aSAndrew Rist  *
3ae15d43aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ae15d43aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ae15d43aSAndrew Rist  * distributed with this work for additional information
6ae15d43aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ae15d43aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ae15d43aSAndrew Rist  * "License"); you may not use this file except in compliance
9ae15d43aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ae15d43aSAndrew Rist  *
11ae15d43aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ae15d43aSAndrew Rist  *
13ae15d43aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ae15d43aSAndrew Rist  * software distributed under the License is distributed on an
15ae15d43aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ae15d43aSAndrew Rist  * KIND, either express or implied.  See the License for the
17ae15d43aSAndrew Rist  * specific language governing permissions and limitations
18ae15d43aSAndrew Rist  * under the License.
19ae15d43aSAndrew Rist  *
20ae15d43aSAndrew Rist  *************************************************************/
21ae15d43aSAndrew Rist 
22cdf0e10cSrcweir package storagetesting;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.XInterface;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.uno.XInterface;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.embed.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import storagetesting.TestHelper;
35cdf0e10cSrcweir import storagetesting.StorageTest;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir public class Test08 implements StorageTest {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     XMultiServiceFactory m_xMSF;
40cdf0e10cSrcweir     XSingleServiceFactory m_xStorageFactory;
41cdf0e10cSrcweir     TestHelper m_aTestHelper;
42cdf0e10cSrcweir 
Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )43cdf0e10cSrcweir     public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory )
44cdf0e10cSrcweir     {
45cdf0e10cSrcweir         m_xMSF = xMSF;
46cdf0e10cSrcweir         m_xStorageFactory = xStorageFactory;
47cdf0e10cSrcweir         m_aTestHelper = new TestHelper( "Test08: " );
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
test()50cdf0e10cSrcweir     public boolean test()
51cdf0e10cSrcweir     {
52cdf0e10cSrcweir         try
53cdf0e10cSrcweir         {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir             // create temporary storage based on arbitrary medium
56cdf0e10cSrcweir             // after such a storage is closed it is lost
57cdf0e10cSrcweir             Object oTempStorage = m_xStorageFactory.createInstance();
58cdf0e10cSrcweir             XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
59cdf0e10cSrcweir             if ( xTempStorage == null )
60cdf0e10cSrcweir             {
61cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create temporary storage representation!" );
62cdf0e10cSrcweir                 return false;
63cdf0e10cSrcweir             }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir             // set the global password for the root storage
66cdf0e10cSrcweir             XEncryptionProtectedSource xTempStorageEncryption =
67cdf0e10cSrcweir                 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             if ( xTempStorageEncryption == null )
70cdf0e10cSrcweir             {
71cdf0e10cSrcweir                 m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
72cdf0e10cSrcweir                 return true;
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir             byte pPass1[] = { 1, 2, 3 };
76cdf0e10cSrcweir             byte pPass2[] = { 3, 2, 1 };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             try {
79cdf0e10cSrcweir                 xTempStorageEncryption.setEncryptionKey( pPass1 );
80cdf0e10cSrcweir             }
81cdf0e10cSrcweir             catch( Exception e )
82cdf0e10cSrcweir             {
83cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
84cdf0e10cSrcweir                 return false;
85cdf0e10cSrcweir             }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             // open a new substorage
88cdf0e10cSrcweir             XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage,
89cdf0e10cSrcweir                                                                         "SubStorage1",
90cdf0e10cSrcweir                                                                         ElementModes.ELEMENT_WRITE );
91cdf0e10cSrcweir             if ( xTempSubStorage == null )
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create substorage!" );
94cdf0e10cSrcweir                 return false;
95cdf0e10cSrcweir             }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
98cdf0e10cSrcweir             // the stream will be encrypted with common password
99cdf0e10cSrcweir             byte pBytes1[] = { 1, 1, 1, 1, 1 };
100cdf0e10cSrcweir             if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) )
101cdf0e10cSrcweir                 return false;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
104cdf0e10cSrcweir             // the stream will not be encrypted
105cdf0e10cSrcweir             byte pBytes2[] = { 2, 2, 2, 2, 2 };
106cdf0e10cSrcweir             if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) )
107cdf0e10cSrcweir                 return false;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
110cdf0e10cSrcweir             // the stream will be compressed with own password
111cdf0e10cSrcweir             byte pBytes3[] = { 3, 3, 3, 3, 3 };
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
114cdf0e10cSrcweir             // the stream will not be encrypted
115cdf0e10cSrcweir             if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, pPass2 ) )
116cdf0e10cSrcweir                 return false;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
119cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage,
120cdf0e10cSrcweir                                                             "MediaType4",
121cdf0e10cSrcweir                                                             true,
122cdf0e10cSrcweir                                                             ElementModes.ELEMENT_READWRITE ) )
123cdf0e10cSrcweir                 return false;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir             // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
126cdf0e10cSrcweir             if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage,
127cdf0e10cSrcweir                                                             "MediaType5",
128cdf0e10cSrcweir                                                             false,
129cdf0e10cSrcweir                                                             ElementModes.ELEMENT_WRITE ) )
130cdf0e10cSrcweir                 return false;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             // create temporary file
133cdf0e10cSrcweir             String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF );
134cdf0e10cSrcweir             if ( sTempFileURL == null || sTempFileURL == "" )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 m_aTestHelper.Error( "No valid temporary file was created!" );
137cdf0e10cSrcweir                 return false;
138cdf0e10cSrcweir             }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             // create temporary storage based on a previously created temporary file
141cdf0e10cSrcweir             Object pArgs[] = new Object[2];
142cdf0e10cSrcweir             pArgs[0] = (Object) sTempFileURL;
143cdf0e10cSrcweir             pArgs[1] = new Integer( ElementModes.ELEMENT_WRITE );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
146cdf0e10cSrcweir             XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage );
147cdf0e10cSrcweir             if ( xTempFileStorage == null )
148cdf0e10cSrcweir             {
149cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't create storage based on temporary file!" );
150cdf0e10cSrcweir                 return false;
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // copy xTempStorage to xTempFileStorage
154*a893be29SPedro Giffuni             // xTempFileStorage will be automatically committed
155cdf0e10cSrcweir             if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) )
156cdf0e10cSrcweir                 return false;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             // dispose used storages to free resources
159cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) )
160cdf0e10cSrcweir                 return false;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir             // ================================================
163cdf0e10cSrcweir             // now check all the written and copied information
164cdf0e10cSrcweir             // ================================================
165cdf0e10cSrcweir 
166cdf0e10cSrcweir             // the temporary file must not be locked any more after storage disposing
167cdf0e10cSrcweir             pArgs[1] = new Integer( ElementModes.ELEMENT_READ );
168cdf0e10cSrcweir             Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs );
169cdf0e10cSrcweir             XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage );
170cdf0e10cSrcweir             if ( xResultStorage == null )
171cdf0e10cSrcweir             {
172cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't reopen storage based on temporary file!" );
173cdf0e10cSrcweir                 return false;
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.ELEMENT_READ ) )
177cdf0e10cSrcweir                 return false;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             // open existing substorage
180cdf0e10cSrcweir             XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage,
181cdf0e10cSrcweir                                                                         "SubStorage1",
182cdf0e10cSrcweir                                                                         ElementModes.ELEMENT_READ );
183cdf0e10cSrcweir             if ( xResultSubStorage == null )
184cdf0e10cSrcweir             {
185cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't open existing substorage!" );
186cdf0e10cSrcweir                 return false;
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir             if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.ELEMENT_READ ) )
190cdf0e10cSrcweir                 return false;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             // set the global password for the root storage
193cdf0e10cSrcweir             XEncryptionProtectedSource xResultStorageEncryption =
194cdf0e10cSrcweir                 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage );
195cdf0e10cSrcweir 
196cdf0e10cSrcweir             if ( xResultStorageEncryption == null )
197cdf0e10cSrcweir             {
198cdf0e10cSrcweir                 m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
199cdf0e10cSrcweir                 return false;
200cdf0e10cSrcweir             }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir             try {
203cdf0e10cSrcweir                 xResultStorageEncryption.setEncryptionKey( pPass2 );
204cdf0e10cSrcweir             }
205cdf0e10cSrcweir             catch( Exception e )
206cdf0e10cSrcweir             {
207cdf0e10cSrcweir                 m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e );
208cdf0e10cSrcweir                 return false;
209cdf0e10cSrcweir             }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, pPass1 ) )
212cdf0e10cSrcweir                 return false;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", pBytes2 ) )
215cdf0e10cSrcweir                 return false;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir             // the common root storage password should allow to open this stream
218cdf0e10cSrcweir             if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", pBytes3 ) )
219cdf0e10cSrcweir                 return false;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             // dispose used storages to free resources
222cdf0e10cSrcweir             if ( !m_aTestHelper.disposeStorage( xResultStorage ) )
223cdf0e10cSrcweir                 return false;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             return true;
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir         catch( Exception e )
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir             m_aTestHelper.Error( "Exception: " + e );
230cdf0e10cSrcweir             return false;
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir }
234