OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/compiler_specific.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/path_service.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/component_updater/component_patcher.h" | |
12 #include "chrome/browser/component_updater/component_patcher_operation.h" | |
13 #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/test_installer.h" | |
16 #include "chrome/common/chrome_paths.h" | |
17 #include "courgette/courgette.h" | |
18 #include "courgette/third_party/bsdiff.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 const char binary_output_hash[] = | |
22 "599aba6d15a7da390621ef1bacb66601ed6aed04dadc1f9b445dcfe31296142a"; | |
23 | |
24 // These constants are duplicated from chrome/installer/util/util_constants.h, | |
25 // to avoid introducing a dependency from the unit tests to the installer. | |
26 const int kCourgetteErrorOffset = 300; | |
27 const int kBsdiffErrorOffset = 600; | |
28 | |
29 base::FilePath test_file(const char* file); | |
30 | |
31 class ComponentPatcherOperationTest : public testing::Test { | |
32 public: | |
33 ComponentPatcherOperationTest(); | |
34 | |
35 protected: | |
36 base::ScopedTempDir input_dir_; | |
37 base::ScopedTempDir installed_dir_; | |
38 base::ScopedTempDir unpack_dir_; | |
39 scoped_ptr<MockComponentPatcher> patcher_; | |
40 scoped_ptr<ReadOnlyTestInstaller> installer_; | |
41 }; | |
42 | |
43 base::FilePath test_file(const char* file) { | |
44 base::FilePath path; | |
45 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
46 return path.AppendASCII("components").AppendASCII(file); | |
47 } | |
48 | |
49 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { | |
50 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); | |
51 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); | |
52 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); | |
53 patcher_.reset(new MockComponentPatcher()); | |
54 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); | |
55 } | |
56 | |
57 ComponentUnpacker::Error MockComponentPatcher::Patch( | |
58 PatchType patch_type, | |
59 const base::FilePath& input_file, | |
60 const base::FilePath& patch_file, | |
61 const base::FilePath& output_file, | |
62 int* error) { | |
63 *error = 0; | |
64 int exit_code; | |
65 if (patch_type == kPatchTypeCourgette) { | |
66 exit_code = courgette::ApplyEnsemblePatch(input_file.value().c_str(), | |
67 patch_file.value().c_str(), | |
68 output_file.value().c_str()); | |
69 if (exit_code == courgette::C_OK) | |
70 return ComponentUnpacker::kNone; | |
71 *error = exit_code + kCourgetteErrorOffset; | |
72 } else if (patch_type == kPatchTypeBsdiff) { | |
73 exit_code = courgette::ApplyBinaryPatch(input_file, | |
74 patch_file, | |
75 output_file); | |
76 if (exit_code == courgette::OK) | |
77 return ComponentUnpacker::kNone; | |
78 *error = exit_code + kBsdiffErrorOffset; | |
79 } | |
80 return ComponentUnpacker::kDeltaOperationFailure; | |
81 } | |
82 | |
83 // Verify that a 'create' delta update operation works correctly. | |
84 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { | |
85 EXPECT_TRUE(file_util::CopyFile( | |
86 test_file("binary_output.bin"), | |
87 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); | |
88 | |
89 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
90 command_args->SetString("output", "output.bin"); | |
91 command_args->SetString("sha256", binary_output_hash); | |
92 command_args->SetString("op", "create"); | |
93 command_args->SetString("patch", "binary_output.bin"); | |
94 | |
95 int error = 0; | |
96 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate()); | |
97 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
98 input_dir_.path(), | |
99 unpack_dir_.path(), | |
100 patcher_.get(), | |
101 NULL, | |
102 &error); | |
103 | |
104 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
105 EXPECT_EQ(0, error); | |
106 EXPECT_TRUE(file_util::ContentsEqual( | |
107 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | |
108 test_file("binary_output.bin"))); | |
109 } | |
110 | |
111 // Verify that a 'copy' delta update operation works correctly. | |
112 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { | |
113 EXPECT_TRUE(file_util::CopyFile( | |
114 test_file("binary_output.bin"), | |
115 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); | |
116 | |
117 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
118 command_args->SetString("output", "output.bin"); | |
119 command_args->SetString("sha256", binary_output_hash); | |
120 command_args->SetString("op", "copy"); | |
121 command_args->SetString("input", "binary_output.bin"); | |
122 | |
123 int error = 0; | |
124 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy()); | |
125 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
126 input_dir_.path(), | |
127 unpack_dir_.path(), | |
128 patcher_.get(), | |
129 installer_.get(), | |
130 &error); | |
131 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
132 EXPECT_EQ(0, error); | |
133 EXPECT_TRUE(file_util::ContentsEqual( | |
134 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | |
135 test_file("binary_output.bin"))); | |
136 } | |
137 | |
138 // Verify that a 'courgette' delta update operation works correctly. | |
139 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { | |
140 EXPECT_TRUE(file_util::CopyFile( | |
141 test_file("binary_input.bin"), | |
142 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); | |
143 EXPECT_TRUE(file_util::CopyFile( | |
144 test_file("binary_courgette_patch.bin"), | |
145 input_dir_.path().Append( | |
146 FILE_PATH_LITERAL("binary_courgette_patch.bin")))); | |
147 | |
148 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
149 command_args->SetString("output", "output.bin"); | |
150 command_args->SetString("sha256", binary_output_hash); | |
151 command_args->SetString("op", "courgette"); | |
152 command_args->SetString("input", "binary_input.bin"); | |
153 command_args->SetString("patch", "binary_courgette_patch.bin"); | |
154 | |
155 int error = 0; | |
156 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette()); | |
157 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
158 input_dir_.path(), | |
159 unpack_dir_.path(), | |
160 patcher_.get(), | |
161 installer_.get(), | |
162 &error); | |
163 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
164 EXPECT_EQ(0, error); | |
165 EXPECT_TRUE(file_util::ContentsEqual( | |
166 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | |
167 test_file("binary_output.bin"))); | |
168 } | |
169 | |
170 // Verify that a 'bsdiff' delta update operation works correctly. | |
171 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { | |
172 EXPECT_TRUE(file_util::CopyFile( | |
173 test_file("binary_input.bin"), | |
174 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); | |
175 EXPECT_TRUE(file_util::CopyFile( | |
176 test_file("binary_bsdiff_patch.bin"), | |
177 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); | |
178 | |
179 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
180 command_args->SetString("output", "output.bin"); | |
181 command_args->SetString("sha256", binary_output_hash); | |
182 command_args->SetString("op", "courgette"); | |
183 command_args->SetString("input", "binary_input.bin"); | |
184 command_args->SetString("patch", "binary_bsdiff_patch.bin"); | |
185 | |
186 int error = 0; | |
187 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff()); | |
188 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
189 input_dir_.path(), | |
190 unpack_dir_.path(), | |
191 patcher_.get(), | |
192 installer_.get(), | |
193 &error); | |
194 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
195 EXPECT_EQ(0, error); | |
196 EXPECT_TRUE(file_util::ContentsEqual( | |
197 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), | |
198 test_file("binary_output.bin"))); | |
199 } | |
200 | |
OLD | NEW |