From 3140b4c865f443bf4db59bca70d965704913ba12 Mon Sep 17 00:00:00 2001
From: Rod Widdowson <rdw@steadingsoftware.com>
Date: Sun, 20 Nov 2016 13:20:21 +0000
Subject: [PATCH 18/31] CPPXT-110 Test for OpenSSL part of
 ExplicitKeyTrustEngine

https://issues.shibboleth.net/jira/browse/CPPXT-110
---
 .../vc10/xmltoolingtest/xmltoolingtest.vcxproj     | 19 ++++-
 .../xmltoolingtest/xmltoolingtest.vcxproj.filters  |  3 +
 .../vc14/xmltoolingtest/xmltoolingtest.vcxproj     | 21 ++++-
 .../xmltoolingtest/xmltoolingtest.vcxproj.filters  |  7 +-
 xmltoolingtest/ExplicitKeyTrustEngineTest.h        | 95 ++++++++++++++++++++++
 xmltoolingtest/Makefile.am                         |  1 +
 xmltoolingtest/data/ExplicitKeyTrustEngine.xml     |  2 +
 .../data/FilesystemCredentialResolverCertOnly.xml  | 18 ++++
 8 files changed, 160 insertions(+), 6 deletions(-)
 create mode 100644 xmltoolingtest/ExplicitKeyTrustEngineTest.h
 create mode 100644 xmltoolingtest/data/ExplicitKeyTrustEngine.xml
 create mode 100644 xmltoolingtest/data/FilesystemCredentialResolverCertOnly.xml

diff --git a/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj b/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj
index 81daf81..be3175c 100644
--- a/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj
+++ b/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj
@@ -1,4 +1,4 @@
-﻿<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup Label="ProjectConfigurations">
     <ProjectConfiguration Include="Debug|Win32">
@@ -175,6 +175,7 @@
     <ClCompile Include="DateTimeTest.cpp" />
     <ClCompile Include="EncryptionTest.cpp" />
     <ClCompile Include="ExceptionTest.cpp" />
+    <ClCompile Include="ExplicitKeyTrustEngineTest.cpp" />
     <ClCompile Include="FilesystemCredentialResolverTest.cpp" />
     <ClCompile Include="InlineKeyResolverTest.cpp" />
     <ClCompile Include="KeyInfoTest.cpp" />
@@ -260,6 +261,20 @@
 </Command>
       <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cpp;%(Outputs)</Outputs>
     </CustomBuild>
+    <CustomBuild Include="..\..\..\XMLToolingTest\ExplicitKeyTrustEngineTest.h">
+      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cpp;%(Outputs)</Outputs>
+    </CustomBuild>
     <CustomBuild Include="..\..\..\XMLToolingTest\FilesystemCredentialResolverTest.h">
       <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
 </Command>
@@ -452,4 +467,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj.filters b/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj.filters
index 58cc379..c4f862f 100644
--- a/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj.filters
+++ b/Projects/vc10/xmltoolingtest/xmltoolingtest.vcxproj.filters
@@ -70,6 +70,9 @@
     <ClInclude Include="..\..\..\XMLToolingTest\XMLObjectBaseTestCase.h">
       <Filter>Unit Tests</Filter>
     </ClInclude>
+    <ClInclude Include="ExplicitKeyTrustEngineTest.h">
+      <Filter>Unit Tests</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="..\..\..\XMLToolingTest\Makefile.am" />
diff --git a/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj b/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj
index 9f7dc3f..e66ecf5 100644
--- a/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj
+++ b/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj
@@ -1,4 +1,4 @@
-﻿<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup Label="ProjectConfigurations">
     <ProjectConfiguration Include="Debug|Win32">
@@ -183,6 +183,7 @@
     <ClCompile Include="DateTimeTest.cpp" />
     <ClCompile Include="EncryptionTest.cpp" />
     <ClCompile Include="ExceptionTest.cpp" />
+    <ClCompile Include="ExplicitKeyTrustEngineTest.cpp" />
     <ClCompile Include="FilesystemCredentialResolverTest.cpp" />
     <ClCompile Include="InlineKeyResolverTest.cpp" />
     <ClCompile Include="KeyInfoTest.cpp" />
@@ -254,6 +255,7 @@
 </Command>
       <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cpp;%(Outputs)</Outputs>
     </CustomBuild>
+
     <CustomBuild Include="..\..\..\XMLToolingTest\ExceptionTest.h">
       <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp "%(FullPath)"
 </Command>
@@ -268,6 +270,21 @@
 </Command>
       <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cpp;%(Outputs)</Outputs>
     </CustomBuild>
+
+    <CustomBuild Include="..\..\..\XMLToolingTest\ExplicitKeyTrustEngineTest.h">
+      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).cpp;%(Outputs)</Outputs>
+      <Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp ../../../xmltoolingtest/"%(FileName)".h"
+</Command>
+      <Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).cpp;%(Outputs)</Outputs>
+    </CustomBuild>
     <CustomBuild Include="..\..\..\XMLToolingTest\FilesystemCredentialResolverTest.h">
       <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">perl.exe -w $(CxxTestRoot)\cxxtestgen.pl --part --have-eh --have-std --abort-on-fail -o "%(Filename)".cpp "%(FullPath)"
 </Command>
@@ -460,4 +477,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj.filters b/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj.filters
index e9f6032..543a0f4 100644
--- a/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj.filters
+++ b/Projects/vc14/xmltoolingtest/xmltoolingtest.vcxproj.filters
@@ -1,4 +1,4 @@
-﻿<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
     <Filter Include="Generated Code">
@@ -87,6 +87,9 @@
     <CustomBuild Include="..\..\..\XMLToolingTest\ExceptionTest.h">
       <Filter>Unit Tests</Filter>
     </CustomBuild>
