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