SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / util / resolvertest.cpp
index d818e80..ae04858 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2007 Internet2
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
 #endif
 
 #include <shibsp/Application.h>
-#include <shibsp/exceptions.h>
 #include <shibsp/SPConfig.h>
 #include <shibsp/ServiceProvider.h>
 #include <shibsp/attribute/Attribute.h>
 #include <shibsp/attribute/resolver/ResolutionContext.h>
 #include <shibsp/handler/AssertionConsumerService.h>
+#include <shibsp/metadata/MetadataProviderCriteria.h>
 #include <shibsp/util/SPConstants.h>
 
+#include <saml/exceptions.h>
 #include <saml/saml1/core/Assertions.h>
 #include <saml/saml2/core/Assertions.h>
 #include <saml/saml2/metadata/Metadata.h>
-#include <xercesc/util/XMLUniDefs.hpp>
+#include <saml/saml2/metadata/MetadataProvider.h>
 #include <xmltooling/XMLToolingConfig.h>
+#include <xmltooling/util/ParserPool.h>
 #include <xmltooling/util/XMLHelper.h>
+#include <xercesc/util/XMLUniDefs.hpp>
+#include <boost/iterator/indirect_iterator.hpp>
 
 using namespace shibsp;
 using namespace opensaml::saml2md;
@@ -53,6 +61,7 @@ using namespace opensaml;
 using namespace xmltooling::logging;
 using namespace xmltooling;
 using namespace xercesc;
+using namespace boost;
 using namespace std;
 
 #if defined (_MSC_VER)
@@ -64,7 +73,7 @@ class ResolverTest : public shibsp::AssertionConsumerService
 {
 public:
     ResolverTest(const DOMElement* e, const char* appId)
-        : shibsp::AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT".Utilities.ResolverTest")) {
+        : shibsp::AssertionConsumerService(e, appId, Category::getInstance(SHIBSP_LOGCAT ".Utilities.ResolverTest")) {
     }
     virtual ~ResolverTest() {}
     
@@ -84,9 +93,10 @@ public:
     }
 
 private:
