2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex
.storages
;
21 import com
.sun
.star
.uno
.XInterface
;
22 import com
.sun
.star
.lang
.XMultiServiceFactory
;
23 import com
.sun
.star
.lang
.XSingleServiceFactory
;
25 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
26 import com
.sun
.star
.uno
.UnoRuntime
;
27 import com
.sun
.star
.uno
.XInterface
;
29 import com
.sun
.star
.embed
.*;
31 import share
.LogWriter
;
32 import complex
.storages
.TestHelper
;
33 import complex
.storages
.StorageTest
;
35 public class Test08
implements StorageTest
{
37 XMultiServiceFactory m_xMSF
;
38 XSingleServiceFactory m_xStorageFactory
;
39 TestHelper m_aTestHelper
;
41 public Test08( XMultiServiceFactory xMSF
, XSingleServiceFactory xStorageFactory
, LogWriter aLogWriter
)
44 m_xStorageFactory
= xStorageFactory
;
45 m_aTestHelper
= new TestHelper( aLogWriter
, "Test08: " );
53 // create temporary storage based on arbitrary medium
54 // after such a storage is closed it is lost
55 Object oTempStorage
= m_xStorageFactory
.createInstance();
56 XStorage xTempStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
57 if ( xTempStorage
== null )
59 m_aTestHelper
.Error( "Can't create temporary storage representation!" );
63 // set the global password for the root storage
64 XEncryptionProtectedSource xTempStorageEncryption
=
65 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xTempStorage
);
67 if ( xTempStorageEncryption
== null )
69 m_aTestHelper
.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
73 String sPass1
= "123";
74 String sPass2
= "321";
77 xTempStorageEncryption
.setEncryptionPassword( sPass1
);
81 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
85 // open a new substorage
86 XStorage xTempSubStorage
= m_aTestHelper
.openSubStorage( xTempStorage
,
89 if ( xTempSubStorage
== null )
91 m_aTestHelper
.Error( "Can't create substorage!" );
95 byte pBigBytes
[] = new byte[33000];
96 for ( int nInd
= 0; nInd
< 33000; nInd
++ )
97 pBigBytes
[nInd
] = (byte)( nInd
% 128 );
99 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
100 // the stream will be encrypted with common password
101 byte pBytes1
[] = { 1, 1, 1, 1, 1 };
102 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream1", "MediaType1", true, pBytes1
, true ) )
104 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "BigSubStream1", "MediaType1", true, pBigBytes
, true ) )
107 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
108 // the stream will not be encrypted
109 byte pBytes2
[] = { 2, 2, 2, 2, 2 };
110 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "SubStream2", "MediaType2", false, pBytes2
, false ) )
112 if ( !m_aTestHelper
.WBToSubstrOfEncr( xTempSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
, false ) )
115 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
116 // the stream will be compressed with own password
117 byte pBytes3
[] = { 3, 3, 3, 3, 3 };
119 // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes
120 // the stream will not be encrypted
121 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "SubStream3", "MediaType3", false, pBytes3
, sPass2
) )
123 if ( !m_aTestHelper
.WriteBytesToEncrSubstream( xTempSubStorage
, "BigSubStream3", "MediaType3", false, pBigBytes
, sPass2
) )
126 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
127 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempStorage
,
130 ElementModes
.WRITE
) )
133 // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly
134 if ( !m_aTestHelper
.setStorageTypeAndCheckProps( xTempSubStorage
,
137 ElementModes
.WRITE
) )
140 // create temporary file
141 String sTempFileURL
= m_aTestHelper
.CreateTempFile( m_xMSF
);
142 if ( sTempFileURL
== null || sTempFileURL
== "" )
144 m_aTestHelper
.Error( "No valid temporary file was created!" );
148 // create temporary storage based on a previously created temporary file
149 Object pArgs
[] = new Object
[2];
150 pArgs
[0] = (Object
) sTempFileURL
;
151 pArgs
[1] = Integer
.valueOf( ElementModes
.WRITE
);
153 Object oTempFileStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
154 XStorage xTempFileStorage
= (XStorage
)UnoRuntime
.queryInterface( XStorage
.class, oTempFileStorage
);
155 if ( xTempFileStorage
== null )
157 m_aTestHelper
.Error( "Can't create storage based on temporary file!" );
161 // copy xTempStorage to xTempFileStorage
162 // xTempFileStorage will be automatically committed
163 if ( !m_aTestHelper
.copyStorage( xTempStorage
, xTempFileStorage
) )
166 // dispose used storages to free resources
167 if ( !m_aTestHelper
.disposeStorage( xTempStorage
) || !m_aTestHelper
.disposeStorage( xTempFileStorage
) )
171 // now check all the written and copied information
174 // the temporary file must not be locked any more after storage disposing
175 pArgs
[1] = Integer
.valueOf( ElementModes
.READ
);
176 Object oResultStorage
= m_xStorageFactory
.createInstanceWithArguments( pArgs
);
177 XStorage xResultStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oResultStorage
);
178 if ( xResultStorage
== null )
180 m_aTestHelper
.Error( "Can't reopen storage based on temporary file!" );
184 if ( !m_aTestHelper
.checkStorageProperties( xResultStorage
, "MediaType4", true, ElementModes
.READ
) )
187 // open existing substorage
188 XStorage xResultSubStorage
= m_aTestHelper
.openSubStorage( xResultStorage
,
191 if ( xResultSubStorage
== null )
193 m_aTestHelper
.Error( "Can't open existing substorage!" );
197 if ( !m_aTestHelper
.checkStorageProperties( xResultSubStorage
, "MediaType5", false, ElementModes
.READ
) )
200 // set the global password for the root storage
201 XEncryptionProtectedSource xResultStorageEncryption
=
202 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xResultStorage
);
204 if ( xResultStorageEncryption
== null )
206 m_aTestHelper
.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" );
211 xResultStorageEncryption
.setEncryptionPassword( sPass2
);
215 m_aTestHelper
.Error( "Can't set a common encryption key for the storage, exception:" + e
);
219 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "SubStream1", "MediaType1", pBytes1
, sPass1
) )
221 if ( !m_aTestHelper
.checkEncrStream( xResultSubStorage
, "BigSubStream1", "MediaType1", pBigBytes
, sPass1
) )
224 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream2", "MediaType2", false, pBytes2
) )
226 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream2", "MediaType2", false, pBigBytes
) )
229 // the common root storage password should allow to open this stream
230 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "SubStream3", "MediaType3", true, pBytes3
) )
232 if ( !m_aTestHelper
.checkStream( xResultSubStorage
, "BigSubStream3", "MediaType3", true, pBigBytes
) )
235 // dispose used storages to free resources
236 if ( !m_aTestHelper
.disposeStorage( xResultStorage
) )
243 m_aTestHelper
.Error( "Exception: " + e
);