Back to index

nordugrid-arc-nox  1.1.0~rc6
test.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <iostream>
00006 #include <signal.h>
00007 #include <string>
00008 
00009 #include <arc/security/ArcPDP/Evaluator.h>
00010 #include <arc/security/ArcPDP/EvaluatorLoader.h>
00011 #include <arc/security/ArcPDP/Request.h>
00012 #include <arc/security/ArcPDP/Response.h>
00013 #include <arc/XMLNode.h>
00014 #include <arc/ArcConfig.h>
00015 //#include <arc/loader/ClassLoader.h>
00016 #include <arc/Logger.h>
00017 #include <arc/security/ArcPDP/attr/AttributeValue.h>
00018 #ifdef WIN32
00019 #include <arc/win32.h>
00020 #endif
00021 
00022 int main(void){
00023   signal(SIGTTOU,SIG_IGN);
00024   signal(SIGTTIN,SIG_IGN);
00025   signal(SIGPIPE,SIG_IGN);
00026   Arc::Logger logger(Arc::Logger::rootLogger, "PDPTest");
00027   //Arc::LogStream logcerr(std::cerr);
00028   //Arc::Logger::rootLogger.addDestination(logcerr);
00029 
00030   logger.msg(Arc::INFO, "Start test");
00031   // NOTE: ClassLoader can't distiguish between object of different kinds of classes
00032   // As result if constructor of some kind of object is not clever enough to distinguish
00033   // that it is supplied with configuration of different type method Instance() may return
00034   // unexpected object type. Because of that we need separate loaders for different types
00035   // of classes.
00036 
00037   ArcSec::EvaluatorLoader eval_loader;
00038   ArcSec::Evaluator* eval = NULL;
00039   ArcSec::Policy* policy = NULL;
00040 
00041   // Load policy dinamically detecting it's type
00042   // Load evaluator by policy type
00043   {
00044     ArcSec::SourceFile source1("policy1.xml");
00045     if(!source1) {
00046       std::cerr<<"policy1.xml failed to load"<<std::endl;
00047     } else {
00048       policy=eval_loader.getPolicy(ArcSec::SourceFile("policy1.xml"));
00049       if(policy == NULL) {
00050         std::cerr<<"policy1.xml failed to parse"<<std::endl;
00051       } else {
00052         std::cout<<"Evaluator for policy1.xml is "<<policy->getEvalName()<<std::endl;
00053         eval=eval_loader.getEvaluator(policy);
00054         if(eval == NULL) {
00055           std::cerr<<"policy1.xml failed to instantiate evaluator"<<std::endl;
00056         } else {
00057           std::cerr<<"policy1.xml test passed"<<std::endl;
00058           delete eval;
00059         };
00060         delete policy;
00061       };
00062     };
00063     ArcSec::SourceFile source2("policy2.xml");
00064     if(!source2) {
00065       std::cerr<<"policy2.xml failed to load"<<std::endl;
00066     } else {
00067       policy=eval_loader.getPolicy(ArcSec::SourceFile("policy2.xml"));
00068       if(policy == NULL) {
00069         std::cerr<<"policy2.xml failed to parse"<<std::endl;
00070       } else {
00071         std::cout<<"Evaluator for policy2.xml is "<<policy->getEvalName()<<std::endl;
00072         eval=eval_loader.getEvaluator(policy);
00073         if(eval == NULL) {
00074           std::cerr<<"policy2.xml failed to instantiate evaluator"<<std::endl;
00075         } else {
00076           std::cerr<<"policy2.xml test passed"<<std::endl;
00077           delete eval;
00078         };
00079         delete policy;
00080       };
00081     };
00082   };
00083 
00084   //Load the Evaluator
00085 
00086 #if 0
00087   Arc::Config modulecfg("EvaluatorCfg.xml");
00088   Arc::ClassLoader* classloader = NULL;
00089   classloader = Arc::ClassLoader::getClassLoader(&modulecfg);
00090   std::string evaluator = "arc.evaluator";
00091   eval = (ArcSec::Evaluator*)(classloader->Instance(evaluator, (void**)(void*)&modulecfg));
00092 #endif
00093 
00094   std::string evaluator = "arc.evaluator";
00095   eval = eval_loader.getEvaluator(evaluator);  
00096   if(eval == NULL) {
00097     logger.msg(Arc::ERROR, "Can not dynamically produce Evaluator");
00098     return 0;  
00099   }
00100 
00101   ArcSec::Response *resp = NULL;
00102 
00103   //Input request from a file: Request.xml
00104   logger.msg(Arc::INFO, "Input request from a file: Request.xml");  
00105   //Evaluate the request
00106   std::ifstream f("Request.xml");
00107   ArcSec::Source source(f);
00108   resp = eval->evaluate(source);
00109   //Get the response
00110   logger.msg(Arc::INFO, "There is %d subjects, which satisfy at least one policy", (resp->getResponseItems()).size());
00111   ArcSec::ResponseList rlist = resp->getResponseItems();
00112   int size = rlist.size();
00113   for(int i = 0; i< size; i++){
00114     ArcSec::ResponseItem* respitem = rlist[i];
00115     ArcSec::RequestTuple* tp = respitem->reqtp;
00116     ArcSec::Subject::iterator it;
00117     ArcSec::Subject subject = tp->sub;
00118     for (it = subject.begin(); it!= subject.end(); it++){
00119       ArcSec::AttributeValue *attrval;
00120       ArcSec::RequestAttribute *attr;
00121       attr = dynamic_cast<ArcSec::RequestAttribute*>(*it);
00122       if(attr){
00123         attrval = (*it)->getAttributeValue();
00124         if(attrval) logger.msg(Arc::INFO,"Attribute Value (1): %s", attrval->encode());
00125       }
00126     }
00127   }
00128   
00129   if(resp){
00130     delete resp;
00131     resp = NULL;
00132   }
00133 
00134   //Input/Set request from code
00135   logger.msg(Arc::INFO, "Input request from code");
00136 
00137 //Request example
00138 /*
00139      <RequestItem>
00140         <Subject AttributeId="urn:arc:subject:dn" Type="string">/O=NorduGrid/OU=UIO/CN=test</Subject>
00141         <Resource AttributeId="urn:arc:resource:file" Type="string">file://home/test</Resource>
00142         <Action AttributeId="urn:arc:action:file-action" Type="string">read</Action>
00143         <Action AttributeId="urn:arc:action:file-action" Type="string">copy</Action>
00144         <Context AttributeId="urn:arc:context:date" Type="period">2007-09-10T20:30:20/P1Y1M</Context>
00145     </RequestItem>
00146  
00147 */
00148 
00149 //Data Structure to compose a request
00150 /*
00151   typedef struct{
00152     std::string value;
00153     std::string type;
00154   } Attr;
00155   typedef std::list<Attr> Attrs;
00156 
00157 */
00158   ArcSec::Attr subject_attr1, subject_attr2, resource_attr1, action_attr1, action_attr2, context_attr1;
00159   ArcSec::Attrs sub, res, act, ctx;
00160   subject_attr1.type = "string";
00161   subject_attr1.value = "/O=NorduGrid/OU=UIO/CN=test";
00162   sub.addItem(subject_attr1);
00163 
00164   resource_attr1.type = "string";
00165   resource_attr1.value = "file://home/test";
00166   res.addItem(resource_attr1);
00167 
00168   action_attr1.type = "string";
00169   action_attr1.value = "read";
00170   act.addItem(action_attr1);
00171 
00172   action_attr2.type = "string";
00173   action_attr2.value = "copy";
00174   act.addItem(action_attr2);
00175 
00176   context_attr1.type = "period";
00177   context_attr1.value = "2007-09-10T20:30:20/P1Y1M";
00178   ctx.addItem(context_attr1);
00179 
00180   ArcSec::Request* request = NULL;
00181   std::string requestor = "arc.request";
00182   Arc::ClassLoader* classloader = NULL;
00183   classloader = Arc::ClassLoader::getClassLoader();
00184   request = (ArcSec::Request*)(classloader->Instance(requestor));
00185   if(request == NULL)
00186     logger.msg(Arc::ERROR, "Can not dynamically produce Request");
00187 
00188   //Add the request information into Request object
00189   request->addRequestItem(sub, res, act, ctx);
00190 
00191   //Evaluate the request
00192   //resp = eval->evaluate(request);
00193 
00194   //Evalute the request with policy argument
00195   std::ifstream f1("Policy_Example.xml");
00196   ArcSec::Source source1(f1);
00197   resp = eval->evaluate(request, source1);
00198 
00199   //Get the response
00200   logger.msg(Arc::INFO, "There is %d subjects, which satisfy at least one policy", (resp->getResponseItems()).size());
00201   rlist = resp->getResponseItems();
00202   size = rlist.size();
00203   for(int i = 0; i < size; i++){
00204     ArcSec::ResponseItem* respitem = rlist[i];
00205     ArcSec::RequestTuple* tp = respitem->reqtp;
00206     ArcSec::Subject::iterator it;
00207     ArcSec::Subject subject = tp->sub;
00208     for (it = subject.begin(); it!= subject.end(); it++){
00209       ArcSec::AttributeValue *attrval;
00210       ArcSec::RequestAttribute *attr;
00211       attr = dynamic_cast<ArcSec::RequestAttribute*>(*it);
00212       if(attr){
00213         attrval = (*it)->getAttributeValue();
00214         if(attrval) logger.msg(Arc::INFO,"Attribute Value (2): %s", attrval->encode());
00215       }
00216     }
00217   }
00218 
00219   if(resp){
00220     delete resp;
00221     resp = NULL;
00222   }
00223  
00224   if(eval) delete eval;
00225   if(request) delete request;
00226 
00227   return 0;
00228 }