-    string implementProtocol(
+    void implementProtocol(
         const Application& application,
         const HTTPRequest& httpRequest,
+        HTTPResponse& httpResponse,
         SecurityPolicy& policy,
         const PropertySet* settings,
         const XMLObject& xmlObject
@@ -107,14 +117,12 @@ void usage()
 
 int main(int argc,char* argv[])
 {
-    char* a_param=NULL;
-    char* n_param=NULL;
-    char* f_param=NULL;
-    char* i_param=NULL;
-    char* prot = NULL;
-    const XMLCh* protocol = NULL;
-    char* path=NULL;
-    char* config=NULL;
+    char* a_param=nullptr;
+    char* n_param=nullptr;
+    char* f_param=nullptr;
+    char* i_param=nullptr;
+    char* prot = nullptr;
+    const XMLCh* protocol = nullptr;
 
     for (int i=1; i<argc; i++) {
         if (!strcmp(argv[i],"-n") && i+1<argc)
@@ -137,57 +145,35 @@ int main(int argc,char* argv[])
 
     if (n_param && !i_param) {
         usage();
-        exit(-10);
+        return -10;
     }
 
-    path=getenv("SHIBSP_SCHEMAS");
-    if (!path)
-        path=SHIBSP_SCHEMAS;
-    config=getenv("SHIBSP_CONFIG");
-    if (!config)
-        config=SHIBSP_CONFIG;
     if (!a_param)
         a_param="default";
 
-    XMLToolingConfig::getConfig().log_config(getenv("SHIBSP_LOGGING") ? getenv("SHIBSP_LOGGING") : SHIBSP_LOGGING);
-
-    SPConfig& conf=SPConfig::getConfig();
-    conf.setFeatures(
-        SPConfig::Metadata |
-        SPConfig::Trust |
-        SPConfig::AttributeResolution |
-        SPConfig::Credentials |
-        SPConfig::OutOfProcess
-        );
-    if (!conf.init(path))
-        return -1;
-
     if (n_param) {
         if (!protocol) {
             if (prot)
                 protocol = XMLString::transcode(prot);
         }
         if (!protocol) {
-            conf.term();
             usage();
-            exit(-10);
+            return -10;
         }
     }
-   
-    try {
-        static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
-        static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
-        xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
-        XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
-        xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
-        auto_ptr_XMLCh src(config);
-        dummy->setAttributeNS(NULL,path,src.get());
-        dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
 
-        conf.setServiceProvider(conf.ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
-        conf.getServiceProvider()->init();
-    }
-    catch (exception&) {
+    SPConfig& conf=SPConfig::getConfig();
+    conf.setFeatures(
+        SPConfig::Metadata |
+        SPConfig::Trust |
+        SPConfig::AttributeResolution |
+        SPConfig::Credentials |
+        SPConfig::OutOfProcess |
+        SPConfig::Caching
+        );
+    if (!conf.init())
+        return -1;
+    if (!conf.instantiate()) {
         conf.term();
         return -2;
     }
@@ -195,7 +181,7 @@ int main(int argc,char* argv[])
     ServiceProvider* sp=conf.getServiceProvider();
     sp->lock();
 
-    Category& log = Category::getInstance(SHIBSP_LOGCAT".Utility.ResolverTest");
+    Category& log = Category::getInstance(SHIBSP_LOGCAT ".Utility.ResolverTest");
 
     const Application* app = sp->getApplication(a_param);
     if (!app) {
@@ -206,59 +192,53 @@ int main(int argc,char* argv[])
     }
 
     try {
-        ResolutionContext* ctx;
+        scoped_ptr<ResolutionContext> ctx;
 
         if (n_param) {
             auto_ptr_XMLCh issuer(i_param);
             auto_ptr_XMLCh name(n_param);
             auto_ptr_XMLCh format(f_param);
 
-            MetadataProvider* m=app->getMetadataProvider();
-            xmltooling::Locker mlocker(m);
-            MetadataProvider::Criteria mc(i_param, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
-            pair<const EntityDescriptor*,const RoleDescriptor*> site=m->getEntityDescriptor(mc);
+            MetadataProvider* m = app->getMetadataProvider();
+            Locker mlocker(m);
+            MetadataProviderCriteria mc(*app, i_param, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
+            pair<const EntityDescriptor*,const RoleDescriptor*> site = m->getEntityDescriptor(mc);
             if (!site.first)
                 throw MetadataException("Unable to locate metadata for IdP ($1).", params(1,i_param));
 
             // Build NameID(s).
-            auto_ptr<saml2::NameID> v2name(saml2::NameIDBuilder::buildNameID());
+            scoped_ptr<saml1::NameIdentifier> v1name;
+            scoped_ptr<saml2::NameID> v2name(saml2::NameIDBuilder::buildNameID());
             v2name->setName(name.get());
             v2name->setFormat(format.get());
-            saml1::NameIdentifier* v1name = NULL;
             if (!XMLString::equals(protocol, samlconstants::SAML20P_NS)) {
-                v1name = saml1::NameIdentifierBuilder::buildNameIdentifier();
+                v1name.reset(saml1::NameIdentifierBuilder::buildNameIdentifier());
                 v1name->setName(name.get());
                 v1name->setFormat(format.get());
                 v1name->setNameQualifier(issuer.get());
             }
 
-            ResolverTest rt(NULL, a_param);
-            try {
-                ctx = rt.resolveAttributes(*app, site.second, protocol, v1name, v2name.get(), NULL, NULL, NULL);
-            }
-            catch (...) {
-                delete v1name;
-                throw;
-            }
+            ResolverTest rt(nullptr, a_param);
+            ctx.reset(rt.resolveAttributes(*app, site.second, protocol, v1name.get(), v2name.get(), nullptr, nullptr, nullptr));
         }
         else {
             // Try and load assertion from stdin.
             DOMDocument* doc = XMLToolingConfig::getConfig().getParser().parse(cin);
             XercesJanitor<DOMDocument> docjan(doc);
-            auto_ptr<XMLObject> token(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
+            scoped_ptr<XMLObject> token(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
             docjan.release();
 
             // Get the issuer and protocol and NameIDs.
-            const XMLCh* issuer = NULL;
-            const saml1::NameIdentifier* v1name = NULL;
-            saml2::NameID* v2name = NULL;
+            const XMLCh* issuer = nullptr;
+            const saml1::NameIdentifier* v1name = nullptr;
+            saml2::NameID* v2name = nullptr;
             saml2::Assertion* a2 = dynamic_cast<saml2::Assertion*>(token.get());
             saml1::Assertion* a1 = dynamic_cast<saml1::Assertion*>(token.get());
             if (a2) {
                 const saml2::Issuer* iss = a2->getIssuer();
-                issuer = iss ? iss->getName() : NULL;
+                issuer = iss ? iss->getName() : nullptr;
                 protocol = samlconstants::SAML20P_NS;
-                v2name = a2->getSubject() ? a2->getSubject()->getNameID() : NULL;
+                v2name = a2->getSubject() ? a2->getSubject()->getNameID() : nullptr;
             }
             else if (a1) {
                 issuer = a1->getIssuer();
@@ -267,55 +247,56 @@ int main(int argc,char* argv[])
                 else
                     protocol = samlconstants::SAML11_PROTOCOL_ENUM;
                 v1name = a1->getAuthenticationStatements().size() ?
-                    a1->getAuthenticationStatements().front()->getSubject()->getNameIdentifier() : NULL;
-                // Normalize the SAML 1.x NameIdentifier...
-                v2name = saml2::NameIDBuilder::buildNameID();
-                v2name->setName(v1name->getName());
-                v2name->setFormat(v1name->getFormat());
-                v2name->setNameQualifier(v1name->getNameQualifier());
+                    a1->getAuthenticationStatements().front()->getSubject()->getNameIdentifier() : nullptr;
+                if (!v1name)
+                    v1name = a1->getAttributeStatements().size() ?
+                    a1->getAttributeStatements().front()->getSubject()->getNameIdentifier() : nullptr;
+                if (v1name) {
+                    // Normalize the SAML 1.x NameIdentifier...
+                    v2name = saml2::NameIDBuilder::buildNameID();
+                    v2name->setName(v1name->getName());
+                    v2name->setFormat(v1name->getFormat());
+                    v2name->setNameQualifier(v1name->getNameQualifier());
+                }
             }
             else {
                 throw FatalProfileException("Unknown assertion type.");
             }
 
-            if (!issuer) {
-                if (v1name)
-                    delete v2name;
+            scoped_ptr<saml2::NameID> nameidwrapper(v1name ? v2name : nullptr);
+
+            if (!issuer)
                 throw FatalProfileException("Unable to determine issuer.");
-            }
 
-            MetadataProvider* m=app->getMetadataProvider();
-            xmltooling::Locker mlocker(m);
-            MetadataProvider::Criteria mc(issuer, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
-            pair<const EntityDescriptor*,const RoleDescriptor*> site=m->getEntityDescriptor(mc);
+            MetadataProvider* m = app->getMetadataProvider();
+            Locker mlocker(m);
+            MetadataProviderCriteria mc(*app, issuer, &IDPSSODescriptor::ELEMENT_QNAME, protocol);
+            pair<const EntityDescriptor*,const RoleDescriptor*> site = m->getEntityDescriptor(mc);
             if (!site.first) {
                 auto_ptr_char temp(issuer);
                 throw MetadataException("Unable to locate metadata for IdP ($1).", params(1,temp.get()));
             }
             
             vector<const Assertion*> tokens(1, dynamic_cast<Assertion*>(token.get()));
-            ResolverTest rt(NULL, a_param);
-            try {
-                ctx = rt.resolveAttributes(*app, site.second, protocol, v1name, v2name, NULL, NULL, &tokens);
-            }
-            catch (...) {
-                if (v1name)
-                    delete v2name;
-                throw;
-            }
+            ResolverTest rt(nullptr, a_param);
+            ctx.reset(rt.resolveAttributes(*app, site.second, protocol, v1name, v2name, nullptr, nullptr, &tokens));
         }
 
-        auto_ptr<ResolutionContext> wrapper(ctx);
-        for (vector<Attribute*>::const_iterator a = ctx->getResolvedAttributes().begin(); a != ctx->getResolvedAttributes().end(); ++a) {
-            cout << endl;
-            for (vector<string>::const_iterator s = (*a)->getAliases().begin(); s != (*a)->getAliases().end(); ++s)
-                cout << "ID: " << *s << endl;
-            for (vector<string>::const_iterator s = (*a)->getSerializedValues().begin(); s != (*a)->getSerializedValues().end(); ++s)
-                cout << "Value: " << *s << endl;
+        for (indirect_iterator<vector<Attribute*>::const_iterator> a = make_indirect_iterator(ctx->getResolvedAttributes().begin());
+                a != make_indirect_iterator(ctx->getResolvedAttributes().end()); ++a) {
+            for (vector<string>::const_iterator s = a->getAliases().begin(); s != a->getAliases().end(); ++s) {
+                cout << *s << ": ";
+                for (vector<string>::const_iterator v = a->getSerializedValues().begin(); v != a->getSerializedValues().end(); ++v) {
+                    if (v != a->getSerializedValues().begin())
+                        cout << ';';
+                    cout << *v;
+                }
+                cout << endl;
+            }
         }
         cout << endl;
     }
-    catch(exception& ex) {
+    catch (std::exception& ex) {
         log.error(ex.what());
         sp->unlock();
         conf.term();