void XmlDocumentUnitTests::testGetNodeValue() { struct testGetNodeValue_TestData { LPCWSTR xpath; LPCWSTR result; LPCWSTR xml; }; std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); testGetNodeValue_TestData testdata[] = { { L"/bookstore/book[@id=1]/title", L"'Emma'", L"<title xmlns=\"http://www.lucernepublishing.com\">'Emma'</title>" } }; for (int i = 0; i < ARRAYSIZE(testdata); i++) { // default value std::wstring default_value = xml.GetNodeValue(L"/invalid/xpath", NULL, L"default"); std::wcout << std::endl << L"Value: " << default_value; CPPUNIT_ASSERT(L"default" == default_value); // value std::wstring result_value = xml.GetNodeValue(testdata[i].xpath); std::wcout << std::endl << L"Value: " << result_value; CPPUNIT_ASSERT(testdata[i].result == result_value); // xml std::wstring result_xml = xml.GetNodeXml(testdata[i].xpath); std::wcout << std::endl << L"Xml: " << result_xml; CPPUNIT_ASSERT(testdata[i].xml == result_xml); } }
void XmlDocumentUnitTests::testGetNodeBoolValue() { struct testGetNodeBoolValue_TestData { LPCWSTR xpath; bool result; }; std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile.c_str(); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); testGetNodeBoolValue_TestData testdata[] = { { L"/bookstore/book[@id=1]/read", false }, { L"/bookstore/book[@id=2]/read", true }, }; for (int i = 0; i < ARRAYSIZE(testdata); i++) { // value bool result_value = xml.GetNodeBoolValue(testdata[i].xpath); std::wcout << std::endl << L"Value: " << result_value; CPPUNIT_ASSERT(testdata[i].result == result_value); } }
void XmlDocumentUnitTests::testSelectNodes() { struct testSelectNodes_TestData { LPCWSTR xpath; long count; }; testSelectNodes_TestData testdata[] = { { L"/bookstore", 1 }, { L"/bookstore/book", 2 } }; std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); for (int i = 0; i < ARRAYSIZE(testdata); i++) { MSXML2::IXMLDOMNodeListPtr nodes = xml.SelectNodes(testdata[i].xpath); std::wcout << std::endl << testdata[i].xpath << L": " << nodes->length; CPPUNIT_ASSERT(testdata[i].count == nodes->length); } }
void XmlDocumentUnitTests::testGetAttributeValueXPath() { struct testGetAttributeValue_TestData { LPCWSTR xpath; LPCWSTR name; LPCWSTR result; }; std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); testGetAttributeValue_TestData testdata[] = { { L"/bookstore/book[@id=1]", L"id", L"1" } }; for (int i = 0; i < ARRAYSIZE(testdata); i++) { MSXML2::IXMLDOMNodePtr node = xml.SelectNode(testdata[i].xpath); // default value std::wstring default_value = xml.GetAttributeValue(L"invalid", node, L"default"); std::wcout << std::endl << L"Value: " << default_value; CPPUNIT_ASSERT(L"default" == default_value); // value std::wstring result_value = xml.GetAttributeValue(testdata[i].name, node); std::wcout << std::endl << L"Value: " << result_value; CPPUNIT_ASSERT(testdata[i].result == result_value); } }
CA_API UINT __stdcall Xml_DeleteNodes(MSIHANDLE hInstall) { MSI_EXCEPTION_HANDLER_PROLOG; MsiInstall msiInstall(hInstall); std::wstring filename = msiInstall.GetProperty(L"XML_FILENAME"); AppSecInc::Xml::XmlDocument doc; doc.Load(filename); std::wstring xpath = msiInstall.GetProperty(L"XML_XPATH"); MSXML2::IXMLDOMNodeListPtr nodes = doc.FindNodes(xpath); int count = 0; if (NULL != nodes) { MSXML2::IXMLDOMNodePtr node = NULL; while (NULL != (node = nodes->nextNode())) { node->parentNode->removeChild(node); count++; } } if (count > 0) { CHECK_HR(doc->save(CComVariant(filename.c_str())), L"Error saving '" << filename << L"'"); } msiInstall.SetProperty(L"XML_DELETED", AppSecInc::StringUtils::toWString(count)); MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }
void XmlDocumentUnitTests::testHasAttribute() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); CPPUNIT_ASSERT(xml.HasAttribute(L"id", xml.SelectNode(L"/bookstore/book[@id=1]"))); CPPUNIT_ASSERT(! xml.HasAttribute(L"iddoesntexist", xml.SelectNode(L"/bookstore/book[@id=1]"))); }
void XmlDocumentUnitTests::testGetNodeXml() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); std::wstring data = xml.GetNodeXml(L"/bookstore/book[@id=1]"); CPPUNIT_ASSERT(AppSecInc::StringUtils::startsWith(data, L"<book")); }
void XmlDocumentUnitTests::testFindNodes() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); CPPUNIT_ASSERT(NULL != xml.FindNodes(L"/bookstore/book[@id=1]")); CPPUNIT_ASSERT(NULL == xml.FindNodes(L"/bookstore/book[@id=1]/invalid/")); }
void XmlDocumentUnitTests::testHasNode() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); CPPUNIT_ASSERT(xml.HasNode(L"/bookstore/book[@id=1]/title")); CPPUNIT_ASSERT(! xml.HasNode(L"/bookstore/book[@id=1]/invalid")); }
void XmlDocumentUnitTests::testLoad() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile.c_str(); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); CPPUNIT_ASSERT(AppSecInc::StringUtils::startsWith(xml.GetXml(), L"<bookstore xmlns=\"http://www.lucernepublishing.com\">")); }
void XmlDocumentUnitTests::testGetXml() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile); std::wstring outerxml = xml.GetXml(); CPPUNIT_ASSERT(! outerxml.empty()); //! \todo read the file with File::ReadToEnd (implement) and compare data }
void XmlDocumentUnitTests::testGetAttributeValue() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile); std::wstring result = xml.GetAttributeValue(L"/bookstore/book", L"id"); std::wcout << std::endl << L"Value: " << result; CPPUNIT_ASSERT(result == L"1"); }
void XmlDocumentUnitTests::testSelectAttribute() { std::wstring xmlfile = GetLocalFileLocation(L"store.xml"); AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile, CLSID_DOMDocument); CPPUNIT_ASSERT(NULL != xml.SelectAttribute(L"id", xml.SelectNode(L"/bookstore/book[@id=1]"))); try { xml.SelectAttribute(L"iddoesntexist", xml.SelectNode(L"/bookstore/book[@id=1]")); throw "expected std::exception"; } catch(std::exception& e) { std::cout << std::endl << "Expected exception: " << e.what(); } }
CA_API UINT __stdcall Xml_SelectNodeValue(MSIHANDLE hInstall) { MSI_EXCEPTION_HANDLER_PROLOG; MsiInstall msiInstall(hInstall); std::wstring filename = msiInstall.GetProperty(L"XML_FILENAME"); AppSecInc::Xml::XmlDocument doc; doc.Load(filename); std::wstring xpath = msiInstall.GetProperty(L"XML_XPATH"); std::wstring value = doc.GetNodeValue(xpath); msiInstall.SetProperty(L"XML_NODEVALUE", value); MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }
void XmlDocumentUnitTests::testXslTransform() { // check test xml file std::wstring xmlfile = GetLocalFileLocation(L"simple.xml"); std::wcout << std::endl << L"Xml: " << xmlfile; CPPUNIT_ASSERT(File::FileExists(xmlfile)); // check test xsl file std::wstring xslfile = GetLocalFileLocation(L"simple.xsl"); std::wcout << std::endl << L"Xsl: " << xslfile; CPPUNIT_ASSERT(File::FileExists(xslfile)); // transform AppSecInc::Xml::XmlDocument xml; xml.Load(xmlfile); AppSecInc::Xml::XmlDocument transformedXml; std::wstring transformResult = xml.XslTransform(xslfile); transformedXml.LoadXml(transformResult); // load the result CPPUNIT_ASSERT(transformResult.length() > 0); }
CA_API UINT __stdcall Xml_XslTransform(MSIHANDLE hInstall) { MSI_EXCEPTION_HANDLER_PROLOG; MsiInstall msiInstall(hInstall); std::wstring filename = msiInstall.GetProperty(L"XML_FILENAME"); AppSecInc::Xml::XmlDocument doc; doc.Load(filename); std::wstring xslt_filename = msiInstall.GetProperty(L"XSLT_FILENAME"); std::wstring xslt_result_filename = msiInstall.GetProperty(L"XSLT_RESULT_FILENAME"); std::wstring transformedData = doc.XslTransform(xslt_filename); std::string char_data = AppSecInc::StringUtils::wc2mb(transformedData); std::vector<char> binary_data; binary_data.assign(char_data.begin(), char_data.end()); AppSecInc::File::FileWrite(xslt_result_filename, binary_data); MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }