1// Copyright 2014 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 <stdint.h>
6#include <vector>
7
8#include "base/files/file_path.h"
9#include "base/files/file_util.h"
10#include "base/files/scoped_temp_dir.h"
11#include "base/macros.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/strings/string_util.h"
14#include "base/strings/utf_string_conversions.h"
15#include "base/values.h"
16#include "base/version.h"
17#include "chrome/browser/component_updater/cld_component_installer.h"
18#include "components/translate/content/browser/browser_cld_data_provider.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#include "testing/platform_test.h"
21
22using component_updater::CldComponentInstallerTraits;
23
24namespace {
25// This has to match what's in cld_component_installer.cc.
26const base::FilePath::CharType kTestCldDataFileName[] =
27    FILE_PATH_LITERAL("cld2_data.bin");
28
29}  // namespace
30
31namespace component_updater {
32
33class CldComponentInstallerTest : public PlatformTest {
34 public:
35  CldComponentInstallerTest() {}
36  virtual void SetUp() OVERRIDE {
37    PlatformTest::SetUp();
38
39    // ScopedTempDir automatically does a recursive delete on the entire
40    // directory in its destructor, so no cleanup is required in TearDown.
41    // Note that all files created by this test case are created within the
42    // directory that is created here, so even though they are not explicitly
43    // created *as temp files*, they will still get cleaned up automagically.
44    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
45
46    // The "latest CLD data file" is a static piece of information, and thus
47    // for correctness we empty it before each test.
48    CldComponentInstallerTraits::SetLatestCldDataFile(base::FilePath());
49    base::FilePath path_now =
50        CldComponentInstallerTraits::GetLatestCldDataFile();
51    ASSERT_TRUE(path_now.empty());
52  }
53
54 protected:
55  base::ScopedTempDir temp_dir_;
56  CldComponentInstallerTraits traits_;
57
58 private:
59  DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest);
60};
61
62TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) {
63  const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test"));
64  CldComponentInstallerTraits::SetLatestCldDataFile(expected);
65  base::FilePath result =
66      CldComponentInstallerTraits::GetLatestCldDataFile();
67  ASSERT_EQ(expected, result);
68}
69
70TEST_F(CldComponentInstallerTest, VerifyInstallation) {
71  // All files are created within a ScopedTempDir, which deletes all
72  // children when its destructor is called (at the end of each test).
73  ASSERT_FALSE(traits_.VerifyInstallation(temp_dir_.path()));
74  const base::FilePath data_file_dir =
75      temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append(
76          FILE_PATH_LITERAL("all"));
77  ASSERT_TRUE(base::CreateDirectory(data_file_dir));
78  const base::FilePath data_file = data_file_dir.Append(kTestCldDataFileName);
79  const std::string test_data("fake cld2 data file content here :)");
80  ASSERT_EQ(static_cast<int32>(test_data.length()),
81            base::WriteFile(data_file, test_data.c_str(), test_data.length()));
82  ASSERT_TRUE(traits_.VerifyInstallation(temp_dir_.path()));
83}
84
85TEST_F(CldComponentInstallerTest, OnCustomInstall) {
86  const base::DictionaryValue manifest;
87  const base::FilePath install_dir;
88  // Sanity: shouldn't crash.
89  ASSERT_TRUE(traits_.OnCustomInstall(manifest, install_dir));
90}
91
92TEST_F(CldComponentInstallerTest, GetInstalledPath) {
93  const base::FilePath base_dir;
94  const base::FilePath result =
95      CldComponentInstallerTraits::GetInstalledPath(base_dir);
96  ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true));
97}
98
99TEST_F(CldComponentInstallerTest, GetBaseDirectory) {
100  const base::FilePath result = traits_.GetBaseDirectory();
101  ASSERT_FALSE(result.empty());
102}
103
104TEST_F(CldComponentInstallerTest, GetHash) {
105  std::vector<uint8_t> hash;
106  traits_.GetHash(&hash);
107  ASSERT_EQ(static_cast<size_t>(32), hash.size());
108}
109
110TEST_F(CldComponentInstallerTest, GetName) {
111  ASSERT_FALSE(traits_.GetName().empty());
112}
113
114TEST_F(CldComponentInstallerTest, ComponentReady) {
115  scoped_ptr<base::DictionaryValue> manifest;
116  const base::FilePath install_dir(FILE_PATH_LITERAL("/foo"));
117  const base::Version version("1.2.3.4");
118  traits_.ComponentReady(version, install_dir, manifest.Pass());
119  base::FilePath result =
120      CldComponentInstallerTraits::GetLatestCldDataFile();
121  ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(),
122                         install_dir.AsUTF16Unsafe(),
123                         true));
124  ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true));
125}
126
127}  // namespace component_updater
128