OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/component_flash_hint_file.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" |
| 11 #include "base/test/scoped_path_override.h" |
| 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace chrome { |
| 16 |
| 17 class ComponentFlashHintFileTest : public testing::Test {}; |
| 18 |
| 19 TEST_F(ComponentFlashHintFileTest, ExistsTest) { |
| 20 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 21 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 22 } |
| 23 |
| 24 TEST_F(ComponentFlashHintFileTest, InstallTest) { |
| 25 ComponentFlashHintFile hint_file; |
| 26 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 27 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 28 |
| 29 base::FilePath flash_dir; |
| 30 ASSERT_TRUE(PathService::Get( |
| 31 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); |
| 32 |
| 33 base::File::Error error; |
| 34 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); |
| 35 |
| 36 // Write out a fixed byte array as the flash file. |
| 37 uint8_t file[] = { |
| 38 0x4c, 0x65, 0x74, 0x20, 0x75, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x67, |
| 39 0x6f, 0x20, 0x67, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x0a, 0x54, 0x6f, 0x20, |
| 40 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x64, 0x6c, 0x65, 0x73, 0x73, 0x20, |
| 41 0x77, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x20, 0x6e, 0x69, 0x67, 0x68, 0x74, |
| 42 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x64, |
| 43 0x20, 0x74, 0x69, 0x64, 0x65, 0x20, 0x6b, 0x69, 0x73, 0x73, 0x65, 0x73, |
| 44 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x68, 0x6f, 0x72, 0x65}; |
| 45 flash_dir = flash_dir.Append("libflash.so"); |
| 46 std::string flash_version = "1.0.0.1"; |
| 47 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 48 sizeof(file)) == sizeof(file)); |
| 49 ASSERT_TRUE(hint_file.RecordFlashUpdate(flash_dir, flash_dir, flash_version)); |
| 50 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); |
| 51 |
| 52 // Confirm that the flash plugin can be verified and returned. |
| 53 base::FilePath returned_flash_path; |
| 54 std::string version; |
| 55 ASSERT_TRUE( |
| 56 hint_file.VerifyAndReturnFlashLocation(&returned_flash_path, &version)); |
| 57 ASSERT_TRUE(returned_flash_path == flash_dir); |
| 58 ASSERT_TRUE(version == flash_version); |
| 59 |
| 60 // Now "corrupt" the flash file and make sure the checksum fails and nothing |
| 61 // is returned. |
| 62 srand48(time(NULL) ^ getpid()); |
| 63 int idx = lrand48() % ((sizeof(file) / sizeof(file[0])) - 1); |
| 64 if (file[idx] == std::numeric_limits<uint8_t>::max()) |
| 65 file[idx] = 0x00; |
| 66 else |
| 67 file[idx] = file[idx] + 1; |
| 68 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 69 sizeof(file)) == sizeof(file)); |
| 70 base::FilePath empty_path; |
| 71 std::string empty_version; |
| 72 ASSERT_FALSE( |
| 73 hint_file.VerifyAndReturnFlashLocation(&empty_path, &empty_version)); |
| 74 ASSERT_FALSE(empty_path == flash_dir); |
| 75 ASSERT_FALSE(empty_version == flash_version); |
| 76 } |
| 77 |
| 78 TEST_F(ComponentFlashHintFileTest, RevertTest) { |
| 79 ComponentFlashHintFile hint_file; |
| 80 base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); |
| 81 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); |
| 82 |
| 83 base::FilePath flash_dir; |
| 84 ASSERT_TRUE(PathService::Get( |
| 85 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); |
| 86 |
| 87 base::File::Error error; |
| 88 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); |
| 89 flash_dir = flash_dir.Append("libflash.so"); |
| 90 |
| 91 const uint8_t file[] = { |
| 92 0x56, 0x61, 0x20, 0x67, 0x75, 0x76, 0x66, 0x20, 0x62, 0x61, 0x72, 0x20, |
| 93 0x62, 0x73, 0x20, 0x7a, 0x6e, 0x61, 0x6c, 0x20, 0x63, 0x62, 0x66, 0x66, |
| 94 0x76, 0x6f, 0x79, 0x72, 0x20, 0x6a, 0x62, 0x65, 0x79, 0x71, 0x66, 0x2c, |
| 95 0xa, 0x4e, 0x79, 0x79, 0x20, 0x73, 0x62, 0x65, 0x20, 0x67, 0x75, 0x72, |
| 96 0x20, 0x6f, 0x72, 0x66, 0x67, 0x20, 0x62, 0x65, 0x20, 0x66, 0x62, 0x7a, |
| 97 0x72, 0x20, 0x6f, 0x76, 0x6d, 0x6e, 0x65, 0x65, 0x72, 0x20, 0x67, 0x72, |
| 98 0x66, 0x67, 0x3f, 0xa, 0x56, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6a, 0x75, |
| 99 0x6e, 0x67, 0x20, 0x76, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6e, 0x61, 0x71, |
| 100 0x20, 0x6a, 0x75, 0x6e, 0x67, 0x72, 0x69, 0x72, 0x65, 0x2c, 0xa, 0x47, |
| 101 0x76, 0x7a, 0x72, 0x20, 0x76, 0x66, 0x20, 0x66, 0x67, 0x76, 0x79, 0x79, |
| 102 0x20, 0x67, 0x75, 0x72, 0x20, 0x76, 0x61, 0x73, 0x76, 0x61, 0x76, 0x67, |
| 103 0x72, 0x20, 0x77, 0x72, 0x66, 0x67, 0xa, 0x47, 0x75, 0x72, 0x20, 0x73, |
| 104 0x68, 0x67, 0x68, 0x65, 0x72, 0x20, 0x71, 0x76, 0x66, 0x6e, 0x63, 0x63, |
| 105 0x72, 0x6e, 0x65, 0x66, 0x20, 0x76, 0x61, 0x67, 0x62, 0x20, 0x7a, 0x72, |
| 106 0x7a, 0x62, 0x65, 0x6c, 0xa, 0x4a, 0x76, 0x67, 0x75, 0x20, 0x62, 0x61, |
| 107 0x79, 0x6c, 0x20, 0x6e, 0x20, 0x7a, 0x62, 0x7a, 0x72, 0x61, 0x67, 0x20, |
| 108 0x6f, 0x72, 0x67, 0x6a, 0x72, 0x72, 0x61, 0x2e, 0xa, 0x53, 0x62, 0x65, |
| 109 0x72, 0x69, 0x72, 0x65, 0x20, 0x71, 0x6a, 0x72, 0x79, 0x79, 0x66, 0x20, |
| 110 0x76, 0x61, 0x20, 0x67, 0x75, 0x6e, 0x67, 0x20, 0x7a, 0x62, 0x7a, 0x72, |
| 111 0x61, 0x67, 0x2c, 0xa, 0x55, 0x62, 0x63, 0x72, 0x20, 0x76, 0x66, 0x20, |
| 112 0x6a, 0x75, 0x6e, 0x67, 0x20, 0x65, 0x72, 0x7a, 0x6e, 0x76, 0x61, 0x66, |
| 113 0x20, 0x67, 0x62, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x72, 0x72, 0x61, 0x2e, |
| 114 0x0}; |
| 115 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), |
| 116 sizeof(file)) == sizeof(file)); |
| 117 std::string flash_version = "1.0.0.1"; |
| 118 ASSERT_TRUE(hint_file.RecordFlashUpdate(flash_dir, flash_dir, flash_version)); |
| 119 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); |
| 120 |
| 121 // Now write out a new flash version that will not be moved into place. |
| 122 const uint8_t updated_file[] = { |
| 123 0x43, 0x72, 0x62, 0x63, 0x79, 0x72, 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76, |
| 124 0x61, 0x74, 0x20, 0x67, 0x75, 0x65, 0x62, 0x68, 0x74, 0x75, 0x20, 0x67, |
| 125 0x75, 0x72, 0x76, 0x65, 0x20, 0x67, 0x72, 0x6e, 0x65, 0x66, 0xa, 0x4a, |
| 126 0x75, 0x62, 0x20, 0x70, 0x6e, 0x61, 0x20, 0x74, 0x76, 0x69, 0x72, 0x20, |
| 127 0x67, 0x75, 0x72, 0x7a, 0x20, 0x6f, 0x6e, 0x70, 0x78, 0x20, 0x67, 0x75, |
| 128 0x72, 0x76, 0x65, 0x20, 0x79, 0x76, 0x69, 0x72, 0x66, 0xa, 0x4e, 0x61, |
| 129 0x71, 0x20, 0x6e, 0x79, 0x79, 0x20, 0x67, 0x75, 0x62, 0x66, 0x72, 0x20, |
| 130 0x6a, 0x6e, 0x66, 0x67, 0x72, 0x71, 0x20, 0x6c, 0x72, 0x6e, 0x65, 0x66, |
| 131 0x3f, 0xa, 0x4a, 0x75, 0x62, 0x20, 0x6a, 0x76, 0x79, 0x79, 0x20, 0x63, |
| 132 0x6e, 0x6c, 0x3f, 0x0}; |
| 133 base::FilePath flash_dir_update; |
| 134 ASSERT_TRUE(PathService::Get( |
| 135 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir_update)); |
| 136 flash_dir_update = flash_dir_update.Append("other_flash.so"); |
| 137 ASSERT_TRUE(base::WriteFile(flash_dir_update, |
| 138 reinterpret_cast<const char*>(updated_file), |
| 139 sizeof(updated_file)) == sizeof(updated_file)); |
| 140 ASSERT_TRUE( |
| 141 hint_file.RecordFlashUpdate(flash_dir_update, flash_dir, flash_version)); |
| 142 // flash_dir_update needs to be moved to flash_dir, but if that fails (for the |
| 143 // test we don't even try), we need to revert the hint file. |
| 144 base::FilePath failed_flash_dir; |
| 145 std::string failed_version; |
| 146 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
| 147 &failed_flash_dir, &failed_version)); |
| 148 // Revert to the good version of the file. |
| 149 ASSERT_TRUE(hint_file.Revert()); |
| 150 ASSERT_TRUE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
| 151 &failed_flash_dir, &failed_version)); |
| 152 ASSERT_TRUE(failed_flash_dir == flash_dir); |
| 153 ASSERT_TRUE(failed_version == flash_version); |
| 154 } |
| 155 |
| 156 } // namespace chrome |
OLD | NEW |