1 package complex
.storages
;
3 import com
.sun
.star
.uno
.UnoRuntime
;
4 import com
.sun
.star
.uno
.XInterface
;
5 import com
.sun
.star
.uno
.AnyConverter
;
7 import com
.sun
.star
.lang
.*;
8 import com
.sun
.star
.embed
.*;
9 import com
.sun
.star
.packages
.*;
10 import com
.sun
.star
.io
.*;
11 import com
.sun
.star
.beans
.*;
13 import share
.LogWriter
;
15 public class TestHelper
{
17 LogWriter m_aLogWriter
;
20 public TestHelper( LogWriter aLogWriter
, String sTestPrefix
)
22 m_aLogWriter
= aLogWriter
;
23 m_sTestPrefix
= sTestPrefix
;
26 public boolean WriteBytesToStream( XStream xStream
,
32 // get output stream of substream
33 XOutputStream xOutput
= xStream
.getOutputStream();
34 if ( xOutput
== null )
36 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
40 // get XTrucate implementation from output stream
41 XTruncate xTruncate
= (XTruncate
) UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
42 if ( xTruncate
== null )
44 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
48 // write requested byte sequence
52 xOutput
.writeBytes( pBytes
);
56 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
60 // get access to the XPropertySet interface
61 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
62 if ( xPropSet
== null )
64 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
68 // set properties to the stream
71 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
72 xPropSet
.setPropertyValue( "Compressed", new Boolean( bCompressed
) );
76 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
80 // check size property of the stream
83 int nSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
84 if ( nSize
!= pBytes
.length
)
86 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
92 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
100 public boolean WriteBytesToSubstream( XStorage xStorage
,
106 // open substream element
107 XStream xSubStream
= null;
110 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
111 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
112 if ( xSubStream
== null )
114 Error( "Can't create substream '" + sStreamName
+ "'!" );
120 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
124 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
127 // free the stream resources, garbage collector may remove the object too late
128 if ( !disposeStream( xSubStream
, sStreamName
) )
134 public boolean WriteBytesToEncrSubstream( XStorage xStorage
,
141 // open substream element
142 XStream xSubStream
= null;
145 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sPass
);
146 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
147 if ( xSubStream
== null )
149 Error( "Can't create substream '" + sStreamName
+ "'!" );
155 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
159 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
162 // free the stream resources, garbage collector may remove the object too late
163 if ( !disposeStream( xSubStream
, sStreamName
) )
169 public boolean WBToSubstrOfEncr( XStorage xStorage
,
176 // open substream element
177 XStream xSubStream
= null;
180 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
181 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
182 if ( xSubStream
== null )
184 Error( "Can't create substream '" + sStreamName
+ "'!" );
190 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
194 // get access to the XPropertySet interface
195 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
196 if ( xPropSet
== null )
198 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
202 // set properties to the stream
205 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted
) );
209 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName
+ "', exception: " + e
);
213 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
216 // free the stream resources, garbage collector may remove the object too late
217 if ( !disposeStream( xSubStream
, sStreamName
) )
223 public boolean WriteBytesToStreamH( XStorage xStorage
,
230 // open substream element
231 XStream xSubStream
= null;
234 XHierarchicalStorageAccess xHStorage
=
235 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
236 if ( xHStorage
== null )
238 Error( "The storage does not support hierarchical access!" );
242 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
243 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
244 if ( xSubStream
== null )
246 Error( "Can't create substream '" + sStreamPath
+ "'!" );
252 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
256 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
259 XTransactedObject xTransact
=
260 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
261 if ( xTransact
== null )
263 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
271 } catch( Exception e
)
273 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
278 // free the stream resources, garbage collector may remove the object too late
279 if ( !disposeStream( xSubStream
, sStreamPath
) )
285 public boolean WriteBytesToEncrStreamH( XStorage xStorage
,
293 // open substream element
294 XStream xSubStream
= null;
297 XHierarchicalStorageAccess xHStorage
=
298 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
299 if ( xHStorage
== null )
301 Error( "The storage does not support hierarchical access!" );
305 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sStreamPath
,
308 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
309 if ( xSubStream
== null )
311 Error( "Can't create substream '" + sStreamPath
+ "'!" );
317 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
321 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
324 XTransactedObject xTransact
=
325 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
326 if ( xTransact
== null )
328 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
336 } catch( Exception e
)
338 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
343 // free the stream resources, garbage collector may remove the object too late
344 if ( !disposeStream( xSubStream
, sStreamPath
) )
350 public boolean WBToSubstrOfEncrH( XStorage xStorage
,
358 // open substream element
359 XStream xSubStream
= null;
362 XHierarchicalStorageAccess xHStorage
=
363 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
364 if ( xHStorage
== null )
366 Error( "The storage does not support hierarchical access!" );
370 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
371 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
372 if ( xSubStream
== null )
374 Error( "Can't create substream '" + sStreamPath
+ "'!" );
380 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
384 // get access to the XPropertySet interface
385 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
386 if ( xPropSet
== null )
388 Error( "Can't get XPropertySet implementation from substream '" + sStreamPath
+ "'!" );
392 // set properties to the stream
395 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted
) );
399 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath
+ "', exception: " + e
);
403 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
406 XTransactedObject xTransact
=
407 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
408 if ( xTransact
== null )
410 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
418 } catch( Exception e
)
420 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
425 // free the stream resources, garbage collector may remove the object too late
426 if ( !disposeStream( xSubStream
, sStreamPath
) )
432 public int ChangeStreamPass( XStorage xStorage
,
437 // open substream element
438 XStream xSubStream
= null;
441 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sOldPass
);
442 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
443 if ( xSubStream
== null )
445 Error( "Can't open substream '" + sStreamName
+ "'!" );
451 Error( "Can't open substream '" + sStreamName
+ "', exception : " + e
+ "!" );
456 // change the password for the stream
457 XEncryptionProtectedSource xStreamEncryption
=
458 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
460 if ( xStreamEncryption
== null )
462 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
467 xStreamEncryption
.setEncryptionPassword( sNewPass
);
471 Error( "Can't change encryption key of the substream '" + sStreamName
+ "', exception:" + e
);
475 // free the stream resources, garbage collector may remove the object too late
476 if ( !disposeStream( xSubStream
, sStreamName
) )
482 public int ChangeStreamPassH( XStorage xStorage
,
488 // open substream element
489 XHierarchicalStorageAccess xHStorage
=
490 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
491 if ( xHStorage
== null )
493 Error( "The storage does not support hierarchical access!" );
497 XStream xSubStream
= null;
500 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.WRITE
, sOldPass
);
501 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
502 if ( xSubStream
== null )
504 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
510 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
514 // change the password for the stream
515 XEncryptionProtectedSource xStreamEncryption
=
516 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
518 if ( xStreamEncryption
== null )
520 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
525 xStreamEncryption
.setEncryptionPassword( sNewPass
);
529 Error( "Can't change encryption key of the substream '" + sPath
+ "', exception:" + e
);
533 XTransactedObject xTransact
=
534 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
535 if ( xTransact
== null )
537 Error( "Substream '" + sPath
+ "', stream opened for writing must be transacted!" );
545 } catch( Exception e
)
547 Error( "Can't commit storage after substream '" + sPath
+ "' change, exception : " + e
+ "!" );
552 // free the stream resources, garbage collector may remove the object too late
553 if ( !disposeStream( xSubStream
, sPath
) )
559 public boolean setStorageTypeAndCheckProps( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
563 // get access to the XPropertySet interface
564 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
565 if ( xPropSet
!= null )
569 // set "MediaType" property to the stream
570 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
572 // get "IsRoot" and "OpenMode" properties and control there values
573 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
574 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
577 if ( bPropIsRoot
!= bIsRoot
)
579 Error( "'IsRoot' property contains wrong value!" );
584 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
585 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
587 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
593 Error( "Can't control properties of substorage, exception: " + e
);
598 Error( "Can't get XPropertySet implementation from storage!" );
604 public boolean checkStorageProperties( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
608 // get access to the XPropertySet interface
609 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
610 if ( xPropSet
!= null )
614 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
615 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
616 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
617 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
620 if ( !sPropMediaType
.equals( sMediaType
) )
622 Error( "'MediaType' property contains wrong value, expected '"
623 + sMediaType
+ "', set '" + sPropMediaType
+ "' !" );
627 if ( bPropIsRoot
!= bIsRoot
)
629 Error( "'IsRoot' property contains wrong value!" );
634 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
635 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
637 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
643 Error( "Can't get properties of substorage, exception: " + e
);
648 Error( "Can't get XPropertySet implementation from storage!" );
654 public boolean InternalCheckStream( XStream xStream
,
659 boolean bCheckCompressed
)
661 // get input stream of substream
662 XInputStream xInput
= xStream
.getInputStream();
663 if ( xInput
== null )
665 Error( "Can't get XInputStream implementation from substream '" + sName
+ "'!" );
669 byte pContents
[][] = new byte[1][]; // ???
674 xInput
.readBytes( pContents
, pBytes
.length
+ 1 );
678 Error( "Can't read from stream '" + sName
+ "', exception: " + e
);
682 // check size of stream data
683 if ( pContents
.length
== 0 )
685 Error( "SubStream '" + sName
+ "' reading produced disaster!" );
689 if ( pBytes
.length
!= pContents
[0].length
)
691 Error( "SubStream '" + sName
+ "' contains wrong amount of data! (" + pContents
[0].length
+ "/" + pBytes
.length
+ ")" );
696 for ( int ind
= 0; ind
< pBytes
.length
; ind
++ )
698 if ( pBytes
[ind
] != pContents
[0][ind
] )
700 Error( "SubStream '" + sName
+ "' contains wrong data! ( byte num. "
701 + ind
+ " should be " + pBytes
[ind
] + " but it is " + pContents
[0][ind
] + ")" );
709 // get access to the XPropertySet interface
710 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
711 if ( xPropSet
!= null )
715 // get "MediaType" and "Size" properties and control there values
716 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
717 int nPropSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
718 boolean bPropCompress
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "Compressed" ) );
721 if ( !sPropMediaType
.equals( sMediaType
) )
723 Error( "'MediaType' property contains wrong value for stream '" + sName
+ "',\nexpected: '"
724 + sMediaType
+ "', set: '" + sPropMediaType
+ "'!" );
728 if ( nPropSize
!= pBytes
.length
)
730 Error( "'Size' property contains wrong value for stream'" + sName
+ "'!" );
734 if ( bCheckCompressed
&& bPropCompress
!= bCompressed
)
736 Error( "'Compressed' property contains wrong value for stream'" + sName
+ "'!" );
742 Error( "Can't get properties of substream '" + sName
+ "', exception: " + e
);
747 Error( "Can't get XPropertySet implementation from stream '" + sName
+ "'!" );
753 public boolean checkStream( XStorage xParentStorage
,
759 // open substream element first
760 XStream xSubStream
= null;
763 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
764 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
765 if ( xSubStream
== null )
767 Error( "Can't open substream '" + sName
+ "'!" );
773 Error( "Can't open substream '" + sName
+ "', exception : " + e
+ "!" );
777 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, bCompressed
, pBytes
, true );
779 // free the stream resources, garbage collector may remove the object too late
780 if ( !disposeStream( xSubStream
, sName
) )
786 public boolean checkEncrStream( XStorage xParentStorage
,
792 // Important: a common password for any of parent storage should not be set or
793 // should be different from sPass
797 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
798 Error( "Encrypted stream '" + sName
+ "' was opened without password!" );
801 catch( WrongPasswordException wpe
)
805 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' without password: " + e
+ "!" );
809 String sWrongPass
= "11";
813 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sWrongPass
);
814 Error( "Encrypted stream '" + sName
+ "' was opened with wrong password!" );
817 catch( WrongPasswordException wpe
)
821 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' with wrong password: " + e
+ "!" );
825 XStream xSubStream
= null;
828 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sPass
);
829 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
830 if ( xSubStream
== null )
832 Error( "Can't open encrypted substream '" + sName
+ "'!" );
838 Error( "Can't open encrypted substream '" + sName
+ "', exception : " + e
+ "!" );
842 // encrypted streams will be compressed always, so after the storing this property is always true,
843 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
844 // before the storing )
845 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, true, pBytes
, false );
847 // free the stream resources, garbage collector may remove the object too late
848 if ( !disposeStream( xSubStream
, sName
) )
854 public boolean checkStreamH( XStorage xParentStorage
,
860 // open substream element first
861 XStream xSubStream
= null;
864 XHierarchicalStorageAccess xHStorage
=
865 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
866 if ( xHStorage
== null )
868 Error( "The storage does not support hierarchical access!" );
872 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
873 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
874 if ( xSubStream
== null )
876 Error( "Can't open substream '" + sPath
+ "'!" );
882 Error( "Can't open substream '" + sPath
+ "', exception : " + e
+ "!" );
886 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, bCompressed
, pBytes
, true );
888 // free the stream resources, garbage collector may remove the object too late
889 if ( !disposeStream( xSubStream
, sPath
) )
895 public boolean checkEncrStreamH( XStorage xParentStorage
,
901 // Important: a common password for any of parent storage should not be set or
902 // should be different from sPass
903 XHierarchicalStorageAccess xHStorage
=
904 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
905 if ( xHStorage
== null )
907 Error( "The storage does not support hierarchical access!" );
913 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
914 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
915 Error( "Encrypted substream '" + sPath
+ "' was opened without password!" );
918 catch( WrongPasswordException wpe
)
922 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' without password: " + e
+ "!" );
926 String sWrongPass
= "11";
930 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sWrongPass
);
931 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
932 Error( "Encrypted substream '" + sPath
+ "' was opened with wrong password!" );
935 catch( WrongPasswordException wpe
)
939 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' with wrong password: " + e
+ "!" );
943 XStream xSubStream
= null;
946 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sPass
);
947 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
948 if ( xSubStream
== null )
950 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
956 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
960 // encrypted streams will be compressed always, so after the storing this property is always true,
961 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
962 // before the storing )
963 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, true, pBytes
, false );
965 // free the stream resources, garbage collector may remove the object too late
966 if ( !disposeStream( xSubStream
, sPath
) )
972 public boolean copyStorage( XStorage xSourceStorage
, XStorage xDestStorage
)
974 // copy xSourceStorage to xDestStorage
977 xSourceStorage
.copyToStorage( xDestStorage
);
981 Error( "Storage copying failed, exception: " + e
);
988 public boolean commitStorage( XStorage xStorage
)
990 // XTransactedObject must be supported by storages
991 XTransactedObject xTransact
= (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xStorage
);
992 if ( xTransact
== null )
994 Error( "Storage doesn't implement transacted access!" );
1002 catch( Exception e
)
1004 Error( "Storage commit failed, exception:" + e
);
1011 public boolean disposeStream( XStream xStream
, String sStreamName
)
1013 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStream
);
1014 if ( xComponent
== null )
1016 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
1022 xComponent
.dispose();
1024 catch( Exception e
)
1026 Error( "Substream '" + sStreamName
+ "' disposing throws exception: " + e
);
1033 public boolean disposeStorage( XStorage xStorage
)
1035 // dispose the storage
1036 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStorage
);
1037 if ( xComponent
== null )
1039 Error( "Can't retrieve XComponent implementation from storage!" );
1045 xComponent
.dispose();
1047 catch( Exception e
)
1049 Error( "Storage disposing failed!" );
1056 public XInputStream
getInputStream( XStream xStream
)
1058 XInputStream xInTemp
= null;
1061 xInTemp
= xStream
.getInputStream();
1062 if ( xInTemp
== null )
1063 Error( "Can't get the input part of a stream!" );
1065 catch ( Exception e
)
1067 Error( "Can't get the input part of a stream, exception :" + e
);
1073 public boolean closeOutput( XStream xStream
)
1075 XOutputStream xOutTemp
= null;
1078 xOutTemp
= xStream
.getOutputStream();
1079 if ( xOutTemp
== null )
1081 Error( "Can't get the output part of a stream!" );
1085 catch ( Exception e
)
1087 Error( "Can't get the output part of a stream, exception :" + e
);
1093 xOutTemp
.closeOutput();
1095 catch ( Exception e
)
1097 Error( "Can't close output part of a stream, exception :" + e
);
1104 public XStorage
openSubStorage( XStorage xStorage
, String sName
, int nMode
)
1106 // open existing substorage
1109 Object oSubStorage
= xStorage
.openStorageElement( sName
, nMode
);
1110 XStorage xSubStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oSubStorage
);
1113 catch( Exception e
)
1115 Error( "Can't open substorage '" + sName
+ "', exception: " + e
);
1121 public XStream
CreateTempFileStream( XMultiServiceFactory xMSF
)
1123 // try to get temporary file representation
1124 XStream xTempFileStream
= null;
1127 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1128 xTempFileStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, oTempFile
);
1130 catch( Exception e
)
1133 if ( xTempFileStream
== null )
1134 Error( "Can't create temporary file!" );
1136 return xTempFileStream
;
1139 public String
CreateTempFile( XMultiServiceFactory xMSF
)
1141 String sResult
= null;
1143 // try to get temporary file representation
1144 XPropertySet xTempFileProps
= null;
1147 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1148 xTempFileProps
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
1150 catch( Exception e
)
1153 if ( xTempFileProps
!= null )
1157 xTempFileProps
.setPropertyValue( "RemoveFile", new Boolean( false ) );
1158 sResult
= AnyConverter
.toString( xTempFileProps
.getPropertyValue( "Uri" ) );
1160 catch( Exception e
)
1162 Error( "Can't control TempFile properties, exception: " + e
);
1167 Error( "Can't create temporary file representation!" );
1170 // close temporary file explicitly
1173 XStream xStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, xTempFileProps
);
1174 if ( xStream
!= null )
1176 XOutputStream xOut
= xStream
.getOutputStream();
1180 XInputStream xIn
= xStream
.getInputStream();
1185 Error( "Can't close TempFile!" );
1187 catch( Exception e
)
1189 Error( "Can't close TempFile, exception: " + e
);
1195 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1197 // copy element with name sName from xSource to xDest
1200 xSource
.copyElementTo( sName
, xDest
, sName
);
1202 catch( Exception e
)
1204 Error( "Element copying failed, exception: " + e
);
1211 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
, String sTargetName
)
1213 // copy element with name sName from xSource to xDest
1216 xSource
.copyElementTo( sName
, xDest
, sTargetName
);
1218 catch( Exception e
)
1220 Error( "Element copying failed, exception: " + e
);
1227 public boolean moveElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1229 // move element with name sName from xSource to xDest
1232 xSource
.moveElementTo( sName
, xDest
, sName
);
1234 catch( Exception e
)
1236 Error( "Element moving failed, exception: " + e
);
1243 public boolean renameElement( XStorage xStorage
, String sOldName
, String sNewName
)
1245 // rename element with name sOldName to sNewName
1248 xStorage
.renameElement( sOldName
, sNewName
);
1250 catch( Exception e
)
1252 Error( "Element renaming failed, exception: " + e
);
1259 public boolean removeElement( XStorage xStorage
, String sName
)
1261 // remove element with name sName
1264 xStorage
.removeElement( sName
);
1266 catch( Exception e
)
1268 Error( "Element removing failed, exception: " + e
);
1275 public XStream
OpenStream( XStorage xStorage
,
1279 // open substream element
1280 XStream xSubStream
= null;
1283 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, nMode
);
1284 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1285 if ( xSubStream
== null )
1286 Error( "Can't create substream '" + sStreamName
+ "'!" );
1288 catch( Exception e
)
1290 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
1296 public boolean compareRawMethodsOnEncrStream( XStorage xStorage
, String sStreamName
)
1299 XStorageRawAccess xRawStorage
;
1302 xRawStorage
= (XStorageRawAccess
) UnoRuntime
.queryInterface( XStorageRawAccess
.class, xStorage
);
1304 catch( Exception e
)
1306 Error( "Can't get raw access to the storage, exception : " + e
+ "!" );
1310 if ( xRawStorage
== null )
1312 Error( "Can't get raw access to the storage!" );
1316 XInputStream xHeadRawStream
= null;
1319 xHeadRawStream
= xRawStorage
.getRawEncrStreamElement( sStreamName
);
1321 catch( Exception e
)
1323 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1326 XInputStream xPlainRawStream
= null;
1329 xPlainRawStream
= xRawStorage
.getPlainRawStreamElement( sStreamName
);
1331 catch( Exception e
)
1333 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1336 if ( xHeadRawStream
== null || xPlainRawStream
== null )
1338 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw modes!" );
1344 byte pData
[][] = new byte[1][22];
1345 if ( xHeadRawStream
.readBytes( pData
, 22 ) != 22 )
1347 Error( "Can't read header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1351 if ( pData
[0][0] != 0x4d || pData
[0][1] != 0x47 || pData
[0][2] != 0x02 || pData
[0][3] != 0x05 )
1353 Error( "No signature in the header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1357 int nVariableHeaderLength
=
1358 ( pData
[0][14] + pData
[0][15] * 0x100 ) // salt length
1359 + ( pData
[0][16] + pData
[0][17] * 0x100 ) // iv length
1360 + ( pData
[0][18] + pData
[0][19] * 0x100 ) // digest length
1361 + ( pData
[0][20] + pData
[0][21] * 0x100 ); // mediatype length
1363 xHeadRawStream
.skipBytes( nVariableHeaderLength
);
1365 byte pRawData1
[][] = new byte[1][32000];
1366 byte pRawData2
[][] = new byte[1][32000];
1372 nRead1
= xHeadRawStream
.readBytes( pRawData1
, 32000 );
1373 nRead2
= xPlainRawStream
.readBytes( pRawData2
, 32000 );
1375 if ( nRead1
!= nRead2
)
1377 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different size!" );
1381 for ( int nInd
= 0; nInd
< nRead1
; nInd
++ )
1382 if ( pRawData1
[0][nInd
] != pRawData2
[0][nInd
] )
1384 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different data!" );
1388 while( nRead1
== 32000 );
1390 catch ( Exception e
)
1392 Error( "Can't compare stream '" + sStreamName
+ "' raw representations, exception : " + e
+ "!" );
1399 public boolean cantOpenStorage( XStorage xStorage
, String sName
)
1401 // try to open an opened substorage, open call must fail
1404 Object oDummyStorage
= xStorage
.openStorageElement( sName
, ElementModes
.READ
);
1405 Error( "The trying to reopen opened substorage '" + sName
+ "' must fail!" );
1407 catch( Exception e
)
1415 public boolean cantOpenStream( XStorage xStorage
, String sName
, int nMode
)
1417 // try to open the substream with specified mode must fail
1420 Object oDummyStream
= xStorage
.openStreamElement( sName
, nMode
);
1421 Error( "The trying to open substream '" + sName
+ "' must fail!" );
1423 catch( Exception e
)
1431 public boolean cantOpenStreamH( XStorage xStorage
, String sPath
, int nMode
)
1433 // try to open the substream with specified mode must fail
1435 XHierarchicalStorageAccess xHStorage
=
1436 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1437 if ( xHStorage
== null )
1439 Error( "The storage does not support hierarchical access!" );
1445 Object oDummyStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, nMode
);
1446 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1448 catch( Exception e
)
1456 public boolean cantOpenEncrStreamH( XStorage xStorage
, String sPath
, int nMode
, String aPass
)
1458 // try to open the substream with specified mode must fail
1460 XHierarchicalStorageAccess xHStorage
=
1461 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1462 if ( xHStorage
== null )
1464 Error( "The storage does not support hierarchical access!" );
1470 Object oDummyStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, nMode
, aPass
);
1471 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1473 catch( WrongPasswordException wpe
)
1475 Error( "The substream '" + sPath
+ "' must not exist!" );
1478 catch( Exception e
)
1486 public XStorage
cloneStorage( XSingleServiceFactory xFactory
, XStorage xStorage
)
1488 // create a copy of a last commited version of specified storage
1489 XStorage xResult
= null;
1492 Object oTempStorage
= xFactory
.createInstance();
1493 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1494 if ( xResult
!= null )
1495 xStorage
.copyLastCommitTo( xResult
);
1497 catch( Exception e
)
1499 Error( "Can't clone storage, exception: " + e
);
1506 public XStorage
cloneSubStorage( XSingleServiceFactory xFactory
, XStorage xStorage
, String sName
)
1508 // create a copy of a last commited version of specified substorage
1509 XStorage xResult
= null;
1512 Object oTempStorage
= xFactory
.createInstance();
1513 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1514 if ( xResult
!= null )
1515 xStorage
.copyStorageElementLastCommitTo( sName
, xResult
);
1517 catch( Exception e
)
1519 Error( "Can't clone substorage '" + sName
+ "', exception: " + e
);
1526 public XStream
cloneSubStream( XStorage xStorage
, String sName
)
1528 // clone existing substream
1531 XStream xStream
= xStorage
.cloneStreamElement( sName
);
1534 catch( Exception e
)
1536 Error( "Can't clone substream '" + sName
+ "', exception: " + e
);
1542 public XStream
cloneEncrSubStream( XStorage xStorage
, String sName
, String sPass
)
1544 // clone existing substream
1547 XStream xStream
= xStorage
.cloneEncryptedStreamElement( sName
, sPass
);
1550 catch( Exception e
)
1552 Error( "Can't clone encrypted substream '" + sName
+ "', exception: " + e
);
1558 public void Error( String sError
)
1560 m_aLogWriter
.println( m_sTestPrefix
+ "Error: " + sError
);
1563 public void Message( String sMessage
)
1565 m_aLogWriter
.println( m_sTestPrefix
+ sMessage
);