1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <stdint.h> 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector> 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/file_path.h" 91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h" 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/scoped_temp_dir.h" 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/macros.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/string_util.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/values.h" 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/version.h" 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/component_updater/cld_component_installer.h" 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/translate/content/browser/browser_cld_data_provider.h" 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/platform_test.h" 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)using component_updater::CldComponentInstallerTraits; 235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace { 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This has to match what's in cld_component_installer.cc. 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst base::FilePath::CharType kTestCldDataFileName[] = 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch FILE_PATH_LITERAL("cld2_data.bin"); 285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace component_updater { 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class CldComponentInstallerTest : public PlatformTest { 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public: 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch CldComponentInstallerTest() {} 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual void SetUp() OVERRIDE { 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) PlatformTest::SetUp(); 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // ScopedTempDir automatically does a recursive delete on the entire 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // directory in its destructor, so no cleanup is required in TearDown. 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Note that all files created by this test case are created within the 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // directory that is created here, so even though they are not explicitly 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // created *as temp files*, they will still get cleaned up automagically. 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // The "latest CLD data file" is a static piece of information, and thus 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // for correctness we empty it before each test. 485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits::SetLatestCldDataFile(base::FilePath()); 49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath path_now = 505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits::GetLatestCldDataFile(); 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(path_now.empty()); 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected: 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::ScopedTempDir temp_dir_; 565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits traits_; 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private: 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DISALLOW_COPY_AND_ASSIGN(CldComponentInstallerTest); 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}; 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, SetLatestCldDataFile) { 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath expected(FILE_PATH_LITERAL("test/foo.test")); 645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits::SetLatestCldDataFile(expected); 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath result = 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits::GetLatestCldDataFile(); 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(expected, result); 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, VerifyInstallation) { 71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // All files are created within a ScopedTempDir, which deletes all 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // children when its destructor is called (at the end of each test). 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_FALSE(traits_.VerifyInstallation(temp_dir_.path())); 74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath data_file_dir = 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) temp_dir_.path().Append(FILE_PATH_LITERAL("_platform_specific")).Append( 76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) FILE_PATH_LITERAL("all")); 77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(base::CreateDirectory(data_file_dir)); 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::FilePath data_file = data_file_dir.Append(kTestCldDataFileName); 79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string test_data("fake cld2 data file content here :)"); 80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(static_cast<int32>(test_data.length()), 81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::WriteFile(data_file, test_data.c_str(), test_data.length())); 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(traits_.VerifyInstallation(temp_dir_.path())); 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, OnCustomInstall) { 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::DictionaryValue manifest; 87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath install_dir; 88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Sanity: shouldn't crash. 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(traits_.OnCustomInstall(manifest, install_dir)); 90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetInstalledPath) { 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath base_dir; 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath result = 95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) CldComponentInstallerTraits::GetInstalledPath(base_dir); 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true)); 97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetBaseDirectory) { 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const base::FilePath result = traits_.GetBaseDirectory(); 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_FALSE(result.empty()); 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetHash) { 1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::vector<uint8_t> hash; 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch traits_.GetHash(&hash); 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_EQ(static_cast<size_t>(32), hash.size()); 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, GetName) { 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_FALSE(traits_.GetName().empty()); 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(CldComponentInstallerTest, ComponentReady) { 115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) scoped_ptr<base::DictionaryValue> manifest; 116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::FilePath install_dir(FILE_PATH_LITERAL("/foo")); 117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::Version version("1.2.3.4"); 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch traits_.ComponentReady(version, install_dir, manifest.Pass()); 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::FilePath result = 1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CldComponentInstallerTraits::GetLatestCldDataFile(); 121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(StartsWith(result.AsUTF16Unsafe(), 122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) install_dir.AsUTF16Unsafe(), 123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) true)); 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ASSERT_TRUE(EndsWith(result.value(), kTestCldDataFileName, true)); 125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace component_updater 128