fix toolbar import
[ooovba.git] / package / qa / storages / TestHelper.java
blob687b7b8df990b8203e2b939bc816fad3bab2ab47
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;
100 public boolean WriteBytesToSubstream( XStorage xStorage,
101 String sStreamName,
102 String sMediaType,
103 boolean bCompressed,
104 byte[] pBytes )
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 + "'!" );
115 return false;
118 catch( Exception e )
120 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
121 return false;
124 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
125 return false;
127 // free the stream resources, garbage collector may remove the object too late
128 if ( !disposeStream( xSubStream, sStreamName ) )
129 return false;
131 return true;
134 public boolean WriteBytesToEncrSubstream( XStorage xStorage,
135 String sStreamName,
136 String sMediaType,
137 boolean bCompressed,
138 byte[] pBytes,
139 String sPass )
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 + "'!" );
150 return false;
153 catch( Exception e )
155 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
156 return false;
159 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
160 return false;
162 // free the stream resources, garbage collector may remove the object too late
163 if ( !disposeStream( xSubStream, sStreamName ) )
164 return false;
166 return true;
169 public boolean WBToSubstrOfEncr( XStorage xStorage,
170 String sStreamName,
171 String sMediaType,
172 boolean bCompressed,
173 byte[] pBytes,
174 boolean bEncrypted )
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 + "'!" );
185 return false;
188 catch( Exception e )
190 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
191 return false;
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 + "'!" );
199 return false;
202 // set properties to the stream
205 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
207 catch( Exception e )
209 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
210 return false;
213 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
214 return false;
216 // free the stream resources, garbage collector may remove the object too late
217 if ( !disposeStream( xSubStream, sStreamName ) )
218 return false;
220 return true;
223 public boolean WriteBytesToStreamH( XStorage xStorage,
224 String sStreamPath,
225 String sMediaType,
226 boolean bCompressed,
227 byte[] pBytes,
228 boolean bCommit )
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!" );
239 return false;
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 + "'!" );
247 return false;
250 catch( Exception e )
252 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
253 return false;
256 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
257 return false;
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!" );
264 return false;
267 if ( bCommit )
269 try {
270 xTransact.commit();
271 } catch( Exception e )
273 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
274 return false;
278 // free the stream resources, garbage collector may remove the object too late
279 if ( !disposeStream( xSubStream, sStreamPath ) )
280 return false;
282 return true;
285 public boolean WriteBytesToEncrStreamH( XStorage xStorage,
286 String sStreamPath,
287 String sMediaType,
288 boolean bCompressed,
289 byte[] pBytes,
290 String sPass,
291 boolean bCommit )
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!" );
302 return false;
305 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
306 ElementModes.WRITE,
307 sPass );
308 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
309 if ( xSubStream == null )
311 Error( "Can't create substream '" + sStreamPath + "'!" );
312 return false;
315 catch( Exception e )
317 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
318 return false;
321 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
322 return false;
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!" );
329 return false;
332 if ( bCommit )
334 try {
335 xTransact.commit();
336 } catch( Exception e )
338 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
339 return false;
343 // free the stream resources, garbage collector may remove the object too late
344 if ( !disposeStream( xSubStream, sStreamPath ) )
345 return false;
347 return true;
350 public boolean WBToSubstrOfEncrH( XStorage xStorage,
351 String sStreamPath,
352 String sMediaType,
353 boolean bCompressed,
354 byte[] pBytes,
355 boolean bEncrypted,
356 boolean bCommit )
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!" );
367 return false;
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 + "'!" );
375 return false;
378 catch( Exception e )
380 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
381 return false;
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 + "'!" );
389 return false;
392 // set properties to the stream
395 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
397 catch( Exception e )
399 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
400 return false;
403 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
404 return false;
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!" );
411 return false;
414 if ( bCommit )
416 try {
417 xTransact.commit();
418 } catch( Exception e )
420 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
421 return false;
425 // free the stream resources, garbage collector may remove the object too late
426 if ( !disposeStream( xSubStream, sStreamPath ) )
427 return false;
429 return true;
432 public int ChangeStreamPass( XStorage xStorage,
433 String sStreamName,
434 String sOldPass,
435 String sNewPass )
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 + "'!" );
446 return 0;
449 catch( Exception e )
451 Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
452 return 0;
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!" );
463 return -1;
466 try {
467 xStreamEncryption.setEncryptionPassword( sNewPass );
469 catch( Exception e )
471 Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
472 return 0;
475 // free the stream resources, garbage collector may remove the object too late
476 if ( !disposeStream( xSubStream, sStreamName ) )
477 return 0;
479 return 1;
482 public int ChangeStreamPassH( XStorage xStorage,
483 String sPath,
484 String sOldPass,
485 String sNewPass,
486 boolean bCommit )
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!" );
494 return 0;
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 + "'!" );
505 return 0;
508 catch( Exception e )
510 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
511 return 0;
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!" );
521 return -1;
524 try {
525 xStreamEncryption.setEncryptionPassword( sNewPass );
527 catch( Exception e )
529 Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
530 return 0;
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!" );
538 return 0;
541 if ( bCommit )
543 try {
544 xTransact.commit();
545 } catch( Exception e )
547 Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
548 return 0;
552 // free the stream resources, garbage collector may remove the object too late
553 if ( !disposeStream( xSubStream, sPath ) )
554 return 0;
556 return 1;
559 public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
561 boolean bOk = false;
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" ) );
576 bOk = true;
577 if ( bPropIsRoot != bIsRoot )
579 Error( "'IsRoot' property contains wrong value!" );
580 bOk = false;
583 if ( ( bIsRoot
584 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
585 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
587 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
588 bOk = false;
591 catch( Exception e )
593 Error( "Can't control properties of substorage, exception: " + e );
596 else
598 Error( "Can't get XPropertySet implementation from storage!" );
601 return bOk;
604 public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
606 boolean bOk = false;
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" ) );
619 bOk = true;
620 if ( !sPropMediaType.equals( sMediaType ) )
622 Error( "'MediaType' property contains wrong value, expected '"
623 + sMediaType + "', set '" + sPropMediaType + "' !" );
624 bOk = false;
627 if ( bPropIsRoot != bIsRoot )
629 Error( "'IsRoot' property contains wrong value!" );
630 bOk = false;
633 if ( ( bIsRoot
634 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
635 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
637 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
638 bOk = false;
641 catch( Exception e )
643 Error( "Can't get properties of substorage, exception: " + e );
646 else
648 Error( "Can't get XPropertySet implementation from storage!" );
651 return bOk;
654 public boolean InternalCheckStream( XStream xStream,
655 String sName,
656 String sMediaType,
657 boolean bCompressed,
658 byte[] pBytes,
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 + "'!" );
666 return false;
669 byte pContents[][] = new byte[1][]; // ???
671 // read contents
674 xInput.readBytes( pContents, pBytes.length + 1 );
676 catch( Exception e )
678 Error( "Can't read from stream '" + sName + "', exception: " + e );
679 return false;
682 // check size of stream data
683 if ( pContents.length == 0 )
685 Error( "SubStream '" + sName + "' reading produced disaster!" );
686 return false;
689 if ( pBytes.length != pContents[0].length )
691 Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
692 return false;
695 // check stream data
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] + ")" );
702 return false;
706 // check properties
707 boolean bOk = false;
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" ) );
720 bOk = true;
721 if ( !sPropMediaType.equals( sMediaType ) )
723 Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
724 + sMediaType + "', set: '" + sPropMediaType + "'!" );
725 bOk = false;
728 if ( nPropSize != pBytes.length )
730 Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
731 bOk = false;
734 if ( bCheckCompressed && bPropCompress != bCompressed )
736 Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
737 bOk = false;
740 catch( Exception e )
742 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
745 else
747 Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
750 return bOk;
753 public boolean checkStream( XStorage xParentStorage,
754 String sName,
755 String sMediaType,
756 boolean bCompressed,
757 byte[] pBytes )
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 + "'!" );
768 return false;
771 catch( Exception e )
773 Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
774 return false;
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 ) )
781 return false;
783 return bResult;
786 public boolean checkEncrStream( XStorage xParentStorage,
787 String sName,
788 String sMediaType,
789 byte[] pBytes,
790 String sPass )
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!" );
799 return false;
801 catch( WrongPasswordException wpe )
803 catch( Exception e )
805 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
806 return false;
809 String sWrongPass = "11";
810 sWrongPass += sPass;
813 Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
814 Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
815 return false;
817 catch( WrongPasswordException wpe )
819 catch( Exception e )
821 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
822 return false;
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 + "'!" );
833 return false;
836 catch( Exception e )
838 Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
839 return false;
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 ) )
849 return false;
851 return bResult;
854 public boolean checkStreamH( XStorage xParentStorage,
855 String sPath,
856 String sMediaType,
857 boolean bCompressed,
858 byte[] pBytes )
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!" );
869 return false;
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 + "'!" );
877 return false;
880 catch( Exception e )
882 Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
883 return false;
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 ) )
890 return false;
892 return bResult;
895 public boolean checkEncrStreamH( XStorage xParentStorage,
896 String sPath,
897 String sMediaType,
898 byte[] pBytes,
899 String sPass )
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!" );
908 return false;
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!" );
916 return false;
918 catch( WrongPasswordException wpe )
920 catch( Exception e )
922 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
923 return false;
926 String sWrongPass = "11";
927 sWrongPass += sPass;
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!" );
933 return false;
935 catch( WrongPasswordException wpe )
937 catch( Exception e )
939 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
940 return false;
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 + "'!" );
951 return false;
954 catch( Exception e )
956 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
957 return false;
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 ) )
967 return false;
969 return bResult;
972 public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
974 // copy xSourceStorage to xDestStorage
977 xSourceStorage.copyToStorage( xDestStorage );
979 catch( Exception e )
981 Error( "Storage copying failed, exception: " + e );
982 return false;
985 return true;
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!" );
995 return false;
1000 xTransact.commit();
1002 catch( Exception e )
1004 Error( "Storage commit failed, exception:" + e );
1005 return false;
1008 return true;
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 + "'!" );
1017 return false;
1022 xComponent.dispose();
1024 catch( Exception e )
1026 Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
1027 return false;
1030 return true;
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!" );
1040 return false;
1045 xComponent.dispose();
1047 catch( Exception e )
1049 Error( "Storage disposing failed!" );
1050 return false;
1053 return true;
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 );
1070 return xInTemp;
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!" );
1082 return false;
1085 catch ( Exception e )
1087 Error( "Can't get the output part of a stream, exception :" + e );
1088 return false;
1093 xOutTemp.closeOutput();
1095 catch ( Exception e )
1097 Error( "Can't close output part of a stream, exception :" + e );
1098 return false;
1101 return true;
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 );
1111 return xSubStorage;
1113 catch( Exception e )
1115 Error( "Can't open substorage '" + sName + "', exception: " + e );
1118 return null;
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 );
1165 else
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();
1177 if ( xOut != null )
1178 xOut.closeOutput();
1180 XInputStream xIn = xStream.getInputStream();
1181 if ( xIn != null )
1182 xIn.closeInput();
1184 else
1185 Error( "Can't close TempFile!" );
1187 catch( Exception e )
1189 Error( "Can't close TempFile, exception: " + e );
1192 return sResult;
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 );
1205 return false;
1208 return true;
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 );
1221 return false;
1224 return true;
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 );
1237 return false;
1240 return true;
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 );
1253 return false;
1256 return true;
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 );
1269 return false;
1272 return true;
1275 public XStream OpenStream( XStorage xStorage,
1276 String sStreamName,
1277 int nMode )
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 + "!" );
1293 return xSubStream;
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 + "!" );
1307 return false;
1310 if ( xRawStorage == null )
1312 Error( "Can't get raw access to the storage!" );
1313 return false;
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!" );
1339 return false;
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!" );
1348 return false;
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!" );
1354 return false;
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];
1367 int nRead1 = 0;
1368 int nRead2 = 0;
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!" );
1378 return false;
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!" );
1385 return false;
1388 while( nRead1 == 32000 );
1390 catch ( Exception e )
1392 Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : " + e + "!" );
1393 return false;
1396 return true;
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 )
1409 return true;
1412 return false;
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 )
1425 return true;
1428 return false;
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!" );
1440 return false;
1445 Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
1446 Error( "The trying to open substream '" + sPath + "' must fail!" );
1448 catch( Exception e )
1450 return true;
1453 return false;
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!" );
1465 return false;
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!" );
1476 return false;
1478 catch( Exception e )
1480 return true;
1483 return false;
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 );
1500 return null;
1503 return xResult;
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 );
1520 return null;
1523 return xResult;
1526 public XStream cloneSubStream( XStorage xStorage, String sName )
1528 // clone existing substream
1531 XStream xStream = xStorage.cloneStreamElement( sName );
1532 return xStream;
1534 catch( Exception e )
1536 Error( "Can't clone substream '" + sName + "', exception: " + e );
1539 return null;
1542 public XStream cloneEncrSubStream( XStorage xStorage, String sName, String sPass )
1544 // clone existing substream
1547 XStream xStream = xStorage.cloneEncryptedStreamElement( sName, sPass );
1548 return xStream;
1550 catch( Exception e )
1552 Error( "Can't clone encrypted substream '" + sName + "', exception: " + e );
1555 return null;
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 );