1*cdf0e10cSrcweir package complex.storages; 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 4*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 5*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 6*cdf0e10cSrcweir 7*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 8*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 9*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 10*cdf0e10cSrcweir 11*cdf0e10cSrcweir import com.sun.star.embed.*; 12*cdf0e10cSrcweir 13*cdf0e10cSrcweir import share.LogWriter; 14*cdf0e10cSrcweir import complex.storages.TestHelper; 15*cdf0e10cSrcweir import complex.storages.StorageTest; 16*cdf0e10cSrcweir 17*cdf0e10cSrcweir public class Test08 implements StorageTest { 18*cdf0e10cSrcweir 19*cdf0e10cSrcweir XMultiServiceFactory m_xMSF; 20*cdf0e10cSrcweir XSingleServiceFactory m_xStorageFactory; 21*cdf0e10cSrcweir TestHelper m_aTestHelper; 22*cdf0e10cSrcweir 23*cdf0e10cSrcweir public Test08( XMultiServiceFactory xMSF, XSingleServiceFactory xStorageFactory, LogWriter aLogWriter ) 24*cdf0e10cSrcweir { 25*cdf0e10cSrcweir m_xMSF = xMSF; 26*cdf0e10cSrcweir m_xStorageFactory = xStorageFactory; 27*cdf0e10cSrcweir m_aTestHelper = new TestHelper( aLogWriter, "Test08: " ); 28*cdf0e10cSrcweir } 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir public boolean test() 31*cdf0e10cSrcweir { 32*cdf0e10cSrcweir try 33*cdf0e10cSrcweir { 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir // create temporary storage based on arbitrary medium 36*cdf0e10cSrcweir // after such a storage is closed it is lost 37*cdf0e10cSrcweir Object oTempStorage = m_xStorageFactory.createInstance(); 38*cdf0e10cSrcweir XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); 39*cdf0e10cSrcweir if ( xTempStorage == null ) 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create temporary storage representation!" ); 42*cdf0e10cSrcweir return false; 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir // set the global password for the root storage 46*cdf0e10cSrcweir XEncryptionProtectedSource xTempStorageEncryption = 47*cdf0e10cSrcweir (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xTempStorage ); 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir if ( xTempStorageEncryption == null ) 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir m_aTestHelper.Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" ); 52*cdf0e10cSrcweir return true; 53*cdf0e10cSrcweir } 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir String sPass1 = "123"; 56*cdf0e10cSrcweir String sPass2 = "321"; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir try { 59*cdf0e10cSrcweir xTempStorageEncryption.setEncryptionPassword( sPass1 ); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir catch( Exception e ) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e ); 64*cdf0e10cSrcweir return false; 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir // open a new substorage 68*cdf0e10cSrcweir XStorage xTempSubStorage = m_aTestHelper.openSubStorage( xTempStorage, 69*cdf0e10cSrcweir "SubStorage1", 70*cdf0e10cSrcweir ElementModes.WRITE ); 71*cdf0e10cSrcweir if ( xTempSubStorage == null ) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create substorage!" ); 74*cdf0e10cSrcweir return false; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir byte pBigBytes[] = new byte[33000]; 78*cdf0e10cSrcweir for ( int nInd = 0; nInd < 33000; nInd++ ) 79*cdf0e10cSrcweir pBigBytes[nInd] = (byte)( nInd % 128 ); 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 82*cdf0e10cSrcweir // the stream will be encrypted with common password 83*cdf0e10cSrcweir byte pBytes1[] = { 1, 1, 1, 1, 1 }; 84*cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream1", "MediaType1", true, pBytes1, true ) ) 85*cdf0e10cSrcweir return false; 86*cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream1", "MediaType1", true, pBigBytes, true ) ) 87*cdf0e10cSrcweir return false; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 90*cdf0e10cSrcweir // the stream will not be encrypted 91*cdf0e10cSrcweir byte pBytes2[] = { 2, 2, 2, 2, 2 }; 92*cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "SubStream2", "MediaType2", false, pBytes2, false ) ) 93*cdf0e10cSrcweir return false; 94*cdf0e10cSrcweir if ( !m_aTestHelper.WBToSubstrOfEncr( xTempSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes, false ) ) 95*cdf0e10cSrcweir return false; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 98*cdf0e10cSrcweir // the stream will be compressed with own password 99*cdf0e10cSrcweir byte pBytes3[] = { 3, 3, 3, 3, 3 }; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir // open a new substream, set "MediaType" and "Compressed" properties to it and write some bytes 102*cdf0e10cSrcweir // the stream will not be encrypted 103*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "SubStream3", "MediaType3", false, pBytes3, sPass2 ) ) 104*cdf0e10cSrcweir return false; 105*cdf0e10cSrcweir if ( !m_aTestHelper.WriteBytesToEncrSubstream( xTempSubStorage, "BigSubStream3", "MediaType3", false, pBigBytes, sPass2 ) ) 106*cdf0e10cSrcweir return false; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 109*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempStorage, 110*cdf0e10cSrcweir "MediaType4", 111*cdf0e10cSrcweir true, 112*cdf0e10cSrcweir ElementModes.WRITE ) ) 113*cdf0e10cSrcweir return false; 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir // set "MediaType" property for storages and check that "IsRoot" and "OpenMode" properties are set correctly 116*cdf0e10cSrcweir if ( !m_aTestHelper.setStorageTypeAndCheckProps( xTempSubStorage, 117*cdf0e10cSrcweir "MediaType5", 118*cdf0e10cSrcweir false, 119*cdf0e10cSrcweir ElementModes.WRITE ) ) 120*cdf0e10cSrcweir return false; 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir // create temporary file 123*cdf0e10cSrcweir String sTempFileURL = m_aTestHelper.CreateTempFile( m_xMSF ); 124*cdf0e10cSrcweir if ( sTempFileURL == null || sTempFileURL == "" ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir m_aTestHelper.Error( "No valid temporary file was created!" ); 127*cdf0e10cSrcweir return false; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir // create temporary storage based on a previously created temporary file 131*cdf0e10cSrcweir Object pArgs[] = new Object[2]; 132*cdf0e10cSrcweir pArgs[0] = (Object) sTempFileURL; 133*cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.WRITE ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir Object oTempFileStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 136*cdf0e10cSrcweir XStorage xTempFileStorage = (XStorage)UnoRuntime.queryInterface( XStorage.class, oTempFileStorage ); 137*cdf0e10cSrcweir if ( xTempFileStorage == null ) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir m_aTestHelper.Error( "Can't create storage based on temporary file!" ); 140*cdf0e10cSrcweir return false; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // copy xTempStorage to xTempFileStorage 144*cdf0e10cSrcweir // xTempFileStorage will be automatically commited 145*cdf0e10cSrcweir if ( !m_aTestHelper.copyStorage( xTempStorage, xTempFileStorage ) ) 146*cdf0e10cSrcweir return false; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir // dispose used storages to free resources 149*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xTempStorage ) || !m_aTestHelper.disposeStorage( xTempFileStorage ) ) 150*cdf0e10cSrcweir return false; 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // ================================================ 153*cdf0e10cSrcweir // now check all the written and copied information 154*cdf0e10cSrcweir // ================================================ 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir // the temporary file must not be locked any more after storage disposing 157*cdf0e10cSrcweir pArgs[1] = new Integer( ElementModes.READ ); 158*cdf0e10cSrcweir Object oResultStorage = m_xStorageFactory.createInstanceWithArguments( pArgs ); 159*cdf0e10cSrcweir XStorage xResultStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oResultStorage ); 160*cdf0e10cSrcweir if ( xResultStorage == null ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir m_aTestHelper.Error( "Can't reopen storage based on temporary file!" ); 163*cdf0e10cSrcweir return false; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultStorage, "MediaType4", true, ElementModes.READ ) ) 167*cdf0e10cSrcweir return false; 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // open existing substorage 170*cdf0e10cSrcweir XStorage xResultSubStorage = m_aTestHelper.openSubStorage( xResultStorage, 171*cdf0e10cSrcweir "SubStorage1", 172*cdf0e10cSrcweir ElementModes.READ ); 173*cdf0e10cSrcweir if ( xResultSubStorage == null ) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir m_aTestHelper.Error( "Can't open existing substorage!" ); 176*cdf0e10cSrcweir return false; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir if ( !m_aTestHelper.checkStorageProperties( xResultSubStorage, "MediaType5", false, ElementModes.READ ) ) 180*cdf0e10cSrcweir return false; 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir // set the global password for the root storage 183*cdf0e10cSrcweir XEncryptionProtectedSource xResultStorageEncryption = 184*cdf0e10cSrcweir (XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xResultStorage ); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir if ( xResultStorageEncryption == null ) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir m_aTestHelper.Error( "XEncryptionProtectedSource was successfully used already, so it must be supported!" ); 189*cdf0e10cSrcweir return false; 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir try { 193*cdf0e10cSrcweir xResultStorageEncryption.setEncryptionPassword( sPass2 ); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir catch( Exception e ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir m_aTestHelper.Error( "Can't set a common encryption key for the storage, exception:" + e ); 198*cdf0e10cSrcweir return false; 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "SubStream1", "MediaType1", pBytes1, sPass1 ) ) 202*cdf0e10cSrcweir return false; 203*cdf0e10cSrcweir if ( !m_aTestHelper.checkEncrStream( xResultSubStorage, "BigSubStream1", "MediaType1", pBigBytes, sPass1 ) ) 204*cdf0e10cSrcweir return false; 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream2", "MediaType2", false, pBytes2 ) ) 207*cdf0e10cSrcweir return false; 208*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream2", "MediaType2", false, pBigBytes ) ) 209*cdf0e10cSrcweir return false; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir // the common root storage password should allow to open this stream 212*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "SubStream3", "MediaType3", true, pBytes3 ) ) 213*cdf0e10cSrcweir return false; 214*cdf0e10cSrcweir if ( !m_aTestHelper.checkStream( xResultSubStorage, "BigSubStream3", "MediaType3", true, pBigBytes ) ) 215*cdf0e10cSrcweir return false; 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir // dispose used storages to free resources 218*cdf0e10cSrcweir if ( !m_aTestHelper.disposeStorage( xResultStorage ) ) 219*cdf0e10cSrcweir return false; 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir return true; 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir catch( Exception e ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir m_aTestHelper.Error( "Exception: " + e ); 226*cdf0e10cSrcweir return false; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231