be54b2d1a0e5777568252701d04311891f1aea39
[shibboleth/cpp-opensaml.git] / samltest / SAMLArtifactCreationTest.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 #include <saml/SAMLConfig.h>
19 #include <saml/saml1/binding/SAMLArtifactType0001.h>
20 #include <saml/saml1/binding/SAMLArtifactType0002.h>
21 #include <saml/saml2/binding/SAML2ArtifactType0004.h>
22 #include <xmltooling/security/SecurityHelper.h>
23
24 using namespace opensaml::saml1p;
25 using namespace opensaml::saml2p;
26 using namespace opensaml;
27 using namespace std;
28
29 class SAMLArtifactCreationTest : public CxxTest::TestSuite
30 {
31 public:
32     string providerIdStr;
33     string handle;
34     void setUp() {
35         if (handle.empty()) {
36             providerIdStr = "https://idp.org/SAML";
37             SAMLConfig::getConfig().generateRandomBytes(handle,SAMLArtifactType0001::HANDLE_LENGTH);
38         }
39     }
40     void testSAMLArtifactType0001(void) {
41         SAMLConfig& conf=SAMLConfig::getConfig();
42         string sourceId;
43         conf.generateRandomBytes(sourceId,SAMLArtifactType0001::SOURCEID_LENGTH);
44         SAMLArtifactType0001 artifact1(sourceId,handle);
45         //printResults(artifact1);
46
47         SAMLArtifactType0001 artifact2(
48             SecurityHelper::doHash("SHA1", providerIdStr.data(), providerIdStr.length(), false), handle
49             );
50         //printResults(artifact2,providerIdStr.c_str());
51     }
52
53     void testSAMLArtifactType0002(void) {
54         SAMLArtifactType0002 artifact(providerIdStr,handle);
55         //printResults(artifact,providerIdStr.c_str());
56     }
57
58     void testSAMLArtifactType0004(void) {
59         SAML2ArtifactType0004 artifact(
60             SecurityHelper::doHash("SHA1", providerIdStr.data(), providerIdStr.length(), false), 666, handle
61             );
62         //printResults(artifact,providerIdStr.c_str());
63     }
64
65     void printResults(SAMLArtifact& artifact, const char* str=NULL) {
66         // print heading:
67         cout << "Artifact Type " << SAMLArtifact::toHex(artifact.getTypeCode());
68         cout << " (size = " << artifact.getBytes().size() << ")" << endl;
69     
70         // print URI:
71         if (str) { 
72           cout << "URI:     " << str << endl; 
73         }
74         else {
75           cout << "URI:     NONE" << endl; 
76         }
77     
78         // print hex-encoded artifact:
79         cout << "Hex:     " << SAMLArtifact::toHex(artifact.getBytes()) << endl;
80     
81         // print base64-encoded artifact:
82         cout << "Base64:  " << artifact.encode() << endl;
83     
84         // print ruler:
85         cout <<  "         ----------------------------------------------------------------------" << endl;
86         cout <<  "         1234567890123456789012345678901234567890123456789012345678901234567890" << endl;
87         cout <<  "                  1         2         3         4         5         6         7" << endl;
88         cout <<  "         ----------------------------------------------------------------------" << endl;
89     }
90 };