bump product version to 6.4.0.3
[LibreOffice.git] / package / qa / storages / TestHelper.java
blobd6a4fd280d0836aa34ffabc3e71c67de7ac133e0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.storages;
21 import com.sun.star.uno.UnoRuntime;
22 import com.sun.star.uno.XInterface;
23 import com.sun.star.uno.AnyConverter;
25 import com.sun.star.lang.*;
26 import com.sun.star.embed.*;
27 import com.sun.star.packages.*;
28 import com.sun.star.io.*;
29 import com.sun.star.beans.*;
31 import share.LogWriter;
33 public class TestHelper {
35 LogWriter m_aLogWriter;
36 String m_sTestPrefix;
38 public TestHelper( LogWriter aLogWriter, String sTestPrefix )
40 m_aLogWriter = aLogWriter;
41 m_sTestPrefix = sTestPrefix;
44 public boolean WriteBytesToStream( XStream xStream,
45 String sStreamName,
46 String sMediaType,
47 boolean bCompressed,
48 byte[] pBytes )
50 // get output stream of substream
51 XOutputStream xOutput = xStream.getOutputStream();
52 if ( xOutput == null )
54 Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
55 return false;
58 // get XTruncate implementation from output stream
59 XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
60 if ( xTruncate == null )
62 Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
63 return false;
66 // write requested byte sequence
67 try
69 xTruncate.truncate();
70 xOutput.writeBytes( pBytes );
72 catch( Exception e )
74 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
75 return false;
78 // get access to the XPropertySet interface
79 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
80 if ( xPropSet == null )
82 Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
83 return false;
86 // set properties to the stream
87 try
89 xPropSet.setPropertyValue( "MediaType", sMediaType );
90 xPropSet.setPropertyValue( "Compressed", Boolean.valueOf( bCompressed ) );
92 catch( Exception e )
94 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
95 return false;
98 // check size property of the stream
99 try
101 long nSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
102 if ( nSize != pBytes.length )
104 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
105 return false;
108 catch( Exception e )
110 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
111 return false;
114 return true;
117 public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage,
118 String sStreamName,
119 String sMediaType,
120 byte[] pBytes )
122 // open substream element
123 XStream xSubStream = null;
126 Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
127 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
128 if ( xSubStream == null )
130 Error( "Can't create substream '" + sStreamName + "'!" );
131 return false;
134 catch( Exception e )
136 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
137 return false;
140 // get output stream of substream
141 XOutputStream xOutput = xSubStream.getOutputStream();
142 if ( xOutput == null )
144 Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
145 return false;
148 // get XTruncate implementation from output stream
149 XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
150 if ( xTruncate == null )
152 Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
153 return false;
156 // write requested byte sequence
159 xTruncate.truncate();
160 xOutput.writeBytes( pBytes );
162 catch( Exception e )
164 Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
165 return false;
168 // get access to the XPropertySet interface
169 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
170 if ( xPropSet == null )
172 Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
173 return false;
176 // set properties to the stream
177 // do not set the compressed property
180 xPropSet.setPropertyValue( "MediaType", sMediaType );
182 catch( Exception e )
184 Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
185 return false;
188 // check size property of the stream
191 long nSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
192 if ( nSize != pBytes.length )
194 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
195 return false;
198 catch( Exception e )
200 Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
201 return false;
204 // free the stream resources, garbage collector may remove the object too late
205 if ( !disposeStream( xSubStream, sStreamName ) )
206 return false;
208 return true;
211 public boolean WriteBytesToSubstream( XStorage xStorage,
212 String sStreamName,
213 String sMediaType,
214 boolean bCompressed,
215 byte[] pBytes )
217 // open substream element
218 XStream xSubStream = null;
221 Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
222 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
223 if ( xSubStream == null )
225 Error( "Can't create substream '" + sStreamName + "'!" );
226 return false;
229 catch( Exception e )
231 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
232 return false;
235 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
236 return false;
238 // free the stream resources, garbage collector may remove the object too late
239 if ( !disposeStream( xSubStream, sStreamName ) )
240 return false;
242 return true;
245 public boolean WriteBytesToEncrSubstream( XStorage xStorage,
246 String sStreamName,
247 String sMediaType,
248 boolean bCompressed,
249 byte[] pBytes,
250 String sPass )
252 // open substream element
253 XStream xSubStream = null;
256 Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sPass );
257 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
258 if ( xSubStream == null )
260 Error( "Can't create substream '" + sStreamName + "'!" );
261 return false;
264 catch( Exception e )
266 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
267 return false;
270 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
271 return false;
273 // free the stream resources, garbage collector may remove the object too late
274 if ( !disposeStream( xSubStream, sStreamName ) )
275 return false;
277 return true;
280 public boolean WBToSubstrOfEncr( XStorage xStorage,
281 String sStreamName,
282 String sMediaType,
283 boolean bCompressed,
284 byte[] pBytes,
285 boolean bEncrypted )
287 // open substream element
288 XStream xSubStream = null;
291 Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
292 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
293 if ( xSubStream == null )
295 Error( "Can't create substream '" + sStreamName + "'!" );
296 return false;
299 catch( Exception e )
301 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
302 return false;
305 // get access to the XPropertySet interface
306 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
307 if ( xPropSet == null )
309 Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
310 return false;
313 // set properties to the stream
316 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", Boolean.valueOf( bEncrypted ) );
318 catch( Exception e )
320 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
321 return false;
324 if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
325 return false;
327 // free the stream resources, garbage collector may remove the object too late
328 if ( !disposeStream( xSubStream, sStreamName ) )
329 return false;
331 return true;
334 public boolean WriteBytesToStreamH( XStorage xStorage,
335 String sStreamPath,
336 String sMediaType,
337 boolean bCompressed,
338 byte[] pBytes,
339 boolean bCommit )
341 // open substream element
342 XStream xSubStream = null;
345 XHierarchicalStorageAccess xHStorage =
346 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
347 if ( xHStorage == null )
349 Error( "The storage does not support hierarchical access!" );
350 return false;
353 Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
354 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
355 if ( xSubStream == null )
357 Error( "Can't create substream '" + sStreamPath + "'!" );
358 return false;
361 catch( Exception e )
363 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
364 return false;
367 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
368 return false;
370 XTransactedObject xTransact =
371 (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
372 if ( xTransact == null )
374 Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
375 return false;
378 if ( bCommit )
380 try {
381 xTransact.commit();
382 } catch( Exception e )
384 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
385 return false;
389 // free the stream resources, garbage collector may remove the object too late
390 if ( !disposeStream( xSubStream, sStreamPath ) )
391 return false;
393 return true;
396 public boolean WriteBytesToEncrStreamH( XStorage xStorage,
397 String sStreamPath,
398 String sMediaType,
399 boolean bCompressed,
400 byte[] pBytes,
401 String sPass,
402 boolean bCommit )
404 // open substream element
405 XStream xSubStream = null;
408 XHierarchicalStorageAccess xHStorage =
409 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
410 if ( xHStorage == null )
412 Error( "The storage does not support hierarchical access!" );
413 return false;
416 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
417 ElementModes.WRITE,
418 sPass );
419 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
420 if ( xSubStream == null )
422 Error( "Can't create substream '" + sStreamPath + "'!" );
423 return false;
426 catch( Exception e )
428 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
429 return false;
432 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
433 return false;
435 XTransactedObject xTransact =
436 (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
437 if ( xTransact == null )
439 Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
440 return false;
443 if ( bCommit )
445 try {
446 xTransact.commit();
447 } catch( Exception e )
449 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
450 return false;
454 // free the stream resources, garbage collector may remove the object too late
455 if ( !disposeStream( xSubStream, sStreamPath ) )
456 return false;
458 return true;
461 public boolean WBToSubstrOfEncrH( XStorage xStorage,
462 String sStreamPath,
463 String sMediaType,
464 boolean bCompressed,
465 byte[] pBytes,
466 boolean bEncrypted,
467 boolean bCommit )
469 // open substream element
470 XStream xSubStream = null;
473 XHierarchicalStorageAccess xHStorage =
474 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
475 if ( xHStorage == null )
477 Error( "The storage does not support hierarchical access!" );
478 return false;
481 Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
482 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
483 if ( xSubStream == null )
485 Error( "Can't create substream '" + sStreamPath + "'!" );
486 return false;
489 catch( Exception e )
491 Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
492 return false;
495 // get access to the XPropertySet interface
496 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
497 if ( xPropSet == null )
499 Error( "Can't get XPropertySet implementation from substream '" + sStreamPath + "'!" );
500 return false;
503 // set properties to the stream
506 xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", Boolean.valueOf( bEncrypted ) );
508 catch( Exception e )
510 Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
511 return false;
514 if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
515 return false;
517 XTransactedObject xTransact =
518 (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
519 if ( xTransact == null )
521 Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
522 return false;
525 if ( bCommit )
527 try {
528 xTransact.commit();
529 } catch( Exception e )
531 Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
532 return false;
536 // free the stream resources, garbage collector may remove the object too late
537 if ( !disposeStream( xSubStream, sStreamPath ) )
538 return false;
540 return true;
543 public int ChangeStreamPass( XStorage xStorage,
544 String sStreamName,
545 String sOldPass,
546 String sNewPass )
548 // open substream element
549 XStream xSubStream = null;
552 Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sOldPass );
553 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
554 if ( xSubStream == null )
556 Error( "Can't open substream '" + sStreamName + "'!" );
557 return 0;
560 catch( Exception e )
562 Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
563 return 0;
567 // change the password for the stream
568 XEncryptionProtectedSource xStreamEncryption =
569 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
571 if ( xStreamEncryption == null )
573 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
574 return -1;
577 try {
578 xStreamEncryption.setEncryptionPassword( sNewPass );
580 catch( Exception e )
582 Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
583 return 0;
586 // free the stream resources, garbage collector may remove the object too late
587 if ( !disposeStream( xSubStream, sStreamName ) )
588 return 0;
590 return 1;
593 public int ChangeStreamPassH( XStorage xStorage,
594 String sPath,
595 String sOldPass,
596 String sNewPass,
597 boolean bCommit )
599 // open substream element
600 XHierarchicalStorageAccess xHStorage =
601 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
602 if ( xHStorage == null )
604 Error( "The storage does not support hierarchical access!" );
605 return 0;
608 XStream xSubStream = null;
611 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.WRITE, sOldPass );
612 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
613 if ( xSubStream == null )
615 Error( "Can't open encrypted substream '" + sPath + "'!" );
616 return 0;
619 catch( Exception e )
621 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
622 return 0;
625 // change the password for the stream
626 XEncryptionProtectedSource xStreamEncryption =
627 (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
629 if ( xStreamEncryption == null )
631 Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
632 return -1;
635 try {
636 xStreamEncryption.setEncryptionPassword( sNewPass );
638 catch( Exception e )
640 Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
641 return 0;
644 XTransactedObject xTransact =
645 (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
646 if ( xTransact == null )
648 Error( "Substream '" + sPath + "', stream opened for writing must be transacted!" );
649 return 0;
652 if ( bCommit )
654 try {
655 xTransact.commit();
656 } catch( Exception e )
658 Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
659 return 0;
663 // free the stream resources, garbage collector may remove the object too late
664 if ( !disposeStream( xSubStream, sPath ) )
665 return 0;
667 return 1;
670 public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
672 boolean bOk = false;
674 // get access to the XPropertySet interface
675 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
676 if ( xPropSet != null )
680 // set "MediaType" property to the stream
681 xPropSet.setPropertyValue( "MediaType", sMediaType );
683 // get "IsRoot" and "OpenMode" properties and control there values
684 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
685 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
687 bOk = true;
688 if ( bPropIsRoot != bIsRoot )
690 Error( "'IsRoot' property contains wrong value!" );
691 bOk = false;
694 if ( ( bIsRoot
695 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
696 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
698 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
699 bOk = false;
702 catch( Exception e )
704 Error( "Can't control properties of substorage, exception: " + e );
707 else
709 Error( "Can't get XPropertySet implementation from storage!" );
712 return bOk;
715 public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
717 boolean bOk = false;
719 // get access to the XPropertySet interface
720 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
721 if ( xPropSet != null )
725 // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
726 String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
727 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
728 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
730 bOk = true;
731 if ( !sPropMediaType.equals( sMediaType ) )
733 Error( "'MediaType' property contains wrong value, expected '"
734 + sMediaType + "', set '" + sPropMediaType + "' !" );
735 bOk = false;
738 if ( bPropIsRoot != bIsRoot )
740 Error( "'IsRoot' property contains wrong value!" );
741 bOk = false;
744 if ( ( bIsRoot
745 && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
746 || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
748 Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
749 bOk = false;
752 catch( Exception e )
754 Error( "Can't get properties of substorage, exception: " + e );
757 else
759 Error( "Can't get XPropertySet implementation from storage!" );
762 return bOk;
765 public boolean InternalCheckStream( XStream xStream,
766 String sName,
767 String sMediaType,
768 boolean bCompressed,
769 byte[] pBytes,
770 boolean bCheckCompressed )
772 // get input stream of substream
773 XInputStream xInput = xStream.getInputStream();
774 if ( xInput == null )
776 Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
777 return false;
780 byte pContents[][] = new byte[1][]; // ???
782 // read contents
785 xInput.readBytes( pContents, pBytes.length + 1 );
787 catch( Exception e )
789 Error( "Can't read from stream '" + sName + "', exception: " + e );
790 return false;
793 // check size of stream data
794 if ( pContents.length == 0 )
796 Error( "SubStream '" + sName + "' reading produced disaster!" );
797 return false;
800 if ( pBytes.length != pContents[0].length )
802 Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
803 return false;
806 // check stream data
807 for ( int ind = 0; ind < pBytes.length; ind++ )
809 if ( pBytes[ind] != pContents[0][ind] )
811 Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
812 + ind + " should be " + pBytes[ind] + " but it is " + pContents[0][ind] + ")" );
813 return false;
817 // check properties
818 boolean bOk = false;
820 // get access to the XPropertySet interface
821 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
822 if ( xPropSet != null )
826 // get "MediaType" and "Size" properties and control there values
827 String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
828 long nPropSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) );
829 boolean bPropCompress = AnyConverter.toBoolean( xPropSet.getPropertyValue( "Compressed" ) );
831 bOk = true;
832 if ( !sPropMediaType.equals( sMediaType ) )
834 Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
835 + sMediaType + "', set: '" + sPropMediaType + "'!" );
836 bOk = false;
839 if ( nPropSize != pBytes.length )
841 Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
842 bOk = false;
845 if ( bCheckCompressed && bPropCompress != bCompressed )
847 Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
848 bOk = false;
851 catch( Exception e )
853 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
856 else
858 Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
861 return bOk;
864 public boolean checkStream( XStorage xParentStorage,
865 String sName,
866 String sMediaType,
867 boolean bCompressed,
868 byte[] pBytes )
870 // open substream element first
871 XStream xSubStream = null;
874 Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
875 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
876 if ( xSubStream == null )
878 Error( "Can't open substream '" + sName + "'!" );
879 return false;
882 catch( Exception e )
884 Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
885 return false;
888 boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, bCompressed, pBytes, true );
890 // free the stream resources, garbage collector may remove the object too late
891 if ( !disposeStream( xSubStream, sName ) )
892 return false;
894 return bResult;
897 public boolean checkEncrStream( XStorage xParentStorage,
898 String sName,
899 String sMediaType,
900 byte[] pBytes,
901 String sPass )
903 // Important: a common password for any of parent storage should not be set or
904 // should be different from sPass
908 Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
909 Error( "Encrypted stream '" + sName + "' was opened without password!" );
910 return false;
912 catch( WrongPasswordException wpe )
914 catch( Exception e )
916 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
917 return false;
920 String sWrongPass = "11";
921 sWrongPass += sPass;
924 Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
925 Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
926 return false;
928 catch( WrongPasswordException wpe )
930 catch( Exception e )
932 Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
933 return false;
936 XStream xSubStream = null;
939 Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sPass );
940 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
941 if ( xSubStream == null )
943 Error( "Can't open encrypted substream '" + sName + "'!" );
944 return false;
947 catch( Exception e )
949 Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
950 return false;
953 // encrypted streams will be compressed always, so after the storing this property is always true,
954 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
955 // before the storing )
956 boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, true, pBytes, false );
958 // free the stream resources, garbage collector may remove the object too late
959 if ( !disposeStream( xSubStream, sName ) )
960 return false;
962 return bResult;
965 public boolean checkStreamH( XStorage xParentStorage,
966 String sPath,
967 String sMediaType,
968 boolean bCompressed,
969 byte[] pBytes )
971 // open substream element first
972 XStream xSubStream = null;
975 XHierarchicalStorageAccess xHStorage =
976 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
977 if ( xHStorage == null )
979 Error( "The storage does not support hierarchical access!" );
980 return false;
983 Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
984 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
985 if ( xSubStream == null )
987 Error( "Can't open substream '" + sPath + "'!" );
988 return false;
991 catch( Exception e )
993 Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
994 return false;
997 boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, bCompressed, pBytes, true );
999 // free the stream resources, garbage collector may remove the object too late
1000 if ( !disposeStream( xSubStream, sPath ) )
1001 return false;
1003 return bResult;
1006 public boolean checkEncrStreamH( XStorage xParentStorage,
1007 String sPath,
1008 String sMediaType,
1009 byte[] pBytes,
1010 String sPass )
1012 // Important: a common password for any of parent storage should not be set or
1013 // should be different from sPass
1014 XHierarchicalStorageAccess xHStorage =
1015 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
1016 if ( xHStorage == null )
1018 Error( "The storage does not support hierarchical access!" );
1019 return false;
1024 Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
1025 XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1026 Error( "Encrypted substream '" + sPath + "' was opened without password!" );
1027 return false;
1029 catch( WrongPasswordException wpe )
1031 catch( Exception e )
1033 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
1034 return false;
1037 String sWrongPass = "11";
1038 sWrongPass += sPass;
1041 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sWrongPass );
1042 XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1043 Error( "Encrypted substream '" + sPath + "' was opened with wrong password!" );
1044 return false;
1046 catch( WrongPasswordException wpe )
1048 catch( Exception e )
1050 Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
1051 return false;
1054 XStream xSubStream = null;
1057 Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sPass );
1058 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1059 if ( xSubStream == null )
1061 Error( "Can't open encrypted substream '" + sPath + "'!" );
1062 return false;
1065 catch( Exception e )
1067 Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
1068 return false;
1071 // encrypted streams will be compressed always, so after the storing this property is always true,
1072 // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
1073 // before the storing )
1074 boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, true, pBytes, false );
1076 // free the stream resources, garbage collector may remove the object too late
1077 if ( !disposeStream( xSubStream, sPath ) )
1078 return false;
1080 return bResult;
1083 public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
1085 // copy xSourceStorage to xDestStorage
1088 xSourceStorage.copyToStorage( xDestStorage );
1090 catch( Exception e )
1092 Error( "Storage copying failed, exception: " + e );
1093 return false;
1096 return true;
1099 public boolean commitStorage( XStorage xStorage )
1101 // XTransactedObject must be supported by storages
1102 XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
1103 if ( xTransact == null )
1105 Error( "Storage doesn't implement transacted access!" );
1106 return false;
1111 xTransact.commit();
1113 catch( Exception e )
1115 Error( "Storage commit failed, exception:" + e );
1116 return false;
1119 return true;
1122 public boolean disposeStream( XStream xStream, String sStreamName )
1124 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
1125 if ( xComponent == null )
1127 Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
1128 return false;
1133 xComponent.dispose();
1135 catch( Exception e )
1137 Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
1138 return false;
1141 return true;
1144 public boolean disposeStorage( XStorage xStorage )
1146 // dispose the storage
1147 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
1148 if ( xComponent == null )
1150 Error( "Can't retrieve XComponent implementation from storage!" );
1151 return false;
1156 xComponent.dispose();
1158 catch( Exception e )
1160 Error( "Storage disposing failed!" );
1161 return false;
1164 return true;
1167 public XInputStream getInputStream( XStream xStream )
1169 XInputStream xInTemp = null;
1172 xInTemp = xStream.getInputStream();
1173 if ( xInTemp == null )
1174 Error( "Can't get the input part of a stream!" );
1176 catch ( Exception e )
1178 Error( "Can't get the input part of a stream, exception :" + e );
1181 return xInTemp;
1184 public boolean closeOutput( XStream xStream )
1186 XOutputStream xOutTemp = null;
1189 xOutTemp = xStream.getOutputStream();
1190 if ( xOutTemp == null )
1192 Error( "Can't get the output part of a stream!" );
1193 return false;
1196 catch ( Exception e )
1198 Error( "Can't get the output part of a stream, exception :" + e );
1199 return false;
1204 xOutTemp.closeOutput();
1206 catch ( Exception e )
1208 Error( "Can't close output part of a stream, exception :" + e );
1209 return false;
1212 return true;
1215 public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
1217 // open existing substorage
1220 Object oSubStorage = xStorage.openStorageElement( sName, nMode );
1221 XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
1222 return xSubStorage;
1224 catch( Exception e )
1226 Error( "Can't open substorage '" + sName + "', exception: " + e );
1229 return null;
1232 public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
1234 // try to get temporary file representation
1235 XStream xTempFileStream = null;
1238 Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1239 xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
1241 catch( Exception e )
1244 if ( xTempFileStream == null )
1245 Error( "Can't create temporary file!" );
1247 return xTempFileStream;
1250 public String CreateTempFile( XMultiServiceFactory xMSF )
1252 String sResult = null;
1254 // try to get temporary file representation
1255 XPropertySet xTempFileProps = null;
1258 Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1259 xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
1261 catch( Exception e )
1264 if ( xTempFileProps != null )
1268 xTempFileProps.setPropertyValue( "RemoveFile", Boolean.FALSE );
1269 sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
1271 catch( Exception e )
1273 Error( "Can't control TempFile properties, exception: " + e );
1276 else
1278 Error( "Can't create temporary file representation!" );
1281 // close temporary file explicitly
1284 XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
1285 if ( xStream != null )
1287 XOutputStream xOut = xStream.getOutputStream();
1288 if ( xOut != null )
1289 xOut.closeOutput();
1291 XInputStream xIn = xStream.getInputStream();
1292 if ( xIn != null )
1293 xIn.closeInput();
1295 else
1296 Error( "Can't close TempFile!" );
1298 catch( Exception e )
1300 Error( "Can't close TempFile, exception: " + e );
1303 return sResult;
1306 public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
1308 // copy element with name sName from xSource to xDest
1311 xSource.copyElementTo( sName, xDest, sName );
1313 catch( Exception e )
1315 Error( "Element copying failed, exception: " + e );
1316 return false;
1319 return true;
1322 public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )
1324 // copy element with name sName from xSource to xDest
1327 xSource.copyElementTo( sName, xDest, sTargetName );
1329 catch( Exception e )
1331 Error( "Element copying failed, exception: " + e );
1332 return false;
1335 return true;
1338 public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
1340 // move element with name sName from xSource to xDest
1343 xSource.moveElementTo( sName, xDest, sName );
1345 catch( Exception e )
1347 Error( "Element moving failed, exception: " + e );
1348 return false;
1351 return true;
1354 public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
1356 // rename element with name sOldName to sNewName
1359 xStorage.renameElement( sOldName, sNewName );
1361 catch( Exception e )
1363 Error( "Element renaming failed, exception: " + e );
1364 return false;
1367 return true;
1370 public boolean removeElement( XStorage xStorage, String sName )
1372 // remove element with name sName
1375 xStorage.removeElement( sName );
1377 catch( Exception e )
1379 Error( "Element removing failed, exception: " + e );
1380 return false;
1383 return true;
1386 public XStream OpenStream( XStorage xStorage,
1387 String sStreamName,
1388 int nMode )
1390 // open substream element
1391 XStream xSubStream = null;
1394 Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
1395 xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1396 if ( xSubStream == null )
1397 Error( "Can't create substream '" + sStreamName + "'!" );
1399 catch( Exception e )
1401 Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
1404 return xSubStream;
1407 public boolean compareRawMethodsOnEncrStream( XStorage xStorage, String sStreamName )
1410 XStorageRawAccess xRawStorage;
1413 xRawStorage = (XStorageRawAccess) UnoRuntime.queryInterface( XStorageRawAccess.class, xStorage );
1415 catch( Exception e )
1417 Error( "Can't get raw access to the storage, exception : " + e + "!" );
1418 return false;
1421 if ( xRawStorage == null )
1423 Error( "Can't get raw access to the storage!" );
1424 return false;
1427 XInputStream xHeadRawStream = null;
1430 xHeadRawStream = xRawStorage.getRawEncrStreamElement( sStreamName );
1432 catch( Exception e )
1434 Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1437 XInputStream xPlainRawStream = null;
1440 xPlainRawStream = xRawStorage.getPlainRawStreamElement( sStreamName );
1442 catch( Exception e )
1444 Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1447 if ( xHeadRawStream == null || xPlainRawStream == null )
1449 Error( "Can't open encrypted stream '" + sStreamName + "' in raw modes!" );
1450 return false;
1455 byte pData[][] = new byte[1][38];
1456 if ( xHeadRawStream.readBytes( pData, 38 ) != 38 )
1458 Error( "Can't read header of encrypted stream '" + sStreamName + "' raw representations!" );
1459 return false;
1462 if ( pData[0][0] != 0x4d || pData[0][1] != 0x4d || pData[0][2] != 0x02 || pData[0][3] != 0x05 )
1464 Error( "No signature in the header of encrypted stream '" + sStreamName + "' raw representations!" );
1465 return false;
1468 int nVariableHeaderLength =
1469 ( pData[0][30] + pData[0][31] * 0x100 ) // salt length
1470 + ( pData[0][32] + pData[0][33] * 0x100 ) // iv length
1471 + ( pData[0][34] + pData[0][35] * 0x100 ) // digest length
1472 + ( pData[0][36] + pData[0][37] * 0x100 ); // mediatype length
1474 xHeadRawStream.skipBytes( nVariableHeaderLength );
1476 byte pRawData1[][] = new byte[1][32000];
1477 byte pRawData2[][] = new byte[1][32000];
1478 int nRead1 = 0;
1479 int nRead2 = 0;
1483 nRead1 = xHeadRawStream.readBytes( pRawData1, 32000 );
1484 nRead2 = xPlainRawStream.readBytes( pRawData2, 32000 );
1486 if ( nRead1 != nRead2 )
1488 Error( "The encrypted stream '" + sStreamName + "' raw representations have different size! nRead1 - nRead2 = " + ( Integer.valueOf( nRead1 - nRead2 ) ).toString() );
1489 return false;
1492 for ( int nInd = 0; nInd < nRead1; nInd++ )
1493 if ( pRawData1[0][nInd] != pRawData2[0][nInd] )
1495 Error( "The encrypted stream '" + sStreamName + "' raw representations have different data!" );
1496 return false;
1499 while( nRead1 == 32000 );
1501 catch ( Exception e )
1503 Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : " + e + "!" );
1504 return false;
1507 return true;
1510 public boolean cantOpenStorage( XStorage xStorage, String sName )
1512 // try to open an opened substorage, open call must fail
1515 Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
1516 Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
1518 catch( Exception e )
1520 return true;
1523 return false;
1526 public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
1528 // try to open the substream with specified mode must fail
1531 Object oDummyStream = xStorage.openStreamElement( sName, nMode );
1532 Error( "The trying to open substream '" + sName + "' must fail!" );
1534 catch( Exception e )
1536 return true;
1539 return false;
1542 public boolean cantOpenStreamH( XStorage xStorage, String sPath, int nMode )
1544 // try to open the substream with specified mode must fail
1546 XHierarchicalStorageAccess xHStorage =
1547 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1548 if ( xHStorage == null )
1550 Error( "The storage does not support hierarchical access!" );
1551 return false;
1556 Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
1557 Error( "The trying to open substream '" + sPath + "' must fail!" );
1559 catch( Exception e )
1561 return true;
1564 return false;
1567 public boolean cantOpenEncrStreamH( XStorage xStorage, String sPath, int nMode, String aPass )
1569 // try to open the substream with specified mode must fail
1571 XHierarchicalStorageAccess xHStorage =
1572 (XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1573 if ( xHStorage == null )
1575 Error( "The storage does not support hierarchical access!" );
1576 return false;
1581 Object oDummyStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, nMode, aPass );
1582 Error( "The trying to open substream '" + sPath + "' must fail!" );
1584 catch( WrongPasswordException wpe )
1586 Error( "The substream '" + sPath + "' must not exist!" );
1587 return false;
1589 catch( Exception e )
1591 return true;
1594 return false;
1597 public XStorage cloneStorage( XSingleServiceFactory xFactory, XStorage xStorage )
1599 // create a copy of a last committed version of specified storage
1600 XStorage xResult = null;
1603 Object oTempStorage = xFactory.createInstance();
1604 xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1605 if ( xResult != null )
1606 xStorage.copyLastCommitTo( xResult );
1608 catch( Exception e )
1610 Error( "Can't clone storage, exception: " + e );
1611 return null;
1614 return xResult;
1617 public XStorage cloneSubStorage( XSingleServiceFactory xFactory, XStorage xStorage, String sName )
1619 // create a copy of a last committed version of specified substorage
1620 XStorage xResult = null;
1623 Object oTempStorage = xFactory.createInstance();
1624 xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1625 if ( xResult != null )
1626 xStorage.copyStorageElementLastCommitTo( sName, xResult );
1628 catch( Exception e )
1630 Error( "Can't clone substorage '" + sName + "', exception: " + e );
1631 return null;
1634 return xResult;
1637 public XStream cloneSubStream( XStorage xStorage, String sName )
1639 // clone existing substream
1642 XStream xStream = xStorage.cloneStreamElement( sName );
1643 return xStream;
1645 catch( Exception e )
1647 Error( "Can't clone substream '" + sName + "', exception: " + e );
1650 return null;
1653 public XStream cloneEncrSubStream( XStorage xStorage, String sName, String sPass )
1655 // clone existing substream
1658 XStream xStream = xStorage.cloneEncryptedStreamElement( sName, sPass );
1659 return xStream;
1661 catch( Exception e )
1663 Error( "Can't clone encrypted substream '" + sName + "', exception: " + e );
1666 return null;
1669 public void Error( String sError )
1671 m_aLogWriter.println( m_sTestPrefix + "Error: " + sError );
1674 public void Message( String sMessage )
1676 m_aLogWriter.println( m_sTestPrefix + sMessage );