+    <CustomBuild Include="..\..\..\XMLToolingTest\ExplicitTrustEngineTest.h">
+      <Filter>Unit Tests</Filter>
+    </CustomBuild>
     <CustomBuild Include="..\..\..\XMLToolingTest\FilesystemCredentialResolverTest.h">
       <Filter>Unit Tests</Filter>
     </CustomBuild>
@@ -130,4 +133,4 @@
       <Filter>Unit Tests</Filter>
     </CustomBuild>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/xmltoolingtest/ExplicitKeyTrustEngineTest.h b/xmltoolingtest/ExplicitKeyTrustEngineTest.h
new file mode 100644
index 0000000..8af3b72
--- /dev/null
+++ b/xmltoolingtest/ExplicitKeyTrustEngineTest.h
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you 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 "XMLObjectBaseTestCase.h"
+#include <xmltooling/security/CredentialResolver.h>
+#include <xmltooling/security/TrustEngine.h>
+#include <xsec/enc/XSECCryptoX509.hpp>
+#include <xmltooling/security/SecurityHelper.h>
+#include <xmltooling/security/X509TrustEngine.h>
+#include <fstream>
+
+class ExplicitKeyTrustEngineTest : public CxxTest::TestSuite {
+
+private:
+    X509TrustEngine* m_trustEngine;
+    CredentialResolver *m_resolver;
+
+public:
+
+    void setUp() {
+        m_resolver=nullptr;
+        xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
+        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
+        XMLObjectBuilder::registerBuilder(qname, new SimpleXMLObjectBuilder());
+        XMLObjectBuilder::registerBuilder(qtype, new SimpleXMLObjectBuilder());
+
+        string config = data_path + "FilesystemCredentialResolverCertOnly.xml";
+        ifstream inFsCred(config.c_str());
+        DOMDocument* docFsCred=XMLToolingConfig::getConfig().getParser().parse(inFsCred);
+        XercesJanitor<DOMDocument> janitorFsCred(docFsCred);
+        m_resolver = XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(
+            CHAINING_CREDENTIAL_RESOLVER,docFsCred->getDocumentElement()
+            );
+
+        config = data_path + "ExplicitKeyTrustEngine.xml";
+        ifstream inTrustEngine(config.c_str());
+        DOMDocument* docTrustEngine=XMLToolingConfig::getConfig().getParser().parse(inTrustEngine);
+        XercesJanitor<DOMDocument> janitor(docTrustEngine);
+
+        TrustEngine *trustEngine =
+            XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(
+                EXPLICIT_KEY_TRUSTENGINE, docTrustEngine->getDocumentElement()
+                );
+
+        m_trustEngine = dynamic_cast<X509TrustEngine*>(trustEngine);
+
+    }
+
+    void tearDown() {
+        xmltooling::QName qname(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME);
+        xmltooling::QName qtype(SimpleXMLObject::NAMESPACE,SimpleXMLObject::TYPE_NAME);
+        XMLObjectBuilder::deregisterBuilder(qname);
+        XMLObjectBuilder::deregisterBuilder(qtype);
+        delete m_resolver;
+        delete m_trustEngine;
+    }
+
+    void testCerts() {
+    
+        vector<XSECCryptoX509*> certs;
+        string pathname = data_path + "cert.pem";
+        SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
+        pathname = data_path + "dsa-cert.pem";
+        SecurityHelper::loadCertificatesFromFile(certs, pathname.c_str());
+        // no EC CPPXT-114
+
+        for (vector<XSECCryptoX509*>::const_iterator cert=certs.begin(); cert!=certs.end(); ++cert) {
+            // certs is ignore but must be present
+            TSM_ASSERT("Trust Engine Validate", m_trustEngine->validate(*cert, certs, *m_resolver));
+        }
+
+        for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
+        certs.clear();
+
+    }
+
+};
+
diff --git a/xmltoolingtest/Makefile.am b/xmltoolingtest/Makefile.am
index e96be98..e4bd429 100644
--- a/xmltoolingtest/Makefile.am
+++ b/xmltoolingtest/Makefile.am
@@ -21,6 +21,7 @@ if BUILD_XMLSEC
 xmlsec_sources = \
     EncryptionTest.h \
     FilesystemCredentialResolverTest.h \
+	ExplicitKeyTrustEngineTest.h \
     InlineKeyResolverTest.h \
     MemoryStorageServiceTest.h \
     PKIXEngineTest.h \
diff --git a/xmltoolingtest/data/ExplicitKeyTrustEngine.xml b/xmltoolingtest/data/ExplicitKeyTrustEngine.xml
new file mode 100644
index 0000000..18a6afb
--- /dev/null
+++ b/xmltoolingtest/data/ExplicitKeyTrustEngine.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TrustEngine type="ExplicitKey"/>
diff --git a/xmltoolingtest/data/FilesystemCredentialResolverCertOnly.xml b/xmltoolingtest/data/FilesystemCredentialResolverCertOnly.xml
new file mode 100644
index 0000000..04da4e7
--- /dev/null
+++ b/xmltoolingtest/data/FilesystemCredentialResolverCertOnly.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CredentialResolver type="Chaining">
+    <CredentialResolver type="File">
+        <Certificate>
+            <Path>../xmltoolingtest/data/cert.pem</Path>
+        </Certificate>
+    </CredentialResolver>
+    <CredentialResolver type="File">
+        <Certificate>
+            <Path>../xmltoolingtest/data/dsa-cert.pem</Path>
+        </Certificate>
+    </CredentialResolver>
+    <CredentialResolver type="File">
+        <Certificate>
+            <Path>../xmltoolingtest/data/ec-cert.pem</Path>
+        </Certificate>
+    </CredentialResolver>
+</CredentialResolver>
-- 
2.13.6

