05db44d27f0897d01db40960d2a7758fc082e7eb
[shibboleth/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 #ifdef WIN32
18 # define _CRT_NONSTDC_NO_DEPRECATE 1
19 # define _CRT_SECURE_NO_DEPRECATE 1
20 #endif
21
22 #include <fstream>
23 #include <shib-target/shib-target.h>
24
25 #include <shibsp/exceptions.h>
26 #include <shibsp/SPConfig.h>
27
28 using namespace shibsp;
29 using namespace shibtarget;
30 using namespace shibboleth;
31 using namespace saml;
32 using namespace std;
33
34 int main(int argc,char* argv[])
35 {
36     char* a_param=NULL;
37     char* r_param=NULL;
38     char* f_param=NULL;
39     char* path=NULL;
40     char* config=NULL;
41
42     for (int i=1; i<argc; i++) {
43         if (!strcmp(argv[i],"-c") && i+1<argc)
44             config=argv[++i];
45         else if (!strcmp(argv[i],"-d") && i+1<argc)
46             path=argv[++i];
47         else if (!strcmp(argv[i],"-r") && i+1<argc)
48             r_param=argv[++i];
49         else if (!strcmp(argv[i],"-f") && i+1<argc)
50             f_param=argv[++i];
51         else if (!strcmp(argv[i],"-a") && i+1<argc)
52             a_param=argv[++i];
53     }
54
55     if (!r_param || !f_param) {
56         cerr << "usage: posttest -f <file> -r <recipient URL> [-a <application_id> -d <schema path> -c <config>]" << endl;
57         exit(0);
58     }
59     
60     if (!path)
61         path=getenv("SHIBSCHEMAS");
62     if (!path)
63         path=SHIB_SCHEMAS;
64     if (!config)
65         config=getenv("SHIBCONFIG");
66     if (!config)
67         config=SHIB_CONFIG;
68     if (!a_param)
69         a_param="default";
70
71     ShibTargetConfig& conf=ShibTargetConfig::getConfig();
72     SPConfig::getConfig().setFeatures(
73         SPConfig::Listener |
74         SPConfig::Metadata |
75         SPConfig::Trust |
76         SPConfig::OutOfProcess
77         );
78     if (!conf.init(path) || !conf.load(config))
79         return -10;
80
81     try {
82         string buf;
83         ifstream is(f_param);
84         char ch;
85         is >> ch;
86         while (!is.fail()) {
87             buf+=ch;
88             is >> ch;
89         }
90
91         auto_ptr_XMLCh recip(r_param);
92
93         ServiceProvider* sp=SPConfig::getConfig().getServiceProvider();
94         xmltooling::Locker locker(sp);
95
96         const IApplication* app=dynamic_cast<const IApplication*>(sp->getApplication(a_param));
97         if (!app) {
98             throw ConfigurationException("Unable to locate application for new session, deleted?");
99         }
100
101         SAMLBrowserProfile::BrowserProfileResponse bpr=
102             app->getBrowserProfile()->receive(buf.c_str(), recip.get(), NULL, 1);
103
104         cout << "Success!" << endl;
105         bpr.clear();
106     }
107     catch(exception& e) {
108         cerr << "caught an exception: " << e.what() << endl;
109     }
110
111     conf.shutdown();
112     return 0;
113 }