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