Reduce catch all handlers, and make them optional.
[shibboleth/cpp-sp.git] / test / posttest.cpp
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "../shib/shib.h"
18 #include <sstream>
19
20 #include <openssl/bio.h>
21 #include <openssl/err.h>
22 #include <openssl/rsa.h>
23 #include <openssl/evp.h>
24 #include <openssl/pem.h>
25
26 using namespace std;
27 using namespace saml;
28 using namespace shibboleth;
29
30 int main(int argc,char* argv[])
31 {
32     SAMLConfig& conf1=SAMLConfig::getConfig();
33     ShibConfig& conf2=ShibConfig::getConfig();
34     char* path="";
35     char* key="";
36
37     for (int i=1; i<argc; i++)
38     {
39         if (!strcmp(argv[i],"-d") && i+1<argc)
40             path=argv[++i];
41         else if (!strcmp(argv[i],"-k") && i+1<argc)
42             key=argv[++i];
43     }
44
45     conf1.schema_dir=path;
46     if (!conf1.init())
47         cerr << "unable to initialize SAML runtime" << endl;
48
49     if (!conf2.init())
50         cerr << "unable to initialize Shibboleth runtime" << endl;
51
52     try
53     {
54
55         DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(NULL);
56         DOMDocument* dummydoc=impl->createDocument();
57         DOMElement* dummy = dummydoc->createElementNS(NULL,L(Request));
58         static const XMLCh url[] = { chLatin_u, chLatin_r, chLatin_l, chNull };
59         auto_ptr_XMLCh src("/opt/shibboleth/etc/shibboleth/sites.xml");
60         dummy->setAttributeNS(NULL,url,src.get());
61
62         IMetadata* metadatas[1];
63         metadatas[0]=dynamic_cast<IMetadata*>(conf1.getPlugMgr().newPlugin("edu.internet2.middleware.shibboleth.provider.XMLMetadata",dummy));
64         dummydoc->release();
65         ArrayIterator<IMetadata*> sites(metadatas,1);
66         
67         Metadata m(sites);
68
69         auto_ptr<XMLCh> recip(XMLString::transcode("https://shib2.internet2.edu/shib/SHIRE"));
70         ShibBrowserProfile p (sites,EMPTY(ITrust*));
71
72         char ch;
73         string buf;
74         cin >> ch;
75         while (!cin.fail())
76         {
77             buf+=ch;
78             cin >> ch;
79         }
80
81         SAMLBrowserProfile::BrowserProfileResponse bpr=p.receive(buf.c_str(),recip.get(),SAMLBrowserProfile::Post);
82         cout << "Consumed Response: " << endl << *bpr.response << endl;
83         bpr.clear();
84     }
85     catch(SAMLException& e)
86     {
87         cerr << "caught a SAML exception: " << e << endl;
88     }
89     catch(XMLException& e)
90     {
91         cerr << "caught an XML exception: "; xmlout(cerr,e.getMessage()); cerr << endl;
92     }
93     catch(...)
94     {
95         cerr << "caught an unknown exception" << endl;
96     }
97
98     conf2.term();
99     conf1.term();
100     return 0;
101 }