Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / SecurityHelperTest.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 "XMLObjectBaseTestCase.h"
18
19 #include <xmltooling/security/SecurityHelper.h>
20
21 class SecurityHelperTest : public CxxTest::TestSuite {
22     vector<XSECCryptoX509*> certs;
23 public:
24     void setUp() {
25     }
26
27     void tearDown() {
28         for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
29     }
30
31     void testKeysFromFiles() {
32         string pathname = data_path + "key.pem";
33         auto_ptr<XSECCryptoKey> key1(SecurityHelper::loadKeyFromFile(pathname.c_str()));
34         pathname = data_path + "key.der";
35         auto_ptr<XSECCryptoKey> key2(SecurityHelper::loadKeyFromFile(pathname.c_str()));
36         pathname = data_path + "test.pfx";
37         auto_ptr<XSECCryptoKey> key3(SecurityHelper::loadKeyFromFile(pathname.c_str(), NULL, "password"));
38
39         TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
40         TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
41
42         pathname = data_path + "key2.pem";
43         auto_ptr<XSECCryptoKey> key4(SecurityHelper::loadKeyFromFile(pathname.c_str()));
44         TSM_ASSERT("Different keys matched", !SecurityHelper::matches(key3.get(), key4.get()));
45     }
46
47     void testCertificatesFromFiles() {
48         string pathname = data_path + "cert.pem";
49         SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
50         pathname = data_path + "cert.der";
51         SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
52         pathname = data_path + "test.pfx";
53         SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str(), NULL, "password");
54
55         TSM_ASSERT_EQUALS("Wrong certificate count", certs.size(), 3);
56
57         auto_ptr<XSECCryptoKey> key1(certs[0]->clonePublicKey());
58         auto_ptr<XSECCryptoKey> key2(certs[0]->clonePublicKey());
59         auto_ptr<XSECCryptoKey> key3(certs[0]->clonePublicKey());
60
61         TSM_ASSERT("PEM/DER keys did not match", SecurityHelper::matches(key1.get(), key2.get()));
62         TSM_ASSERT("DER/PKCS12 keys did not match", SecurityHelper::matches(key2.get(), key3.get()));
63
64         for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
65     }
66 };