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