| OLD | NEW |
| (Empty) |
| 1 // Copyright 2005-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include <windows.h> | |
| 17 #include <atlstr.h> | |
| 18 #include <vector> | |
| 19 #include "omaha/base/file.h" | |
| 20 #include "omaha/base/module_utils.h" | |
| 21 #include "omaha/base/string.h" | |
| 22 #include "omaha/base/utils.h" | |
| 23 #include "omaha/base/scoped_any.h" | |
| 24 #include "omaha/base/xml_utils.h" | |
| 25 #include "omaha/testing/unit_test.h" | |
| 26 | |
| 27 namespace omaha { | |
| 28 | |
| 29 const TCHAR kTestXMLFile[] = _T("manifest.xml"); | |
| 30 const TCHAR kTempXMLFile[] = _T("foobar.xml"); | |
| 31 | |
| 32 const XMLFQName fqLowNoURI(NULL, _T("Bar")); | |
| 33 const XMLFQName fqLowNoURI2(NULL, _T("Bar")); | |
| 34 const XMLFQName fqHighNoURI(NULL, _T("Foo")); | |
| 35 const XMLFQName fqLowURI(_T("Zebra"), _T("Bar")); | |
| 36 const XMLFQName fqLowURI2(_T("Zebra"), _T("Bar")); | |
| 37 const XMLFQName fqHighURI(_T("Zebra"), _T("Foo")); | |
| 38 const XMLFQName fqDifferentURI(_T("Xray"), _T("Bar")); | |
| 39 | |
| 40 TEST(XmlUtilsTest, XMLFQName) { | |
| 41 ASSERT_TRUE(fqLowNoURI == fqLowNoURI2); | |
| 42 ASSERT_TRUE(fqHighNoURI == fqHighNoURI); | |
| 43 ASSERT_TRUE(fqLowNoURI != fqHighNoURI); | |
| 44 ASSERT_TRUE(fqLowNoURI != fqLowURI); | |
| 45 ASSERT_TRUE(fqLowNoURI < fqHighNoURI); | |
| 46 ASSERT_TRUE(fqLowNoURI <= fqHighNoURI); | |
| 47 ASSERT_TRUE(fqHighNoURI > fqLowNoURI); | |
| 48 ASSERT_TRUE(fqHighNoURI >= fqLowNoURI); | |
| 49 ASSERT_TRUE(fqLowURI == fqLowURI2); | |
| 50 ASSERT_TRUE(fqHighURI == fqHighURI); | |
| 51 ASSERT_TRUE(fqLowURI != fqHighURI); | |
| 52 ASSERT_TRUE(fqLowURI < fqHighURI); | |
| 53 ASSERT_TRUE(fqLowURI <= fqHighURI); | |
| 54 ASSERT_TRUE(fqHighURI > fqLowURI); | |
| 55 ASSERT_TRUE(fqHighURI >= fqLowURI); | |
| 56 ASSERT_TRUE(fqLowURI != fqDifferentURI); | |
| 57 } | |
| 58 | |
| 59 TEST(XmlUtilsTest, LoadSave) { | |
| 60 scoped_co_init co_init; | |
| 61 | |
| 62 // Get some directory and file names to start with. | |
| 63 TCHAR directory[MAX_PATH] = {0}; | |
| 64 ASSERT_TRUE(GetModuleDirectory(NULL, directory)); | |
| 65 CString test_file; | |
| 66 test_file.Format(_T("%s\\unittest_support\\%s"), directory, kTestXMLFile); | |
| 67 | |
| 68 TCHAR temp_path[MAX_PATH] = {0}; | |
| 69 ASSERT_TRUE(::GetTempPath(MAX_PATH, temp_path)); | |
| 70 CString temp_file; | |
| 71 temp_file.AppendFormat(_T("%s%s"), temp_path, kTempXMLFile); | |
| 72 | |
| 73 // Test loading and storing to a file. | |
| 74 CComPtr<IXMLDOMDocument> xmldoc; | |
| 75 ASSERT_SUCCEEDED(LoadXMLFromFile(test_file, true, &xmldoc)); | |
| 76 ASSERT_TRUE(xmldoc); | |
| 77 ASSERT_SUCCEEDED(SaveXMLToFile(xmldoc, temp_file)); | |
| 78 | |
| 79 // Test loading and storing raw UTF8 data to memory. | |
| 80 std::vector<byte> buffer_utf8; | |
| 81 ASSERT_SUCCEEDED(ReadEntireFile(temp_file, 0, &buffer_utf8)); | |
| 82 | |
| 83 CComPtr<IXMLDOMDocument> xmldoc2; | |
| 84 ASSERT_SUCCEEDED(LoadXMLFromRawData(buffer_utf8, true, &xmldoc2)); | |
| 85 ASSERT_TRUE(xmldoc2); | |
| 86 std::vector<byte> xml_utf8; | |
| 87 ASSERT_SUCCEEDED(SaveXMLToRawData(xmldoc2, &xml_utf8)); | |
| 88 | |
| 89 CStringA input_utf8(reinterpret_cast<char*>(&buffer_utf8.front()), | |
| 90 buffer_utf8.size()); | |
| 91 CStringA output_utf8(reinterpret_cast<char*>(&xml_utf8.front()), | |
| 92 xml_utf8.size()); | |
| 93 ASSERT_STREQ(input_utf8, output_utf8); | |
| 94 | |
| 95 // Test loading and storing Unicode to memory. | |
| 96 // The input must be Unicode for the calls below, but our test file is UTF-8. | |
| 97 // So read it and convert it to Unicode. | |
| 98 int len(::MultiByteToWideChar(CP_UTF8, | |
| 99 0, /*flags*/ | |
| 100 reinterpret_cast<const char*>(&buffer_utf8[0]), | |
| 101 buffer_utf8.size(), | |
| 102 NULL, | |
| 103 0)); | |
| 104 std::vector<wchar_t> buffer_unicode(len+1); | |
| 105 int len2(::MultiByteToWideChar(CP_UTF8, | |
| 106 0, /*flags*/ | |
| 107 reinterpret_cast<const char*>(&buffer_utf8[0]), | |
| 108 buffer_utf8.size(), | |
| 109 &buffer_unicode[0], | |
| 110 len)); | |
| 111 ASSERT_EQ(len, len2); | |
| 112 buffer_unicode[len] = 0; // null terminate the unicode string. | |
| 113 | |
| 114 // Now round-trip the load from memory and save to memory. | |
| 115 xmldoc2 = NULL; | |
| 116 ASSERT_SUCCEEDED(LoadXMLFromMemory(&buffer_unicode.front(), true, &xmldoc2)); | |
| 117 ASSERT_TRUE(xmldoc2); | |
| 118 CString xmlmemory; | |
| 119 ASSERT_SUCCEEDED(SaveXMLToMemory(xmldoc2, &xmlmemory)); | |
| 120 | |
| 121 // Now compare that the result of the round-trip is the same as the input. | |
| 122 CString input(&buffer_unicode.front()); | |
| 123 CString output(xmlmemory); | |
| 124 // Except must first remove the " encoding="UTF-8"" attribute from the | |
| 125 // input string. | |
| 126 ReplaceCString(input, L" encoding=\"UTF-8\"", L""); | |
| 127 ASSERT_STREQ(input, output); | |
| 128 | |
| 129 // Clean up. | |
| 130 ASSERT_SUCCEEDED(File::Remove(temp_file)); | |
| 131 } | |
| 132 | |
| 133 } // namespace omaha | |
| 134 | |
| OLD | NEW |