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
.UnoRuntime
;
22 import com
.sun
.star
.uno
.XInterface
;
23 import com
.sun
.star
.uno
.AnyConverter
;
25 import com
.sun
.star
.lang
.*;
26 import com
.sun
.star
.embed
.*;
27 import com
.sun
.star
.packages
.*;
28 import com
.sun
.star
.io
.*;
29 import com
.sun
.star
.beans
.*;
31 import share
.LogWriter
;
33 public class TestHelper
{
35 LogWriter m_aLogWriter
;
38 public TestHelper( LogWriter aLogWriter
, String sTestPrefix
)
40 m_aLogWriter
= aLogWriter
;
41 m_sTestPrefix
= sTestPrefix
;
44 public boolean WriteBytesToStream( XStream xStream
,
50 // get output stream of substream
51 XOutputStream xOutput
= xStream
.getOutputStream();
52 if ( xOutput
== null )
54 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
58 // get XTruncate implementation from output stream
59 XTruncate xTruncate
= (XTruncate
) UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
60 if ( xTruncate
== null )
62 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
66 // write requested byte sequence
70 xOutput
.writeBytes( pBytes
);
74 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
78 // get access to the XPropertySet interface
79 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
80 if ( xPropSet
== null )
82 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
86 // set properties to the stream
89 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
90 xPropSet
.setPropertyValue( "Compressed", Boolean
.valueOf( bCompressed
) );
94 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
98 // check size property of the stream
101 long nSize
= AnyConverter
.toLong( xPropSet
.getPropertyValue( "Size" ) );
102 if ( nSize
!= pBytes
.length
)
104 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
110 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
117 public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage
,
122 // open substream element
123 XStream xSubStream
= null;
126 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
127 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
128 if ( xSubStream
== null )
130 Error( "Can't create substream '" + sStreamName
+ "'!" );
136 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
140 // get output stream of substream
141 XOutputStream xOutput
= xSubStream
.getOutputStream();
142 if ( xOutput
== null )
144 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
148 // get XTruncate implementation from output stream
149 XTruncate xTruncate
= (XTruncate
) UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
150 if ( xTruncate
== null )
152 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
156 // write requested byte sequence
159 xTruncate
.truncate();
160 xOutput
.writeBytes( pBytes
);
164 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
168 // get access to the XPropertySet interface
169 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
170 if ( xPropSet
== null )
172 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
176 // set properties to the stream
177 // do not set the compressed property
180 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
184 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
188 // check size property of the stream
191 long nSize
= AnyConverter
.toLong( xPropSet
.getPropertyValue( "Size" ) );
192 if ( nSize
!= pBytes
.length
)
194 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
200 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
204 // free the stream resources, garbage collector may remove the object too late
205 if ( !disposeStream( xSubStream
, sStreamName
) )
211 public boolean WriteBytesToSubstream( XStorage xStorage
,
217 // open substream element
218 XStream xSubStream
= null;
221 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
222 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
223 if ( xSubStream
== null )
225 Error( "Can't create substream '" + sStreamName
+ "'!" );
231 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
235 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
238 // free the stream resources, garbage collector may remove the object too late
239 if ( !disposeStream( xSubStream
, sStreamName
) )
245 public boolean WriteBytesToEncrSubstream( XStorage xStorage
,
252 // open substream element
253 XStream xSubStream
= null;
256 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sPass
);
257 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
258 if ( xSubStream
== null )
260 Error( "Can't create substream '" + sStreamName
+ "'!" );
266 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
270 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
273 // free the stream resources, garbage collector may remove the object too late
274 if ( !disposeStream( xSubStream
, sStreamName
) )
280 public boolean WBToSubstrOfEncr( XStorage xStorage
,
287 // open substream element
288 XStream xSubStream
= null;
291 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
292 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
293 if ( xSubStream
== null )
295 Error( "Can't create substream '" + sStreamName
+ "'!" );
301 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
305 // get access to the XPropertySet interface
306 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
307 if ( xPropSet
== null )
309 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
313 // set properties to the stream
316 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", Boolean
.valueOf( bEncrypted
) );
320 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName
+ "', exception: " + e
);
324 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
327 // free the stream resources, garbage collector may remove the object too late
328 if ( !disposeStream( xSubStream
, sStreamName
) )
334 public boolean WriteBytesToStreamH( XStorage xStorage
,
341 // open substream element
342 XStream xSubStream
= null;
345 XHierarchicalStorageAccess xHStorage
=
346 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
347 if ( xHStorage
== null )
349 Error( "The storage does not support hierarchical access!" );
353 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
354 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
355 if ( xSubStream
== null )
357 Error( "Can't create substream '" + sStreamPath
+ "'!" );
363 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
367 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
370 XTransactedObject xTransact
=
371 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
372 if ( xTransact
== null )
374 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
382 } catch( Exception e
)
384 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
389 // free the stream resources, garbage collector may remove the object too late
390 if ( !disposeStream( xSubStream
, sStreamPath
) )
396 public boolean WriteBytesToEncrStreamH( XStorage xStorage
,
404 // open substream element
405 XStream xSubStream
= null;
408 XHierarchicalStorageAccess xHStorage
=
409 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
410 if ( xHStorage
== null )
412 Error( "The storage does not support hierarchical access!" );
416 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sStreamPath
,
419 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
420 if ( xSubStream
== null )
422 Error( "Can't create substream '" + sStreamPath
+ "'!" );
428 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
432 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
435 XTransactedObject xTransact
=
436 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
437 if ( xTransact
== null )
439 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
447 } catch( Exception e
)
449 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
454 // free the stream resources, garbage collector may remove the object too late
455 if ( !disposeStream( xSubStream
, sStreamPath
) )
461 public boolean WBToSubstrOfEncrH( XStorage xStorage
,
469 // open substream element
470 XStream xSubStream
= null;
473 XHierarchicalStorageAccess xHStorage
=
474 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
475 if ( xHStorage
== null )
477 Error( "The storage does not support hierarchical access!" );
481 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
482 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
483 if ( xSubStream
== null )
485 Error( "Can't create substream '" + sStreamPath
+ "'!" );
491 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
495 // get access to the XPropertySet interface
496 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
497 if ( xPropSet
== null )
499 Error( "Can't get XPropertySet implementation from substream '" + sStreamPath
+ "'!" );
503 // set properties to the stream
506 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", Boolean
.valueOf( bEncrypted
) );
510 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath
+ "', exception: " + e
);
514 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
517 XTransactedObject xTransact
=
518 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
519 if ( xTransact
== null )
521 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
529 } catch( Exception e
)
531 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
536 // free the stream resources, garbage collector may remove the object too late
537 if ( !disposeStream( xSubStream
, sStreamPath
) )
543 public int ChangeStreamPass( XStorage xStorage
,
548 // open substream element
549 XStream xSubStream
= null;
552 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sOldPass
);
553 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
554 if ( xSubStream
== null )
556 Error( "Can't open substream '" + sStreamName
+ "'!" );
562 Error( "Can't open substream '" + sStreamName
+ "', exception : " + e
+ "!" );
567 // change the password for the stream
568 XEncryptionProtectedSource xStreamEncryption
=
569 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
571 if ( xStreamEncryption
== null )
573 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
578 xStreamEncryption
.setEncryptionPassword( sNewPass
);
582 Error( "Can't change encryption key of the substream '" + sStreamName
+ "', exception:" + e
);
586 // free the stream resources, garbage collector may remove the object too late
587 if ( !disposeStream( xSubStream
, sStreamName
) )
593 public int ChangeStreamPassH( XStorage xStorage
,
599 // open substream element
600 XHierarchicalStorageAccess xHStorage
=
601 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
602 if ( xHStorage
== null )
604 Error( "The storage does not support hierarchical access!" );
608 XStream xSubStream
= null;
611 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.WRITE
, sOldPass
);
612 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
613 if ( xSubStream
== null )
615 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
621 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
625 // change the password for the stream
626 XEncryptionProtectedSource xStreamEncryption
=
627 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
629 if ( xStreamEncryption
== null )
631 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
636 xStreamEncryption
.setEncryptionPassword( sNewPass
);
640 Error( "Can't change encryption key of the substream '" + sPath
+ "', exception:" + e
);
644 XTransactedObject xTransact
=
645 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
646 if ( xTransact
== null )
648 Error( "Substream '" + sPath
+ "', stream opened for writing must be transacted!" );
656 } catch( Exception e
)
658 Error( "Can't commit storage after substream '" + sPath
+ "' change, exception : " + e
+ "!" );
663 // free the stream resources, garbage collector may remove the object too late
664 if ( !disposeStream( xSubStream
, sPath
) )
670 public boolean setStorageTypeAndCheckProps( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
674 // get access to the XPropertySet interface
675 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
676 if ( xPropSet
!= null )
680 // set "MediaType" property to the stream
681 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
683 // get "IsRoot" and "OpenMode" properties and control there values
684 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
685 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
688 if ( bPropIsRoot
!= bIsRoot
)
690 Error( "'IsRoot' property contains wrong value!" );
695 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
696 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
698 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
704 Error( "Can't control properties of substorage, exception: " + e
);
709 Error( "Can't get XPropertySet implementation from storage!" );
715 public boolean checkStorageProperties( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
719 // get access to the XPropertySet interface
720 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
721 if ( xPropSet
!= null )
725 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
726 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
727 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
728 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
731 if ( !sPropMediaType
.equals( sMediaType
) )
733 Error( "'MediaType' property contains wrong value, expected '"
734 + sMediaType
+ "', set '" + sPropMediaType
+ "' !" );
738 if ( bPropIsRoot
!= bIsRoot
)
740 Error( "'IsRoot' property contains wrong value!" );
745 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
746 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
748 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
754 Error( "Can't get properties of substorage, exception: " + e
);
759 Error( "Can't get XPropertySet implementation from storage!" );
765 public boolean InternalCheckStream( XStream xStream
,
770 boolean bCheckCompressed
)
772 // get input stream of substream
773 XInputStream xInput
= xStream
.getInputStream();
774 if ( xInput
== null )
776 Error( "Can't get XInputStream implementation from substream '" + sName
+ "'!" );
780 byte pContents
[][] = new byte[1][]; // ???
785 xInput
.readBytes( pContents
, pBytes
.length
+ 1 );
789 Error( "Can't read from stream '" + sName
+ "', exception: " + e
);
793 // check size of stream data
794 if ( pContents
.length
== 0 )
796 Error( "SubStream '" + sName
+ "' reading produced disaster!" );
800 if ( pBytes
.length
!= pContents
[0].length
)
802 Error( "SubStream '" + sName
+ "' contains wrong amount of data! (" + pContents
[0].length
+ "/" + pBytes
.length
+ ")" );
807 for ( int ind
= 0; ind
< pBytes
.length
; ind
++ )
809 if ( pBytes
[ind
] != pContents
[0][ind
] )
811 Error( "SubStream '" + sName
+ "' contains wrong data! ( byte num. "
812 + ind
+ " should be " + pBytes
[ind
] + " but it is " + pContents
[0][ind
] + ")" );
820 // get access to the XPropertySet interface
821 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
822 if ( xPropSet
!= null )
826 // get "MediaType" and "Size" properties and control there values
827 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
828 long nPropSize
= AnyConverter
.toLong( xPropSet
.getPropertyValue( "Size" ) );
829 boolean bPropCompress
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "Compressed" ) );
832 if ( !sPropMediaType
.equals( sMediaType
) )
834 Error( "'MediaType' property contains wrong value for stream '" + sName
+ "',\nexpected: '"
835 + sMediaType
+ "', set: '" + sPropMediaType
+ "'!" );
839 if ( nPropSize
!= pBytes
.length
)
841 Error( "'Size' property contains wrong value for stream'" + sName
+ "'!" );
845 if ( bCheckCompressed
&& bPropCompress
!= bCompressed
)
847 Error( "'Compressed' property contains wrong value for stream'" + sName
+ "'!" );
853 Error( "Can't get properties of substream '" + sName
+ "', exception: " + e
);
858 Error( "Can't get XPropertySet implementation from stream '" + sName
+ "'!" );
864 public boolean checkStream( XStorage xParentStorage
,
870 // open substream element first
871 XStream xSubStream
= null;
874 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
875 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
876 if ( xSubStream
== null )
878 Error( "Can't open substream '" + sName
+ "'!" );
884 Error( "Can't open substream '" + sName
+ "', exception : " + e
+ "!" );
888 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, bCompressed
, pBytes
, true );
890 // free the stream resources, garbage collector may remove the object too late
891 if ( !disposeStream( xSubStream
, sName
) )
897 public boolean checkEncrStream( XStorage xParentStorage
,
903 // Important: a common password for any of parent storage should not be set or
904 // should be different from sPass
908 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
909 Error( "Encrypted stream '" + sName
+ "' was opened without password!" );
912 catch( WrongPasswordException wpe
)
916 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' without password: " + e
+ "!" );
920 String sWrongPass
= "11";
924 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sWrongPass
);
925 Error( "Encrypted stream '" + sName
+ "' was opened with wrong password!" );
928 catch( WrongPasswordException wpe
)
932 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' with wrong password: " + e
+ "!" );
936 XStream xSubStream
= null;
939 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sPass
);
940 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
941 if ( xSubStream
== null )
943 Error( "Can't open encrypted substream '" + sName
+ "'!" );
949 Error( "Can't open encrypted substream '" + sName
+ "', exception : " + e
+ "!" );
953 // encrypted streams will be compressed always, so after the storing this property is always true,
954 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
955 // before the storing )
956 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, true, pBytes
, false );
958 // free the stream resources, garbage collector may remove the object too late
959 if ( !disposeStream( xSubStream
, sName
) )
965 public boolean checkStreamH( XStorage xParentStorage
,
971 // open substream element first
972 XStream xSubStream
= null;
975 XHierarchicalStorageAccess xHStorage
=
976 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
977 if ( xHStorage
== null )
979 Error( "The storage does not support hierarchical access!" );
983 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
984 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
985 if ( xSubStream
== null )
987 Error( "Can't open substream '" + sPath
+ "'!" );
993 Error( "Can't open substream '" + sPath
+ "', exception : " + e
+ "!" );
997 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, bCompressed
, pBytes
, true );
999 // free the stream resources, garbage collector may remove the object too late
1000 if ( !disposeStream( xSubStream
, sPath
) )
1006 public boolean checkEncrStreamH( XStorage xParentStorage
,
1012 // Important: a common password for any of parent storage should not be set or
1013 // should be different from sPass
1014 XHierarchicalStorageAccess xHStorage
=
1015 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
1016 if ( xHStorage
== null )
1018 Error( "The storage does not support hierarchical access!" );
1024 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
1025 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1026 Error( "Encrypted substream '" + sPath
+ "' was opened without password!" );
1029 catch( WrongPasswordException wpe
)
1031 catch( Exception e
)
1033 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' without password: " + e
+ "!" );
1037 String sWrongPass
= "11";
1038 sWrongPass
+= sPass
;
1041 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sWrongPass
);
1042 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1043 Error( "Encrypted substream '" + sPath
+ "' was opened with wrong password!" );
1046 catch( WrongPasswordException wpe
)
1048 catch( Exception e
)
1050 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' with wrong password: " + e
+ "!" );
1054 XStream xSubStream
= null;
1057 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sPass
);
1058 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1059 if ( xSubStream
== null )
1061 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
1065 catch( Exception e
)
1067 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
1071 // encrypted streams will be compressed always, so after the storing this property is always true,
1072 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
1073 // before the storing )
1074 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, true, pBytes
, false );
1076 // free the stream resources, garbage collector may remove the object too late
1077 if ( !disposeStream( xSubStream
, sPath
) )
1083 public boolean copyStorage( XStorage xSourceStorage
, XStorage xDestStorage
)
1085 // copy xSourceStorage to xDestStorage
1088 xSourceStorage
.copyToStorage( xDestStorage
);
1090 catch( Exception e
)
1092 Error( "Storage copying failed, exception: " + e
);
1099 public boolean commitStorage( XStorage xStorage
)
1101 // XTransactedObject must be supported by storages
1102 XTransactedObject xTransact
= (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xStorage
);
1103 if ( xTransact
== null )
1105 Error( "Storage doesn't implement transacted access!" );
1113 catch( Exception e
)
1115 Error( "Storage commit failed, exception:" + e
);
1122 public boolean disposeStream( XStream xStream
, String sStreamName
)
1124 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStream
);
1125 if ( xComponent
== null )
1127 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
1133 xComponent
.dispose();
1135 catch( Exception e
)
1137 Error( "Substream '" + sStreamName
+ "' disposing throws exception: " + e
);
1144 public boolean disposeStorage( XStorage xStorage
)
1146 // dispose the storage
1147 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStorage
);
1148 if ( xComponent
== null )
1150 Error( "Can't retrieve XComponent implementation from storage!" );
1156 xComponent
.dispose();
1158 catch( Exception e
)
1160 Error( "Storage disposing failed!" );
1167 public XInputStream
getInputStream( XStream xStream
)
1169 XInputStream xInTemp
= null;
1172 xInTemp
= xStream
.getInputStream();
1173 if ( xInTemp
== null )
1174 Error( "Can't get the input part of a stream!" );
1176 catch ( Exception e
)
1178 Error( "Can't get the input part of a stream, exception :" + e
);
1184 public boolean closeOutput( XStream xStream
)
1186 XOutputStream xOutTemp
= null;
1189 xOutTemp
= xStream
.getOutputStream();
1190 if ( xOutTemp
== null )
1192 Error( "Can't get the output part of a stream!" );
1196 catch ( Exception e
)
1198 Error( "Can't get the output part of a stream, exception :" + e
);
1204 xOutTemp
.closeOutput();
1206 catch ( Exception e
)
1208 Error( "Can't close output part of a stream, exception :" + e
);
1215 public XStorage
openSubStorage( XStorage xStorage
, String sName
, int nMode
)
1217 // open existing substorage
1220 Object oSubStorage
= xStorage
.openStorageElement( sName
, nMode
);
1221 XStorage xSubStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oSubStorage
);
1224 catch( Exception e
)
1226 Error( "Can't open substorage '" + sName
+ "', exception: " + e
);
1232 public XStream
CreateTempFileStream( XMultiServiceFactory xMSF
)
1234 // try to get temporary file representation
1235 XStream xTempFileStream
= null;
1238 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1239 xTempFileStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, oTempFile
);
1241 catch( Exception e
)
1244 if ( xTempFileStream
== null )
1245 Error( "Can't create temporary file!" );
1247 return xTempFileStream
;
1250 public String
CreateTempFile( XMultiServiceFactory xMSF
)
1252 String sResult
= null;
1254 // try to get temporary file representation
1255 XPropertySet xTempFileProps
= null;
1258 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1259 xTempFileProps
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
1261 catch( Exception e
)
1264 if ( xTempFileProps
!= null )
1268 xTempFileProps
.setPropertyValue( "RemoveFile", Boolean
.FALSE
);
1269 sResult
= AnyConverter
.toString( xTempFileProps
.getPropertyValue( "Uri" ) );
1271 catch( Exception e
)
1273 Error( "Can't control TempFile properties, exception: " + e
);
1278 Error( "Can't create temporary file representation!" );
1281 // close temporary file explicitly
1284 XStream xStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, xTempFileProps
);
1285 if ( xStream
!= null )
1287 XOutputStream xOut
= xStream
.getOutputStream();
1291 XInputStream xIn
= xStream
.getInputStream();
1296 Error( "Can't close TempFile!" );
1298 catch( Exception e
)
1300 Error( "Can't close TempFile, exception: " + e
);
1306 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1308 // copy element with name sName from xSource to xDest
1311 xSource
.copyElementTo( sName
, xDest
, sName
);
1313 catch( Exception e
)
1315 Error( "Element copying failed, exception: " + e
);
1322 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
, String sTargetName
)
1324 // copy element with name sName from xSource to xDest
1327 xSource
.copyElementTo( sName
, xDest
, sTargetName
);
1329 catch( Exception e
)
1331 Error( "Element copying failed, exception: " + e
);
1338 public boolean moveElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1340 // move element with name sName from xSource to xDest
1343 xSource
.moveElementTo( sName
, xDest
, sName
);
1345 catch( Exception e
)
1347 Error( "Element moving failed, exception: " + e
);
1354 public boolean renameElement( XStorage xStorage
, String sOldName
, String sNewName
)
1356 // rename element with name sOldName to sNewName
1359 xStorage
.renameElement( sOldName
, sNewName
);
1361 catch( Exception e
)
1363 Error( "Element renaming failed, exception: " + e
);
1370 public boolean removeElement( XStorage xStorage
, String sName
)
1372 // remove element with name sName
1375 xStorage
.removeElement( sName
);
1377 catch( Exception e
)
1379 Error( "Element removing failed, exception: " + e
);
1386 public XStream
OpenStream( XStorage xStorage
,
1390 // open substream element
1391 XStream xSubStream
= null;
1394 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, nMode
);
1395 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1396 if ( xSubStream
== null )
1397 Error( "Can't create substream '" + sStreamName
+ "'!" );
1399 catch( Exception e
)
1401 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
1407 public boolean compareRawMethodsOnEncrStream( XStorage xStorage
, String sStreamName
)
1410 XStorageRawAccess xRawStorage
;
1413 xRawStorage
= (XStorageRawAccess
) UnoRuntime
.queryInterface( XStorageRawAccess
.class, xStorage
);
1415 catch( Exception e
)
1417 Error( "Can't get raw access to the storage, exception : " + e
+ "!" );
1421 if ( xRawStorage
== null )
1423 Error( "Can't get raw access to the storage!" );
1427 XInputStream xHeadRawStream
= null;
1430 xHeadRawStream
= xRawStorage
.getRawEncrStreamElement( sStreamName
);
1432 catch( Exception e
)
1434 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1437 XInputStream xPlainRawStream
= null;
1440 xPlainRawStream
= xRawStorage
.getPlainRawStreamElement( sStreamName
);
1442 catch( Exception e
)
1444 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1447 if ( xHeadRawStream
== null || xPlainRawStream
== null )
1449 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw modes!" );
1455 byte pData
[][] = new byte[1][38];
1456 if ( xHeadRawStream
.readBytes( pData
, 38 ) != 38 )
1458 Error( "Can't read header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1462 if ( pData
[0][0] != 0x4d || pData
[0][1] != 0x4d || pData
[0][2] != 0x02 || pData
[0][3] != 0x05 )
1464 Error( "No signature in the header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1468 int nVariableHeaderLength
=
1469 ( pData
[0][30] + pData
[0][31] * 0x100 ) // salt length
1470 + ( pData
[0][32] + pData
[0][33] * 0x100 ) // iv length
1471 + ( pData
[0][34] + pData
[0][35] * 0x100 ) // digest length
1472 + ( pData
[0][36] + pData
[0][37] * 0x100 ); // mediatype length
1474 xHeadRawStream
.skipBytes( nVariableHeaderLength
);
1476 byte pRawData1
[][] = new byte[1][32000];
1477 byte pRawData2
[][] = new byte[1][32000];
1483 nRead1
= xHeadRawStream
.readBytes( pRawData1
, 32000 );
1484 nRead2
= xPlainRawStream
.readBytes( pRawData2
, 32000 );
1486 if ( nRead1
!= nRead2
)
1488 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different size! nRead1 - nRead2 = " + ( Integer
.valueOf( nRead1
- nRead2
) ).toString() );
1492 for ( int nInd
= 0; nInd
< nRead1
; nInd
++ )
1493 if ( pRawData1
[0][nInd
] != pRawData2
[0][nInd
] )
1495 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different data!" );
1499 while( nRead1
== 32000 );
1501 catch ( Exception e
)
1503 Error( "Can't compare stream '" + sStreamName
+ "' raw representations, exception : " + e
+ "!" );
1510 public boolean cantOpenStorage( XStorage xStorage
, String sName
)
1512 // try to open an opened substorage, open call must fail
1515 Object oDummyStorage
= xStorage
.openStorageElement( sName
, ElementModes
.READ
);
1516 Error( "The trying to reopen opened substorage '" + sName
+ "' must fail!" );
1518 catch( Exception e
)
1526 public boolean cantOpenStream( XStorage xStorage
, String sName
, int nMode
)
1528 // try to open the substream with specified mode must fail
1531 Object oDummyStream
= xStorage
.openStreamElement( sName
, nMode
);
1532 Error( "The trying to open substream '" + sName
+ "' must fail!" );
1534 catch( Exception e
)
1542 public boolean cantOpenStreamH( XStorage xStorage
, String sPath
, int nMode
)
1544 // try to open the substream with specified mode must fail
1546 XHierarchicalStorageAccess xHStorage
=
1547 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1548 if ( xHStorage
== null )
1550 Error( "The storage does not support hierarchical access!" );
1556 Object oDummyStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, nMode
);
1557 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1559 catch( Exception e
)
1567 public boolean cantOpenEncrStreamH( XStorage xStorage
, String sPath
, int nMode
, String aPass
)
1569 // try to open the substream with specified mode must fail
1571 XHierarchicalStorageAccess xHStorage
=
1572 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1573 if ( xHStorage
== null )
1575 Error( "The storage does not support hierarchical access!" );
1581 Object oDummyStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, nMode
, aPass
);
1582 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1584 catch( WrongPasswordException wpe
)
1586 Error( "The substream '" + sPath
+ "' must not exist!" );
1589 catch( Exception e
)
1597 public XStorage
cloneStorage( XSingleServiceFactory xFactory
, XStorage xStorage
)
1599 // create a copy of a last committed version of specified storage
1600 XStorage xResult
= null;
1603 Object oTempStorage
= xFactory
.createInstance();
1604 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1605 if ( xResult
!= null )
1606 xStorage
.copyLastCommitTo( xResult
);
1608 catch( Exception e
)
1610 Error( "Can't clone storage, exception: " + e
);
1617 public XStorage
cloneSubStorage( XSingleServiceFactory xFactory
, XStorage xStorage
, String sName
)
1619 // create a copy of a last committed version of specified substorage
1620 XStorage xResult
= null;
1623 Object oTempStorage
= xFactory
.createInstance();
1624 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1625 if ( xResult
!= null )
1626 xStorage
.copyStorageElementLastCommitTo( sName
, xResult
);
1628 catch( Exception e
)
1630 Error( "Can't clone substorage '" + sName
+ "', exception: " + e
);
1637 public XStream
cloneSubStream( XStorage xStorage
, String sName
)
1639 // clone existing substream
1642 XStream xStream
= xStorage
.cloneStreamElement( sName
);
1645 catch( Exception e
)
1647 Error( "Can't clone substream '" + sName
+ "', exception: " + e
);
1653 public XStream
cloneEncrSubStream( XStorage xStorage
, String sName
, String sPass
)
1655 // clone existing substream
1658 XStream xStream
= xStorage
.cloneEncryptedStreamElement( sName
, sPass
);
1661 catch( Exception e
)
1663 Error( "Can't clone encrypted substream '" + sName
+ "', exception: " + e
);
1669 public void Error( String sError
)
1671 m_aLogWriter
.println( m_sTestPrefix
+ "Error: " + sError
);
1674 public void Message( String sMessage
)
1676 m_aLogWriter
.println( m_sTestPrefix
+ sMessage
);