| 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 // Unittests for pe_utils | |
| 17 | |
| 18 | |
| 19 #include "omaha/base/debug.h" | |
| 20 #include "omaha/base/file.h" | |
| 21 #include "omaha/base/pe_utils.h" | |
| 22 #include "omaha/base/utils.h" | |
| 23 #include "omaha/testing/unit_test.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 TEST(PEUtilsTest, PEUtils) { | |
| 28 // Get some known directories | |
| 29 CString windows_dir; | |
| 30 CString temp_dir; | |
| 31 DWORD dw = ::GetEnvironmentVariable(L"SystemRoot", | |
| 32 CStrBuf(windows_dir, MAX_PATH), MAX_PATH); | |
| 33 ASSERT_TRUE(dw); | |
| 34 dw = ::GetEnvironmentVariable(L"TEMP", CStrBuf(temp_dir, MAX_PATH), MAX_PATH); | |
| 35 ASSERT_TRUE(dw); | |
| 36 | |
| 37 // Get a known executable to play with | |
| 38 CString notepad(windows_dir + L"\\NOTEPAD.EXE"); | |
| 39 ASSERT_TRUE(File::Exists(notepad)); | |
| 40 | |
| 41 CString temp_exe(temp_dir + L"\\pe_utils_test.exe"); | |
| 42 if (File::Exists(temp_exe)) { | |
| 43 File::Remove(temp_exe); | |
| 44 } | |
| 45 ASSERT_FALSE(File::Exists(temp_exe)); | |
| 46 ASSERT_SUCCEEDED(File::Copy(notepad, temp_exe, true)); | |
| 47 | |
| 48 // Stomp on its checksum and check the result | |
| 49 const unsigned int kChk1 = 0xFEE1BAD; | |
| 50 const unsigned int kChk2 = 0x600DF00D; | |
| 51 | |
| 52 unsigned int checksum = 0; | |
| 53 | |
| 54 // Test Get/SetPEChecksum | |
| 55 ASSERT_SUCCEEDED(SetPEChecksum(temp_exe, kChk1)); | |
| 56 ASSERT_SUCCEEDED(GetPEChecksum(temp_exe, &checksum)); | |
| 57 ASSERT_EQ(kChk1, checksum); | |
| 58 | |
| 59 ASSERT_SUCCEEDED(SetPEChecksum(temp_exe, kChk2)); | |
| 60 ASSERT_SUCCEEDED(GetPEChecksum(temp_exe, &checksum)); | |
| 61 ASSERT_EQ(kChk2, checksum); | |
| 62 | |
| 63 // Test GetPEChecksumFromBuffer/SetPEChecksumToBuffer | |
| 64 std::vector<byte> buffer; | |
| 65 ASSERT_SUCCEEDED(ReadEntireFile(temp_exe, 0, &buffer)); | |
| 66 | |
| 67 int buffer_data_len = buffer.size(); | |
| 68 uint8 *buffer_data = reinterpret_cast<uint8*>(&buffer.front()); | |
| 69 | |
| 70 ASSERT_SUCCEEDED(SetPEChecksumToBuffer(buffer_data, buffer_data_len, kChk1)); | |
| 71 ASSERT_SUCCEEDED(GetPEChecksumFromBuffer(buffer_data, | |
| 72 buffer_data_len, | |
| 73 &checksum)); | |
| 74 ASSERT_EQ(kChk1, checksum); | |
| 75 | |
| 76 ASSERT_SUCCEEDED(SetPEChecksumToBuffer(buffer_data, buffer_data_len, kChk2)); | |
| 77 ASSERT_SUCCEEDED(GetPEChecksumFromBuffer(buffer_data, | |
| 78 buffer_data_len, | |
| 79 &checksum)); | |
| 80 ASSERT_EQ(kChk2, checksum); | |
| 81 | |
| 82 // Clean up | |
| 83 ASSERT_SUCCEEDED(File::Remove(temp_exe)); | |
| 84 } | |
| 85 | |
| 86 } // namespace omaha | |
| 87 | |
| OLD | NEW |