Back to index

nordugrid-arc-nox  1.1.0~rc6
InfoFilterTest.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 <string>
00007 
00008 #include <cppunit/extensions/HelperMacros.h>
00009 
00010 #include <glibmm/fileutils.h>
00011 #include <glibmm/miscutils.h>
00012 
00013 #include <arc/Utils.h>
00014 
00015 #include <arc/infosys/InfoFilter.h>
00016 
00017 using namespace Arc;
00018 
00019 class InfoFilterTest
00020   : public CppUnit::TestFixture {
00021 
00022   CPPUNIT_TEST_SUITE(InfoFilterTest);
00023   CPPUNIT_TEST(TestInfoFilter);
00024   CPPUNIT_TEST_SUITE_END();
00025 
00026 public:
00027   void setUp();
00028   void tearDown();
00029   void TestInfoFilter();
00030 };
00031 
00032 static void add_libs(std::string& paths,const std::string& curpath) {
00033   std::string fname = Glib::build_filename(curpath,".libs");
00034   if(file_test(fname, Glib::FILE_TEST_IS_DIR)) {
00035     paths+=":"+fname;
00036   };
00037   Glib::Dir dir(curpath);
00038   for(;;) {
00039     fname = dir.read_name();
00040     if(fname.empty()) break;
00041     fname=Glib::build_filename(curpath,fname);
00042     if(fname == ".libs") {
00043     } else if(file_test(fname, Glib::FILE_TEST_IS_DIR)) {
00044       add_libs(paths,fname);
00045     };
00046   };
00047 }
00048 
00049 void InfoFilterTest::setUp() {
00050   std::string paths;
00051   std::string toppath = Glib::get_current_dir();
00052   // Find top source directory by looking for configure file
00053   for(int n=0;n<15;++n) {
00054     std::string fname = Glib::build_filename(toppath, "configure");
00055     if(file_test(fname, Glib::FILE_TEST_IS_REGULAR)) {
00056       // Go to all .libs directories
00057       add_libs(paths,toppath);
00058       break;
00059     };
00060     toppath = Glib::path_get_dirname(toppath);
00061   };
00062   Arc::SetEnv("ARC_PLUGIN_PATH",paths);
00063 }
00064 
00065 
00066 void InfoFilterTest::tearDown() {
00067 }
00068 
00069 
00070 class TestSecAttr: public Arc::SecAttr {
00071  public:
00072   TestSecAttr(const char* id):id_(id) { };
00073   virtual ~TestSecAttr(void) { };
00074   virtual operator bool(void) const { return true; };
00075   virtual bool Export(SecAttrFormat format,XMLNode &val) const;
00076  protected:
00077   virtual bool equal(const SecAttr &b) const { return false; };
00078  private:
00079   std::string id_;
00080 };
00081 
00082 
00083 static void add_subject_attribute(XMLNode item,const std::string& subject,const
00084 char* id) {
00085    XMLNode attr = item.NewChild("ra:SubjectAttribute");
00086    attr=subject; attr.NewAttribute("Type")="string";
00087    attr.NewAttribute("AttributeId")=id;
00088 }
00089 
00090 bool TestSecAttr::Export(SecAttrFormat format,XMLNode &val) const {
00091   if(format != ARCAuth) return false;
00092   NS ns;
00093   ns["ra"]="http://www.nordugrid.org/schemas/request-arc";
00094   val.Namespaces(ns); val.Name("ra:Request");
00095   XMLNode item = val.NewChild("ra:RequestItem");
00096   XMLNode subj = item.NewChild("ra:Subject");
00097   XMLNode attr = subj.NewChild("ra:SubjectAttribute");
00098   attr=id_;
00099   attr.NewAttribute("Type")="string";
00100   attr.NewAttribute("AttributeId")="urn:testID";
00101   return true;
00102 }
00103 
00104 
00105 void InfoFilterTest::TestInfoFilter() {
00106 
00107   // Service description document
00108   Arc::XMLNode infodoc("\
00109 <?xml version=\"1.0\"?>\n\
00110 <InfoDoc xmlns=\"urn:info\">\n\
00111   <Resource>\n\
00112     <Memory>A lot</Memory>\n\
00113     <Performance>Turltle-like</Performance>\n\
00114   </Resource>\n\
00115   <Owner>\n\
00116       <Name>Unknown</Name>\n\
00117   </Owner>\n\
00118   <Resource>\n\
00119       <Memory>640kb enough for everyone</Memory>\n\
00120       <Performance>Quantum computer</Performance>\n\
00121   </Resource>\n\
00122 </InfoDoc>\n");
00123 
00124   // Policies
00125   Arc::XMLNode policy1("\
00126 <?xml version=\"1.0\"?>\n\
00127 <InfoFilterDefinition xmlns=\"http://www.nordugrid.org/schemas/InfoFilter/2008\" id=\"policy1\">\n\
00128   <Policy xmlns=\"http://www.nordugrid.org/schemas/policy-arc\" PolicyId=\"policy1\" CombiningAlg=\"Deny-Overrides\">\n\
00129     <Rule RuleId=\"rule1\" Effect=\"Permit\">\n\
00130       <Subjects>\n\
00131         <Subject Type=\"string\" AttributeId=\"urn:testID\">USER1</Subject>\n\
00132       </Subjects>\n\
00133     </Rule>\n\
00134   </Policy>\n\
00135 </InfoFilterDefinition>");
00136 
00137   Arc::XMLNode policy2("\
00138 <?xml version=\"1.0\"?>\n\
00139 <InfoFilterDefinition xmlns=\"http://www.nordugrid.org/schemas/InfoFilter/2008\" id=\"policy2\">\n\
00140   <Policy xmlns=\"http://www.nordugrid.org/schemas/policy-arc\" PolicyId=\"policy1\" CombiningAlg=\"Deny-Overrides\">\n\
00141     <Rule RuleId=\"rule1\" Effect=\"Permit\">\n\
00142       <Subjects>\n\
00143         <Subject Type=\"string\" AttributeId=\"urn:testID\">USER2</Subject>\n\
00144       </Subjects>\n\
00145     </Rule>\n\
00146   </Policy>\n\
00147 </InfoFilterDefinition>");
00148 
00149   // Service description with policies
00150   XMLNode infodoc_sec;
00151   infodoc.New(infodoc_sec);
00152   infodoc_sec["Resource"][0].NewChild(policy1);
00153   infodoc_sec["Resource"][0].NewAttribute("InfoFilterTag")="policy1";
00154   infodoc_sec["Resource"][1].NewChild(policy2);
00155   infodoc_sec["Resource"][1].NewAttribute("InfoFilterTag")="policy2";
00156 
00157   // Requestor's identifier
00158   MessageAuth user_id;
00159   TestSecAttr* user_attr = new TestSecAttr("USER1");
00160   user_id.set("TEST",user_attr);
00161 
00162   // Filter
00163   InfoFilter filter(user_id);
00164 
00165   // External policies
00166   //Arc::NS ns;
00167   //XMLNode policy1();
00168   std::list< std::pair<std::string,XMLNode> > policies;
00169 
00170   // Service description for filtering
00171   XMLNode infodoc_filtered;
00172 
00173   // Applying filter
00174   infodoc_sec.New(infodoc_filtered);
00175   CPPUNIT_ASSERT(filter.Filter(infodoc_filtered));
00176 
00177 }
00178 
00179 CPPUNIT_TEST_SUITE_REGISTRATION(InfoFilterTest);
00180