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.
5 #include "chrome/common/component_flash_hint_file_linux.h"
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"
24 class ComponentFlashHintFileTest
: public base::MultiProcessTest
{};
26 TEST_F(ComponentFlashHintFileTest
, ExistsTest
) {
27 const base::ScopedPathOverride
path_override(chrome::DIR_USER_DATA
);
28 EXPECT_FALSE(component_flash_hint_file::DoesHintFileExist());
31 TEST_F(ComponentFlashHintFileTest
, InstallTest
) {
32 const base::ScopedPathOverride
path_override(chrome::DIR_USER_DATA
);
33 EXPECT_FALSE(component_flash_hint_file::DoesHintFileExist());
35 base::FilePath flash_dir
;
36 ASSERT_TRUE(PathService::Get(
37 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &flash_dir
));
39 base::File::Error error
;
40 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir
, &error
));
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_EQ(static_cast<int>(sizeof(file
)),
48 base::WriteFile(flash_dir
, reinterpret_cast<const char*>(file
),
50 ASSERT_TRUE(component_flash_hint_file::RecordFlashUpdate(flash_dir
, flash_dir
,
52 ASSERT_TRUE(component_flash_hint_file::DoesHintFileExist());
54 // Confirm that the flash plugin can be verified and returned.
55 base::FilePath returned_flash_path
;
57 ASSERT_TRUE(component_flash_hint_file::VerifyAndReturnFlashLocation(
58 &returned_flash_path
, &version
));
59 ASSERT_EQ(returned_flash_path
, flash_dir
);
60 ASSERT_EQ(version
, flash_version
);
62 // Now "corrupt" the flash file and make sure the checksum fails and nothing
65 ASSERT_TRUE(base::WriteFile(flash_dir
, reinterpret_cast<const char*>(file
),
66 sizeof(file
)) == sizeof(file
));
67 base::FilePath empty_path
;
68 std::string empty_version
;
69 ASSERT_FALSE(component_flash_hint_file::VerifyAndReturnFlashLocation(
70 &empty_path
, &empty_version
));
71 ASSERT_NE(empty_path
, flash_dir
);
72 ASSERT_FALSE(empty_version
== flash_version
);
75 TEST_F(ComponentFlashHintFileTest
, CorruptionTest
) {
76 const base::ScopedPathOverride
path_override(chrome::DIR_USER_DATA
);
77 EXPECT_FALSE(component_flash_hint_file::DoesHintFileExist());
79 base::FilePath flash_dir
;
80 ASSERT_TRUE(PathService::Get(
81 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &flash_dir
));
83 base::File::Error error
;
84 ASSERT_TRUE(base::CreateDirectoryAndGetError(flash_dir
, &error
));
85 flash_dir
= flash_dir
.Append("libflash.so");
87 const uint8_t file
[] = {0x56, 0x61, 0x20, 0x67, 0x75, 0x76,
88 0x66, 0x20, 0x62, 0x61, 0x72, 0x20};
89 ASSERT_TRUE(base::WriteFile(flash_dir
, reinterpret_cast<const char*>(file
),
90 sizeof(file
)) == sizeof(file
));
91 const std::string flash_version
= "1.0.0.1";
92 ASSERT_TRUE(component_flash_hint_file::RecordFlashUpdate(flash_dir
, flash_dir
,
94 ASSERT_TRUE(component_flash_hint_file::DoesHintFileExist());
96 // Now write out a new flash version that will not be moved into place.
97 const uint8_t updated_file
[] = {0x43, 0x72, 0x62, 0x63, 0x79, 0x72,
98 0x20, 0x66, 0x7a, 0x76, 0x79, 0x76};
99 base::FilePath flash_dir_update
;
100 ASSERT_TRUE(PathService::Get(
101 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN
, &flash_dir_update
));
102 flash_dir_update
= flash_dir_update
.Append("other_flash.so");
103 ASSERT_TRUE(base::WriteFile(flash_dir_update
,
104 reinterpret_cast<const char*>(updated_file
),
105 sizeof(updated_file
)) == sizeof(updated_file
));
106 ASSERT_TRUE(component_flash_hint_file::RecordFlashUpdate(
107 flash_dir_update
, flash_dir
, flash_version
));
108 // |flash_dir_update| needs to be moved to |flash_dir|, but this test
109 // deliberately skips that step, so VerifyAndReturnFlashLocation should fail.
110 base::FilePath failed_flash_dir
;
111 std::string failed_version
;
112 ASSERT_FALSE(component_flash_hint_file::VerifyAndReturnFlashLocation(
113 &failed_flash_dir
, &failed_version
));
116 TEST_F(ComponentFlashHintFileTest
, ExecTest1
) {
117 base::ScopedTempDir temp_dir
;
118 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
119 base::FilePath file_path
= temp_dir
.path().Append("plugin.so");
120 const uint8_t file
[] = {0x55, 0x62, 0x79, 0x71, 0x20,
121 0x6c, 0x62, 0x68, 0x65, 0x20};
123 ASSERT_TRUE(base::WriteFile(file_path
, reinterpret_cast<const char*>(file
),
124 sizeof(file
)) == sizeof(file
));
125 ASSERT_TRUE(component_flash_hint_file::TestExecutableMapping(file_path
));
128 MULTIPROCESS_TEST_MAIN(NoExecMountTest
) {
129 if (unshare(CLONE_NEWUSER
| CLONE_NEWNS
) != 0) {
130 LOG(ERROR
) << "This kernel does not support unprivileged namespaces. "
131 "ExecTest2 will succeed without running.";
134 // Now mount a NOEXEC fs.
135 const unsigned long tmpfs_flags
= MS_NODEV
| MS_NOSUID
| MS_NOEXEC
;
136 base::ScopedTempDir temp_dir
;
137 CHECK(temp_dir
.CreateUniqueTempDir());
138 CHECK_EQ(0, mount("tmpfs", temp_dir
.path().value().c_str(), "tmpfs",
139 tmpfs_flags
, nullptr));
140 const base::FilePath file_path
= temp_dir
.path().Append("plugin.so");
141 const uint8_t file
[] = {0x56, 0x61, 0x20, 0x67, 0x75, 0x72,
142 0x20, 0x70, 0x76, 0x67, 0x6c, 0x20};
143 bool test_exec
= false;
145 base::WriteFile(file_path
, reinterpret_cast<const char*>(file
),
146 sizeof(file
)) == static_cast<int>(sizeof(file
));
148 test_exec
= component_flash_hint_file::TestExecutableMapping(file_path
);
150 if (umount(temp_dir
.path().value().c_str()) != 0)
151 LOG(ERROR
) << "Could not unmount directory " << temp_dir
.path().value();
158 TEST_F(ComponentFlashHintFileTest
, ExecTest2
) {
159 base::Process process
= SpawnChild("NoExecMountTest");
160 ASSERT_TRUE(process
.IsValid());
162 ASSERT_TRUE(process
.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
164 EXPECT_EQ(0, exit_code
);
167 } // namespace chrome