From 67a31004a90df0a1ee0976ffdf31176a598ea3e1 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Fri, 23 Jun 2006 19:04:12 +0000 Subject: [PATCH] Refactor some tests. --- samltest/Makefile.am | 3 +- samltest/samltest.vcproj | 12 +++-- samltest/signature/SAML1AssertionTest.h | 55 ++------------------- samltest/signature/SAML1RequestTest.h | 57 ++-------------------- samltest/signature/SAML1ResponseTest.h | 56 ++------------------- samltest/signature/SAMLSignatureTestBase.h | 78 ++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 159 deletions(-) create mode 100644 samltest/signature/SAMLSignatureTestBase.h diff --git a/samltest/Makefile.am b/samltest/Makefile.am index 77ef05b..7c519e5 100644 --- a/samltest/Makefile.am +++ b/samltest/Makefile.am @@ -24,7 +24,8 @@ samltest_h = \ saml1/core/impl/AuthenticationStatementTest.h noinst_HEADERS = \ - internal.h + internal.h \ + signature/SAMLSignatureTestBase.h nodist_samltest_SOURCES = $(samltest_h:.h=.cpp) diff --git a/samltest/samltest.vcproj b/samltest/samltest.vcproj index 2887473..5aca512 100644 --- a/samltest/samltest.vcproj +++ b/samltest/samltest.vcproj @@ -547,7 +547,7 @@ > @@ -556,7 +556,7 @@ > @@ -569,7 +569,7 @@ > @@ -578,11 +578,15 @@ > + + -#include -#include #include -#include -#include -#include -#include -#include -#include -#include using namespace opensaml::saml1; -using namespace xmlsignature; -class _addcert : public std::binary_function { -public: - void operator()(X509Data* bag, XSECCryptoX509* cert) const { - safeBuffer& buf=cert->getDEREncodingSB(); - X509Certificate* x=X509CertificateBuilder::buildX509Certificate(); - x->setValue(buf.sbStrToXMLCh()); - bag->getX509Certificates().push_back(x); - } -}; - -class SAML1AssertionTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase { - XSECCryptoKey* m_key; - vector m_certs; +class SAML1AssertionTest : public CxxTest::TestSuite, public SAMLSignatureTestBase { public: void setUp() { childElementsFile = data_path + "signature/SAML1Assertion.xml"; - SAMLObjectBaseTestCase::setUp(); - string keypath=data_path + "key.pem"; - BIO* in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,keypath.c_str())>0) { - EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); - if (pkey) { - m_key=new OpenSSLCryptoKeyRSA(pkey); - EVP_PKEY_free(pkey); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_key!=NULL); - - string certpath=data_path + "cert.pem"; - in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,certpath.c_str())>0) { - X509* x=NULL; - while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) { - m_certs.push_back(new OpenSSLCryptoX509(x)); - X509_free(x); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_certs.size()>0); + SAMLSignatureTestBase::setUp(); } void tearDown() { - SAMLObjectBaseTestCase::tearDown(); - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + SAMLSignatureTestBase::tearDown(); } void testSignature() { diff --git a/samltest/signature/SAML1RequestTest.h b/samltest/signature/SAML1RequestTest.h index 2d1fb8f..18870b0 100644 --- a/samltest/signature/SAML1RequestTest.h +++ b/samltest/signature/SAML1RequestTest.h @@ -14,71 +14,24 @@ * limitations under the License. */ -#include "internal.h" -#include -#include -#include +#include "signature/SAMLSignatureTestBase.h" +#include #include -#include -#include -#include -#include -#include -#include -#include using namespace opensaml::saml1p; using namespace opensaml::saml1; -using namespace xmlsignature; -class _addcert : public std::binary_function { -public: - void operator()(X509Data* bag, XSECCryptoX509* cert) const { - safeBuffer& buf=cert->getDEREncodingSB(); - X509Certificate* x=X509CertificateBuilder::buildX509Certificate(); - x->setValue(buf.sbStrToXMLCh()); - bag->getX509Certificates().push_back(x); - } -}; - -class SAML1RequestTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase { - XSECCryptoKey* m_key; - vector m_certs; +class SAML1RequestTest : public CxxTest::TestSuite, public SAMLSignatureTestBase { public: void setUp() { childElementsFile = data_path + "signature/SAML1Request.xml"; - SAMLObjectBaseTestCase::setUp(); - string keypath=data_path + "key.pem"; - BIO* in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,keypath.c_str())>0) { - EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); - if (pkey) { - m_key=new OpenSSLCryptoKeyRSA(pkey); - EVP_PKEY_free(pkey); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_key!=NULL); - - string certpath=data_path + "cert.pem"; - in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,certpath.c_str())>0) { - X509* x=NULL; - while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) { - m_certs.push_back(new OpenSSLCryptoX509(x)); - X509_free(x); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_certs.size()>0); + SAMLSignatureTestBase::setUp(); } void tearDown() { - SAMLObjectBaseTestCase::tearDown(); - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + SAMLSignatureTestBase::tearDown(); } void testSignature() { diff --git a/samltest/signature/SAML1ResponseTest.h b/samltest/signature/SAML1ResponseTest.h index cc8f2ba..592edc2 100644 --- a/samltest/signature/SAML1ResponseTest.h +++ b/samltest/signature/SAML1ResponseTest.h @@ -14,70 +14,24 @@ * limitations under the License. */ -#include "internal.h" +#include "signature/SAMLSignatureTestBase.h" + #include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include using namespace opensaml::saml1p; using namespace opensaml::saml1; -using namespace xmlsignature; -class _addcert : public std::binary_function { -public: - void operator()(X509Data* bag, XSECCryptoX509* cert) const { - safeBuffer& buf=cert->getDEREncodingSB(); - X509Certificate* x=X509CertificateBuilder::buildX509Certificate(); - x->setValue(buf.sbStrToXMLCh()); - bag->getX509Certificates().push_back(x); - } -}; - -class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase { - XSECCryptoKey* m_key; - vector m_certs; +class SAML1ResponseTest : public CxxTest::TestSuite, public SAMLSignatureTestBase { public: void setUp() { childElementsFile = data_path + "signature/SAML1Response.xml"; - SAMLObjectBaseTestCase::setUp(); - string keypath=data_path + "key.pem"; - BIO* in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,keypath.c_str())>0) { - EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); - if (pkey) { - m_key=new OpenSSLCryptoKeyRSA(pkey); - EVP_PKEY_free(pkey); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_key!=NULL); - - string certpath=data_path + "cert.pem"; - in=BIO_new(BIO_s_file_internal()); - if (in && BIO_read_filename(in,certpath.c_str())>0) { - X509* x=NULL; - while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) { - m_certs.push_back(new OpenSSLCryptoX509(x)); - X509_free(x); - } - } - if (in) BIO_free(in); - TS_ASSERT(m_certs.size()>0); + SAMLSignatureTestBase::setUp(); } void tearDown() { - SAMLObjectBaseTestCase::tearDown(); - delete m_key; - for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + SAMLSignatureTestBase::tearDown(); } void testSignature() { diff --git a/samltest/signature/SAMLSignatureTestBase.h b/samltest/signature/SAMLSignatureTestBase.h new file mode 100644 index 0000000..24a8873 --- /dev/null +++ b/samltest/signature/SAMLSignatureTestBase.h @@ -0,0 +1,78 @@ +/* + * Copyright 2001-2005 Internet2 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "internal.h" +#include +#include + + +#include +#include +#include +#include +#include +#include + +using namespace xmlsignature; + +class _addcert : public std::binary_function { +public: + void operator()(X509Data* bag, XSECCryptoX509* cert) const { + safeBuffer& buf=cert->getDEREncodingSB(); + X509Certificate* x=X509CertificateBuilder::buildX509Certificate(); + x->setValue(buf.sbStrToXMLCh()); + bag->getX509Certificates().push_back(x); + } +}; + +class SAMLSignatureTestBase : public SAMLObjectBaseTestCase { +protected: + XSECCryptoKey* m_key; + vector m_certs; +public: + void setUp() { + SAMLObjectBaseTestCase::setUp(); + string keypath=data_path + "key.pem"; + BIO* in=BIO_new(BIO_s_file_internal()); + if (in && BIO_read_filename(in,keypath.c_str())>0) { + EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); + if (pkey) { + m_key=new OpenSSLCryptoKeyRSA(pkey); + EVP_PKEY_free(pkey); + } + } + if (in) BIO_free(in); + TS_ASSERT(m_key!=NULL); + + string certpath=data_path + "cert.pem"; + in=BIO_new(BIO_s_file_internal()); + if (in && BIO_read_filename(in,certpath.c_str())>0) { + X509* x=NULL; + while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) { + m_certs.push_back(new OpenSSLCryptoX509(x)); + X509_free(x); + } + } + if (in) BIO_free(in); + TS_ASSERT(m_certs.size()>0); + } + + void tearDown() { + SAMLObjectBaseTestCase::tearDown(); + delete m_key; + for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup()); + } +}; -- 2.1.4