00001
00002
00003 #include <iostream>
00004
00005 using std::cerr ;
00006 using std::cout ;
00007 using std::endl ;
00008
00009 #include "containerT.h"
00010 #include "TheBESKeys.h"
00011 #include "BESContainerStorageList.h"
00012 #include "BESFileContainer.h"
00013 #include "BESContainerStorage.h"
00014 #include "BESContainerStorageFile.h"
00015 #include "BESCache.h"
00016 #include "BESException.h"
00017 #include <test_config.h>
00018
00019 int containerT::
00020 run(void)
00021 {
00022 cout << endl << "*****************************************" << endl;
00023 cout << "Entered containerT::run" << endl;
00024 int retVal = 0;
00025
00026
00027
00028
00029 try
00030 {
00031 string key = (string)"BES.Container.Persistence.File.TheFile=" +
00032 TEST_SRC_DIR + "/container01.file" ;
00033 TheBESKeys::TheKeys()->set_key( key ) ;
00034 BESContainerStorageList::TheList()->add_persistence( new BESContainerStorageFile( "TheFile" ) ) ;
00035 }
00036 catch( BESException &e )
00037 {
00038 cerr << "couldn't add storage to storage list:" << endl ;
00039 cerr << e.get_message() << endl ;
00040 return 1 ;
00041 }
00042
00043 cout << endl << "*****************************************" << endl;
00044 cout << "try to find symbolic name that doesn't exist, default" << endl;
00045 try
00046 {
00047 BESContainer *c =
00048 BESContainerStorageList::TheList()->look_for( "nosym" ) ;
00049 cerr << "Found nosym, shouldn't have" << endl ;
00050 if( c )
00051 cerr << "container is valid, should not be" << endl ;
00052 cerr << " real_name = " << c->get_real_name() << endl ;
00053 cerr << " constraint = " << c->get_constraint() << endl ;
00054 cerr << " sym_name = " << c->get_symbolic_name() << endl ;
00055 cerr << " container type = " << c->get_container_type() << endl ;
00056 return 1 ;
00057 }
00058 catch( BESException &e )
00059 {
00060 cout << "caught exception, didn't find nosym, good" << endl ;
00061 cout << e.get_message() << endl ;
00062 }
00063
00064 cout << endl << "*****************************************" << endl;
00065 cout << "try to find symbolic name that does exist, default" << endl;
00066 try
00067 {
00068 BESContainer *c =
00069 BESContainerStorageList::TheList()->look_for( "sym1" ) ;
00070 if( c ) cout << "found sym1" << endl ;
00071 if( c->get_symbolic_name() != "sym1" )
00072 {
00073 cerr << "symbolic name != sym1, " << c->get_symbolic_name()
00074 << endl ;
00075 return 1 ;
00076 }
00077 if( c->get_real_name() != "real1" )
00078 {
00079 cerr << "real name != real1, " << c->get_real_name()
00080 << endl ;
00081 return 1 ;
00082 }
00083 if( c->get_container_type() != "type1" )
00084 {
00085 cerr << "real name != type1, " << c->get_container_type()
00086 << endl ;
00087 return 1 ;
00088 }
00089 delete c ;
00090 }
00091 catch( BESException &e )
00092 {
00093 cerr << "didn't find sym1, should have" << endl ;
00094 return 1 ;
00095 }
00096
00097 cout << endl << "*****************************************" << endl;
00098 cout << "set to strict" << endl;
00099 TheBESKeys::TheKeys()->set_key( "BES.Container.Persistence=strict" ) ;
00100
00101 cout << endl << "*****************************************" << endl;
00102 cout << "try to find symbolic name that doesn't exist, strict" << endl;
00103 try
00104 {
00105 BESContainer *c =
00106 BESContainerStorageList::TheList()->look_for( "nosym" ) ;
00107 if( c )
00108 {
00109 cerr << "Found nosym, shouldn't have" << endl ;
00110 cerr << " real_name = " << c->get_real_name() << endl ;
00111 cerr << " constraint = " << c->get_constraint() << endl ;
00112 cerr << " sym_name = " << c->get_symbolic_name() << endl ;
00113 cerr << " container type = " << c->get_container_type() << endl ;
00114 }
00115 else
00116 {
00117 cerr << "look_for returned with null c, should have thrown"
00118 << endl ;
00119 }
00120 return 1 ;
00121 }
00122 catch( BESException &e )
00123 {
00124 cout << "caught exception, didn't find nosym, good" << endl ;
00125 cout << e.get_message() << endl ;
00126 }
00127
00128 cout << endl << "*****************************************" << endl;
00129 cout << "try to find symbolic name that does exist, strict" << endl;
00130 try
00131 {
00132 BESContainer *c =
00133 BESContainerStorageList::TheList()->look_for( "sym1" ) ;
00134 if( c )
00135 {
00136 cout << "found sym1" << endl ;
00137 if( c->get_symbolic_name() != "sym1" )
00138 {
00139 cerr << "symbolic name != sym1, " << c->get_symbolic_name()
00140 << endl ;
00141 return 1 ;
00142 }
00143 if( c->get_real_name() != "real1" )
00144 {
00145 cerr << "real name != real1, " << c->get_real_name()
00146 << endl ;
00147 return 1 ;
00148 }
00149 if( c->get_container_type() != "type1" )
00150 {
00151 cerr << "real name != type1, " << c->get_container_type()
00152 << endl ;
00153 return 1 ;
00154 }
00155 }
00156 else
00157 {
00158 cerr << "returned but not found" << endl ;
00159 return 1 ;
00160 }
00161 }
00162 catch( BESException &e )
00163 {
00164 cerr << "didn't find sym1, should have" << endl ;
00165 return 1 ;
00166 }
00167
00168 cout << endl << "*****************************************" << endl;
00169 cout << "set to nice" << endl;
00170 TheBESKeys::TheKeys()->set_key( "BES.Container.Persistence=nice" ) ;
00171
00172 cout << endl << "*****************************************" << endl;
00173 cout << "try to find symbolic name that doesn't exist, nice" << endl;
00174 try
00175 {
00176 BESContainer *c =
00177 BESContainerStorageList::TheList()->look_for( "nosym" ) ;
00178 if( c )
00179 {
00180 cerr << "Found nosym, shouldn't have" << endl ;
00181 cerr << " real_name = " << c->get_real_name() << endl ;
00182 cerr << " constraint = " << c->get_constraint() << endl ;
00183 cerr << " sym_name = " << c->get_symbolic_name() << endl ;
00184 cerr << " container type = " << c->get_container_type() << endl ;
00185 return 1 ;
00186 }
00187 else
00188 {
00189 cout << "didn't find nosym, didn't throw exception, good" << endl ;
00190 }
00191 }
00192 catch( BESException &e )
00193 {
00194 cerr << "caught exception, shouldn't have" << endl ;
00195 cerr << e.get_message() << endl ;
00196 return 1 ;
00197 }
00198
00199 cout << endl << "*****************************************" << endl;
00200 cout << "try to find symbolic name that does exist, nice" << endl;
00201 try
00202 {
00203 BESContainer *c =
00204 BESContainerStorageList::TheList()->look_for( "sym1" ) ;
00205 if( c )
00206 {
00207 if( c->get_symbolic_name() != "sym1" )
00208 {
00209 cerr << "symbolic name != sym1, " << c->get_symbolic_name()
00210 << endl ;
00211 return 1 ;
00212 }
00213 if( c->get_real_name() != "real1" )
00214 {
00215 cerr << "real name != real1, " << c->get_real_name()
00216 << endl ;
00217 return 1 ;
00218 }
00219 if( c->get_container_type() != "type1" )
00220 {
00221 cerr << "real name != type1, " << c->get_container_type()
00222 << endl ;
00223 return 1 ;
00224 }
00225 }
00226 else
00227 {
00228 cerr << "didn't find sym1" << endl ;
00229 return 1 ;
00230 }
00231 }
00232 catch( BESException &e )
00233 {
00234 cerr << "didn't find sym1, should have" << endl ;
00235 return 1 ;
00236 }
00237
00238
00239
00240
00241
00242
00243 string cache_dir = (string)TEST_SRC_DIR + "/cache" ;
00244 bool isdotdot = false ;
00245 string::size_type dotdot = cache_dir.find( "../" ) ;
00246 if( dotdot != string::npos )
00247 isdotdot = true ;
00248
00249 string src_file = cache_dir + "/testfile.txt" ;
00250 string com_file = cache_dir + "/testfile.txt.gz" ;
00251
00252 TheBESKeys::TheKeys()->set_key( "BES.CacheDir", cache_dir ) ;
00253 TheBESKeys::TheKeys()->set_key( "BES.CachePrefix", "cont_cache" ) ;
00254 TheBESKeys::TheKeys()->set_key( "BES.CacheSize", "1" ) ;
00255
00256 string chmod = (string)"chmod a+w " + TEST_SRC_DIR + "/cache" ;
00257 system( chmod.c_str() ) ;
00258
00259 cout << endl << "*****************************************" << endl;
00260 cout << "access a non compressed file" << endl;
00261 if( !isdotdot )
00262 {
00263 try
00264 {
00265 BESFileContainer c( "sym", src_file, "txt" ) ;
00266
00267 string result = c.access() ;
00268 if( result != src_file )
00269 {
00270 cerr << "result " << result << " does not match src "
00271 << src_file << endl ;
00272 return 1 ;
00273 }
00274 else
00275 {
00276 cout << "result matches src" << endl ;
00277 }
00278 }
00279 catch( BESException &e )
00280 {
00281 cerr << "Failed to access non compressed file" << endl ;
00282 cerr << e.get_message() << endl ;
00283 return 1 ;
00284 }
00285 catch( ... )
00286 {
00287 cerr << "Failed to access non compressed file" << endl ;
00288 cerr << "Unknown error" << endl ;
00289 return 1 ;
00290 }
00291 }
00292 else
00293 {
00294 try
00295 {
00296 BESFileContainer c( "sym", src_file, "txt" ) ;
00297
00298 string result = c.access() ;
00299 cerr << "Should have failed with ../ in container real name: "
00300 << src_file << endl ;
00301 return 1 ;
00302 }
00303 catch( BESException &e )
00304 {
00305 cout << "Failed to access file with ../ in name, good" << endl ;
00306 }
00307 catch( ... )
00308 {
00309 cerr << "Failed to access non compressed file" << endl ;
00310 cerr << "Unknown error" << endl ;
00311 return 1 ;
00312 }
00313 }
00314
00315 cout << endl << "*****************************************" << endl;
00316 cout << "access a compressed file" << endl;
00317 if( !isdotdot )
00318 {
00319 try
00320 {
00321 BESCache cache( *(TheBESKeys::TheKeys()),
00322 "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00323 string target ;
00324 bool is_it = cache.is_cached( com_file, target ) ;
00325 if( is_it )
00326 {
00327 if( remove( target.c_str() ) != 0 )
00328 {
00329 cerr << "Unable to remove target file " << target
00330 << " , initializing test" << endl ;
00331 return 1 ;
00332 }
00333 }
00334
00335 BESFileContainer c( "sym", com_file, "txt" ) ;
00336
00337 string result = c.access() ;
00338 if( result != target )
00339 {
00340 cerr << "result " << result << " does not match target "
00341 << target << endl ;
00342 return 1 ;
00343 }
00344 else
00345 {
00346 cout << "result matches src" << endl ;
00347 }
00348
00349 if( cache.is_cached( com_file, target ) )
00350 {
00351 cout << "file is now cached" << endl ;
00352 }
00353 else
00354 {
00355 cerr << "file should be cached in " << target << endl ;
00356 return 1 ;
00357 }
00358 }
00359 catch( BESException &e )
00360 {
00361 cerr << "Failed to access compressed file" << endl ;
00362 cerr << e.get_message() << endl ;
00363 return 1 ;
00364 }
00365 catch( ... )
00366 {
00367 cerr << "Failed to access compressed file" << endl ;
00368 cerr << "Unknown error" << endl ;
00369 return 1 ;
00370 }
00371 }
00372 else
00373 {
00374 try
00375 {
00376 BESCache cache( *(TheBESKeys::TheKeys()),
00377 "BES.CacheDir", "BES.CachePrefix", "BES.CacheSize" ) ;
00378 string target ;
00379 bool is_it = cache.is_cached( com_file, target ) ;
00380 if( is_it )
00381 {
00382 if( remove( target.c_str() ) != 0 )
00383 {
00384 cerr << "Unable to remove target file " << target
00385 << " , initializing test" << endl ;
00386 return 1 ;
00387 }
00388 }
00389
00390 BESFileContainer c( "sym", com_file, "txt" ) ;
00391
00392 string result = c.access() ;
00393 cerr << "Should have failed with ../ in container real name: "
00394 << com_file << endl ;
00395 return 1 ;
00396 }
00397 catch( BESException &e )
00398 {
00399 cout << "Failed to access file with ../ in name, good" << endl ;
00400 }
00401 catch( ... )
00402 {
00403 cerr << "Failed to access compressed file" << endl ;
00404 cerr << "Unknown error" << endl ;
00405 return 1 ;
00406 }
00407 }
00408
00409 cout << endl << "*****************************************" << endl;
00410 cout << "Returning from containerT::run" << endl;
00411
00412 return retVal;
00413 }
00414
00415 int
00416 main(int argC, char **argV) {
00417 Application *app = new containerT();
00418 string env_var = (string)"BES_CONF=" + TEST_SRC_DIR + "/empty.ini" ;
00419 putenv( (char *)env_var.c_str() ) ;
00420 return app->main(argC, argV);
00421 }
00422