6c5d45de57c4ee436c5a006ae203e943d3ed21e3
[shibboleth/cpp-opensaml.git] / samltest / saml1 / profile / SAML1PolicyTest.h
1 /*
2  *  Copyright 2001-2009 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 "internal.h"
18
19 #include <saml/SAMLConfig.h>
20 #include <saml/binding/SecurityPolicy.h>
21 #include <saml/binding/SecurityPolicyRule.h>
22 #include <saml/saml1/core/Assertions.h>
23
24 using namespace opensaml;
25
26 class SAML1PolicyTest : public CxxTest::TestSuite {
27     SecurityPolicy* m_policy;
28     vector<SecurityPolicyRule*> m_rules;
29 public:
30     void setUp() {
31         m_policy = NULL;
32         m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(CONDITIONS_POLICY_RULE, NULL));
33         m_rules.push_back(SAMLConfig::getConfig().SecurityPolicyRuleManager.newPlugin(SAML1BROWSERSSO_POLICY_RULE, NULL));
34         m_policy = new SecurityPolicy();
35         m_policy->getRules().assign(m_rules.begin(), m_rules.end());
36     }
37
38     void tearDown() {
39         for_each(m_rules.begin(), m_rules.end(), xmltooling::cleanup<SecurityPolicyRule>());
40         delete m_policy;
41     }
42
43     void testSAML1Policy() {
44         try {
45             // Read assertion to use from file.
46             string path = data_path + "saml1/profile/SAML1Assertion.xml";
47             ifstream in(path.c_str());
48             DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);
49             XercesJanitor<DOMDocument> janitor(doc);
50             auto_ptr<saml1::Assertion> assertion(
51                 dynamic_cast<saml1::Assertion*>(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(),true))
52                 );
53             janitor.release();
54
55             TSM_ASSERT_THROWS("Policy should have tripped on AudienceRestriction", m_policy->evaluate(*assertion.get()), SecurityPolicyException);
56
57             auto_ptr_XMLCh recipient("https://sp.example.org");
58             m_policy->getAudiences().push_back(recipient.get());
59             m_policy->evaluate(*assertion.get());
60         }
61         catch (exception& ex) {
62             TS_TRACE(ex.what());
63             throw;
64         }
65     }
66 };