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 TemplateFiles_Deferred(MSIHANDLE hInstall) { MSI_EXCEPTION_HANDLER_PROLOG; MsiInstall msiInstall(hInstall); AppSecInc::Xml::XmlDocument xmlDocument; xmlDocument.LoadXml(msiInstall.GetActionData()); MSXML2::IXMLDOMNodeListPtr rows = xmlDocument.SelectNodes(L"//TemplateFile[@execute='true']"); // \todo //Row[@rollback='false'] MSXML2::IXMLDOMNodePtr row = NULL; while (NULL != (row = rows->nextNode())) { std::wstring id = xmlDocument.GetAttributeValue(L"id", row); std::wstring source = xmlDocument.GetNodeValue(L"Source", row); std::wstring target = xmlDocument.GetNodeValue(L"Target", row, source); msiInstall.LogInfo(L"TemplateFiles_Deferred", source + L" => " + target); std::map<std::wstring, std::wstring> properties; { MSXML2::IXMLDOMNodeListPtr property_rows = xmlDocument.SelectNodes(L"Properties/Property", row); MSXML2::IXMLDOMNodePtr property_row = NULL; while (NULL != (property_row = property_rows->nextNode())) { std::wstring name = xmlDocument.GetAttributeValue(L"name", property_row); std::wstring value = xmlDocument.GetAttributeValue(L"value", property_row); long escape = AppSecInc::StringUtils::stringToLong(xmlDocument.GetAttributeValue(L"escape", property_row, L"0")); properties[name] = escape == 1 ? AppSecInc::StringUtils::escape(value) : value; } } std::wstring data; bool utf8 = AppSecInc::File::ReadAndConvertToEnd(source, data); data = AppSecInc::Formatter::FormatTemplate(data, properties); std::string char_data; if (utf8) { char_data = AppSecInc::StringUtils::wc2utf8(data); char_data.insert(0, std::string(reinterpret_cast<char *>(AppSecInc::File::utf8_bom))); } else { char_data = AppSecInc::StringUtils::wc2mb(data); } std::vector<char> binary_data; binary_data.assign(char_data.begin(), char_data.end()); AppSecInc::File::FileWrite(target, binary_data); } MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }
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"); }
CA_API UINT __stdcall Xml_SelectNodeAttributeValue(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 attributename = msiInstall.GetProperty(L"XML_ATTRIBUTENAME"); std::wstring value = doc.GetAttributeValue(xpath, attributename); msiInstall.SetProperty(L"XML_ATTRIBUTEVALUE", value); MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }
CA_API UINT __stdcall LocalGroupMembers_Deferred(MSIHANDLE hInstall) { MSI_EXCEPTION_HANDLER_PROLOG; MsiInstall msiInstall(hInstall); AppSecInc::Xml::XmlDocument xmlDocument; xmlDocument.LoadXml(msiInstall.GetActionData()); MSXML2::IXMLDOMNodeListPtr rows = xmlDocument.SelectNodes(L"/LocalGroupMembers/LocalGroupMember"); MSXML2::IXMLDOMNodePtr row = NULL; while (NULL != (row = rows->nextNode())) { std::wstring id = xmlDocument.GetAttributeValue(L"id", row); std::wstring username = xmlDocument.GetNodeValue(L"Username", row); std::wstring groupname = xmlDocument.GetNodeValue(L"Group", row, L""); bool add_member = xmlDocument.GetAttributeBoolValue(L"add", row); bool remove_member = xmlDocument.GetAttributeBoolValue(L"remove", row); bool check = xmlDocument.GetAttributeBoolValue(L"check", row); if (remove_member && (! check || AppSecInc::LSA::LocalGroup::IsMember(groupname, username))) { msiInstall.LogInfo(_T(__FUNCTION__), L"Removing \"" + username + L"\" from \"" + groupname + L"\""); AppSecInc::LSA::LocalGroup::DeleteMember(groupname, username); } if (add_member && (! check || ! AppSecInc::LSA::LocalGroup::IsMember(groupname, username))) { msiInstall.LogInfo(_T(__FUNCTION__), L"Adding \"" + username + L"\" to \"" + groupname + L"\""); AppSecInc::LSA::LocalGroup::AddMember(groupname, username); } } MSI_EXCEPTION_HANDLER_EPILOG; return ERROR_SUCCESS; }