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[] = { | |
44 0x4c, 0x65, 0x74, 0x20, 0x75, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x67, | |
45 0x6f, 0x20, 0x67, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x0a, 0x54, 0x6f, 0x20, | |
46 0x74, 0x68, 0x65, 0x20, 0x65, 0x6e, 0x64, 0x6c, 0x65, 0x73, 0x73, 0x20, | |
47 0x77, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x20, 0x6e, 0x69, 0x67, 0x68, 0x74, | |
48 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x72, 0x65, 0x64, | |
49 0x20, 0x74, 0x69, 0x64, 0x65, 0x20, 0x6b, 0x69, 0x73, 0x73, 0x65, 0x73, | |
50 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x68, 0x6f, 0x72, 0x65}; | |
51 flash_dir = flash_dir.Append("libflash.so"); | |
52 const std::string flash_version = "1.0.0.1"; | |
53 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
54 sizeof(file)) == sizeof(file)); | |
55 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, | |
56 flash_version)); | |
57 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); | |
58 | |
59 // Confirm that the flash plugin can be verified and returned. | |
60 base::FilePath returned_flash_path; | |
61 std::string version; | |
62 ASSERT_TRUE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
63 &returned_flash_path, &version)); | |
64 ASSERT_TRUE(returned_flash_path == flash_dir); | |
65 ASSERT_TRUE(version == flash_version); | |
66 | |
67 // Now "corrupt" the flash file and make sure the checksum fails and nothing | |
68 // is returned. | |
69 file[0] = 0xAA; | |
70 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
71 sizeof(file)) == sizeof(file)); | |
72 base::FilePath empty_path; | |
73 std::string empty_version; | |
74 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
75 &empty_path, &empty_version)); | |
76 ASSERT_FALSE(empty_path == flash_dir); | |
77 ASSERT_FALSE(empty_version == flash_version); | |
78 } | |
79 | |
80 TEST_F(ComponentFlashHintFileTest, CorruptionTest) { | |
81 const base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); | |
82 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); | |
83 | |
84 base::FilePath flash_dir; | |
85 ASSERT_TRUE(PathService::Get( | |
86 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); | |
87 | |
88 base::File::Error error; | |
89 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); | |
90 flash_dir = flash_dir.Append("libflash.so"); | |
91 | |
92 const uint8_t file[] = { | |
93 0x56, 0x61, 0x20, 0x67, 0x75, 0x76, 0x66, 0x20, 0x62, 0x61, 0x72, 0x20, | |
94 0x62, 0x73, 0x20, 0x7a, 0x6e, 0x61, 0x6c, 0x20, 0x63, 0x62, 0x66, 0x66, | |
95 0x76, 0x6f, 0x79, 0x72, 0x20, 0x6a, 0x62, 0x65, 0x79, 0x71, 0x66, 0x2c, | |
96 0xa, 0x4e, 0x79, 0x79, 0x20, 0x73, 0x62, 0x65, 0x20, 0x67, 0x75, 0x72, | |
97 0x20, 0x6f, 0x72, 0x66, 0x67, 0x20, 0x62, 0x65, 0x20, 0x66, 0x62, 0x7a, | |
98 0x72, 0x20, 0x6f, 0x76, 0x6d, 0x6e, 0x65, 0x65, 0x72, 0x20, 0x67, 0x72, | |
99 0x66, 0x67, 0x3f, 0xa, 0x56, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6a, 0x75, | |
100 0x6e, 0x67, 0x20, 0x76, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6e, 0x61, 0x71, | |
101 0x20, 0x6a, 0x75, 0x6e, 0x67, 0x72, 0x69, 0x72, 0x65, 0x2c, 0xa, 0x47, | |
102 0x76, 0x7a, 0x72, 0x20, 0x76, 0x66, 0x20, 0x66, 0x67, 0x76, 0x79, 0x79, | |
103 0x20, 0x67, 0x75, 0x72, 0x20, 0x76, 0x61, 0x73, 0x76, 0x61, 0x76, 0x67, | |
104 0x72, 0x20, 0x77, 0x72, 0x66, 0x67, 0xa, 0x47, 0x75, 0x72, 0x20, 0x73, | |
105 0x68, 0x67, 0x68, 0x65, 0x72, 0x20, 0x71, 0x76, 0x66, 0x6e, 0x63, 0x63, | |
106 0x72, 0x6e, 0x65, 0x66, 0x20, 0x76, 0x61, 0x67, 0x62, 0x20, 0x7a, 0x72, | |
107 0x7a, 0x62, 0x65, 0x6c, 0xa, 0x4a, 0x76, 0x67, 0x75, 0x20, 0x62, 0x61, | |
108 0x79, 0x6c, 0x20, 0x6e, 0x20, 0x7a, 0x62, 0x7a, 0x72, 0x61, 0x67, 0x20, | |
109 0x6f, 0x72, 0x67, 0x6a, 0x72, 0x72, 0x61, 0x2e, 0xa, 0x53, 0x62, 0x65, | |
110 0x72, 0x69, 0x72, 0x65, 0x20, 0x71, 0x6a, 0x72, 0x79, 0x79, 0x66, 0x20, | |
111 0x76, 0x61, 0x20, 0x67, 0x75, 0x6e, 0x67, 0x20, 0x7a, 0x62, 0x7a, 0x72, | |
112 0x61, 0x67, 0x2c, 0xa, 0x55, 0x62, 0x63, 0x72, 0x20, 0x76, 0x66, 0x20, | |
113 0x6a, 0x75, 0x6e, 0x67, 0x20, 0x65, 0x72, 0x7a, 0x6e, 0x76, 0x61, 0x66, | |
114 0x20, 0x67, 0x62, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x72, 0x72, 0x61, 0x2e, | |
115 0x0}; | |
116 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
117 sizeof(file)) == sizeof(file)); | |
118 const std::string flash_version = "1.0.0.1"; | |
119 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, | |
120 flash_version)); | |
121 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); | |
122 | |
123 // Now write out a new flash version that will not be moved into place. | |
124 const uint8_t updated_file[] = { | |
125 0x43, 0x72, 0x62, 0x63, 0x79, 0x72, 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76, | |
126 0x61, 0x74, 0x20, 0x67, 0x75, 0x65, 0x62, 0x68, 0x74, 0x75, 0x20, 0x67, | |
127 0x75, 0x72, 0x76, 0x65, 0x20, 0x67, 0x72, 0x6e, 0x65, 0x66, 0xa, 0x4a, | |
128 0x75, 0x62, 0x20, 0x70, 0x6e, 0x61, 0x20, 0x74, 0x76, 0x69, 0x72, 0x20, | |
129 0x67, 0x75, 0x72, 0x7a, 0x20, 0x6f, 0x6e, 0x70, 0x78, 0x20, 0x67, 0x75, | |
130 0x72, 0x76, 0x65, 0x20, 0x79, 0x76, 0x69, 0x72, 0x66, 0xa, 0x4e, 0x61, | |
131 0x71, 0x20, 0x6e, 0x79, 0x79, 0x20, 0x67, 0x75, 0x62, 0x66, 0x72, 0x20, | |
132 0x6a, 0x6e, 0x66, 0x67, 0x72, 0x71, 0x20, 0x6c, 0x72, 0x6e, 0x65, 0x66, | |
133 0x3f, 0xa, 0x4a, 0x75, 0x62, 0x20, 0x6a, 0x76, 0x79, 0x79, 0x20, 0x63, | |
134 0x6e, 0x6c, 0x3f, 0x0}; | |
135 base::FilePath flash_dir_update; | |
136 ASSERT_TRUE(PathService::Get( | |
137 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir_update)); | |
138 flash_dir_update = flash_dir_update.Append("other_flash.so"); | |
139 ASSERT_TRUE(base::WriteFile(flash_dir_update, | |
140 reinterpret_cast<const char*>(updated_file), | |
141 sizeof(updated_file)) == sizeof(updated_file)); | |
142 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate( | |
143 flash_dir_update, flash_dir, flash_version)); | |
144 // flash_dir_update needs to be moved to flash_dir, but if that fails (for the | |
145 // test we don't even try), we need to revert the hint file. | |
146 base::FilePath failed_flash_dir; | |
147 std::string failed_version; | |
148 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
149 &failed_flash_dir, &failed_version)); | |
150 } | |
151 | |
152 TEST_F(ComponentFlashHintFileTest, ExecTest1) { | |
153 base::ScopedTempDir temp_dir; | |
154 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
155 base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
156 const uint8_t file[] = { | |
157 0x55, 0x62, 0x79, 0x71, 0x20, 0x6c, 0x62, 0x68, 0x65, 0x20, | |
158 0x73, 0x76, 0x65, 0x72, 0x58, 0x72, 0x72, 0x63, 0x20, 0x76, | |
159 0x67, 0x20, 0x6f, 0x68, 0x65, 0x61, 0x76, 0x61, 0x74, 0x20, | |
160 0x6f, 0x65, 0x76, 0x74, 0x75, 0x67, 0x0, | |
161 }; | |
162 | |
163 ASSERT_TRUE(base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
164 sizeof(file)) == sizeof(file)); | |
165 ASSERT_TRUE(ComponentFlashHintFile::TestExecutableMapping(file_path)); | |
166 } | |
167 | |
168 MULTIPROCESS_TEST_MAIN(NoExecMountTest) { | |
169 if (unshare(CLONE_NEWUSER | CLONE_NEWNS) != 0) { | |
170 LOG(ERROR) << "This kernel does not support unpriveleged namespaces. " | |
171 "ExecTest2 will succeed without running."; | |
172 return 0; | |
173 } | |
174 // Now mount a NOEXEC fs. | |
175 const unsigned long tmpfs_flags = MS_NODEV | MS_NOSUID | MS_NOEXEC; | |
176 base::ScopedTempDir temp_dir; | |
177 CHECK(temp_dir.CreateUniqueTempDir()); | |
178 CHECK(mount("tmpfs", temp_dir.path().value().c_str(), "tmpfs", tmpfs_flags, | |
179 nullptr) == 0); | |
180 const base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
181 const uint8_t file[] = { | |
182 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, | |
183 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x61, 0x62, 0x6f, 0x62, 0x71, 0x6c, | |
184 0x20, 0x66, 0x7a, 0x76, 0x79, 0x72, 0x66, 0x4e, 0x61, 0x71, 0x20, 0x61, | |
185 0x62, 0x6f, 0x62, 0x71, 0x6c, 0x20, 0x71, 0x65, 0x72, 0x6e, 0x7a, 0x66, | |
186 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, | |
187 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x71, 0x72, 0x66, 0x63, 0x72, 0x65, | |
188 0x6e, 0x67, 0x76, 0x62, 0x61, 0x51, 0x65, 0x76, 0x69, 0x72, 0x66, 0x20, | |
189 0x67, 0x75, 0x72, 0x20, 0x6f, 0x62, 0x65, 0x72, 0x71, 0x20, 0x67, 0x62, | |
190 0x20, 0x72, 0x6b, 0x67, 0x65, 0x72, 0x7a, 0x72, 0x66, 0x57, 0x68, 0x66, | |
191 0x67, 0x20, 0x62, 0x61, 0x72, 0x20, 0x66, 0x63, 0x6e, 0x65, 0x78, 0x20, | |
192 0x62, 0x73, 0x20, 0x71, 0x72, 0x70, 0x72, 0x61, 0x70, 0x6c, 0x4e, 0x74, | |
193 0x6e, 0x76, 0x61, 0x66, 0x67, 0x20, 0x6e, 0x20, 0x66, 0x67, 0x6e, 0x65, | |
194 0x79, 0x72, 0x66, 0x66, 0x20, 0x61, 0x76, 0x74, 0x75, 0x67, 0x42, 0x61, | |
195 0x72, 0x20, 0x74, 0x79, 0x62, 0x6a, 0x20, 0x62, 0x73, 0x20, 0x75, 0x62, | |
196 0x63, 0x72, 0x20, 0x6e, 0x61, 0x71, 0x20, 0x71, 0x76, 0x74, 0x61, 0x76, | |
197 0x67, 0x6c, 0x4e, 0x20, 0x70, 0x75, 0x76, 0x79, 0x71, 0x20, 0x70, 0x6e, | |
198 0x61, 0x20, 0x73, 0x62, 0x79, 0x79, 0x62, 0x6a, 0x20, 0x67, 0x75, 0x72, | |
199 0x20, 0x79, 0x76, 0x74, 0x75, 0x67, 0x0, | |
200 }; | |
201 EXPECT_EQ(base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
jln (very slow on Chromium)
2015/08/07 22:27:20
Are you sure EXPECT_* macros work in MULTIPROCESS_
Greg K
2015/08/08 00:23:57
Indeed, it actually needs to crash, so I have rest
| |
202 sizeof(file)), | |
203 (int)sizeof(file)); | |
204 EXPECT_FALSE(ComponentFlashHintFile::TestExecutableMapping(file_path)); | |
205 EXPECT_TRUE(umount(temp_dir.path().value().c_str()) == 0); | |
206 return 0; | |
207 } | |
208 | |
209 TEST_F(ComponentFlashHintFileTest, ExecTest2) { | |
210 base::Process process = SpawnChild("NoExecMountTest"); | |
211 ASSERT_TRUE(process.IsValid()); | |
212 int exit_code = 42; | |
213 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
214 &exit_code)); | |
215 EXPECT_EQ(exit_code, 0); | |
216 } | |
217 | |
218 } // namespace chrome | |
OLD | NEW |