Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / test / component_patcher_unittest.cc
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/component_patcher_unittest.h"
16 #include "chrome/browser/component_updater/test/test_installer.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "courgette/courgette.h"
19 #include "courgette/third_party/bsdiff.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace component_updater {
23
24 namespace {
25
26 base::FilePath test_file(const char* file) {
27   base::FilePath path;
28   PathService::Get(chrome::DIR_TEST_DATA, &path);
29   return path.AppendASCII("components").AppendASCII(file);
30 }
31
32 }  // namespace
33
34 ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
35   EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir());
36   EXPECT_TRUE(input_dir_.CreateUniqueTempDir());
37   EXPECT_TRUE(installed_dir_.CreateUniqueTempDir());
38   patcher_.reset(new MockComponentPatcher());
39   installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path()));
40 }
41
42 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
43 }
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
71 // Verify that a 'create' delta update operation works correctly.
72 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) {
73   EXPECT_TRUE(base::CopyFile(
74       test_file("binary_output.bin"),
75       input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
76
77   scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
78   command_args->SetString("output", "output.bin");
79   command_args->SetString("sha256", binary_output_hash);
80   command_args->SetString("op", "create");
81   command_args->SetString("patch", "binary_output.bin");
82
83   int error = 0;
84   scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate());
85   ComponentUnpacker::Error result = op->Run(command_args.get(),
86                                             input_dir_.path(),
87                                             unpack_dir_.path(),
88                                             patcher_.get(),
89                                             NULL,
90                                             &error);
91
92   EXPECT_EQ(ComponentUnpacker::kNone, result);
93   EXPECT_EQ(0, error);
94   EXPECT_TRUE(base::ContentsEqual(
95       unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
96       test_file("binary_output.bin")));
97 }
98
99 // Verify that a 'copy' delta update operation works correctly.
100 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) {
101   EXPECT_TRUE(base::CopyFile(
102       test_file("binary_output.bin"),
103       installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
104
105   scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
106   command_args->SetString("output", "output.bin");
107   command_args->SetString("sha256", binary_output_hash);
108   command_args->SetString("op", "copy");
109   command_args->SetString("input", "binary_output.bin");
110
111   int error = 0;
112   scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy());
113   ComponentUnpacker::Error result = op->Run(command_args.get(),
114                                             input_dir_.path(),
115                                             unpack_dir_.path(),
116                                             patcher_.get(),
117                                             installer_.get(),
118                                             &error);
119   EXPECT_EQ(ComponentUnpacker::kNone, result);
120   EXPECT_EQ(0, error);
121   EXPECT_TRUE(base::ContentsEqual(
122       unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
123       test_file("binary_output.bin")));
124 }
125
126 // Verify that a 'courgette' delta update operation works correctly.
127 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) {
128   EXPECT_TRUE(base::CopyFile(
129       test_file("binary_input.bin"),
130       installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
131   EXPECT_TRUE(base::CopyFile(
132       test_file("binary_courgette_patch.bin"),
133       input_dir_.path().Append(
134           FILE_PATH_LITERAL("binary_courgette_patch.bin"))));
135
136   scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
137   command_args->SetString("output", "output.bin");
138   command_args->SetString("sha256", binary_output_hash);
139   command_args->SetString("op", "courgette");
140   command_args->SetString("input", "binary_input.bin");
141   command_args->SetString("patch", "binary_courgette_patch.bin");
142
143   int error = 0;
144   scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette());
145   ComponentUnpacker::Error result = op->Run(command_args.get(),
146                                             input_dir_.path(),
147                                             unpack_dir_.path(),
148                                             patcher_.get(),
149                                             installer_.get(),
150                                             &error);
151   EXPECT_EQ(ComponentUnpacker::kNone, result);
152   EXPECT_EQ(0, error);
153   EXPECT_TRUE(base::ContentsEqual(
154       unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
155       test_file("binary_output.bin")));
156 }
157
158 // Verify that a 'bsdiff' delta update operation works correctly.
159 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) {
160   EXPECT_TRUE(base::CopyFile(
161       test_file("binary_input.bin"),
162       installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
163   EXPECT_TRUE(base::CopyFile(
164       test_file("binary_bsdiff_patch.bin"),
165       input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin"))));
166
167   scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
168   command_args->SetString("output", "output.bin");
169   command_args->SetString("sha256", binary_output_hash);
170   command_args->SetString("op", "courgette");
171   command_args->SetString("input", "binary_input.bin");
172   command_args->SetString("patch", "binary_bsdiff_patch.bin");
173
174   int error = 0;
175   scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff());
176   ComponentUnpacker::Error result = op->Run(command_args.get(),
177                                             input_dir_.path(),
178                                             unpack_dir_.path(),
179                                             patcher_.get(),
180                                             installer_.get(),
181                                             &error);
182   EXPECT_EQ(ComponentUnpacker::kNone, result);
183   EXPECT_EQ(0, error);
184   EXPECT_TRUE(base::ContentsEqual(
185       unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
186       test_file("binary_output.bin")));
187 }
188
189 }  // namespace component_updater