xref: /trunk/main/package/qa/storages/TestHelper.java (revision a893be29)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package complex.storages;
23 
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XInterface;
26 import com.sun.star.uno.AnyConverter;
27 
28 import com.sun.star.lang.*;
29 import com.sun.star.embed.*;
30 import com.sun.star.packages.*;
31 import com.sun.star.io.*;
32 import com.sun.star.beans.*;
33 
34 import share.LogWriter;
35 
36 public class TestHelper  {
37 
38 	LogWriter m_aLogWriter;
39 	String m_sTestPrefix;
40 
TestHelper( LogWriter aLogWriter, String sTestPrefix )41 	public TestHelper( LogWriter aLogWriter, String sTestPrefix )
42 	{
43 		m_aLogWriter = aLogWriter;
44 		m_sTestPrefix = sTestPrefix;
45 	}
46 
WriteBytesToStream( XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes )47 	public boolean WriteBytesToStream( XStream xStream,
48 										String sStreamName,
49 										String sMediaType,
50 										boolean bCompressed,
51 										byte[] pBytes )
52 	{
53 		// get output stream of substream
54 		XOutputStream xOutput = xStream.getOutputStream();
55 		if ( xOutput == null )
56 		{
57 			Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
58 			return false;
59 		}
60 
61 		// get XTrucate implementation from output stream
62 		XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
63 		if ( xTruncate == null )
64 		{
65 			Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
66 			return false;
67 		}
68 
69 		// write requested byte sequence
70 		try
71 		{
72 			xTruncate.truncate();
73 			xOutput.writeBytes( pBytes );
74 		}
75 		catch( Exception e )
76 		{
77 			Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
78 			return false;
79 		}
80 
81 		// get access to the XPropertySet interface
82 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
83 		if ( xPropSet == null )
84 		{
85 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
86 			return false;
87 		}
88 
89 		// set properties to the stream
90 		try
91 		{
92 			xPropSet.setPropertyValue( "MediaType", sMediaType );
93 			xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
94 		}
95 		catch( Exception e )
96 		{
97 			Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
98 			return false;
99 		}
100 
101 		// check size property of the stream
102 		try
103 		{
104 			int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
105 			if ( nSize != pBytes.length )
106 			{
107 				Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
108 				return false;
109 			}
110 		}
111 		catch( Exception e )
112 		{
113 			Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
114 			return false;
115 		}
116 
117 		return true;
118 	}
119 
WriteBytesToSubstreamDefaultCompressed( XStorage xStorage, String sStreamName, String sMediaType, byte[] pBytes )120 	public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage,
121 										                    String sStreamName,
122 										                    String sMediaType,
123 										                    byte[] pBytes )
124 	{
125 		// open substream element
126 		XStream xSubStream = null;
127 		try
128 		{
129 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
130 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
131 			if ( xSubStream == null )
132 			{
133 				Error( "Can't create substream '" + sStreamName + "'!" );
134 				return false;
135 			}
136 		}
137 		catch( Exception e )
138 		{
139 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
140 			return false;
141 		}
142 
143 		// get output stream of substream
144 		XOutputStream xOutput = xSubStream.getOutputStream();
145 		if ( xOutput == null )
146 		{
147 			Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
148 			return false;
149 		}
150 
151 		// get XTrucate implementation from output stream
152 		XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
153 		if ( xTruncate == null )
154 		{
155 			Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
156 			return false;
157 		}
158 
159 		// write requested byte sequence
160 		try
161 		{
162 			xTruncate.truncate();
163 			xOutput.writeBytes( pBytes );
164 		}
165 		catch( Exception e )
166 		{
167 			Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
168 			return false;
169 		}
170 
171 		// get access to the XPropertySet interface
172 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
173 		if ( xPropSet == null )
174 		{
175 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
176 			return false;
177 		}
178 
179 		// set properties to the stream
180         // do not set the compressed property
181 		try
182 		{
183 			xPropSet.setPropertyValue( "MediaType", sMediaType );
184 		}
185 		catch( Exception e )
186 		{
187 			Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
188 			return false;
189 		}
190 
191 		// check size property of the stream
192 		try
193 		{
194 			int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
195 			if ( nSize != pBytes.length )
196 			{
197 				Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
198 				return false;
199 			}
200 		}
201 		catch( Exception e )
202 		{
203 			Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
204 			return false;
205 		}
206 
207 		// free the stream resources, garbage collector may remove the object too late
208 		if ( !disposeStream( xSubStream, sStreamName ) )
209 			return false;
210 
211 		return true;
212 	}
213 
WriteBytesToSubstream( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes )214 	public boolean WriteBytesToSubstream( XStorage xStorage,
215 										  String sStreamName,
216 										  String sMediaType,
217 										  boolean bCompressed,
218 										  byte[] pBytes )
219 	{
220 		// open substream element
221 		XStream xSubStream = null;
222 		try
223 		{
224 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
225 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
226 			if ( xSubStream == null )
227 			{
228 				Error( "Can't create substream '" + sStreamName + "'!" );
229 				return false;
230 			}
231 		}
232 		catch( Exception e )
233 		{
234 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
235 			return false;
236 		}
237 
238 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
239 			return false;
240 
241 		// free the stream resources, garbage collector may remove the object too late
242 		if ( !disposeStream( xSubStream, sStreamName ) )
243 			return false;
244 
245 		return true;
246 	}
247 
WriteBytesToEncrSubstream( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, String sPass )248 	public boolean WriteBytesToEncrSubstream( XStorage xStorage,
249 											  String sStreamName,
250 											  String sMediaType,
251 											  boolean bCompressed,
252 											  byte[] pBytes,
253 											  String sPass )
254 	{
255 		// open substream element
256 		XStream xSubStream = null;
257 		try
258 		{
259 			Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sPass );
260 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
261 			if ( xSubStream == null )
262 			{
263 				Error( "Can't create substream '" + sStreamName + "'!" );
264 				return false;
265 			}
266 		}
267 		catch( Exception e )
268 		{
269 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
270 			return false;
271 		}
272 
273 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
274 			return false;
275 
276 		// free the stream resources, garbage collector may remove the object too late
277 		if ( !disposeStream( xSubStream, sStreamName ) )
278 			return false;
279 
280 		return true;
281 	}
282 
WBToSubstrOfEncr( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, boolean bEncrypted )283 	public boolean WBToSubstrOfEncr( XStorage xStorage,
284 											  String sStreamName,
285 											  String sMediaType,
286 											  boolean bCompressed,
287 											  byte[] pBytes,
288 											  boolean bEncrypted )
289 	{
290 		// open substream element
291 		XStream xSubStream = null;
292 		try
293 		{
294 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
295 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
296 			if ( xSubStream == null )
297 			{
298 				Error( "Can't create substream '" + sStreamName + "'!" );
299 				return false;
300 			}
301 		}
302 		catch( Exception e )
303 		{
304 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
305 			return false;
306 		}
307 
308 		// get access to the XPropertySet interface
309 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
310 		if ( xPropSet == null )
311 		{
312 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
313 			return false;
314 		}
315 
316 		// set properties to the stream
317 		try
318 		{
319 			xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
320 		}
321 		catch( Exception e )
322 		{
323 			Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
324 			return false;
325 		}
326 
327 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
328 			return false;
329 
330 		// free the stream resources, garbage collector may remove the object too late
331 		if ( !disposeStream( xSubStream, sStreamName ) )
332 			return false;
333 
334 		return true;
335 	}
336 
WriteBytesToStreamH( XStorage xStorage, String sStreamPath, String sMediaType, boolean bCompressed, byte[] pBytes, boolean bCommit )337 	public boolean WriteBytesToStreamH( XStorage xStorage,
338 										  String sStreamPath,
339 										  String sMediaType,
340 										  boolean bCompressed,
341 										  byte[] pBytes,
342 										  boolean bCommit )
343 	{
344 		// open substream element
345 		XStream xSubStream = null;
346 		try
347 		{
348 			XHierarchicalStorageAccess xHStorage =
349 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
350 			if ( xHStorage == null )
351 			{
352 				Error( "The storage does not support hierarchical access!" );
353 				return false;
354 			}
355 
356 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
357 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
358 			if ( xSubStream == null )
359 			{
360 				Error( "Can't create substream '" + sStreamPath + "'!" );
361 				return false;
362 			}
363 		}
364 		catch( Exception e )
365 		{
366 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
367 			return false;
368 		}
369 
370 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
371 			return false;
372 
373 		XTransactedObject xTransact =
374 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
375 		if ( xTransact == null )
376 		{
377 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
378 			return false;
379 		}
380 
381 		if ( bCommit )
382 		{
383 			try {
384 				xTransact.commit();
385 			} catch( Exception e )
386 			{
387 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
388 				return false;
389 			}
390 		}
391 
392 		// free the stream resources, garbage collector may remove the object too late
393 		if ( !disposeStream( xSubStream, sStreamPath ) )
394 			return false;
395 
396 		return true;
397 	}
398 
WriteBytesToEncrStreamH( XStorage xStorage, String sStreamPath, String sMediaType, boolean bCompressed, byte[] pBytes, String sPass, boolean bCommit )399 	public boolean WriteBytesToEncrStreamH( XStorage xStorage,
400 										  String sStreamPath,
401 										  String sMediaType,
402 										  boolean bCompressed,
403 										  byte[] pBytes,
404 										  String sPass,
405 										  boolean bCommit )
406 	{
407 		// open substream element
408 		XStream xSubStream = null;
409 		try
410 		{
411 			XHierarchicalStorageAccess xHStorage =
412 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
413 			if ( xHStorage == null )
414 			{
415 				Error( "The storage does not support hierarchical access!" );
416 				return false;
417 			}
418 
419 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
420 																						ElementModes.WRITE,
421 																						sPass );
422 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
423 			if ( xSubStream == null )
424 			{
425 				Error( "Can't create substream '" + sStreamPath + "'!" );
426 				return false;
427 			}
428 		}
429 		catch( Exception e )
430 		{
431 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
432 			return false;
433 		}
434 
435 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
436 			return false;
437 
438 		XTransactedObject xTransact =
439 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
440 		if ( xTransact == null )
441 		{
442 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
443 			return false;
444 		}
445 
446 		if ( bCommit )
447 		{
448 			try {
449 				xTransact.commit();
450 			} catch( Exception e )
451 			{
452 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
453 				return false;
454 			}
455 		}
456 
457 		// free the stream resources, garbage collector may remove the object too late
458 		if ( !disposeStream( xSubStream, sStreamPath ) )
459 			return false;
460 
461 		return true;
462 	}
463 
WBToSubstrOfEncrH( XStorage xStorage, String sStreamPath, String sMediaType, boolean bCompressed, byte[] pBytes, boolean bEncrypted, boolean bCommit )464 	public boolean WBToSubstrOfEncrH( XStorage xStorage,
465 									  String sStreamPath,
466 									  String sMediaType,
467 									  boolean bCompressed,
468 									  byte[] pBytes,
469 									  boolean bEncrypted,
470 									  boolean bCommit )
471 	{
472 		// open substream element
473 		XStream xSubStream = null;
474 		try
475 		{
476 			XHierarchicalStorageAccess xHStorage =
477 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
478 			if ( xHStorage == null )
479 			{
480 				Error( "The storage does not support hierarchical access!" );
481 				return false;
482 			}
483 
484 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
485 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
486 			if ( xSubStream == null )
487 			{
488 				Error( "Can't create substream '" + sStreamPath + "'!" );
489 				return false;
490 			}
491 		}
492 		catch( Exception e )
493 		{
494 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
495 			return false;
496 		}
497 
498 		// get access to the XPropertySet interface
499 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
500 		if ( xPropSet == null )
501 		{
502 			Error( "Can't get XPropertySet implementation from substream '" + sStreamPath + "'!" );
503 			return false;
504 		}
505 
506 		// set properties to the stream
507 		try
508 		{
509 			xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
510 		}
511 		catch( Exception e )
512 		{
513 			Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
514 			return false;
515 		}
516 
517 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
518 			return false;
519 
520 		XTransactedObject xTransact =
521 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
522 		if ( xTransact == null )
523 		{
524 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
525 			return false;
526 		}
527 
528 		if ( bCommit )
529 		{
530 			try {
531 				xTransact.commit();
532 			} catch( Exception e )
533 			{
534 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
535 				return false;
536 			}
537 		}
538 
539 		// free the stream resources, garbage collector may remove the object too late
540 		if ( !disposeStream( xSubStream, sStreamPath ) )
541 			return false;
542 
543 		return true;
544 	}
545 
ChangeStreamPass( XStorage xStorage, String sStreamName, String sOldPass, String sNewPass )546 	public int ChangeStreamPass( XStorage xStorage,
547 								 String sStreamName,
548 								 String sOldPass,
549 								 String sNewPass )
550 	{
551 		// open substream element
552 		XStream xSubStream = null;
553 		try
554 		{
555 			Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sOldPass );
556 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
557 			if ( xSubStream == null )
558 			{
559 				Error( "Can't open substream '" + sStreamName + "'!" );
560 				return 0;
561 			}
562 		}
563 		catch( Exception e )
564 		{
565 			Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
566 			return 0;
567 		}
568 
569 
570 		// change the password for the stream
571 		XEncryptionProtectedSource xStreamEncryption =
572 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
573 
574 		if ( xStreamEncryption == null )
575 		{
576 			Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
577 			return -1;
578 		}
579 
580 		try {
581 			xStreamEncryption.setEncryptionPassword( sNewPass );
582 		}
583 		catch( Exception e )
584 		{
585 			Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
586 			return 0;
587 		}
588 
589 		// free the stream resources, garbage collector may remove the object too late
590 		if ( !disposeStream( xSubStream, sStreamName ) )
591 			return 0;
592 
593 		return 1;
594 	}
595 
ChangeStreamPassH( XStorage xStorage, String sPath, String sOldPass, String sNewPass, boolean bCommit )596 	public int ChangeStreamPassH( XStorage xStorage,
597 								 String sPath,
598 								 String sOldPass,
599 								 String sNewPass,
600 								 boolean bCommit )
601 	{
602 		// open substream element
603 		XHierarchicalStorageAccess xHStorage =
604 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
605 		if ( xHStorage == null )
606 		{
607 			Error( "The storage does not support hierarchical access!" );
608 			return 0;
609 		}
610 
611 		XStream xSubStream = null;
612 		try
613 		{
614 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.WRITE, sOldPass );
615 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
616 			if ( xSubStream == null )
617 			{
618 				Error( "Can't open encrypted substream '" + sPath + "'!" );
619 				return 0;
620 			}
621 		}
622 		catch( Exception e )
623 		{
624 			Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
625 			return 0;
626 		}
627 
628 		// change the password for the stream
629 		XEncryptionProtectedSource xStreamEncryption =
630 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
631 
632 		if ( xStreamEncryption == null )
633 		{
634 			Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
635 			return -1;
636 		}
637 
638 		try {
639 			xStreamEncryption.setEncryptionPassword( sNewPass );
640 		}
641 		catch( Exception e )
642 		{
643 			Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
644 			return 0;
645 		}
646 
647 		XTransactedObject xTransact =
648 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
649 		if ( xTransact == null )
650 		{
651 			Error( "Substream '" + sPath + "', stream opened for writing must be transacted!" );
652 			return 0;
653 		}
654 
655 		if ( bCommit )
656 		{
657 			try {
658 				xTransact.commit();
659 			} catch( Exception e )
660 			{
661 				Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
662 				return 0;
663 			}
664 		}
665 
666 		// free the stream resources, garbage collector may remove the object too late
667 		if ( !disposeStream( xSubStream, sPath ) )
668 			return 0;
669 
670 		return 1;
671 	}
672 
setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )673 	public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
674 	{
675 		boolean bOk = false;
676 
677 		// get access to the XPropertySet interface
678 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
679 		if ( xPropSet != null )
680 		{
681 			try
682 			{
683 				// set "MediaType" property to the stream
684 				xPropSet.setPropertyValue( "MediaType", sMediaType );
685 
686 				// get "IsRoot" and "OpenMode" properties and control there values
687 				boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
688 				int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
689 
690 				bOk = true;
691 				if ( bPropIsRoot != bIsRoot )
692 				{
693 					Error( "'IsRoot' property contains wrong value!" );
694 					bOk = false;
695 				}
696 
697 				if ( ( bIsRoot
698 				  && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
699 				  || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
700 				{
701 					Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
702 					bOk = false;
703 				}
704 			}
705 			catch( Exception e )
706 			{
707 				Error( "Can't control properties of substorage, exception: " + e );
708 			}
709 		}
710 		else
711 		{
712 			Error( "Can't get XPropertySet implementation from storage!" );
713 		}
714 
715 		return bOk;
716 	}
717 
checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )718 	public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
719 	{
720 		boolean bOk = false;
721 
722 		// get access to the XPropertySet interface
723 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
724 		if ( xPropSet != null )
725 		{
726 			try
727 			{
728 				// get "MediaType", "IsRoot" and "OpenMode" properties and control there values
729 				String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
730 				boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
731 				int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
732 
733 				bOk = true;
734 				if ( !sPropMediaType.equals( sMediaType ) )
735 				{
736 					Error( "'MediaType' property contains wrong value, expected '"
737 							+ sMediaType + "', set '" + sPropMediaType + "' !" );
738 					bOk = false;
739 				}
740 
741 				if ( bPropIsRoot != bIsRoot )
742 				{
743 					Error( "'IsRoot' property contains wrong value!" );
744 					bOk = false;
745 				}
746 
747 				if ( ( bIsRoot
748 				  && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
749 				  || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
750 				{
751 					Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
752 					bOk = false;
753 				}
754 			}
755 			catch( Exception e )
756 			{
757 				Error( "Can't get properties of substorage, exception: " + e );
758 			}
759 		}
760 		else
761 		{
762 			Error( "Can't get XPropertySet implementation from storage!" );
763 		}
764 
765 		return bOk;
766 	}
767 
InternalCheckStream( XStream xStream, String sName, String sMediaType, boolean bCompressed, byte[] pBytes, boolean bCheckCompressed )768 	public boolean InternalCheckStream( XStream xStream,
769 										String sName,
770 										String sMediaType,
771                                         boolean bCompressed,
772 										byte[] pBytes,
773                                         boolean bCheckCompressed )
774 	{
775 		// get input stream of substream
776 		XInputStream xInput = xStream.getInputStream();
777 		if ( xInput == null )
778 		{
779 			Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
780 			return false;
781 		}
782 
783 		byte pContents[][] = new byte[1][]; // ???
784 
785 		// read contents
786 		try
787 		{
788 			xInput.readBytes( pContents, pBytes.length + 1 );
789 		}
790 		catch( Exception e )
791 		{
792 			Error( "Can't read from stream '" + sName + "', exception: " + e );
793 			return false;
794 		}
795 
796 		// check size of stream data
797 		if ( pContents.length == 0 )
798 		{
799 			Error( "SubStream '" + sName + "' reading produced disaster!"  );
800 			return false;
801 		}
802 
803 		if ( pBytes.length != pContents[0].length )
804 		{
805 			Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
806 			return false;
807 		}
808 
809 		// check stream data
810 		for ( int ind = 0; ind < pBytes.length; ind++ )
811 		{
812 			if ( pBytes[ind] != pContents[0][ind] )
813 			{
814 				Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
815 						+ ind + " should be " + pBytes[ind] + " but it is " + pContents[0][ind] + ")" );
816 				return false;
817 			}
818 		}
819 
820 		// check properties
821 		boolean bOk = false;
822 
823 		// get access to the XPropertySet interface
824 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
825 		if ( xPropSet != null )
826 		{
827 			try
828 			{
829 				// get "MediaType" and "Size" properties and control there values
830 				String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
831 				int nPropSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
832                 boolean bPropCompress = AnyConverter.toBoolean( xPropSet.getPropertyValue( "Compressed" ) );
833 
834 				bOk = true;
835 				if ( !sPropMediaType.equals( sMediaType ) )
836 				{
837 					Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
838 							+ sMediaType + "', set: '" + sPropMediaType + "'!" );
839 					bOk = false;
840 				}
841 
842 				if ( nPropSize != pBytes.length )
843 				{
844 					Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
845 					bOk = false;
846 				}
847 
848                 if ( bCheckCompressed && bPropCompress != bCompressed )
849                 {
850     				Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
851 					bOk = false;
852                 }
853 			}
854 			catch( Exception e )
855 			{
856 				Error( "Can't get properties of substream '" + sName + "', exception: " + e );
857 			}
858 		}
859 		else
860 		{
861 			Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
862 		}
863 
864 		return bOk;
865 	}
866 
checkStream( XStorage xParentStorage, String sName, String sMediaType, boolean bCompressed, byte[] pBytes )867 	public boolean checkStream( XStorage xParentStorage,
868 								String sName,
869 								String sMediaType,
870                                 boolean bCompressed,
871 								byte[] pBytes )
872 	{
873 		// open substream element first
874 		XStream xSubStream = null;
875 		try
876 		{
877 			Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
878 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
879 			if ( xSubStream == null )
880 			{
881 				Error( "Can't open substream '" + sName + "'!" );
882 				return false;
883 			}
884 		}
885 		catch( Exception e )
886 		{
887 			Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
888 			return false;
889 		}
890 
891 		boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, bCompressed, pBytes, true );
892 
893 		// free the stream resources, garbage collector may remove the object too late
894 		if ( !disposeStream( xSubStream, sName ) )
895 			return false;
896 
897 		return bResult;
898 	}
899 
checkEncrStream( XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes, String sPass )900 	public boolean checkEncrStream( XStorage xParentStorage,
901 									String sName,
902 									String sMediaType,
903 									byte[] pBytes,
904 									String sPass )
905 	{
906 		// Important: a common password for any of parent storage should not be set or
907 		// 			  should be different from sPass
908 
909 		try
910 		{
911 			Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
912 			Error( "Encrypted stream '" + sName + "' was opened without password!" );
913 			return false;
914 		}
915 		catch( WrongPasswordException wpe )
916 		{}
917 		catch( Exception e )
918 		{
919 			Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
920 			return false;
921 		}
922 
923 		String sWrongPass = "11";
924 		sWrongPass += sPass;
925 		try
926 		{
927 			Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
928 			Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
929 			return false;
930 		}
931 		catch( WrongPasswordException wpe )
932 		{}
933 		catch( Exception e )
934 		{
935 			Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
936 			return false;
937 		}
938 
939 		XStream xSubStream = null;
940 		try
941 		{
942 			Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sPass );
943 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
944 			if ( xSubStream == null )
945 			{
946 				Error( "Can't open encrypted substream '" + sName + "'!" );
947 				return false;
948 			}
949 		}
950 		catch( Exception e )
951 		{
952 			Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
953 			return false;
954 		}
955 
956         // encrypted streams will be compressed always, so after the storing this property is always true,
957         // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
958         // before the storing )
959 		boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, true, pBytes, false );
960 
961 		// free the stream resources, garbage collector may remove the object too late
962 		if ( !disposeStream( xSubStream, sName ) )
963 			return false;
964 
965 		return bResult;
966 	}
967 
checkStreamH( XStorage xParentStorage, String sPath, String sMediaType, boolean bCompressed, byte[] pBytes )968 	public boolean checkStreamH( XStorage xParentStorage,
969 								String sPath,
970 								String sMediaType,
971                                 boolean bCompressed,
972 								byte[] pBytes )
973 	{
974 		// open substream element first
975 		XStream xSubStream = null;
976 		try
977 		{
978 			XHierarchicalStorageAccess xHStorage =
979 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
980 			if ( xHStorage == null )
981 			{
982 				Error( "The storage does not support hierarchical access!" );
983 				return false;
984 			}
985 
986 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
987 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
988 			if ( xSubStream == null )
989 			{
990 				Error( "Can't open substream '" + sPath + "'!" );
991 				return false;
992 			}
993 		}
994 		catch( Exception e )
995 		{
996 			Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
997 			return false;
998 		}
999 
1000 		boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, bCompressed, pBytes, true );
1001 
1002 		// free the stream resources, garbage collector may remove the object too late
1003 		if ( !disposeStream( xSubStream, sPath ) )
1004 			return false;
1005 
1006 		return bResult;
1007 	}
1008 
checkEncrStreamH( XStorage xParentStorage, String sPath, String sMediaType, byte[] pBytes, String sPass )1009 	public boolean checkEncrStreamH( XStorage xParentStorage,
1010 									String sPath,
1011 									String sMediaType,
1012 									byte[] pBytes,
1013 									String sPass )
1014 	{
1015 		// Important: a common password for any of parent storage should not be set or
1016 		// 			  should be different from sPass
1017 		XHierarchicalStorageAccess xHStorage =
1018 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
1019 		if ( xHStorage == null )
1020 		{
1021 			Error( "The storage does not support hierarchical access!" );
1022 			return false;
1023 		}
1024 
1025 		try
1026 		{
1027 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
1028 			XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1029 			Error( "Encrypted substream '" + sPath + "' was opened without password!" );
1030 			return false;
1031 		}
1032 		catch( WrongPasswordException wpe )
1033 		{}
1034 		catch( Exception e )
1035 		{
1036 			Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
1037 			return false;
1038 		}
1039 
1040 		String sWrongPass = "11";
1041 		sWrongPass += sPass;
1042 		try
1043 		{
1044 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sWrongPass );
1045 			XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1046 			Error( "Encrypted substream '" + sPath + "' was opened with wrong password!" );
1047 			return false;
1048 		}
1049 		catch( WrongPasswordException wpe )
1050 		{}
1051 		catch( Exception e )
1052 		{
1053 			Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
1054 			return false;
1055 		}
1056 
1057 		XStream xSubStream = null;
1058 		try
1059 		{
1060 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sPass );
1061 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1062 			if ( xSubStream == null )
1063 			{
1064 				Error( "Can't open encrypted substream '" + sPath + "'!" );
1065 				return false;
1066 			}
1067 		}
1068 		catch( Exception e )
1069 		{
1070 			Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
1071 			return false;
1072 		}
1073 
1074         // encrypted streams will be compressed always, so after the storing this property is always true,
1075         // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
1076         // before the storing )
1077 		boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, true, pBytes, false );
1078 
1079 		// free the stream resources, garbage collector may remove the object too late
1080 		if ( !disposeStream( xSubStream, sPath ) )
1081 			return false;
1082 
1083 		return bResult;
1084 	}
1085 
copyStorage( XStorage xSourceStorage, XStorage xDestStorage )1086 	public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
1087 	{
1088 		// copy xSourceStorage to xDestStorage
1089 		try
1090 		{
1091 			xSourceStorage.copyToStorage( xDestStorage );
1092 		}
1093 		catch( Exception e )
1094 		{
1095 			Error( "Storage copying failed, exception: " + e );
1096 			return false;
1097 		}
1098 
1099 		return true;
1100 	}
1101 
commitStorage( XStorage xStorage )1102 	public boolean commitStorage( XStorage xStorage )
1103 	{
1104 		// XTransactedObject must be supported by storages
1105 		XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
1106 		if ( xTransact == null )
1107 		{
1108 			Error( "Storage doesn't implement transacted access!" );
1109 			return false;
1110 		}
1111 
1112 		try
1113 		{
1114 			xTransact.commit();
1115 		}
1116 		catch( Exception e )
1117 		{
1118 			Error( "Storage commit failed, exception:" + e );
1119 			return false;
1120 		}
1121 
1122 		return true;
1123 	}
1124 
disposeStream( XStream xStream, String sStreamName )1125 	public boolean disposeStream( XStream xStream, String sStreamName )
1126 	{
1127 		XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
1128 		if ( xComponent == null )
1129 		{
1130 			Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
1131 			return false;
1132 		}
1133 
1134 		try
1135 		{
1136 			xComponent.dispose();
1137 		}
1138 		catch( Exception e )
1139 		{
1140 			Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
1141 			return false;
1142 		}
1143 
1144 		return true;
1145 	}
1146 
disposeStorage( XStorage xStorage )1147 	public boolean disposeStorage( XStorage xStorage )
1148 	{
1149 		// dispose the storage
1150 		XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
1151 		if ( xComponent == null )
1152 		{
1153 			Error( "Can't retrieve XComponent implementation from storage!" );
1154 			return false;
1155 		}
1156 
1157 		try
1158 		{
1159 			xComponent.dispose();
1160 		}
1161 		catch( Exception e )
1162 		{
1163 			Error( "Storage disposing failed!" );
1164 			return false;
1165 		}
1166 
1167 		return true;
1168 	}
1169 
getInputStream( XStream xStream )1170 	public XInputStream getInputStream( XStream xStream )
1171 	{
1172 		XInputStream xInTemp = null;
1173 		try
1174 		{
1175 			xInTemp = xStream.getInputStream();
1176 			if ( xInTemp == null )
1177 				Error( "Can't get the input part of a stream!" );
1178 		}
1179 		catch ( Exception e )
1180 		{
1181 			Error( "Can't get the input part of a stream, exception :" + e );
1182 		}
1183 
1184 		return xInTemp;
1185 	}
1186 
closeOutput( XStream xStream )1187 	public boolean closeOutput( XStream xStream )
1188 	{
1189 		XOutputStream xOutTemp = null;
1190 		try
1191 		{
1192 			xOutTemp = xStream.getOutputStream();
1193 			if ( xOutTemp == null )
1194 			{
1195 				Error( "Can't get the output part of a stream!" );
1196 				return false;
1197 			}
1198 		}
1199 		catch ( Exception e )
1200 		{
1201 			Error( "Can't get the output part of a stream, exception :" + e );
1202 			return false;
1203 		}
1204 
1205 		try
1206 		{
1207 			xOutTemp.closeOutput();
1208 		}
1209 		catch ( Exception e )
1210 		{
1211 			Error( "Can't close output part of a stream, exception :" + e );
1212 			return false;
1213 		}
1214 
1215 		return true;
1216 	}
1217 
openSubStorage( XStorage xStorage, String sName, int nMode )1218 	public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
1219 	{
1220 		// open existing substorage
1221 		try
1222 		{
1223 			Object oSubStorage = xStorage.openStorageElement( sName, nMode );
1224 			XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
1225 			return xSubStorage;
1226 		}
1227 		catch( Exception e )
1228 		{
1229 			Error( "Can't open substorage '" + sName + "', exception: " + e );
1230 		}
1231 
1232 		return null;
1233 	}
1234 
CreateTempFileStream( XMultiServiceFactory xMSF )1235 	public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
1236 	{
1237 		// try to get temporary file representation
1238 		XStream xTempFileStream = null;
1239 		try
1240 		{
1241 			Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1242 			xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
1243 		}
1244 		catch( Exception e )
1245 		{}
1246 
1247 		if ( xTempFileStream == null )
1248 			Error( "Can't create temporary file!" );
1249 
1250 		return xTempFileStream;
1251 	}
1252 
CreateTempFile( XMultiServiceFactory xMSF )1253 	public String CreateTempFile( XMultiServiceFactory xMSF )
1254 	{
1255 		String sResult = null;
1256 
1257 		// try to get temporary file representation
1258 		XPropertySet xTempFileProps = null;
1259 		try
1260 		{
1261 			Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1262 			xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
1263 		}
1264 		catch( Exception e )
1265 		{}
1266 
1267 		if ( xTempFileProps != null )
1268 		{
1269 			try
1270 			{
1271 				xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
1272 				sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
1273 			}
1274 			catch( Exception e )
1275 			{
1276 				Error( "Can't control TempFile properties, exception: " + e );
1277 			}
1278 		}
1279 		else
1280 		{
1281 			Error( "Can't create temporary file representation!" );
1282 		}
1283 
1284 		// close temporary file explicitly
1285 		try
1286 		{
1287 			XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
1288 			if ( xStream != null )
1289 			{
1290 				XOutputStream xOut = xStream.getOutputStream();
1291 				if ( xOut != null )
1292 					xOut.closeOutput();
1293 
1294 				XInputStream xIn = xStream.getInputStream();
1295 				if ( xIn != null )
1296 					xIn.closeInput();
1297 			}
1298 			else
1299 				Error( "Can't close TempFile!" );
1300 		}
1301 		catch( Exception e )
1302 		{
1303 			Error( "Can't close TempFile, exception: " + e );
1304 		}
1305 
1306 		return sResult;
1307 	}
1308 
copyElementTo( XStorage xSource, String sName, XStorage xDest )1309 	public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
1310 	{
1311 		// copy element with name sName from xSource to xDest
1312 		try
1313 		{
1314 			xSource.copyElementTo( sName, xDest, sName );
1315 		}
1316 		catch( Exception e )
1317 		{
1318 			Error( "Element copying failed, exception: " + e );
1319 			return false;
1320 		}
1321 
1322 		return true;
1323 	}
1324 
copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )1325 	public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )
1326 	{
1327 		// copy element with name sName from xSource to xDest
1328 		try
1329 		{
1330 			xSource.copyElementTo( sName, xDest, sTargetName );
1331 		}
1332 		catch( Exception e )
1333 		{
1334 			Error( "Element copying failed, exception: " + e );
1335 			return false;
1336 		}
1337 
1338 		return true;
1339 	}
1340 
moveElementTo( XStorage xSource, String sName, XStorage xDest )1341 	public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
1342 	{
1343 		// move element with name sName from xSource to xDest
1344 		try
1345 		{
1346 			xSource.moveElementTo( sName, xDest, sName );
1347 		}
1348 		catch( Exception e )
1349 		{
1350 			Error( "Element moving failed, exception: " + e );
1351 			return false;
1352 		}
1353 
1354 		return true;
1355 	}
1356 
renameElement( XStorage xStorage, String sOldName, String sNewName )1357 	public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
1358 	{
1359 		// rename element with name sOldName to sNewName
1360 		try
1361 		{
1362 			xStorage.renameElement( sOldName, sNewName );
1363 		}
1364 		catch( Exception e )
1365 		{
1366 			Error( "Element renaming failed, exception: " + e );
1367 			return false;
1368 		}
1369 
1370 		return true;
1371 	}
1372 
removeElement( XStorage xStorage, String sName )1373 	public boolean removeElement( XStorage xStorage, String sName )
1374 	{
1375 		// remove element with name sName
1376 		try
1377 		{
1378 			xStorage.removeElement( sName );
1379 		}
1380 		catch( Exception e )
1381 		{
1382 			Error( "Element removing failed, exception: " + e );
1383 			return false;
1384 		}
1385 
1386 		return true;
1387 	}
1388 
OpenStream( XStorage xStorage, String sStreamName, int nMode )1389 	public XStream OpenStream( XStorage xStorage,
1390 								String sStreamName,
1391 								int nMode )
1392 	{
1393 		// open substream element
1394 		XStream xSubStream = null;
1395 		try
1396 		{
1397 			Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
1398 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1399 			if ( xSubStream == null )
1400 				Error( "Can't create substream '" + sStreamName + "'!" );
1401 		}
1402 		catch( Exception e )
1403 		{
1404 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
1405 		}
1406 
1407 		return xSubStream;
1408 	}
1409 
compareRawMethodsOnEncrStream( XStorage xStorage, String sStreamName )1410 	public boolean compareRawMethodsOnEncrStream( XStorage xStorage, String sStreamName )
1411 	{
1412 
1413 		XStorageRawAccess xRawStorage;
1414 		try
1415 		{
1416 			xRawStorage = (XStorageRawAccess) UnoRuntime.queryInterface( XStorageRawAccess.class, xStorage );
1417 		}
1418 		catch( Exception e )
1419 		{
1420 			Error( "Can't get raw access to the storage, exception : " + e + "!" );
1421 			return false;
1422 		}
1423 
1424 		if ( xRawStorage == null )
1425 		{
1426 			Error( "Can't get raw access to the storage!" );
1427 			return false;
1428 		}
1429 
1430 		XInputStream xHeadRawStream = null;
1431 		try
1432 		{
1433 			xHeadRawStream = xRawStorage.getRawEncrStreamElement( sStreamName );
1434 		}
1435 		catch( Exception e )
1436 		{
1437 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1438 		}
1439 
1440 		XInputStream xPlainRawStream = null;
1441 		try
1442 		{
1443 			xPlainRawStream = xRawStorage.getPlainRawStreamElement( sStreamName );
1444 		}
1445 		catch( Exception e )
1446 		{
1447 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1448 		}
1449 
1450 		if ( xHeadRawStream == null || xPlainRawStream == null )
1451 		{
1452 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw modes!" );
1453 			return false;
1454 		}
1455 
1456 		try
1457 		{
1458 			byte pData[][] = new byte[1][38];
1459 			if ( xHeadRawStream.readBytes( pData, 38 ) != 38 )
1460 			{
1461 				Error( "Can't read header of encrypted stream '" + sStreamName + "' raw representations!" );
1462 				return false;
1463 			}
1464 
1465 			if ( pData[0][0] != 0x4d || pData[0][1] != 0x4d || pData[0][2] != 0x02 || pData[0][3] != 0x05 )
1466 			{
1467 				Error( "No signature in the header of encrypted stream '" + sStreamName + "' raw representations!" );
1468 				return false;
1469 			}
1470 
1471 			int nVariableHeaderLength =
1472 						( pData[0][30] + pData[0][31] * 0x100 ) // salt length
1473 						+ ( pData[0][32] + pData[0][33] * 0x100 ) // iv length
1474 						+ ( pData[0][34] + pData[0][35] * 0x100 ) // digest length
1475 						+ ( pData[0][36] + pData[0][37] * 0x100 ); // mediatype length
1476 
1477 			xHeadRawStream.skipBytes( nVariableHeaderLength );
1478 
1479 			byte pRawData1[][] = new byte[1][32000];
1480 			byte pRawData2[][] = new byte[1][32000];
1481 			int nRead1 = 0;
1482 			int nRead2 = 0;
1483 
1484 			do
1485 			{
1486 				nRead1 = xHeadRawStream.readBytes( pRawData1, 32000 );
1487 				nRead2 = xPlainRawStream.readBytes( pRawData2, 32000 );
1488 
1489 				if ( nRead1 != nRead2 )
1490 				{
1491 					Error( "The encrypted stream '" + sStreamName + "' raw representations have different size! nRead1 - nRead2 = " + ( new Integer( nRead1 - nRead2 ) ).toString() );
1492 					return false;
1493 				}
1494 
1495 				for ( int nInd = 0; nInd < nRead1; nInd++ )
1496 					if ( pRawData1[0][nInd] != pRawData2[0][nInd] )
1497 					{
1498 						Error( "The encrypted stream '" + sStreamName + "' raw representations have different data!" );
1499 						return false;
1500 					}
1501 			}
1502 			while( nRead1 == 32000 );
1503 		}
1504 		catch ( Exception e )
1505 		{
1506 			Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : " + e + "!" );
1507 			return false;
1508 		}
1509 
1510 		return true;
1511 	}
1512 
cantOpenStorage( XStorage xStorage, String sName )1513 	public boolean cantOpenStorage( XStorage xStorage, String sName )
1514 	{
1515 		// try to open an opened substorage, open call must fail
1516 		try
1517 		{
1518 			Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
1519 			Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
1520 		}
1521 		catch( Exception e )
1522 		{
1523 			return true;
1524 		}
1525 
1526 		return false;
1527 	}
1528 
cantOpenStream( XStorage xStorage, String sName, int nMode )1529 	public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
1530 	{
1531 		// try to open the substream with specified mode must fail
1532 		try
1533 		{
1534 			Object oDummyStream = xStorage.openStreamElement( sName, nMode );
1535 			Error( "The trying to open substream '" + sName + "' must fail!" );
1536 		}
1537 		catch( Exception e )
1538 		{
1539 			return true;
1540 		}
1541 
1542 		return false;
1543 	}
1544 
cantOpenStreamH( XStorage xStorage, String sPath, int nMode )1545 	public boolean cantOpenStreamH( XStorage xStorage, String sPath, int nMode )
1546 	{
1547 		// try to open the substream with specified mode must fail
1548 
1549 		XHierarchicalStorageAccess xHStorage =
1550 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1551 		if ( xHStorage == null )
1552 		{
1553 			Error( "The storage does not support hierarchical access!" );
1554 			return false;
1555 		}
1556 
1557 		try
1558 		{
1559 			Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
1560 			Error( "The trying to open substream '" + sPath + "' must fail!" );
1561 		}
1562 		catch( Exception e )
1563 		{
1564 			return true;
1565 		}
1566 
1567 		return false;
1568 	}
1569 
cantOpenEncrStreamH( XStorage xStorage, String sPath, int nMode, String aPass )1570 	public boolean cantOpenEncrStreamH( XStorage xStorage, String sPath, int nMode, String aPass )
1571 	{
1572 		// try to open the substream with specified mode must fail
1573 
1574 		XHierarchicalStorageAccess xHStorage =
1575 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1576 		if ( xHStorage == null )
1577 		{
1578 			Error( "The storage does not support hierarchical access!" );
1579 			return false;
1580 		}
1581 
1582 		try
1583 		{
1584 			Object oDummyStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, nMode, aPass );
1585 			Error( "The trying to open substream '" + sPath + "' must fail!" );
1586 		}
1587 		catch( WrongPasswordException wpe )
1588 		{
1589 			Error( "The substream '" + sPath + "' must not exist!" );
1590 			return false;
1591 		}
1592 		catch( Exception e )
1593 		{
1594 			return true;
1595 		}
1596 
1597 		return false;
1598 	}
1599 
cloneStorage( XSingleServiceFactory xFactory, XStorage xStorage )1600 	public XStorage cloneStorage( XSingleServiceFactory xFactory, XStorage xStorage )
1601 	{
1602 		// create a copy of a last committed version of specified storage
1603 		XStorage xResult = null;
1604 		try
1605 		{
1606 			Object oTempStorage = xFactory.createInstance();
1607 			xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1608 			if ( xResult != null )
1609 				xStorage.copyLastCommitTo( xResult );
1610 		}
1611 		catch( Exception e )
1612 		{
1613 			Error( "Can't clone storage, exception: " + e );
1614 			return null;
1615 		}
1616 
1617 		return xResult;
1618 	}
1619 
cloneSubStorage( XSingleServiceFactory xFactory, XStorage xStorage, String sName )1620 	public XStorage cloneSubStorage( XSingleServiceFactory xFactory, XStorage xStorage, String sName )
1621 	{
1622 		// create a copy of a last committed version of specified substorage
1623 		XStorage xResult = null;
1624 		try
1625 		{
1626 			Object oTempStorage = xFactory.createInstance();
1627 			xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1628 			if ( xResult != null )
1629 				xStorage.copyStorageElementLastCommitTo( sName, xResult );
1630 		}
1631 		catch( Exception e )
1632 		{
1633 			Error( "Can't clone substorage '" + sName + "', exception: " + e );
1634 			return null;
1635 		}
1636 
1637 		return xResult;
1638 	}
1639 
cloneSubStream( XStorage xStorage, String sName )1640 	public XStream cloneSubStream( XStorage xStorage, String sName )
1641 	{
1642 		// clone existing substream
1643 		try
1644 		{
1645 			XStream xStream = xStorage.cloneStreamElement( sName );
1646 			return xStream;
1647 		}
1648 		catch( Exception e )
1649 		{
1650 			Error( "Can't clone substream '" + sName + "', exception: " + e );
1651 		}
1652 
1653 		return null;
1654 	}
1655 
cloneEncrSubStream( XStorage xStorage, String sName, String sPass )1656 	public XStream cloneEncrSubStream( XStorage xStorage, String sName, String sPass )
1657 	{
1658 		// clone existing substream
1659 		try
1660 		{
1661 			XStream xStream = xStorage.cloneEncryptedStreamElement( sName, sPass );
1662 			return xStream;
1663 		}
1664 		catch( Exception e )
1665 		{
1666 			Error( "Can't clone encrypted substream '" + sName + "', exception: " + e );
1667 		}
1668 
1669 		return null;
1670 	}
1671 
Error( String sError )1672 	public void Error( String sError )
1673 	{
1674 		m_aLogWriter.println( m_sTestPrefix + "Error: " + sError );
1675 	}
1676 
Message( String sMessage )1677 	public void Message( String sMessage )
1678 	{
1679 		m_aLogWriter.println( m_sTestPrefix + sMessage );
1680 	}
1681 }
1682 
1683