1*a740f2aaSAndrew Rist /************************************************************** 2*a740f2aaSAndrew Rist * 3*a740f2aaSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a740f2aaSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a740f2aaSAndrew Rist * distributed with this work for additional information 6*a740f2aaSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a740f2aaSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a740f2aaSAndrew Rist * "License"); you may not use this file except in compliance 9*a740f2aaSAndrew Rist * with the License. You may obtain a copy of the License at 10*a740f2aaSAndrew Rist * 11*a740f2aaSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a740f2aaSAndrew Rist * 13*a740f2aaSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a740f2aaSAndrew Rist * software distributed under the License is distributed on an 15*a740f2aaSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a740f2aaSAndrew Rist * KIND, either express or implied. See the License for the 17*a740f2aaSAndrew Rist * specific language governing permissions and limitations 18*a740f2aaSAndrew Rist * under the License. 19*a740f2aaSAndrew Rist * 20*a740f2aaSAndrew Rist *************************************************************/ 21*a740f2aaSAndrew Rist 22cdf0e10cSrcweir package complex.storages; 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.XInterface; 25cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30cdf0e10cSrcweir import com.sun.star.uno.XInterface; 31cdf0e10cSrcweir 32cdf0e10cSrcweir import com.sun.star.embed.*; 33cdf0e10cSrcweir 34cdf0e10cSrcweir import share.LogWriter; 35cdf0e10cSrcweir import complex.storages.TestHelper; 36cdf0e10cSrcweir import complex.storages.StorageTest; 37cdf0e10cSrcweir 38cdf0e10cSrcweir public class Test15 implements StorageTest { 39cdf0e10cSrcweir 40cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 41cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 42cdf0e10cSrcweir TestHelper m_aTestHelper; 43cdf0e10cSrcweir Test15( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter )44cdf0e10cSrcweir public Test15( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 45cdf0e10cSrcweir { 46cdf0e10cSrcweir m_xMSF = xMSF; 47cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 48cdf0e10cSrcweir m_aTestHelper = new TestHelper( aLogWriter, "Test15: " ); 49cdf0e10cSrcweir } 50cdf0e10cSrcweir test()51cdf0e10cSrcweir public boolean test() 52cdf0e10cSrcweir { 53cdf0e10cSrcweir String aStreamPrefix = ""; 54cdf0e10cSrcweir for ( int nInd = 0; nInd < 4; ++nInd, aStreamPrefix += "SubStorage" + nInd ) 55cdf0e10cSrcweir if ( !testForPath( aStreamPrefix ) ) 56cdf0e10cSrcweir return false; 57cdf0e10cSrcweir 58cdf0e10cSrcweir return true; 59cdf0e10cSrcweir } 60cdf0e10cSrcweir testForPath( String aStreamPrefix )61cdf0e10cSrcweir public boolean testForPath( String aStreamPrefix ) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir try 64cdf0e10cSrcweir { 65cdf0e10cSrcweir String aSubStream1Path = aStreamPrefix + "SubStream1"; 66cdf0e10cSrcweir String aSubStream2Path = aStreamPrefix + "SubStream2"; 67cdf0e10cSrcweir String aSubStream3Path = aStreamPrefix + "SubStream3"; 68cdf0e10cSrcweir String aSubStream4Path = aStreamPrefix + "SubStream4"; 69cdf0e10cSrcweir 70cdf0e10cSrcweir String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 71cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL == "" ) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir m_aTestHelper.Error( "No valid temporary file was created!" ); 74cdf0e10cSrcweir return false; 75cdf0e10cSrcweir } 76cdf0e10cSrcweir 77cdf0e10cSrcweir // create temporary storage based on a previously created temporary file 78cdf0e10cSrcweir Object pArgs[] = new Object[2]; 79cdf0e10cSrcweir pArgs[0] = (Object) sTempFileURL; 80cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.WRITE ); 81cdf0e10cSrcweir 82cdf0e10cSrcweir Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 83cdf0e10cSrcweir XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 84cdf0e10cSrcweir if ( xTempFileStorage == null ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 87cdf0e10cSrcweir return false; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir 90cdf0e10cSrcweir // set the global password for the root storage 91cdf0e10cSrcweir XEncryptionProtectedSource xTempStorageEncryption = 92cdf0e10cSrcweir (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir if ( xTempStorageEncryption == null ) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" ); 97cdf0e10cSrcweir return true; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir String sPass1 = "12345"; 101cdf0e10cSrcweir String sPass2 = "54321"; 102cdf0e10cSrcweir 103cdf0e10cSrcweir try { 104cdf0e10cSrcweir xTempStorageEncryption.setEncryptionPassword( sPass1 ); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir catch( Exception e ) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e ); 109cdf0e10cSrcweir return false; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir 113cdf0e10cSrcweir byte pBytes1[] = { 1, 1, 1, 1, 1 }; 114cdf0e10cSrcweir byte pBytes2[] = { 2, 2, 2, 2, 2 }; 115cdf0e10cSrcweir 116cdf0e10cSrcweir // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 117cdf0e10cSrcweir // and commit 118cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream1Path, "MediaType1", true, pBytes1, true, true ) ) 119cdf0e10cSrcweir return false; 120cdf0e10cSrcweir 121cdf0e10cSrcweir // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 122cdf0e10cSrcweir // and commit 123cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", false, pBytes2, sPass2, true ) ) 124cdf0e10cSrcweir return false; 125cdf0e10cSrcweir 126cdf0e10cSrcweir // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 127cdf0e10cSrcweir // and commit 128cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", false, pBytes2, sPass2, true ) ) 129cdf0e10cSrcweir return false; 130cdf0e10cSrcweir 131cdf0e10cSrcweir // open a new substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 132cdf0e10cSrcweir // and dont commit 133cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncrH( xTempFileStorage, aSubStream4Path, "MediaType2", true, pBytes1, true, false ) ) 134cdf0e10cSrcweir return false; 135cdf0e10cSrcweir 136cdf0e10cSrcweir 137cdf0e10cSrcweir // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 138cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempFileStorage, 139cdf0e10cSrcweir "MediaType3", 140cdf0e10cSrcweir true, 141cdf0e10cSrcweir ElementModes.WRITE ) ) 142cdf0e10cSrcweir return false; 143cdf0e10cSrcweir 144cdf0e10cSrcweir // commit the root storage so the contents must be stored now 145cdf0e10cSrcweir if ( !m_aTestHelper.commitStorage( xTempFileStorage ) ) 146cdf0e10cSrcweir return false; 147cdf0e10cSrcweir 148cdf0e10cSrcweir // dispose used storages to free resources 149cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 150cdf0e10cSrcweir return false; 151cdf0e10cSrcweir 152cdf0e10cSrcweir // ================================================ 153cdf0e10cSrcweir // now reopen the storage, 154cdf0e10cSrcweir // check all the written and copied information 155cdf0e10cSrcweir // and change it 156cdf0e10cSrcweir // ================================================ 157cdf0e10cSrcweir 158cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 159cdf0e10cSrcweir oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 160cdf0e10cSrcweir xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 161cdf0e10cSrcweir if ( xTempFileStorage == null ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 164cdf0e10cSrcweir return false; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir 167cdf0e10cSrcweir // set the global password for the root storage 168cdf0e10cSrcweir xTempStorageEncryption = 169cdf0e10cSrcweir (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempFileStorage ); 170cdf0e10cSrcweir 171cdf0e10cSrcweir if ( xTempStorageEncryption == null ) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" ); 174cdf0e10cSrcweir return false; 175cdf0e10cSrcweir } 176cdf0e10cSrcweir 177cdf0e10cSrcweir try { 178cdf0e10cSrcweir xTempStorageEncryption.setEncryptionPassword( sPass2 ); 179cdf0e10cSrcweir } 180cdf0e10cSrcweir catch( Exception e ) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e ); 183cdf0e10cSrcweir return false; 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir 187cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xTempFileStorage, "MediaType3", true, ElementModes.WRITE ) ) 188cdf0e10cSrcweir return false; 189cdf0e10cSrcweir 190cdf0e10cSrcweir if ( !m_aTestHelper.checkEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType1", pBytes1, sPass1 ) ) 191cdf0e10cSrcweir return false; 192cdf0e10cSrcweir 193cdf0e10cSrcweir if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream2Path, "MediaType2", true, pBytes2 ) ) 194cdf0e10cSrcweir return false; 195cdf0e10cSrcweir 196cdf0e10cSrcweir if ( !m_aTestHelper.checkStreamH( xTempFileStorage, aSubStream3Path, "MediaType3", true, pBytes2 ) ) 197cdf0e10cSrcweir return false; 198cdf0e10cSrcweir 199cdf0e10cSrcweir if ( !m_aTestHelper.cantOpenEncrStreamH( xTempFileStorage, aSubStream4Path, ElementModes.READ, sPass1 ) ) 200cdf0e10cSrcweir return false; 201cdf0e10cSrcweir 202cdf0e10cSrcweir // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 203cdf0e10cSrcweir // and commit 204cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToEncrStreamH( xTempFileStorage, aSubStream1Path, "MediaType4", true, pBytes2, sPass1, true ) ) 205cdf0e10cSrcweir return false; 206cdf0e10cSrcweir 207cdf0e10cSrcweir // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 208cdf0e10cSrcweir // and don't commit 209cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream2Path, "MediaType5", true, pBytes1, true ) ) 210cdf0e10cSrcweir return false; 211cdf0e10cSrcweir 212cdf0e10cSrcweir // change the password of the existing stream 213cdf0e10cSrcweir if ( m_aTestHelper.ChangeStreamPassH( xTempFileStorage, aSubStream2Path, sPass2, sPass1, true ) != 1 ) 214cdf0e10cSrcweir return false; 215cdf0e10cSrcweir 216cdf0e10cSrcweir // open existing substream hierarchically, set "MediaType" and "Compressed" properties to it, write some bytes 217cdf0e10cSrcweir // and don't commit 218cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToStreamH( xTempFileStorage, aSubStream3Path, "MediaType5", true, pBytes1, false ) ) 219cdf0e10cSrcweir return false; 220cdf0e10cSrcweir 221cdf0e10cSrcweir // commit the root storage so the contents must be stored now 222cdf0e10cSrcweir if ( !m_aTestHelper.commitStorage( xTempFileStorage ) ) 223cdf0e10cSrcweir return false; 224cdf0e10cSrcweir 225cdf0e10cSrcweir // dispose used storages to free resources 226cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 227cdf0e10cSrcweir return false; 228cdf0e10cSrcweir 229cdf0e10cSrcweir // ================================================ 230cdf0e10cSrcweir // now reopen the storage, 231cdf0e10cSrcweir // check all the written information 232cdf0e10cSrcweir // ================================================ 233cdf0e10cSrcweir 234cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 235cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.READ ); 236cdf0e10cSrcweir Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 237cdf0e10cSrcweir XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage ); 238cdf0e10cSrcweir if ( xResultStorage == null ) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 241cdf0e10cSrcweir return false; 242cdf0e10cSrcweir } 243cdf0e10cSrcweir 244cdf0e10cSrcweir // set the global password for the root storage 245cdf0e10cSrcweir xTempStorageEncryption = 246cdf0e10cSrcweir (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage ); 247cdf0e10cSrcweir 248cdf0e10cSrcweir if ( xTempStorageEncryption == null ) 249cdf0e10cSrcweir { 250cdf0e10cSrcweir m_aTestHelper.Error( "XEncryptionProtectedSource is supported, but can not be retrieved!" ); 251cdf0e10cSrcweir return false; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir try { 255cdf0e10cSrcweir xTempStorageEncryption.setEncryptionPassword( sPass1 ); 256cdf0e10cSrcweir } 257cdf0e10cSrcweir catch( Exception e ) 258cdf0e10cSrcweir { 259cdf0e10cSrcweir m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e ); 260cdf0e10cSrcweir return false; 261cdf0e10cSrcweir } 262cdf0e10cSrcweir 263cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType3", true, ElementModes.READ ) ) 264cdf0e10cSrcweir return false; 265cdf0e10cSrcweir 266cdf0e10cSrcweir if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream1Path, "MediaType4", true, pBytes2 ) ) 267cdf0e10cSrcweir return false; 268cdf0e10cSrcweir 269cdf0e10cSrcweir if ( !m_aTestHelper.checkStreamH( xResultStorage, aSubStream2Path, "MediaType5", true, pBytes1 ) ) 270cdf0e10cSrcweir return false; 271cdf0e10cSrcweir 272cdf0e10cSrcweir if ( !m_aTestHelper.checkEncrStreamH( xResultStorage, aSubStream3Path, "MediaType3", pBytes2, sPass2 ) ) 273cdf0e10cSrcweir return false; 274cdf0e10cSrcweir 275cdf0e10cSrcweir // dispose used storages to free resources 276cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 277cdf0e10cSrcweir return false; 278cdf0e10cSrcweir 279cdf0e10cSrcweir return true; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir catch( Exception e ) 282cdf0e10cSrcweir { 283cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 284cdf0e10cSrcweir return false; 285cdf0e10cSrcweir } 286cdf0e10cSrcweir } 287cdf0e10cSrcweir 288cdf0e10cSrcweir } 289cdf0e10cSrcweir 290