Remove some tracing, fix string compares.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / EncryptionTest.h
index cff6670..3f88890 100644 (file)
 \r
 #include <xmltooling/encryption/Decrypter.h>\r
 #include <xmltooling/encryption/Encrypter.h>\r
+#include <xmltooling/signature/CredentialResolver.h>\r
 \r
 #include <fstream>\r
-#include <openssl/pem.h>\r
 #include <xercesc/util/XMLUniDefs.hpp>\r
 #include <xsec/dsig/DSIGReference.hpp>\r
-#include <xsec/enc/XSECKeyInfoResolverDefault.hpp>\r
-#include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>\r
-#include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>\r
-#include <xsec/enc/XSECCryptoException.hpp>\r
-#include <xsec/framework/XSECException.hpp>\r
 \r
 using namespace xmlencryption;\r
 \r
@@ -42,53 +37,34 @@ public:
 };\r
 \r
 class EncryptionTest : public CxxTest::TestSuite {\r
-    XSECCryptoKey* m_key;\r
-    vector<XSECCryptoX509*> m_certs;\r
+    CredentialResolver* m_resolver;\r
 public:\r
     void setUp() {\r
-        string keypath=data_path + "key.pem";\r
-        BIO* in=BIO_new(BIO_s_file_internal());\r
-        if (in && BIO_read_filename(in,keypath.c_str())>0) {\r
-            EVP_PKEY* pkey=PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);\r
-            if (pkey) {\r
-                m_key=new OpenSSLCryptoKeyRSA(pkey);\r
-                EVP_PKEY_free(pkey);\r
-            }\r
-        }\r
-        if (in) BIO_free(in);\r
-        TS_ASSERT(m_key!=NULL);\r
-\r
-        string certpath=data_path + "cert.pem";\r
-        in=BIO_new(BIO_s_file_internal());\r
-        if (in && BIO_read_filename(in,certpath.c_str())>0) {\r
-            X509* x=NULL;\r
-            while (x=PEM_read_bio_X509(in,NULL,NULL,NULL)) {\r
-                m_certs.push_back(new OpenSSLCryptoX509(x));\r
-                X509_free(x);\r
-            }\r
-        }\r
-        if (in) BIO_free(in);\r
-        TS_ASSERT(m_certs.size()>0);\r
-        \r
+        m_resolver=NULL;\r
+        string config = data_path + "FilesystemCredentialResolver.xml";\r
+        ifstream in(config.c_str());\r
+        DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(in);\r
+        XercesJanitor<DOMDocument> janitor(doc);\r
+        m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(\r
+            FILESYSTEM_CREDENTIAL_RESOLVER,doc->getDocumentElement()\r
+            );\r
     }\r
 \r
     void tearDown() {\r
-        delete m_key;\r
-        for_each(m_certs.begin(),m_certs.end(),xmltooling::cleanup<XSECCryptoX509>());\r
+        delete m_resolver;\r
     }\r
 \r
-    void testBasic() {\r
-        TS_TRACE("testBasic");\r
-\r
+    void testEncryption() {\r
         string path=data_path + "ComplexXMLObject.xml";\r
         ifstream fs(path.c_str());\r
         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(fs);\r
         TS_ASSERT(doc!=NULL);\r
 \r
         try {\r
+            Locker locker(m_resolver);\r
             Encrypter encrypter;\r
             Encrypter::EncryptionParams ep;\r
-            Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_key->clone());\r
+            Encrypter::KeyEncryptionParams kep(DSIGConstants::s_unicodeStrURIRSA_1_5,m_resolver->getKey());\r
             auto_ptr<EncryptedData> encData(encrypter.encryptElement(doc->getDocumentElement(),ep,&kep));\r
 \r
             string buf;\r
@@ -99,10 +75,10 @@ public:
                 dynamic_cast<EncryptedData*>(XMLObjectBuilder::buildOneFromElement(doc2->getDocumentElement(),true))\r
                 );\r
 \r
-            Decrypter decrypter(new KeyResolver(m_key->clone()));\r
+            Decrypter decrypter(new KeyResolver(m_resolver->getKey()));\r
             DOMDocumentFragment* frag = decrypter.decryptData(encData2.get());\r
             XMLHelper::serialize(static_cast<DOMElement*>(frag->getFirstChild()), buf);\r
-            TS_TRACE(buf.c_str());\r
+            //TS_TRACE(buf.c_str());\r
             TS_ASSERT(doc->getDocumentElement()->isEqualNode(frag->getFirstChild()));\r
             frag->release();\r
             doc->release();\r