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 srand48(time(NULL) ^ getpid()); | |
jln (very slow on Chromium)
2015/08/06 18:48:15
Unit tests should reproduce 100% of the time. If y
Greg K
2015/08/07 21:15:29
Done.
| |
70 const int idx = lrand48() % ((sizeof(file) / sizeof(file[0])) - 1); | |
71 if (file[idx] == std::numeric_limits<uint8_t>::max()) | |
72 file[idx] = 0x00; | |
73 else | |
74 file[idx] = file[idx] + 1; | |
75 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
76 sizeof(file)) == sizeof(file)); | |
77 base::FilePath empty_path; | |
78 std::string empty_version; | |
79 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
80 &empty_path, &empty_version)); | |
81 ASSERT_FALSE(empty_path == flash_dir); | |
82 ASSERT_FALSE(empty_version == flash_version); | |
83 } | |
84 | |
85 TEST_F(ComponentFlashHintFileTest, CorruptionTest) { | |
86 const base::ScopedPathOverride path_override(chrome::DIR_USER_DATA); | |
87 EXPECT_FALSE(ComponentFlashHintFile::DoesHintFileExist()); | |
88 | |
89 base::FilePath flash_dir; | |
90 ASSERT_TRUE(PathService::Get( | |
91 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir)); | |
92 | |
93 base::File::Error error; | |
94 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir, &error)); | |
95 flash_dir = flash_dir.Append("libflash.so"); | |
96 | |
97 const uint8_t file[] = { | |
98 0x56, 0x61, 0x20, 0x67, 0x75, 0x76, 0x66, 0x20, 0x62, 0x61, 0x72, 0x20, | |
99 0x62, 0x73, 0x20, 0x7a, 0x6e, 0x61, 0x6c, 0x20, 0x63, 0x62, 0x66, 0x66, | |
100 0x76, 0x6f, 0x79, 0x72, 0x20, 0x6a, 0x62, 0x65, 0x79, 0x71, 0x66, 0x2c, | |
101 0xa, 0x4e, 0x79, 0x79, 0x20, 0x73, 0x62, 0x65, 0x20, 0x67, 0x75, 0x72, | |
102 0x20, 0x6f, 0x72, 0x66, 0x67, 0x20, 0x62, 0x65, 0x20, 0x66, 0x62, 0x7a, | |
103 0x72, 0x20, 0x6f, 0x76, 0x6d, 0x6e, 0x65, 0x65, 0x72, 0x20, 0x67, 0x72, | |
104 0x66, 0x67, 0x3f, 0xa, 0x56, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6a, 0x75, | |
105 0x6e, 0x67, 0x20, 0x76, 0x67, 0x20, 0x76, 0x66, 0x20, 0x6e, 0x61, 0x71, | |
106 0x20, 0x6a, 0x75, 0x6e, 0x67, 0x72, 0x69, 0x72, 0x65, 0x2c, 0xa, 0x47, | |
107 0x76, 0x7a, 0x72, 0x20, 0x76, 0x66, 0x20, 0x66, 0x67, 0x76, 0x79, 0x79, | |
108 0x20, 0x67, 0x75, 0x72, 0x20, 0x76, 0x61, 0x73, 0x76, 0x61, 0x76, 0x67, | |
109 0x72, 0x20, 0x77, 0x72, 0x66, 0x67, 0xa, 0x47, 0x75, 0x72, 0x20, 0x73, | |
110 0x68, 0x67, 0x68, 0x65, 0x72, 0x20, 0x71, 0x76, 0x66, 0x6e, 0x63, 0x63, | |
111 0x72, 0x6e, 0x65, 0x66, 0x20, 0x76, 0x61, 0x67, 0x62, 0x20, 0x7a, 0x72, | |
112 0x7a, 0x62, 0x65, 0x6c, 0xa, 0x4a, 0x76, 0x67, 0x75, 0x20, 0x62, 0x61, | |
113 0x79, 0x6c, 0x20, 0x6e, 0x20, 0x7a, 0x62, 0x7a, 0x72, 0x61, 0x67, 0x20, | |
114 0x6f, 0x72, 0x67, 0x6a, 0x72, 0x72, 0x61, 0x2e, 0xa, 0x53, 0x62, 0x65, | |
115 0x72, 0x69, 0x72, 0x65, 0x20, 0x71, 0x6a, 0x72, 0x79, 0x79, 0x66, 0x20, | |
116 0x76, 0x61, 0x20, 0x67, 0x75, 0x6e, 0x67, 0x20, 0x7a, 0x62, 0x7a, 0x72, | |
117 0x61, 0x67, 0x2c, 0xa, 0x55, 0x62, 0x63, 0x72, 0x20, 0x76, 0x66, 0x20, | |
118 0x6a, 0x75, 0x6e, 0x67, 0x20, 0x65, 0x72, 0x7a, 0x6e, 0x76, 0x61, 0x66, | |
119 0x20, 0x67, 0x62, 0x20, 0x6f, 0x72, 0x20, 0x66, 0x72, 0x72, 0x61, 0x2e, | |
120 0x0}; | |
121 ASSERT_TRUE(base::WriteFile(flash_dir, reinterpret_cast<const char*>(file), | |
122 sizeof(file)) == sizeof(file)); | |
123 const std::string flash_version = "1.0.0.1"; | |
124 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate(flash_dir, flash_dir, | |
125 flash_version)); | |
126 ASSERT_TRUE(ComponentFlashHintFile::DoesHintFileExist()); | |
127 | |
128 // Now write out a new flash version that will not be moved into place. | |
129 const uint8_t updated_file[] = { | |
130 0x43, 0x72, 0x62, 0x63, 0x79, 0x72, 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76, | |
131 0x61, 0x74, 0x20, 0x67, 0x75, 0x65, 0x62, 0x68, 0x74, 0x75, 0x20, 0x67, | |
132 0x75, 0x72, 0x76, 0x65, 0x20, 0x67, 0x72, 0x6e, 0x65, 0x66, 0xa, 0x4a, | |
133 0x75, 0x62, 0x20, 0x70, 0x6e, 0x61, 0x20, 0x74, 0x76, 0x69, 0x72, 0x20, | |
134 0x67, 0x75, 0x72, 0x7a, 0x20, 0x6f, 0x6e, 0x70, 0x78, 0x20, 0x67, 0x75, | |
135 0x72, 0x76, 0x65, 0x20, 0x79, 0x76, 0x69, 0x72, 0x66, 0xa, 0x4e, 0x61, | |
136 0x71, 0x20, 0x6e, 0x79, 0x79, 0x20, 0x67, 0x75, 0x62, 0x66, 0x72, 0x20, | |
137 0x6a, 0x6e, 0x66, 0x67, 0x72, 0x71, 0x20, 0x6c, 0x72, 0x6e, 0x65, 0x66, | |
138 0x3f, 0xa, 0x4a, 0x75, 0x62, 0x20, 0x6a, 0x76, 0x79, 0x79, 0x20, 0x63, | |
139 0x6e, 0x6c, 0x3f, 0x0}; | |
140 base::FilePath flash_dir_update; | |
141 ASSERT_TRUE(PathService::Get( | |
142 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &flash_dir_update)); | |
143 flash_dir_update = flash_dir_update.Append("other_flash.so"); | |
144 ASSERT_TRUE(base::WriteFile(flash_dir_update, | |
145 reinterpret_cast<const char*>(updated_file), | |
146 sizeof(updated_file)) == sizeof(updated_file)); | |
147 ASSERT_TRUE(ComponentFlashHintFile::RecordFlashUpdate( | |
148 flash_dir_update, flash_dir, flash_version)); | |
149 // flash_dir_update needs to be moved to flash_dir, but if that fails (for the | |
150 // test we don't even try), we need to revert the hint file. | |
151 base::FilePath failed_flash_dir; | |
152 std::string failed_version; | |
153 ASSERT_FALSE(ComponentFlashHintFile::VerifyAndReturnFlashLocation( | |
154 &failed_flash_dir, &failed_version)); | |
155 } | |
156 | |
157 TEST_F(ComponentFlashHintFileTest, ExecTest1) { | |
158 base::ScopedTempDir temp_dir; | |
159 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
160 base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
161 const uint8_t file[] = { | |
162 0x55, 0x62, 0x79, 0x71, 0x20, 0x6c, 0x62, 0x68, 0x65, 0x20, | |
163 0x73, 0x76, 0x65, 0x72, 0x58, 0x72, 0x72, 0x63, 0x20, 0x76, | |
164 0x67, 0x20, 0x6f, 0x68, 0x65, 0x61, 0x76, 0x61, 0x74, 0x20, | |
165 0x6f, 0x65, 0x76, 0x74, 0x75, 0x67, 0x0, | |
166 }; | |
167 | |
168 ASSERT_TRUE(base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
169 sizeof(file)) == sizeof(file)); | |
170 ASSERT_TRUE(ComponentFlashHintFile::TestExecutableMapping(file_path)); | |
171 } | |
172 | |
173 MULTIPROCESS_TEST_MAIN(NoExecMountTest) { | |
174 if (unshare(CLONE_NEWUSER | CLONE_NEWNS) != 0) { | |
175 LOG(ERROR) << "This kernel does not support unpriveleged namespaces. " | |
176 "ExecTest2 will succeed without running."; | |
177 return 0; | |
178 } | |
179 // Now mount a NOEXEC fs. | |
180 const unsigned long tmpfs_flags = MS_NODEV | MS_NOSUID | MS_NOEXEC; | |
181 base::ScopedTempDir temp_dir; | |
182 CHECK(temp_dir.CreateUniqueTempDir()); | |
183 CHECK(mount("tmpfs", temp_dir.path().value().c_str(), "tmpfs", tmpfs_flags, | |
184 nullptr) == 0); | |
185 const base::FilePath file_path = temp_dir.path().Append("plugin.so"); | |
186 const uint8_t file[] = { | |
187 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, | |
188 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x61, 0x62, 0x6f, 0x62, 0x71, 0x6c, | |
189 0x20, 0x66, 0x7a, 0x76, 0x79, 0x72, 0x66, 0x4e, 0x61, 0x71, 0x20, 0x61, | |
190 0x62, 0x6f, 0x62, 0x71, 0x6c, 0x20, 0x71, 0x65, 0x72, 0x6e, 0x7a, 0x66, | |
191 0x56, 0x61, 0x20, 0x67, 0x75, 0x72, 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20, | |
192 0x6a, 0x75, 0x72, 0x65, 0x72, 0x20, 0x71, 0x72, 0x66, 0x63, 0x72, 0x65, | |
193 0x6e, 0x67, 0x76, 0x62, 0x61, 0x51, 0x65, 0x76, 0x69, 0x72, 0x66, 0x20, | |
194 0x67, 0x75, 0x72, 0x20, 0x6f, 0x62, 0x65, 0x72, 0x71, 0x20, 0x67, 0x62, | |
195 0x20, 0x72, 0x6b, 0x67, 0x65, 0x72, 0x7a, 0x72, 0x66, 0x57, 0x68, 0x66, | |
196 0x67, 0x20, 0x62, 0x61, 0x72, 0x20, 0x66, 0x63, 0x6e, 0x65, 0x78, 0x20, | |
197 0x62, 0x73, 0x20, 0x71, 0x72, 0x70, 0x72, 0x61, 0x70, 0x6c, 0x4e, 0x74, | |
198 0x6e, 0x76, 0x61, 0x66, 0x67, 0x20, 0x6e, 0x20, 0x66, 0x67, 0x6e, 0x65, | |
199 0x79, 0x72, 0x66, 0x66, 0x20, 0x61, 0x76, 0x74, 0x75, 0x67, 0x42, 0x61, | |
200 0x72, 0x20, 0x74, 0x79, 0x62, 0x6a, 0x20, 0x62, 0x73, 0x20, 0x75, 0x62, | |
201 0x63, 0x72, 0x20, 0x6e, 0x61, 0x71, 0x20, 0x71, 0x76, 0x74, 0x61, 0x76, | |
202 0x67, 0x6c, 0x4e, 0x20, 0x70, 0x75, 0x76, 0x79, 0x71, 0x20, 0x70, 0x6e, | |
203 0x61, 0x20, 0x73, 0x62, 0x79, 0x79, 0x62, 0x6a, 0x20, 0x67, 0x75, 0x72, | |
204 0x20, 0x79, 0x76, 0x74, 0x75, 0x67, 0x0, | |
205 }; | |
206 CHECK(base::WriteFile(file_path, reinterpret_cast<const char*>(file), | |
jln (very slow on Chromium)
2015/08/06 18:48:15
If this fails we're not unmounting the path.
Greg K
2015/08/07 21:15:29
Changed all those to EXPECT_TRUE.
| |
207 sizeof(file)) == sizeof(file)); | |
208 CHECK(!ComponentFlashHintFile::TestExecutableMapping(file_path)); | |
209 EXPECT_TRUE(umount(temp_dir.path().value().c_str()) == 0); | |
210 return 0; | |
211 } | |
212 | |
213 TEST_F(ComponentFlashHintFileTest, ExecTest2) { | |
214 base::Process process = SpawnChild("NoExecMountTest"); | |
215 ASSERT_TRUE(process.IsValid()); | |
216 int exit_code = 42; | |
217 EXPECT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(), | |
218 &exit_code)); | |
219 EXPECT_EQ(exit_code, 0); | |
220 } | |
221 | |
222 } // namespace chrome | |
OLD | NEW |