Chromium Code Reviews| 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 <errno.h> | |
| 8 #include <stdlib.h> | |
| 9 #include <sys/mount.h> | |
| 10 | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/process/kill.h" | |
| 15 #include "base/test/multiprocess_test.h" | |
| 16 #include "base/test/scoped_path_override.h" | |
| 17 #include "base/test/test_timeouts.h" | |
| 18 #include "chrome/common/chrome_paths.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "testing/multiprocess_func_list.h" | |
| 21 | |
| 22 namespace chrome { | |
| 23 | |
| 24 class ComponentFlashHintFileTest : public base::MultiProcessTest {}; | |
| 25 | |
| 26 TEST_F(ComponentFlashHintFileTest, ExistsTest) { | |
| 27 const base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); | |
| 28 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); | |
| 29 } | |
| 30 | |
| 31 TEST_F(ComponentFlashHintFileTest, InstallTest) { | |
| 32 const base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); | |
| 33 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); | |
| 34 | |
| 35 base::FilePath flash_dir; | |
| 36 ASSERT_TRUE(PathService::Get( | |
| 37 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); | |
| 38 | |
| 39 base::File::Error error; | |
| 40 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); | |
| 41 | |
| 42 // Write out a fixed byte array as the flash file. | |
| 43 uint8_t file[] = {0x4c, 0x65, 0x74, 0x20, 0x75, 0x73, | |
| 44 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x67}; | |
| 45 flash_dir = flash_dir.Append("libflash.so"); | |
| 46 const std::string flash_version = "1.0.0.1"; | |
| 47 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
|
Bernhard Bauer
2015/08/13 12:40:26
ASSERT_EQ
Greg K
2015/08/13 22:00:45
Done.
| |
| 48 sizeof(file)) == sizeof(file)); | |
| 49 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, | |
| 50 flash_version)); | |
| 51 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); | |
| 52 | |
| 53 // Confirm that the flash plugin can be verified and returned. | |
| 54 base::FilePath returned_flash_path; | |
| 55 std::string version; | |
| 56 ASSERT_TRUE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
| 57 &returned_flash_path, &version)); | |
| 58 ASSERT_TRUE(returned_flash_path == flash_dir); | |
|
Bernhard Bauer
2015/08/13 12:40:27
ASSERT_EQ
Greg K
2015/08/13 22:00:45
Done.
| |
| 59 ASSERT_TRUE(version == flash_version); | |
| 60 | |
| 61 // Now "corrupt" the flash file and make sure the checksum fails and nothing | |
| 62 // is returned. | |
| 63 file[0] = 0xAA; | |
| 64 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
| 65 sizeof(file)) == sizeof(file)); | |
| 66 base::FilePath empty_path; | |
| 67 std::string empty_version; | |
| 68 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
| 69 &empty_path, &empty_version)); | |
| 70 ASSERT_FALSE(empty_path == flash_dir); | |
|
Bernhard Bauer
2015/08/13 12:40:26
ASSERT_NE
Greg K
2015/08/13 22:00:45
Done.
| |
| 71 ASSERT_FALSE(empty_version == flash_version); | |
| 72 } | |
| 73 | |
| 74 TEST_F(ComponentFlashHintFileTest, CorruptionTest) { | |
| 75 const base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); | |
| 76 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); | |
| 77 | |
| 78 base::FilePath flash_dir; | |
| 79 ASSERT_TRUE(PathService::Get( | |
| 80 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); | |
| 81 | |
| 82 base::File::Error error; | |
| 83 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); | |
| 84 flash_dir = flash_dir.Append("libflash.so"); | |
| 85 | |
| 86 const uint8_t file[] = {0x56, 0x61, 0x20, 0x67, 0x75, 0x76, | |
| 87 0x66, 0x20, 0x62, 0x61, 0x72, 0x20}; | |
| 88 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
| 89 sizeof(file)) == sizeof(file)); | |
| 90 const std::string flash_version = "1.0.0.1"; | |
| 91 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, | |
| 92 flash_version)); | |
| 93 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); | |
| 94 | |
| 95 // Now write out a new flash version that will not be moved into place. | |
| 96 const uint8_t updated_file[] = {0x43, 0x72, 0x62, 0x63, 0x79, 0x72, | |
| 97 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76}; | |
| 98 base::FilePath flash_dir_update; | |
| 99 ASSERT_TRUE(PathService::Get( | |
| 100 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir_update)); | |
| 101 flash_dir_update = flash_dir_update.Append("other_flash.so"); | |
| 102 ASSERT_TRUE(base::WriteFile(flash_dir_update, | |
| 103 reinterpret_cast<const char*>(updated_file), | |
| 104 sizeof(updated_file)) == sizeof(updated_file)); | |
| 105 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate( | |
| 106 flash_dir_update, flash_dir, flash_version)); | |
| 107 // flash_dir_update needs to be moved to flash_dir, but if that fails (for the | |
|
Bernhard Bauer
2015/08/13 12:40:26
Nit: Put pipe symbols around variables if you use
Greg K
2015/08/13 22:00:45
This comment actually turns out to be irrelevant n
| |
| 108 // test we don't even try), we need to revert the hint file. | |
| 109 base::FilePath failed_flash_dir; | |
| 110 std::string failed_version; | |
| 111 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
| 112 &failed_flash_dir, &failed_version)); | |
| 113 } | |
| 114 | |
| 115 TEST_F(ComponentFlashHintFileTest, ExecTest1) { | |
| 116 base::ScopedTempDir temp_dir; | |
| 117 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 118 base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
| 119 const uint8_t file[] = {0x55, 0x62, 0x79, 0x71, 0x20, | |
| 120 0x6c, 0x62, 0x68, 0x65, 0x20}; | |
| 121 | |
| 122 ASSERT_TRUE(base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
| 123 sizeof(file)) == sizeof(file)); | |
| 124 ASSERT_TRUE(ComponentFlashHintFile::TestExecutableMapping(file_path)); | |
| 125 } | |
| 126 | |
| 127 MULTIPROCESS_TEST_MAIN(NoExecMountTest) { | |
| 128 if (unshare(CLONE_NEWUSER | CLONE_NEWNS) != 0) { | |
| 129 LOG(ERROR) << "This kernel does not support unpriveleged namespaces. " | |
|
Bernhard Bauer
2015/08/13 12:40:26
Nit: "unprivileged" 😃
Greg K
2015/08/13 22:00:45
Done.
| |
| 130 "ExecTest2 will succeed without running."; | |
| 131 return 0; | |
| 132 } | |
| 133 // Now mount a NOEXEC fs. | |
| 134 const unsigned long tmpfs_flags = MS_NODEV | MS_NOSUID | MS_NOEXEC; | |
| 135 base::ScopedTempDir temp_dir; | |
| 136 CHECK(temp_dir.CreateUniqueTempDir()); | |
| 137 CHECK(mount("tmpfs", temp_dir.path().value().c_str(), "tmpfs", tmpfs_flags, | |
|
Bernhard Bauer
2015/08/13 12:40:26
CHECK_EQ(0, ...)
Greg K
2015/08/13 22:00:45
Done.
| |
| 138 nullptr) == 0); | |
| 139 const base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
| 140 const uint8_t file[] = {0x56, 0x61, 0x20, 0x67, 0x75, 0x72, | |
| 141 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20}; | |
| 142 bool test_exec = false; | |
| 143 bool file_written = | |
| 144 base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
| 145 sizeof(file)) == (int)sizeof(file); | |
|
Bernhard Bauer
2015/08/13 12:40:26
Use static_cast<int>().
Greg K
2015/08/13 22:00:45
Done.
| |
| 146 if (file_written) | |
| 147 test_exec = ComponentFlashHintFile::TestExecutableMapping(file_path); | |
| 148 | |
| 149 if (umount(temp_dir.path().value().c_str()) != 0) | |
| 150 LOG(ERROR) << "Could not unmount directory " << temp_dir.path().value(); | |
| 151 | |
| 152 CHECK(file_written); | |
| 153 CHECK(!test_exec); | |
| 154 return 0; | |
| 155 } | |
| 156 | |
| 157 TEST_F(ComponentFlashHintFileTest, ExecTest2) { | |
| 158 base::Process process = SpawnChild("NoExecMountTest"); | |
| 159 ASSERT_TRUE(process.IsValid()); | |
| 160 int exit_code = 42; | |
| 161 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
|
Bernhard Bauer
2015/08/13 12:40:26
I would probably make this an ASSERT -- if this ti
Greg K
2015/08/13 22:00:45
Done.
| |
| 162 &exit_code)); | |
| 163 EXPECT_EQ(exit_code, 0); | |
|
Bernhard Bauer
2015/08/13 12:40:26
Put the expected value first.
Greg K
2015/08/13 22:00:45
Done.
| |
| 164 } | |
| 165 | |
| 166 } // namespace chrome | |
| OLD | NEW |