| // Copyright 2021 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/updater/util.h" |
| |
| #include "base/command_line.h" |
| #include "base/files/file_path.h" |
| #include "base/files/file_util.h" |
| #include "base/path_service.h" |
| #include "base/strings/strcat.h" |
| #include "base/test/scoped_command_line.h" |
| #include "chrome/updater/constants.h" |
| #include "chrome/updater/tag.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/abseil-cpp/absl/types/optional.h" |
| |
| namespace updater { |
| |
| TEST(Util, AppArgsAndAP) { |
| base::test::ScopedCommandLine original_command_line; |
| { |
| base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| command_line->AppendSwitchASCII(kTagSwitch, |
| "appguid=8a69f345-c564-463c-aff1-" |
| "a69d9e530f96&appname=TestApp&ap=TestAP"); |
| |
| // Test GetAppArgs. |
| EXPECT_EQ(GetAppArgs("NonExistentAppId"), absl::nullopt); |
| absl::optional<tagging::AppArgs> app_args = |
| GetAppArgs("8a69f345-c564-463c-aff1-a69d9e530f96"); |
| ASSERT_NE(app_args, absl::nullopt); |
| EXPECT_STREQ(app_args->app_id.c_str(), |
| "8a69f345-c564-463c-aff1-a69d9e530f96"); |
| EXPECT_STREQ(app_args->app_name.c_str(), "TestApp"); |
| } |
| } |
| |
| TEST(Util, WriteInstallerDataToTempFile) { |
| base::FilePath directory; |
| ASSERT_TRUE(base::PathService::Get(base::DIR_MODULE, &directory)); |
| |
| EXPECT_FALSE(WriteInstallerDataToTempFile(directory, "")); |
| |
| const std::string kInstallerData = |
| R"({"distribution":{"msi":true,"allow_downgrade":false}})"; |
| EXPECT_FALSE(WriteInstallerDataToTempFile( |
| directory.Append(FILE_PATH_LITERAL("NonExistentDirectory")), |
| kInstallerData)); |
| |
| const absl::optional<base::FilePath> installer_data_file = |
| WriteInstallerDataToTempFile(directory, kInstallerData); |
| ASSERT_TRUE(installer_data_file); |
| |
| std::string contents; |
| EXPECT_TRUE(base::ReadFileToString(*installer_data_file, &contents)); |
| EXPECT_EQ(base::StrCat({kUTF8BOM, kInstallerData}), contents); |
| |
| EXPECT_TRUE(base::DeleteFile(*installer_data_file)); |
| } |
| |
| } // namespace updater |