OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/bind.h" |
5 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
6 #include "base/file_util.h" | 7 #include "base/file_util.h" |
7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
9 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/run_loop.h" |
10 #include "base/values.h" | 13 #include "base/values.h" |
11 #include "chrome/browser/component_updater/component_patcher.h" | 14 #include "chrome/browser/component_updater/component_patcher.h" |
12 #include "chrome/browser/component_updater/component_patcher_operation.h" | 15 #include "chrome/browser/component_updater/component_patcher_operation.h" |
13 #include "chrome/browser/component_updater/component_updater_service.h" | 16 #include "chrome/browser/component_updater/component_updater_service.h" |
14 #include "chrome/browser/component_updater/test/component_patcher_mock.h" | |
15 #include "chrome/browser/component_updater/test/component_patcher_unittest.h" | 17 #include "chrome/browser/component_updater/test/component_patcher_unittest.h" |
16 #include "chrome/browser/component_updater/test/test_installer.h" | 18 #include "chrome/browser/component_updater/test/test_installer.h" |
17 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 20 #include "content/public/browser/browser_thread.h" |
18 #include "courgette/courgette.h" | 21 #include "courgette/courgette.h" |
19 #include "courgette/third_party/bsdiff.h" | 22 #include "courgette/third_party/bsdiff.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
21 | 24 |
| 25 namespace { |
| 26 |
| 27 class TestCallback { |
| 28 public: |
| 29 TestCallback(); |
| 30 virtual ~TestCallback() {} |
| 31 void Set(component_updater::ComponentUnpacker::Error error, int extra_code); |
| 32 |
| 33 int error_; |
| 34 int extra_code_; |
| 35 bool called_; |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(TestCallback); |
| 39 }; |
| 40 |
| 41 TestCallback::TestCallback() |
| 42 : error_(-1), extra_code_(-1), called_(false) { |
| 43 } |
| 44 |
| 45 void TestCallback::Set(component_updater::ComponentUnpacker::Error error, |
| 46 int extra_code) { |
| 47 error_ = error; |
| 48 extra_code_ = extra_code; |
| 49 called_ = true; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
22 namespace component_updater { | 54 namespace component_updater { |
23 | 55 |
24 namespace { | 56 namespace { |
25 | 57 |
26 base::FilePath test_file(const char* file) { | 58 base::FilePath test_file(const char* file) { |
27 base::FilePath path; | 59 base::FilePath path; |
28 PathService::Get(chrome::DIR_TEST_DATA, &path); | 60 PathService::Get(chrome::DIR_TEST_DATA, &path); |
29 return path.AppendASCII("components").AppendASCII(file); | 61 return path.AppendASCII("components").AppendASCII(file); |
30 } | 62 } |
31 | 63 |
32 } // namespace | 64 } // namespace |
33 | 65 |
34 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { | 66 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { |
35 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); | 67 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); |
36 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); | 68 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); |
37 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); | 69 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); |
38 patcher_.reset(new MockComponentPatcher()); | |
39 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); | 70 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); |
| 71 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
| 72 content::BrowserThread::FILE); |
40 } | 73 } |
41 | 74 |
42 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { | 75 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { |
43 } | 76 } |
44 | 77 |
45 ComponentUnpacker::Error MockComponentPatcher::Patch( | |
46 PatchType patch_type, | |
47 const base::FilePath& input_file, | |
48 const base::FilePath& patch_file, | |
49 const base::FilePath& output_file, | |
50 int* error) { | |
51 *error = 0; | |
52 int exit_code; | |
53 if (patch_type == kPatchTypeCourgette) { | |
54 exit_code = courgette::ApplyEnsemblePatch(input_file.value().c_str(), | |
55 patch_file.value().c_str(), | |
56 output_file.value().c_str()); | |
57 if (exit_code == courgette::C_OK) | |
58 return ComponentUnpacker::kNone; | |
59 *error = exit_code + kCourgetteErrorOffset; | |
60 } else if (patch_type == kPatchTypeBsdiff) { | |
61 exit_code = courgette::ApplyBinaryPatch(input_file, | |
62 patch_file, | |
63 output_file); | |
64 if (exit_code == courgette::OK) | |
65 return ComponentUnpacker::kNone; | |
66 *error = exit_code + kBsdiffErrorOffset; | |
67 } | |
68 return ComponentUnpacker::kDeltaOperationFailure; | |
69 } | |
70 | |
71 // Verify that a 'create' delta update operation works correctly. | 78 // Verify that a 'create' delta update operation works correctly. |
72 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { | 79 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { |
73 EXPECT_TRUE(base::CopyFile( | 80 EXPECT_TRUE(base::CopyFile( |
74 test_file("binary_output.bin"), | 81 test_file("binary_output.bin"), |
75 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); | 82 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); |
76 | 83 |
77 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | 84 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
78 command_args->SetString("output", "output.bin"); | 85 command_args->SetString("output", "output.bin"); |
79 command_args->SetString("sha256", binary_output_hash); | 86 command_args->SetString("sha256", binary_output_hash); |
80 command_args->SetString("op", "create"); | 87 command_args->SetString("op", "create"); |
81 command_args->SetString("patch", "binary_output.bin"); | 88 command_args->SetString("patch", "binary_output.bin"); |
82 | 89 |
83 int error = 0; | 90 TestCallback callback; |
84 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate()); | 91 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate()); |
85 ComponentUnpacker::Error result = op->Run(command_args.get(), | 92 op->Run(command_args.get(), |
86 input_dir_.path(), | 93 input_dir_.path(), |
87 unpack_dir_.path(), | 94 unpack_dir_.path(), |
88 patcher_.get(), | 95 NULL, |
89 NULL, | 96 true, |
90 &error); | 97 base::Bind(&TestCallback::Set, base::Unretained(&callback)), |
| 98 task_runner_); |
| 99 base::RunLoop().RunUntilIdle(); |
91 | 100 |
92 EXPECT_EQ(ComponentUnpacker::kNone, result); | 101 EXPECT_EQ(true, callback.called_); |
93 EXPECT_EQ(0, error); | 102 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); |
| 103 EXPECT_EQ(0, callback.extra_code_); |
94 EXPECT_TRUE(base::ContentsEqual( | 104 EXPECT_TRUE(base::ContentsEqual( |
95 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | 105 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
96 test_file("binary_output.bin"))); | 106 test_file("binary_output.bin"))); |
97 } | 107 } |
98 | 108 |
99 // Verify that a 'copy' delta update operation works correctly. | 109 // Verify that a 'copy' delta update operation works correctly. |
100 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { | 110 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { |
101 EXPECT_TRUE(base::CopyFile( | 111 EXPECT_TRUE(base::CopyFile( |
102 test_file("binary_output.bin"), | 112 test_file("binary_output.bin"), |
103 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); | 113 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); |
104 | 114 |
105 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | 115 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
106 command_args->SetString("output", "output.bin"); | 116 command_args->SetString("output", "output.bin"); |
107 command_args->SetString("sha256", binary_output_hash); | 117 command_args->SetString("sha256", binary_output_hash); |
108 command_args->SetString("op", "copy"); | 118 command_args->SetString("op", "copy"); |
109 command_args->SetString("input", "binary_output.bin"); | 119 command_args->SetString("input", "binary_output.bin"); |
110 | 120 |
111 int error = 0; | 121 TestCallback callback; |
112 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy()); | 122 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy()); |
113 ComponentUnpacker::Error result = op->Run(command_args.get(), | 123 op->Run(command_args.get(), |
114 input_dir_.path(), | 124 input_dir_.path(), |
115 unpack_dir_.path(), | 125 unpack_dir_.path(), |
116 patcher_.get(), | 126 installer_.get(), |
117 installer_.get(), | 127 true, |
118 &error); | 128 base::Bind(&TestCallback::Set, base::Unretained(&callback)), |
119 EXPECT_EQ(ComponentUnpacker::kNone, result); | 129 task_runner_); |
120 EXPECT_EQ(0, error); | 130 base::RunLoop().RunUntilIdle(); |
| 131 |
| 132 EXPECT_EQ(true, callback.called_); |
| 133 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); |
| 134 EXPECT_EQ(0, callback.extra_code_); |
121 EXPECT_TRUE(base::ContentsEqual( | 135 EXPECT_TRUE(base::ContentsEqual( |
122 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | 136 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
123 test_file("binary_output.bin"))); | 137 test_file("binary_output.bin"))); |
124 } | 138 } |
125 | 139 |
126 // Verify that a 'courgette' delta update operation works correctly. | 140 // Verify that a 'courgette' delta update operation works correctly. |
127 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { | 141 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { |
128 EXPECT_TRUE(base::CopyFile( | 142 EXPECT_TRUE(base::CopyFile( |
129 test_file("binary_input.bin"), | 143 test_file("binary_input.bin"), |
130 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); | 144 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); |
131 EXPECT_TRUE(base::CopyFile( | 145 EXPECT_TRUE(base::CopyFile( |
132 test_file("binary_courgette_patch.bin"), | 146 test_file("binary_courgette_patch.bin"), |
133 input_dir_.path().Append( | 147 input_dir_.path().Append( |
134 FILE_PATH_LITERAL("binary_courgette_patch.bin")))); | 148 FILE_PATH_LITERAL("binary_courgette_patch.bin")))); |
135 | 149 |
136 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | 150 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
137 command_args->SetString("output", "output.bin"); | 151 command_args->SetString("output", "output.bin"); |
138 command_args->SetString("sha256", binary_output_hash); | 152 command_args->SetString("sha256", binary_output_hash); |
139 command_args->SetString("op", "courgette"); | 153 command_args->SetString("op", "courgette"); |
140 command_args->SetString("input", "binary_input.bin"); | 154 command_args->SetString("input", "binary_input.bin"); |
141 command_args->SetString("patch", "binary_courgette_patch.bin"); | 155 command_args->SetString("patch", "binary_courgette_patch.bin"); |
142 | 156 |
143 int error = 0; | 157 TestCallback callback; |
144 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette()); | 158 scoped_ptr<DeltaUpdateOp> op( |
145 ComponentUnpacker::Error result = op->Run(command_args.get(), | 159 new DeltaUpdateOpPatch(component_updater::kPatchTypeCourgette)); |
146 input_dir_.path(), | 160 op->Run(command_args.get(), |
147 unpack_dir_.path(), | 161 input_dir_.path(), |
148 patcher_.get(), | 162 unpack_dir_.path(), |
149 installer_.get(), | 163 installer_.get(), |
150 &error); | 164 true, |
151 EXPECT_EQ(ComponentUnpacker::kNone, result); | 165 base::Bind(&TestCallback::Set, base::Unretained(&callback)), |
152 EXPECT_EQ(0, error); | 166 task_runner_); |
| 167 base::RunLoop().RunUntilIdle(); |
| 168 |
| 169 EXPECT_EQ(true, callback.called_); |
| 170 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); |
| 171 EXPECT_EQ(0, callback.extra_code_); |
153 EXPECT_TRUE(base::ContentsEqual( | 172 EXPECT_TRUE(base::ContentsEqual( |
154 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | 173 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
155 test_file("binary_output.bin"))); | 174 test_file("binary_output.bin"))); |
156 } | 175 } |
157 | 176 |
158 // Verify that a 'bsdiff' delta update operation works correctly. | 177 // Verify that a 'bsdiff' delta update operation works correctly. |
159 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { | 178 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { |
160 EXPECT_TRUE(base::CopyFile( | 179 EXPECT_TRUE(base::CopyFile( |
161 test_file("binary_input.bin"), | 180 test_file("binary_input.bin"), |
162 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); | 181 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); |
163 EXPECT_TRUE(base::CopyFile( | 182 EXPECT_TRUE(base::CopyFile( |
164 test_file("binary_bsdiff_patch.bin"), | 183 test_file("binary_bsdiff_patch.bin"), |
165 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); | 184 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); |
166 | 185 |
167 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | 186 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
168 command_args->SetString("output", "output.bin"); | 187 command_args->SetString("output", "output.bin"); |
169 command_args->SetString("sha256", binary_output_hash); | 188 command_args->SetString("sha256", binary_output_hash); |
170 command_args->SetString("op", "courgette"); | 189 command_args->SetString("op", "courgette"); |
171 command_args->SetString("input", "binary_input.bin"); | 190 command_args->SetString("input", "binary_input.bin"); |
172 command_args->SetString("patch", "binary_bsdiff_patch.bin"); | 191 command_args->SetString("patch", "binary_bsdiff_patch.bin"); |
173 | 192 |
174 int error = 0; | 193 TestCallback callback; |
175 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff()); | 194 scoped_ptr<DeltaUpdateOp> op( |
176 ComponentUnpacker::Error result = op->Run(command_args.get(), | 195 new DeltaUpdateOpPatch(component_updater::kPatchTypeBsdiff)); |
177 input_dir_.path(), | 196 op->Run(command_args.get(), |
178 unpack_dir_.path(), | 197 input_dir_.path(), |
179 patcher_.get(), | 198 unpack_dir_.path(), |
180 installer_.get(), | 199 installer_.get(), |
181 &error); | 200 true, |
182 EXPECT_EQ(ComponentUnpacker::kNone, result); | 201 base::Bind(&TestCallback::Set, base::Unretained(&callback)), |
183 EXPECT_EQ(0, error); | 202 task_runner_); |
| 203 base::RunLoop().RunUntilIdle(); |
| 204 |
| 205 EXPECT_EQ(true, callback.called_); |
| 206 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); |
| 207 EXPECT_EQ(0, callback.extra_code_); |
184 EXPECT_TRUE(base::ContentsEqual( | 208 EXPECT_TRUE(base::ContentsEqual( |
185 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | 209 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
186 test_file("binary_output.bin"))); | 210 test_file("binary_output.bin"))); |
187 } | 211 } |
188 | 212 |
189 } // namespace component_updater | 213 } // namespace component_updater |
OLD | NEW |