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