Fix KeyVaue validator, add ECKeyValue test case.
authorScott Cantor <cantor.2@osu.edu>
Wed, 9 Jun 2010 01:12:09 +0000 (01:12 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 9 Jun 2010 01:12:09 +0000 (01:12 +0000)
xmltooling/signature/impl/KeyInfoSchemaValidators.cpp
xmltoolingtest/KeyInfoTest.h
xmltoolingtest/data/KeyInfo4.xml [new file with mode: 0644]

index 23bfdb1..6b80265 100644 (file)
@@ -32,6 +32,19 @@ using namespace std;
 using xmlconstants::XMLSIG_NS;
 using xmlconstants::XMLSIG11_NS;
 
+#define XMLOBJECTVALIDATOR_ONLYONEOF4(cname,proper1,proper2,proper3,proper4) \
+    int c##proper1##proper2##proper3##proper4=0; \
+    if (ptr->get##proper1()!=nullptr) \
+        c##proper1##proper2##proper3##proper4++; \
+    if (ptr->get##proper2()!=nullptr) \
+        c##proper1##proper2##proper3##proper4++; \
+    if (ptr->get##proper3()!=nullptr) \
+        c##proper1##proper2##proper3##proper4++; \
+    if (ptr->get##proper4()!=nullptr) \
+        c##proper1##proper2##proper3##proper4++; \
+    if (c##proper1##proper2##proper3##proper4 != 1) \
+        throw xmltooling::ValidationException(#cname" must have only one of "#proper1", "#proper2", "#proper3", or "#proper4".")
+
 namespace xmlsignature {
 
     XMLOBJECTVALIDATOR_SIMPLE(XMLTOOL_DLLLOCAL,KeyName);
@@ -72,7 +85,7 @@ namespace xmlsignature {
     END_XMLOBJECTVALIDATOR;
 
     BEGIN_XMLOBJECTVALIDATOR(XMLTOOL_DLLLOCAL,KeyValue);
-        XMLOBJECTVALIDATOR_ONLYONEOF3(KeyValue,DSAKeyValue,RSAKeyValue,UnknownXMLObject);
+        XMLOBJECTVALIDATOR_ONLYONEOF4(KeyValue,DSAKeyValue,RSAKeyValue,ECKeyValue,UnknownXMLObject);
     END_XMLOBJECTVALIDATOR;
 
     BEGIN_XMLOBJECTVALIDATOR(XMLTOOL_DLLLOCAL,Transform);
index 3ebae4c..26b8579 100644 (file)
@@ -92,4 +92,25 @@ public:
         TS_ASSERT(kiObject.get()!=nullptr);
         TS_ASSERT_THROWS(SchemaValidators.validate(kiObject.get()),ValidationException);
     }
+
+    void testKeyInfo4() {
+        string path=data_path + "KeyInfo4.xml";
+        ifstream fs(path.c_str());
+        DOMDocument* doc=XMLToolingConfig::getConfig().getValidatingParser().parse(fs);
+        TS_ASSERT(doc!=nullptr);
+
+        const XMLObjectBuilder* b = XMLObjectBuilder::getBuilder(doc->getDocumentElement());
+        TS_ASSERT(b!=nullptr);
+
+        auto_ptr<KeyInfo> kiObject(dynamic_cast<KeyInfo*>(b->buildFromDocument(doc)));
+        TS_ASSERT(kiObject.get()!=nullptr);
+        TSM_ASSERT_EQUALS("Number of child elements was not expected value",
+            1, kiObject->getKeyValues().size());
+        ECKeyValue* kv = kiObject->getKeyValues().front()->getECKeyValue();
+        TSM_ASSERT("Missing ECKeyValue", kv!=nullptr);
+        TSM_ASSERT("Missing NamedCurve", kv->getNamedCurve()!=nullptr);
+        TSM_ASSERT("Missing PublicKey", kv->getPublicKey()!=nullptr);
+
+        SchemaValidators.validate(kiObject.get());
+    }
 };
diff --git a/xmltoolingtest/data/KeyInfo4.xml b/xmltoolingtest/data/KeyInfo4.xml
new file mode 100644 (file)
index 0000000..90b011e
--- /dev/null
@@ -0,0 +1,11 @@
+<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ds11="http://www.w3.org/2009/xmldsig11#">\r
+    <ds:KeyValue>\r
+        <ds11:ECKeyValue>\r
+            <ds11:NamedCurve URI="urn:oid:1.2.840.10045.3.1.7"/>\r
+            <ds11:PublicKey>\r
+            MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAER9z9tFObV/MdgFT0IDEUq6b8VZFL\r
+            sj+qeNjJMVgEIrioShi5HfJfZq/aLuTvt5DZEybsFNC1Fit5cYx7+0DN3g==\r
+            </ds11:PublicKey>\r
+        </ds11:ECKeyValue>\r
+    </ds:KeyValue>\r
+</ds:KeyInfo>\r