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
);
99 public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage
,
104 // open substream element
105 XStream xSubStream
= null;
108 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
109 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
110 if ( xSubStream
== null )
112 Error( "Can't create substream '" + sStreamName
+ "'!" );
118 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
122 // get output stream of substream
123 XOutputStream xOutput
= xSubStream
.getOutputStream();
124 if ( xOutput
== null )
126 Error( "Can't get XOutputStream implementation from substream '" + sStreamName
+ "'!" );
130 // get XTrucate implementation from output stream
131 XTruncate xTruncate
= (XTruncate
) UnoRuntime
.queryInterface( XTruncate
.class, xOutput
);
132 if ( xTruncate
== null )
134 Error( "Can't get XTruncate implementation from substream '" + sStreamName
+ "'!" );
138 // write requested byte sequence
141 xTruncate
.truncate();
142 xOutput
.writeBytes( pBytes
);
146 Error( "Can't write to stream '" + sStreamName
+ "', exception: " + e
);
150 // get access to the XPropertySet interface
151 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
152 if ( xPropSet
== null )
154 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
158 // set properties to the stream
159 // do not set the compressed property
162 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
166 Error( "Can't set properties to substream '" + sStreamName
+ "', exception: " + e
);
170 // check size property of the stream
173 int nSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
174 if ( nSize
!= pBytes
.length
)
176 Error( "The 'Size' property of substream '" + sStreamName
+ "' contains wrong value!" );
182 Error( "Can't get 'Size' property from substream '" + sStreamName
+ "', exception: " + e
);
186 // free the stream resources, garbage collector may remove the object too late
187 if ( !disposeStream( xSubStream
, sStreamName
) )
193 public boolean WriteBytesToSubstream( XStorage xStorage
,
199 // open substream element
200 XStream xSubStream
= null;
203 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
204 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
205 if ( xSubStream
== null )
207 Error( "Can't create substream '" + sStreamName
+ "'!" );
213 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
217 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
220 // free the stream resources, garbage collector may remove the object too late
221 if ( !disposeStream( xSubStream
, sStreamName
) )
227 public boolean WriteBytesToEncrSubstream( XStorage xStorage
,
234 // open substream element
235 XStream xSubStream
= null;
238 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sPass
);
239 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
240 if ( xSubStream
== null )
242 Error( "Can't create substream '" + sStreamName
+ "'!" );
248 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
252 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
255 // free the stream resources, garbage collector may remove the object too late
256 if ( !disposeStream( xSubStream
, sStreamName
) )
262 public boolean WBToSubstrOfEncr( XStorage xStorage
,
269 // open substream element
270 XStream xSubStream
= null;
273 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, ElementModes
.WRITE
);
274 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
275 if ( xSubStream
== null )
277 Error( "Can't create substream '" + sStreamName
+ "'!" );
283 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
287 // get access to the XPropertySet interface
288 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
289 if ( xPropSet
== null )
291 Error( "Can't get XPropertySet implementation from substream '" + sStreamName
+ "'!" );
295 // set properties to the stream
298 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted
) );
302 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName
+ "', exception: " + e
);
306 if ( !WriteBytesToStream( xSubStream
, sStreamName
, sMediaType
, bCompressed
, pBytes
) )
309 // free the stream resources, garbage collector may remove the object too late
310 if ( !disposeStream( xSubStream
, sStreamName
) )
316 public boolean WriteBytesToStreamH( XStorage xStorage
,
323 // open substream element
324 XStream xSubStream
= null;
327 XHierarchicalStorageAccess xHStorage
=
328 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
329 if ( xHStorage
== null )
331 Error( "The storage does not support hierarchical access!" );
335 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
336 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
337 if ( xSubStream
== null )
339 Error( "Can't create substream '" + sStreamPath
+ "'!" );
345 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
349 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
352 XTransactedObject xTransact
=
353 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
354 if ( xTransact
== null )
356 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
364 } catch( Exception e
)
366 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
371 // free the stream resources, garbage collector may remove the object too late
372 if ( !disposeStream( xSubStream
, sStreamPath
) )
378 public boolean WriteBytesToEncrStreamH( XStorage xStorage
,
386 // open substream element
387 XStream xSubStream
= null;
390 XHierarchicalStorageAccess xHStorage
=
391 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
392 if ( xHStorage
== null )
394 Error( "The storage does not support hierarchical access!" );
398 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sStreamPath
,
401 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
402 if ( xSubStream
== null )
404 Error( "Can't create substream '" + sStreamPath
+ "'!" );
410 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
414 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
417 XTransactedObject xTransact
=
418 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
419 if ( xTransact
== null )
421 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
429 } catch( Exception e
)
431 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
436 // free the stream resources, garbage collector may remove the object too late
437 if ( !disposeStream( xSubStream
, sStreamPath
) )
443 public boolean WBToSubstrOfEncrH( XStorage xStorage
,
451 // open substream element
452 XStream xSubStream
= null;
455 XHierarchicalStorageAccess xHStorage
=
456 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
457 if ( xHStorage
== null )
459 Error( "The storage does not support hierarchical access!" );
463 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sStreamPath
, ElementModes
.WRITE
);
464 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
465 if ( xSubStream
== null )
467 Error( "Can't create substream '" + sStreamPath
+ "'!" );
473 Error( "Can't create substream '" + sStreamPath
+ "', exception : " + e
+ "!" );
477 // get access to the XPropertySet interface
478 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xSubStream
);
479 if ( xPropSet
== null )
481 Error( "Can't get XPropertySet implementation from substream '" + sStreamPath
+ "'!" );
485 // set properties to the stream
488 xPropSet
.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted
) );
492 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath
+ "', exception: " + e
);
496 if ( !WriteBytesToStream( xSubStream
, sStreamPath
, sMediaType
, bCompressed
, pBytes
) )
499 XTransactedObject xTransact
=
500 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
501 if ( xTransact
== null )
503 Error( "Substream '" + sStreamPath
+ "', stream opened for writing must be transacted!" );
511 } catch( Exception e
)
513 Error( "Can't commit storage after substream '" + sStreamPath
+ "' change, exception : " + e
+ "!" );
518 // free the stream resources, garbage collector may remove the object too late
519 if ( !disposeStream( xSubStream
, sStreamPath
) )
525 public int ChangeStreamPass( XStorage xStorage
,
530 // open substream element
531 XStream xSubStream
= null;
534 Object oSubStream
= xStorage
.openEncryptedStreamElement( sStreamName
, ElementModes
.WRITE
, sOldPass
);
535 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
536 if ( xSubStream
== null )
538 Error( "Can't open substream '" + sStreamName
+ "'!" );
544 Error( "Can't open substream '" + sStreamName
+ "', exception : " + e
+ "!" );
549 // change the password for the stream
550 XEncryptionProtectedSource xStreamEncryption
=
551 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
553 if ( xStreamEncryption
== null )
555 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
560 xStreamEncryption
.setEncryptionPassword( sNewPass
);
564 Error( "Can't change encryption key of the substream '" + sStreamName
+ "', exception:" + e
);
568 // free the stream resources, garbage collector may remove the object too late
569 if ( !disposeStream( xSubStream
, sStreamName
) )
575 public int ChangeStreamPassH( XStorage xStorage
,
581 // open substream element
582 XHierarchicalStorageAccess xHStorage
=
583 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
584 if ( xHStorage
== null )
586 Error( "The storage does not support hierarchical access!" );
590 XStream xSubStream
= null;
593 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.WRITE
, sOldPass
);
594 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
595 if ( xSubStream
== null )
597 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
603 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
607 // change the password for the stream
608 XEncryptionProtectedSource xStreamEncryption
=
609 (XEncryptionProtectedSource
) UnoRuntime
.queryInterface( XEncryptionProtectedSource
.class, xSubStream
);
611 if ( xStreamEncryption
== null )
613 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
618 xStreamEncryption
.setEncryptionPassword( sNewPass
);
622 Error( "Can't change encryption key of the substream '" + sPath
+ "', exception:" + e
);
626 XTransactedObject xTransact
=
627 (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xSubStream
);
628 if ( xTransact
== null )
630 Error( "Substream '" + sPath
+ "', stream opened for writing must be transacted!" );
638 } catch( Exception e
)
640 Error( "Can't commit storage after substream '" + sPath
+ "' change, exception : " + e
+ "!" );
645 // free the stream resources, garbage collector may remove the object too late
646 if ( !disposeStream( xSubStream
, sPath
) )
652 public boolean setStorageTypeAndCheckProps( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
656 // get access to the XPropertySet interface
657 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
658 if ( xPropSet
!= null )
662 // set "MediaType" property to the stream
663 xPropSet
.setPropertyValue( "MediaType", sMediaType
);
665 // get "IsRoot" and "OpenMode" properties and control there values
666 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
667 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
670 if ( bPropIsRoot
!= bIsRoot
)
672 Error( "'IsRoot' property contains wrong value!" );
677 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
678 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
680 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
686 Error( "Can't control properties of substorage, exception: " + e
);
691 Error( "Can't get XPropertySet implementation from storage!" );
697 public boolean checkStorageProperties( XStorage xStorage
, String sMediaType
, boolean bIsRoot
, int nMode
)
701 // get access to the XPropertySet interface
702 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStorage
);
703 if ( xPropSet
!= null )
707 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
708 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
709 boolean bPropIsRoot
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "IsRoot" ) );
710 int nPropMode
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "OpenMode" ) );
713 if ( !sPropMediaType
.equals( sMediaType
) )
715 Error( "'MediaType' property contains wrong value, expected '"
716 + sMediaType
+ "', set '" + sPropMediaType
+ "' !" );
720 if ( bPropIsRoot
!= bIsRoot
)
722 Error( "'IsRoot' property contains wrong value!" );
727 && ( nPropMode
| ElementModes
.READ
) != ( nMode
| ElementModes
.READ
) )
728 || ( !bIsRoot
&& ( nPropMode
& nMode
) != nMode
) )
730 Error( "'OpenMode' property contains wrong value, expected " + nMode
+ ", in reality " + nPropMode
+ "!" );
736 Error( "Can't get properties of substorage, exception: " + e
);
741 Error( "Can't get XPropertySet implementation from storage!" );
747 public boolean InternalCheckStream( XStream xStream
,
752 boolean bCheckCompressed
)
754 // get input stream of substream
755 XInputStream xInput
= xStream
.getInputStream();
756 if ( xInput
== null )
758 Error( "Can't get XInputStream implementation from substream '" + sName
+ "'!" );
762 byte pContents
[][] = new byte[1][]; // ???
767 xInput
.readBytes( pContents
, pBytes
.length
+ 1 );
771 Error( "Can't read from stream '" + sName
+ "', exception: " + e
);
775 // check size of stream data
776 if ( pContents
.length
== 0 )
778 Error( "SubStream '" + sName
+ "' reading produced disaster!" );
782 if ( pBytes
.length
!= pContents
[0].length
)
784 Error( "SubStream '" + sName
+ "' contains wrong amount of data! (" + pContents
[0].length
+ "/" + pBytes
.length
+ ")" );
789 for ( int ind
= 0; ind
< pBytes
.length
; ind
++ )
791 if ( pBytes
[ind
] != pContents
[0][ind
] )
793 Error( "SubStream '" + sName
+ "' contains wrong data! ( byte num. "
794 + ind
+ " should be " + pBytes
[ind
] + " but it is " + pContents
[0][ind
] + ")" );
802 // get access to the XPropertySet interface
803 XPropertySet xPropSet
= (XPropertySet
) UnoRuntime
.queryInterface( XPropertySet
.class, xStream
);
804 if ( xPropSet
!= null )
808 // get "MediaType" and "Size" properties and control there values
809 String sPropMediaType
= AnyConverter
.toString( xPropSet
.getPropertyValue( "MediaType" ) );
810 int nPropSize
= AnyConverter
.toInt( xPropSet
.getPropertyValue( "Size" ) );
811 boolean bPropCompress
= AnyConverter
.toBoolean( xPropSet
.getPropertyValue( "Compressed" ) );
814 if ( !sPropMediaType
.equals( sMediaType
) )
816 Error( "'MediaType' property contains wrong value for stream '" + sName
+ "',\nexpected: '"
817 + sMediaType
+ "', set: '" + sPropMediaType
+ "'!" );
821 if ( nPropSize
!= pBytes
.length
)
823 Error( "'Size' property contains wrong value for stream'" + sName
+ "'!" );
827 if ( bCheckCompressed
&& bPropCompress
!= bCompressed
)
829 Error( "'Compressed' property contains wrong value for stream'" + sName
+ "'!" );
835 Error( "Can't get properties of substream '" + sName
+ "', exception: " + e
);
840 Error( "Can't get XPropertySet implementation from stream '" + sName
+ "'!" );
846 public boolean checkStream( XStorage xParentStorage
,
852 // open substream element first
853 XStream xSubStream
= null;
856 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
857 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
858 if ( xSubStream
== null )
860 Error( "Can't open substream '" + sName
+ "'!" );
866 Error( "Can't open substream '" + sName
+ "', exception : " + e
+ "!" );
870 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, bCompressed
, pBytes
, true );
872 // free the stream resources, garbage collector may remove the object too late
873 if ( !disposeStream( xSubStream
, sName
) )
879 public boolean checkEncrStream( XStorage xParentStorage
,
885 // Important: a common password for any of parent storage should not be set or
886 // should be different from sPass
890 Object oSubStream
= xParentStorage
.openStreamElement( sName
, ElementModes
.READ
);
891 Error( "Encrypted stream '" + sName
+ "' was opened without password!" );
894 catch( WrongPasswordException wpe
)
898 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' without password: " + e
+ "!" );
902 String sWrongPass
= "11";
906 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sWrongPass
);
907 Error( "Encrypted stream '" + sName
+ "' was opened with wrong password!" );
910 catch( WrongPasswordException wpe
)
914 Error( "Unexpected exception in case of opening of encrypted stream '" + sName
+ "' with wrong password: " + e
+ "!" );
918 XStream xSubStream
= null;
921 Object oSubStream
= xParentStorage
.openEncryptedStreamElement( sName
, ElementModes
.READ
, sPass
);
922 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
923 if ( xSubStream
== null )
925 Error( "Can't open encrypted substream '" + sName
+ "'!" );
931 Error( "Can't open encrypted substream '" + sName
+ "', exception : " + e
+ "!" );
935 // encrypted streams will be compressed always, so after the storing this property is always true,
936 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
937 // before the storing )
938 boolean bResult
= InternalCheckStream( xSubStream
, sName
, sMediaType
, true, pBytes
, false );
940 // free the stream resources, garbage collector may remove the object too late
941 if ( !disposeStream( xSubStream
, sName
) )
947 public boolean checkStreamH( XStorage xParentStorage
,
953 // open substream element first
954 XStream xSubStream
= null;
957 XHierarchicalStorageAccess xHStorage
=
958 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
959 if ( xHStorage
== null )
961 Error( "The storage does not support hierarchical access!" );
965 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
966 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
967 if ( xSubStream
== null )
969 Error( "Can't open substream '" + sPath
+ "'!" );
975 Error( "Can't open substream '" + sPath
+ "', exception : " + e
+ "!" );
979 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, bCompressed
, pBytes
, true );
981 // free the stream resources, garbage collector may remove the object too late
982 if ( !disposeStream( xSubStream
, sPath
) )
988 public boolean checkEncrStreamH( XStorage xParentStorage
,
994 // Important: a common password for any of parent storage should not be set or
995 // should be different from sPass
996 XHierarchicalStorageAccess xHStorage
=
997 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xParentStorage
);
998 if ( xHStorage
== null )
1000 Error( "The storage does not support hierarchical access!" );
1006 Object oSubStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, ElementModes
.READ
);
1007 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1008 Error( "Encrypted substream '" + sPath
+ "' was opened without password!" );
1011 catch( WrongPasswordException wpe
)
1013 catch( Exception e
)
1015 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' without password: " + e
+ "!" );
1019 String sWrongPass
= "11";
1020 sWrongPass
+= sPass
;
1023 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sWrongPass
);
1024 XStream xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1025 Error( "Encrypted substream '" + sPath
+ "' was opened with wrong password!" );
1028 catch( WrongPasswordException wpe
)
1030 catch( Exception e
)
1032 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath
+ "' with wrong password: " + e
+ "!" );
1036 XStream xSubStream
= null;
1039 Object oSubStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, ElementModes
.READ
, sPass
);
1040 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1041 if ( xSubStream
== null )
1043 Error( "Can't open encrypted substream '" + sPath
+ "'!" );
1047 catch( Exception e
)
1049 Error( "Can't open encrypted substream '" + sPath
+ "', exception : " + e
+ "!" );
1053 // encrypted streams will be compressed always, so after the storing this property is always true,
1054 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
1055 // before the storing )
1056 boolean bResult
= InternalCheckStream( xSubStream
, sPath
, sMediaType
, true, pBytes
, false );
1058 // free the stream resources, garbage collector may remove the object too late
1059 if ( !disposeStream( xSubStream
, sPath
) )
1065 public boolean copyStorage( XStorage xSourceStorage
, XStorage xDestStorage
)
1067 // copy xSourceStorage to xDestStorage
1070 xSourceStorage
.copyToStorage( xDestStorage
);
1072 catch( Exception e
)
1074 Error( "Storage copying failed, exception: " + e
);
1081 public boolean commitStorage( XStorage xStorage
)
1083 // XTransactedObject must be supported by storages
1084 XTransactedObject xTransact
= (XTransactedObject
) UnoRuntime
.queryInterface( XTransactedObject
.class, xStorage
);
1085 if ( xTransact
== null )
1087 Error( "Storage doesn't implement transacted access!" );
1095 catch( Exception e
)
1097 Error( "Storage commit failed, exception:" + e
);
1104 public boolean disposeStream( XStream xStream
, String sStreamName
)
1106 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStream
);
1107 if ( xComponent
== null )
1109 Error( "Can't get XComponent implementation from substream '" + sStreamName
+ "'!" );
1115 xComponent
.dispose();
1117 catch( Exception e
)
1119 Error( "Substream '" + sStreamName
+ "' disposing throws exception: " + e
);
1126 public boolean disposeStorage( XStorage xStorage
)
1128 // dispose the storage
1129 XComponent xComponent
= (XComponent
) UnoRuntime
.queryInterface( XComponent
.class, xStorage
);
1130 if ( xComponent
== null )
1132 Error( "Can't retrieve XComponent implementation from storage!" );
1138 xComponent
.dispose();
1140 catch( Exception e
)
1142 Error( "Storage disposing failed!" );
1149 public XInputStream
getInputStream( XStream xStream
)
1151 XInputStream xInTemp
= null;
1154 xInTemp
= xStream
.getInputStream();
1155 if ( xInTemp
== null )
1156 Error( "Can't get the input part of a stream!" );
1158 catch ( Exception e
)
1160 Error( "Can't get the input part of a stream, exception :" + e
);
1166 public boolean closeOutput( XStream xStream
)
1168 XOutputStream xOutTemp
= null;
1171 xOutTemp
= xStream
.getOutputStream();
1172 if ( xOutTemp
== null )
1174 Error( "Can't get the output part of a stream!" );
1178 catch ( Exception e
)
1180 Error( "Can't get the output part of a stream, exception :" + e
);
1186 xOutTemp
.closeOutput();
1188 catch ( Exception e
)
1190 Error( "Can't close output part of a stream, exception :" + e
);
1197 public XStorage
openSubStorage( XStorage xStorage
, String sName
, int nMode
)
1199 // open existing substorage
1202 Object oSubStorage
= xStorage
.openStorageElement( sName
, nMode
);
1203 XStorage xSubStorage
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oSubStorage
);
1206 catch( Exception e
)
1208 Error( "Can't open substorage '" + sName
+ "', exception: " + e
);
1214 public XStream
CreateTempFileStream( XMultiServiceFactory xMSF
)
1216 // try to get temporary file representation
1217 XStream xTempFileStream
= null;
1220 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1221 xTempFileStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, oTempFile
);
1223 catch( Exception e
)
1226 if ( xTempFileStream
== null )
1227 Error( "Can't create temporary file!" );
1229 return xTempFileStream
;
1232 public String
CreateTempFile( XMultiServiceFactory xMSF
)
1234 String sResult
= null;
1236 // try to get temporary file representation
1237 XPropertySet xTempFileProps
= null;
1240 Object oTempFile
= xMSF
.createInstance( "com.sun.star.io.TempFile" );
1241 xTempFileProps
= (XPropertySet
)UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
1243 catch( Exception e
)
1246 if ( xTempFileProps
!= null )
1250 xTempFileProps
.setPropertyValue( "RemoveFile", new Boolean( false ) );
1251 sResult
= AnyConverter
.toString( xTempFileProps
.getPropertyValue( "Uri" ) );
1253 catch( Exception e
)
1255 Error( "Can't control TempFile properties, exception: " + e
);
1260 Error( "Can't create temporary file representation!" );
1263 // close temporary file explicitly
1266 XStream xStream
= (XStream
)UnoRuntime
.queryInterface( XStream
.class, xTempFileProps
);
1267 if ( xStream
!= null )
1269 XOutputStream xOut
= xStream
.getOutputStream();
1273 XInputStream xIn
= xStream
.getInputStream();
1278 Error( "Can't close TempFile!" );
1280 catch( Exception e
)
1282 Error( "Can't close TempFile, exception: " + e
);
1288 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1290 // copy element with name sName from xSource to xDest
1293 xSource
.copyElementTo( sName
, xDest
, sName
);
1295 catch( Exception e
)
1297 Error( "Element copying failed, exception: " + e
);
1304 public boolean copyElementTo( XStorage xSource
, String sName
, XStorage xDest
, String sTargetName
)
1306 // copy element with name sName from xSource to xDest
1309 xSource
.copyElementTo( sName
, xDest
, sTargetName
);
1311 catch( Exception e
)
1313 Error( "Element copying failed, exception: " + e
);
1320 public boolean moveElementTo( XStorage xSource
, String sName
, XStorage xDest
)
1322 // move element with name sName from xSource to xDest
1325 xSource
.moveElementTo( sName
, xDest
, sName
);
1327 catch( Exception e
)
1329 Error( "Element moving failed, exception: " + e
);
1336 public boolean renameElement( XStorage xStorage
, String sOldName
, String sNewName
)
1338 // rename element with name sOldName to sNewName
1341 xStorage
.renameElement( sOldName
, sNewName
);
1343 catch( Exception e
)
1345 Error( "Element renaming failed, exception: " + e
);
1352 public boolean removeElement( XStorage xStorage
, String sName
)
1354 // remove element with name sName
1357 xStorage
.removeElement( sName
);
1359 catch( Exception e
)
1361 Error( "Element removing failed, exception: " + e
);
1368 public XStream
OpenStream( XStorage xStorage
,
1372 // open substream element
1373 XStream xSubStream
= null;
1376 Object oSubStream
= xStorage
.openStreamElement( sStreamName
, nMode
);
1377 xSubStream
= (XStream
) UnoRuntime
.queryInterface( XStream
.class, oSubStream
);
1378 if ( xSubStream
== null )
1379 Error( "Can't create substream '" + sStreamName
+ "'!" );
1381 catch( Exception e
)
1383 Error( "Can't create substream '" + sStreamName
+ "', exception : " + e
+ "!" );
1389 public boolean compareRawMethodsOnEncrStream( XStorage xStorage
, String sStreamName
)
1392 XStorageRawAccess xRawStorage
;
1395 xRawStorage
= (XStorageRawAccess
) UnoRuntime
.queryInterface( XStorageRawAccess
.class, xStorage
);
1397 catch( Exception e
)
1399 Error( "Can't get raw access to the storage, exception : " + e
+ "!" );
1403 if ( xRawStorage
== null )
1405 Error( "Can't get raw access to the storage!" );
1409 XInputStream xHeadRawStream
= null;
1412 xHeadRawStream
= xRawStorage
.getRawEncrStreamElement( sStreamName
);
1414 catch( Exception e
)
1416 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1419 XInputStream xPlainRawStream
= null;
1422 xPlainRawStream
= xRawStorage
.getPlainRawStreamElement( sStreamName
);
1424 catch( Exception e
)
1426 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw mode with header, exception : " + e
+ "!" );
1429 if ( xHeadRawStream
== null || xPlainRawStream
== null )
1431 Error( "Can't open encrypted stream '" + sStreamName
+ "' in raw modes!" );
1437 byte pData
[][] = new byte[1][22];
1438 if ( xHeadRawStream
.readBytes( pData
, 22 ) != 22 )
1440 Error( "Can't read header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1444 if ( pData
[0][0] != 0x4d || pData
[0][1] != 0x47 || pData
[0][2] != 0x02 || pData
[0][3] != 0x05 )
1446 Error( "No signature in the header of encrypted stream '" + sStreamName
+ "' raw representations!" );
1450 int nVariableHeaderLength
=
1451 ( pData
[0][14] + pData
[0][15] * 0x100 ) // salt length
1452 + ( pData
[0][16] + pData
[0][17] * 0x100 ) // iv length
1453 + ( pData
[0][18] + pData
[0][19] * 0x100 ) // digest length
1454 + ( pData
[0][20] + pData
[0][21] * 0x100 ); // mediatype length
1456 xHeadRawStream
.skipBytes( nVariableHeaderLength
);
1458 byte pRawData1
[][] = new byte[1][32000];
1459 byte pRawData2
[][] = new byte[1][32000];
1465 nRead1
= xHeadRawStream
.readBytes( pRawData1
, 32000 );
1466 nRead2
= xPlainRawStream
.readBytes( pRawData2
, 32000 );
1468 if ( nRead1
!= nRead2
)
1470 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different size!" );
1474 for ( int nInd
= 0; nInd
< nRead1
; nInd
++ )
1475 if ( pRawData1
[0][nInd
] != pRawData2
[0][nInd
] )
1477 Error( "The encrypted stream '" + sStreamName
+ "' raw representations have different data!" );
1481 while( nRead1
== 32000 );
1483 catch ( Exception e
)
1485 Error( "Can't compare stream '" + sStreamName
+ "' raw representations, exception : " + e
+ "!" );
1492 public boolean cantOpenStorage( XStorage xStorage
, String sName
)
1494 // try to open an opened substorage, open call must fail
1497 Object oDummyStorage
= xStorage
.openStorageElement( sName
, ElementModes
.READ
);
1498 Error( "The trying to reopen opened substorage '" + sName
+ "' must fail!" );
1500 catch( Exception e
)
1508 public boolean cantOpenStream( XStorage xStorage
, String sName
, int nMode
)
1510 // try to open the substream with specified mode must fail
1513 Object oDummyStream
= xStorage
.openStreamElement( sName
, nMode
);
1514 Error( "The trying to open substream '" + sName
+ "' must fail!" );
1516 catch( Exception e
)
1524 public boolean cantOpenStreamH( XStorage xStorage
, String sPath
, int nMode
)
1526 // try to open the substream with specified mode must fail
1528 XHierarchicalStorageAccess xHStorage
=
1529 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1530 if ( xHStorage
== null )
1532 Error( "The storage does not support hierarchical access!" );
1538 Object oDummyStream
= xHStorage
.openStreamElementByHierarchicalName( sPath
, nMode
);
1539 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1541 catch( Exception e
)
1549 public boolean cantOpenEncrStreamH( XStorage xStorage
, String sPath
, int nMode
, String aPass
)
1551 // try to open the substream with specified mode must fail
1553 XHierarchicalStorageAccess xHStorage
=
1554 (XHierarchicalStorageAccess
) UnoRuntime
.queryInterface( XHierarchicalStorageAccess
.class, xStorage
);
1555 if ( xHStorage
== null )
1557 Error( "The storage does not support hierarchical access!" );
1563 Object oDummyStream
= xHStorage
.openEncryptedStreamElementByHierarchicalName( sPath
, nMode
, aPass
);
1564 Error( "The trying to open substream '" + sPath
+ "' must fail!" );
1566 catch( WrongPasswordException wpe
)
1568 Error( "The substream '" + sPath
+ "' must not exist!" );
1571 catch( Exception e
)
1579 public XStorage
cloneStorage( XSingleServiceFactory xFactory
, XStorage xStorage
)
1581 // create a copy of a last commited version of specified storage
1582 XStorage xResult
= null;
1585 Object oTempStorage
= xFactory
.createInstance();
1586 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1587 if ( xResult
!= null )
1588 xStorage
.copyLastCommitTo( xResult
);
1590 catch( Exception e
)
1592 Error( "Can't clone storage, exception: " + e
);
1599 public XStorage
cloneSubStorage( XSingleServiceFactory xFactory
, XStorage xStorage
, String sName
)
1601 // create a copy of a last commited version of specified substorage
1602 XStorage xResult
= null;
1605 Object oTempStorage
= xFactory
.createInstance();
1606 xResult
= (XStorage
) UnoRuntime
.queryInterface( XStorage
.class, oTempStorage
);
1607 if ( xResult
!= null )
1608 xStorage
.copyStorageElementLastCommitTo( sName
, xResult
);
1610 catch( Exception e
)
1612 Error( "Can't clone substorage '" + sName
+ "', exception: " + e
);
1619 public XStream
cloneSubStream( XStorage xStorage
, String sName
)
1621 // clone existing substream
1624 XStream xStream
= xStorage
.cloneStreamElement( sName
);
1627 catch( Exception e
)
1629 Error( "Can't clone substream '" + sName
+ "', exception: " + e
);
1635 public XStream
cloneEncrSubStream( XStorage xStorage
, String sName
, String sPass
)
1637 // clone existing substream
1640 XStream xStream
= xStorage
.cloneEncryptedStreamElement( sName
, sPass
);
1643 catch( Exception e
)
1645 Error( "Can't clone encrypted substream '" + sName
+ "', exception: " + e
);
1651 public void Error( String sError
)
1653 m_aLogWriter
.println( m_sTestPrefix
+ "Error: " + sError
);
1656 public void Message( String sMessage
)
1658 m_aLogWriter
.println( m_sTestPrefix
+ sMessage
);