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