// WsClientGenericRepository_t.cxx #include "dial_ws_repository/WsClientGenericRepository.h" #include #include #include #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/getcwd.h" #include "dataset_util/mkdir.h" #include "dataset_util/ThreadCredential.h" #include "dataset_catalog/ConnectionResolver.h" #include "dataset_credential/GssCredentialManager.h" #include "dataset_credential/CredentialSelectionCatalog.h" #include "dial_ws/dial_ws_flags.h" using std::string; using std::cout; using std::endl; using std::ostream; using std::ifstream; using dset::ConnectionResolver; using dset::GenericRepository; using dset::GssCredentialManager; using dset::CredentialSelectionCatalog; using dial::WsClientGenericRepository; typedef GenericRepository::size_type size_type; typedef GenericRepository::IdList IdList; #ifdef WITH_GSI #include "gsi.h" #endif void msg(string txt) { cout << "*** "; cout << txt; cout << " ***" << endl; } void sigpipe_handler(int) { cout << "Another broken pipe" << endl; } #ifdef WITH_GSI int client_authorization(struct soap* psoap) { struct gsi_plugin_data* pgsiplug = (struct gsi_plugin_data *) soap_lookup_plugin(psoap, GSI_PLUGIN_ID); //string client_identity = pgsiplug->client_identity; string service_identity = pgsiplug->server_identity; //cout << "GSI Client ID: " << client_identity << endl; cout << "GSI Service ID: " << service_identity << endl; return 0; } #endif ostream& operator<<(ostream& str, const IdList& ids) { str << "ID list has " << ids.size() << " entries:"; for ( IdList::const_iterator iid=ids.begin(); iid!=ids.end(); ++iid ) { str << " \n" << *iid; } return str; } int WsClientGenericRepository_t() { signal(SIGPIPE, sigpipe_handler); msg("Define service connection resolver"); string confdir = getcwd(); unlink("resolver.dat"); Text txt("resolver.dat"); txt.append("DatasetRepository"); txt.append("SQLRESULT:" + confdir + "/dr.dat"); txt.write(); msg("Construct file description of DR"); system("rm -f dr.dat"); Text tdsc("dr.dat"); tdsc.append("SQLRESULT"); tdsc.append("idhi,idlo,sxml"); tdsc.append("0,0,DatasetRepository"); tdsc.append("101,1,"); tdsc.write(); msg("Create default instance of CSC."); assert( CredentialSelectionCatalog::create_default_instance() == 0 ); CredentialSelectionCatalog& sc = CredentialSelectionCatalog::default_instance(); cout << sc << endl; assert( sc.is_valid() ); msg("Set credential and owner"); string owner = "me"; GssCredentialManager::set_default(); GssCredentialManager::set_owner(owner); cout << ThreadCredential() << endl; int pid = -1; if ( FileStatus("server/pid").exists() ) { msg("Clean up from earlier test"); ifstream pidfile("serverpid"); pidfile >> pid; if ( pid > 0 ) { cout << "Killing process " << pid << endl; kill(pid, SIGKILL); } unlink("server/pid"); pid = -1; sleep(1); // Give system time to free up port } msg("Create and change to server directory"); system("rm -rf server"); assert( mkdir("server") == 0 ); assert( chdir("server") == 0 ); #ifdef WITH_GSI // Create auth file. string globus_version = "3"; string gcom = "rm -f authorized_dn"; gcom += "; "; if ( globus_version == "2" ) { gcom += "grid-proxy-info -subject"; gcom += " | sed 's#/CN=proxy##g'"; } else { gcom += "grid-proxy-info -identity"; } gcom += " > authorized_dn"; system(gcom.c_str()); #endif msg("Start server"); assert( ! FileStatus("pid").exists() ); string com = "rm -rf server.log; "; com += "DIAL_CATALOG_CONF=" + confdir + "/resolver.dat; "; com += "export DIAL_CATALOG_CONF; "; string libs; libs += "dataset_id:dataset_sql:dataset_catalog:dataset_base"; libs += ":dial_ws_repository"; com += "dialws " + libs + ":repository 8080 server.log"; com += " gsi"; com += " service"; com += " >service.log 2>&1 &"; cout << com << endl; int sysstat = system(com.c_str()); assert( sysstat == 0 ); for ( int i=0; i<35; ++i ) { cout << "."; if ( FileStatus("pid").exists() ) break; sleep(1); } cout << endl; assert( FileStatus("pid").exists() ); msg("Set server URL"); #ifdef WITH_GSI string server = "http://127.0.0.1:8080"; #else string server = "http://127.0.0.1:8080"; #endif msg("Check server process"); int nloop = 0; while ( nloop < 10 ) { if ( FileStatus("pid").exists() ) break; sleep(1); ++nloop; } assert( nloop < 10 ); { ifstream pidfile("pid"); pidfile >> pid; } cout << "Server process ID is " << pid << endl; assert( pid > 0 ); msg("Change back to client directory"); assert( chdir("..") == 0 ); msg("Create bad client"); string type = "DatasetRepository"; string conn = "SQLRESULT"; WsClientGenericRepository badclient(server, type, "nosuchconn"); assert( ! badclient.is_valid() ); assert( badclient.error() == 101 ); msg("Create client"); WsClientGenericRepository client(server, type, conn); assert( client.is_valid() ); msg("Check validity"); assert( client.is_valid() ); msg("Check retrieval"); assert( client.has("101-1") ); string sobj = client.get("101-1"); cout << sobj << endl; assert( sobj.size() ); assert( client.error() == 0 ); msg("Check insertion"); string desc2 = ""; string desc22 = ""; string desc3 = ""; assert( ! client.has("101-2") ); assert( client.get("101-2") == "" ); assert( client.insert("101-2", desc2) == 0 ); assert( client.has("101-2") ); assert( client.get("101-2") == desc2 ); assert( client.insert("101-2", desc22) != 0 ); msg("Check removal"); assert( client.has("101-2") ); assert( client.remove("101-2") == 0 ); assert( ! client.has("101-2") ); assert( client.remove("101-2") != 0 ); msg("Check ID list"); assert( client.insert("101-2", desc2) == 0 ); assert( client.insert("101-3", desc3) == 0 ); IdList ids = client.get_ids(100); cout << ids << endl; assert( ids.size() == 3 ); assert( ids.front() == "101-1" ); assert( ids.back() == "101-3" ); msg("Check size"); size_type count = client.size(); cout << "Size is " << count << endl; assert( count == 3 ); msg("Check mget"); GenericRepository::ElementList eles; assert( client.mget(ids, eles) == 0 ); assert( eles.size() == 3 ); assert( eles[1] == desc2 ); assert( client.mget(ids, eles) == 0 ); assert( eles.size() == 6 ); assert( eles[4] == desc2 ); msg("Check short ID list"); ids = client.get_ids(2); cout << ids << endl; assert( ids.size() == 2 ); assert( ids.front() == "101-1" ); assert( ids.back() == "101-2" ); msg("Define a client connection resolver"); unlink("local_resolver.dat"); Text txt2("local_resolver.dat"); txt2.append("DatasetRepository"); txt2.append("@WS:http://127.0.0.1:8080"); txt2.write(); ConnectionResolver::set_configuration_file("local_resolver.dat"); msg("Check creator"); GenericRepository* prep = GenericRepository::create("DatasetRepository", ""); assert( prep != 0 ); assert( prep->is_valid() ); assert( client.has("101-2") ); sobj = client.get("101-2"); cout << sobj << endl; assert( sobj.size() ); assert( prep->get_ids().size() == 3 ); msg("Terminate service."); client.terminate_service(); assert( ! client.is_valid() ); while ( nloop < 10 ) { cout << "."; if ( ! FileStatus("pid").exists() ) break; sleep(1); ++nloop; } cout << endl; return 0; } #ifdef CTEST_MAIN int main() { return WsClientGenericRepository_t(); } #endif