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