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