merge the formfield patch from ooo-build
[ooovba.git] / package / qa / storages / TestHelper.java
blob0c1580fc701429ff0284b7a7803f7f1793491485
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;
18 String m_sTestPrefix;
20 public TestHelper( LogWriter aLogWriter, String sTestPrefix )
22 m_aLogWriter = aLogWriter;
23 m_sTestPrefix = sTestPrefix;
26 public boolean WriteBytesToStream( XStream xStream,
27 String sStreamName,
28 String sMediaType,
29 boolean bCompressed,
30 byte[] pBytes )
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 + "'!" );
37 return false;
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 + "'!" );
45 return false;
48 // write requested byte sequence
49 try
51 xTruncate.truncate();
52 xOutput.writeBytes( pBytes );
54 catch( Exception e )
56 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
57 return false;
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 + "'!" );
65 return false;
68 // set properties to the stream
69 try
71 xPropSet.setPropertyValue( "MediaType", sMediaType );
72 xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
74 catch( Exception e )
76 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
77 return false;
80 // check size property of the stream
81 try
83 int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
84 if ( nSize != pBytes.length )
86 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
87 return false;
90 catch( Exception e )
92 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
93 return false;
96 return true;
99 public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage,
100 String sStreamName,
101 String sMediaType,
102 byte[] pBytes )
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 + "'!" );
113 return false;
116 catch( Exception e )
118 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
119 return false;
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 + "'!" );
127 return false;
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 + "'!" );
135 return false;
138 // write requested byte sequence
141 xTruncate.truncate();
142 xOutput.writeBytes( pBytes );
144 catch( Exception e )
146 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
147 return false;
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 + "'!" );
155 return false;
158 // set properties to the stream
159 // do not set the compressed property
162 xPropSet.setPropertyValue( "MediaType", sMediaType );
164 catch( Exception e )
166 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
167 return false;
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!" );
177 return false;
180 catch( Exception e )
182 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
183 return false;
186 // free the stream resources, garbage collector may remove the object too late
187 if ( !disposeStream( xSubStream, sStreamName ) )
188 return false;
190 return true;
193 public boolean WriteBytesToSubstream( XStorage xStorage,
194 String sStreamName,
195 String sMediaType,
196 boolean bCompressed,
197 byte[] pBytes )
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 + "'!" );
208 return false;
211 catch( Exception e )
213 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
214 return false;
217 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
218 return false;
220 // free the stream resources, garbage collector may remove the object too late
221 if ( !disposeStream( xSubStream, sStreamName ) )
222 return false;
224 return true;
227 public boolean WriteBytesToEncrSubstream( XStorage xStorage,
228 String sStreamName,
229 String sMediaType,
230 boolean bCompressed,
231 byte[] pBytes,
232 String sPass )
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 + "'!" );
243 return false;
246 catch( Exception e )
248 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
249 return false;
252 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
253 return false;
255 // free the stream resources, garbage collector may remove the object too late
256 if ( !disposeStream( xSubStream, sStreamName ) )
257 return false;
259 return true;
262 public boolean WBToSubstrOfEncr( XStorage xStorage,
263 String sStreamName,
264 String sMediaType,
265 boolean bCompressed,
266 byte[] pBytes,
267 boolean bEncrypted )
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 + "'!" );
278 return false;
281 catch( Exception e )
283 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
284 return false;
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 + "'!" );
292 return false;
295 // set properties to the stream
298 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
300 catch( Exception e )
302 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
303 return false;
306 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
307 return false;
309 // free the stream resources, garbage collector may remove the object too late
310 if ( !disposeStream( xSubStream, sStreamName ) )
311 return false;
313 return true;
316 public boolean WriteBytesToStreamH( XStorage xStorage,
317 String sStreamPath,
318 String sMediaType,
319 boolean bCompressed,
320 byte[] pBytes,
321 boolean bCommit )
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!" );
332 return false;
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 + "'!" );
340 return false;
343 catch( Exception e )
345 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
346 return false;
349 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
350 return false;
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!" );
357 return false;
360 if ( bCommit )
362 try {
363 xTransact.commit();
364 } catch( Exception e )
366 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
367 return false;
371 // free the stream resources, garbage collector may remove the object too late
372 if ( !disposeStream( xSubStream, sStreamPath ) )
373 return false;
375 return true;
378 public boolean WriteBytesToEncrStreamH( XStorage xStorage,
379 String sStreamPath,
380 String sMediaType,
381 boolean bCompressed,
382 byte[] pBytes,
383 String sPass,
384 boolean bCommit )
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!" );
395 return false;
398 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
399 ElementModes.WRITE,
400 sPass );
401 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
402 if ( xSubStream == null )
404 Error( "Can't create substream '" + sStreamPath + "'!" );
405 return false;
408 catch( Exception e )
410 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
411 return false;
414 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
415 return false;
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!" );
422 return false;
425 if ( bCommit )
427 try {
428 xTransact.commit();
429 } catch( Exception e )
431 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
432 return false;
436 // free the stream resources, garbage collector may remove the object too late
437 if ( !disposeStream( xSubStream, sStreamPath ) )
438 return false;
440 return true;
443 public boolean WBToSubstrOfEncrH( XStorage xStorage,
444 String sStreamPath,
445 String sMediaType,
446 boolean bCompressed,
447 byte[] pBytes,
448 boolean bEncrypted,
449 boolean bCommit )
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!" );
460 return false;
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 + "'!" );
468 return false;
471 catch( Exception e )
473 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
474 return false;
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 + "'!" );
482 return false;
485 // set properties to the stream
488 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
490 catch( Exception e )
492 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
493 return false;
496 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
497 return false;
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!" );
504 return false;
507 if ( bCommit )
509 try {
510 xTransact.commit();
511 } catch( Exception e )
513 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
514 return false;
518 // free the stream resources, garbage collector may remove the object too late
519 if ( !disposeStream( xSubStream, sStreamPath ) )
520 return false;
522 return true;
525 public int ChangeStreamPass( XStorage xStorage,
526 String sStreamName,
527 String sOldPass,
528 String sNewPass )
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 + "'!" );
539 return 0;
542 catch( Exception e )
544 Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
545 return 0;
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!" );
556 return -1;
559 try {
560 xStreamEncryption.setEncryptionPassword( sNewPass );
562 catch( Exception e )
564 Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
565 return 0;
568 // free the stream resources, garbage collector may remove the object too late
569 if ( !disposeStream( xSubStream, sStreamName ) )
570 return 0;
572 return 1;
575 public int ChangeStreamPassH( XStorage xStorage,
576 String sPath,
577 String sOldPass,
578 String sNewPass,
579 boolean bCommit )
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!" );
587 return 0;
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 + "'!" );
598 return 0;
601 catch( Exception e )
603 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
604 return 0;
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!" );
614 return -1;
617 try {
618 xStreamEncryption.setEncryptionPassword( sNewPass );
620 catch( Exception e )
622 Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
623 return 0;
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!" );
631 return 0;
634 if ( bCommit )
636 try {
637 xTransact.commit();
638 } catch( Exception e )
640 Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
641 return 0;
645 // free the stream resources, garbage collector may remove the object too late
646 if ( !disposeStream( xSubStream, sPath ) )
647 return 0;
649 return 1;
652 public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
654 boolean bOk = false;
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" ) );
669 bOk = true;
670 if ( bPropIsRoot != bIsRoot )
672 Error( "'IsRoot' property contains wrong value!" );
673 bOk = false;
676 if ( ( bIsRoot
677 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
678 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
680 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
681 bOk = false;
684 catch( Exception e )
686 Error( "Can't control properties of substorage, exception: " + e );
689 else
691 Error( "Can't get XPropertySet implementation from storage!" );
694 return bOk;
697 public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
699 boolean bOk = false;
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" ) );
712 bOk = true;
713 if ( !sPropMediaType.equals( sMediaType ) )
715 Error( "'MediaType' property contains wrong value, expected '"
716 + sMediaType + "', set '" + sPropMediaType + "' !" );
717 bOk = false;
720 if ( bPropIsRoot != bIsRoot )
722 Error( "'IsRoot' property contains wrong value!" );
723 bOk = false;
726 if ( ( bIsRoot
727 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
728 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
730 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
731 bOk = false;
734 catch( Exception e )
736 Error( "Can't get properties of substorage, exception: " + e );
739 else
741 Error( "Can't get XPropertySet implementation from storage!" );
744 return bOk;
747 public boolean InternalCheckStream( XStream xStream,
748 String sName,
749 String sMediaType,
750 boolean bCompressed,
751 byte[] pBytes,
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 + "'!" );
759 return false;
762 byte pContents[][] = new byte[1][]; // ???
764 // read contents
767 xInput.readBytes( pContents, pBytes.length + 1 );
769 catch( Exception e )
771 Error( "Can't read from stream '" + sName + "', exception: " + e );
772 return false;
775 // check size of stream data
776 if ( pContents.length == 0 )
778 Error( "SubStream '" + sName + "' reading produced disaster!" );
779 return false;
782 if ( pBytes.length != pContents[0].length )
784 Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
785 return false;
788 // check stream data
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] + ")" );
795 return false;
799 // check properties
800 boolean bOk = false;
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" ) );
813 bOk = true;
814 if ( !sPropMediaType.equals( sMediaType ) )
816 Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
817 + sMediaType + "', set: '" + sPropMediaType + "'!" );
818 bOk = false;
821 if ( nPropSize != pBytes.length )
823 Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
824 bOk = false;
827 if ( bCheckCompressed && bPropCompress != bCompressed )
829 Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
830 bOk = false;
833 catch( Exception e )
835 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
838 else
840 Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
843 return bOk;
846 public boolean checkStream( XStorage xParentStorage,
847 String sName,
848 String sMediaType,
849 boolean bCompressed,
850 byte[] pBytes )
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 + "'!" );
861 return false;
864 catch( Exception e )
866 Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
867 return false;
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 ) )
874 return false;
876 return bResult;
879 public boolean checkEncrStream( XStorage xParentStorage,
880 String sName,
881 String sMediaType,
882 byte[] pBytes,
883 String sPass )
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!" );
892 return false;
894 catch( WrongPasswordException wpe )
896 catch( Exception e )
898 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
899 return false;
902 String sWrongPass = "11";
903 sWrongPass += sPass;
906 Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
907 Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
908 return false;
910 catch( WrongPasswordException wpe )
912 catch( Exception e )
914 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
915 return false;
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 + "'!" );
926 return false;
929 catch( Exception e )
931 Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
932 return false;
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 ) )
942 return false;
944 return bResult;
947 public boolean checkStreamH( XStorage xParentStorage,
948 String sPath,
949 String sMediaType,
950 boolean bCompressed,
951 byte[] pBytes )
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!" );
962 return false;
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 + "'!" );
970 return false;
973 catch( Exception e )
975 Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
976 return false;
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 ) )
983 return false;
985 return bResult;
988 public boolean checkEncrStreamH( XStorage xParentStorage,
989 String sPath,
990 String sMediaType,
991 byte[] pBytes,
992 String sPass )
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!" );
1001 return false;
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!" );
1009 return false;
1011 catch( WrongPasswordException wpe )
1013 catch( Exception e )
1015 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
1016 return false;
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!" );
1026 return false;
1028 catch( WrongPasswordException wpe )
1030 catch( Exception e )
1032 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
1033 return false;
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 + "'!" );
1044 return false;
1047 catch( Exception e )
1049 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
1050 return false;
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 ) )
1060 return false;
1062 return bResult;
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 );
1075 return false;
1078 return true;
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!" );
1088 return false;
1093 xTransact.commit();
1095 catch( Exception e )
1097 Error( "Storage commit failed, exception:" + e );
1098 return false;
1101 return true;
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 + "'!" );
1110 return false;
1115 xComponent.dispose();
1117 catch( Exception e )
1119 Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
1120 return false;
1123 return true;
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!" );
1133 return false;
1138 xComponent.dispose();
1140 catch( Exception e )
1142 Error( "Storage disposing failed!" );
1143 return false;
1146 return true;
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 );
1163 return xInTemp;
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!" );
1175 return false;
1178 catch ( Exception e )
1180 Error( "Can't get the output part of a stream, exception :" + e );
1181 return false;
1186 xOutTemp.closeOutput();
1188 catch ( Exception e )
1190 Error( "Can't close output part of a stream, exception :" + e );
1191 return false;
1194 return true;
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 );
1204 return xSubStorage;
1206 catch( Exception e )
1208 Error( "Can't open substorage '" + sName + "', exception: " + e );
1211 return null;
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 );
1258 else
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();
1270 if ( xOut != null )
1271 xOut.closeOutput();
1273 XInputStream xIn = xStream.getInputStream();
1274 if ( xIn != null )
1275 xIn.closeInput();
1277 else
1278 Error( "Can't close TempFile!" );
1280 catch( Exception e )
1282 Error( "Can't close TempFile, exception: " + e );
1285 return sResult;
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 );
1298 return false;
1301 return true;
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 );
1314 return false;
1317 return true;
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 );
1330 return false;
1333 return true;
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 );
1346 return false;
1349 return true;
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 );
1362 return false;
1365 return true;
1368 public XStream OpenStream( XStorage xStorage,
1369 String sStreamName,
1370 int nMode )
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 + "!" );
1386 return xSubStream;
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 + "!" );
1400 return false;
1403 if ( xRawStorage == null )
1405 Error( "Can't get raw access to the storage!" );
1406 return false;
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!" );
1432 return false;
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!" );
1441 return false;
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!" );
1447 return false;
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];
1460 int nRead1 = 0;
1461 int nRead2 = 0;
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!" );
1471 return false;
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!" );
1478 return false;
1481 while( nRead1 == 32000 );
1483 catch ( Exception e )
1485 Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : " + e + "!" );
1486 return false;
1489 return true;
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 )
1502 return true;
1505 return false;
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 )
1518 return true;
1521 return false;
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!" );
1533 return false;
1538 Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
1539 Error( "The trying to open substream '" + sPath + "' must fail!" );
1541 catch( Exception e )
1543 return true;
1546 return false;
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!" );
1558 return false;
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!" );
1569 return false;
1571 catch( Exception e )
1573 return true;
1576 return false;
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 );
1593 return null;
1596 return xResult;
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 );
1613 return null;
1616 return xResult;
1619 public XStream cloneSubStream( XStorage xStorage, String sName )
1621 // clone existing substream
1624 XStream xStream = xStorage.cloneStreamElement( sName );
1625 return xStream;
1627 catch( Exception e )
1629 Error( "Can't clone substream '" + sName + "', exception: " + e );
1632 return null;
1635 public XStream cloneEncrSubStream( XStorage xStorage, String sName, String sPass )
1637 // clone existing substream
1640 XStream xStream = xStorage.cloneEncryptedStreamElement( sName, sPass );
1641 return xStream;
1643 catch( Exception e )
1645 Error( "Can't clone encrypted substream '" + sName + "', exception: " + e );
1648 return null;
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 );