optimization_guide_switches_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2019 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. #include "components/optimization_guide/core/optimization_guide_switches.h"
  5. #include "base/base64.h"
  6. #include "base/command_line.h"
  7. #include "build/build_config.h"
  8. #include "components/optimization_guide/proto/hints.pb.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace optimization_guide {
  12. namespace switches {
  13. #if !BUILDFLAG(IS_WIN)
  14. TEST(OptimizationGuideSwitchesTest, ParseHintsFetchOverrideFromCommandLine) {
  15. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(kFetchHintsOverride,
  16. "whatever.com");
  17. absl::optional<std::vector<std::string>> parsed_hosts =
  18. ParseHintsFetchOverrideFromCommandLine();
  19. EXPECT_TRUE(parsed_hosts.has_value());
  20. EXPECT_EQ(1ul, parsed_hosts.value().size());
  21. EXPECT_EQ("whatever.com", parsed_hosts.value()[0]);
  22. }
  23. TEST(OptimizationGuideSwitchesTest,
  24. ParseHintsFetchOverrideFromCommandLineMultipleHosts) {
  25. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  26. kFetchHintsOverride, "whatever.com, whatever-2.com, ,");
  27. absl::optional<std::vector<std::string>> parsed_hosts =
  28. ParseHintsFetchOverrideFromCommandLine();
  29. EXPECT_TRUE(parsed_hosts.has_value());
  30. EXPECT_EQ(2ul, parsed_hosts.value().size());
  31. EXPECT_EQ("whatever.com", parsed_hosts.value()[0]);
  32. EXPECT_EQ("whatever-2.com", parsed_hosts.value()[1]);
  33. }
  34. TEST(OptimizationGuideSwitchesTest,
  35. ParseHintsFetchOverrideFromCommandLineNoSwitch) {
  36. absl::optional<std::vector<std::string>> parsed_hosts =
  37. ParseHintsFetchOverrideFromCommandLine();
  38. EXPECT_FALSE(parsed_hosts.has_value());
  39. }
  40. TEST(OptimizationGuideSwitchesTest, ParseComponentConfigFromCommandLine) {
  41. optimization_guide::proto::Configuration config;
  42. optimization_guide::proto::Hint* hint = config.add_hints();
  43. hint->set_key("somedomain.org");
  44. hint->set_key_representation(optimization_guide::proto::HOST);
  45. std::string encoded_config;
  46. config.SerializeToString(&encoded_config);
  47. base::Base64Encode(encoded_config, &encoded_config);
  48. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(kHintsProtoOverride,
  49. encoded_config);
  50. std::unique_ptr<optimization_guide::proto::Configuration> parsed_config =
  51. ParseComponentConfigFromCommandLine();
  52. EXPECT_EQ(1, parsed_config->hints_size());
  53. EXPECT_EQ("somedomain.org", parsed_config->hints(0).key());
  54. }
  55. TEST(OptimizationGuideSwitchesTest,
  56. ParseComponentConfigFromCommandLineNotAProto) {
  57. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(kHintsProtoOverride,
  58. "not-a-proto");
  59. std::unique_ptr<optimization_guide::proto::Configuration> parsed_config =
  60. ParseComponentConfigFromCommandLine();
  61. EXPECT_EQ(nullptr, parsed_config);
  62. }
  63. TEST(OptimizationGuideSwitchesTest,
  64. ParseComponentConfigFromCommandLineSwitchNotSet) {
  65. std::unique_ptr<optimization_guide::proto::Configuration> parsed_config =
  66. ParseComponentConfigFromCommandLine();
  67. EXPECT_EQ(nullptr, parsed_config);
  68. }
  69. TEST(OptimizationGuideSwitchesTest,
  70. ParseComponentConfigFromCommandLineNotAConfiguration) {
  71. optimization_guide::proto::HostInfo host_info;
  72. host_info.set_host("whatever.com");
  73. std::string encoded_proto;
  74. host_info.SerializeToString(&encoded_proto);
  75. base::Base64Encode(encoded_proto, &encoded_proto);
  76. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(kHintsProtoOverride,
  77. encoded_proto);
  78. std::unique_ptr<optimization_guide::proto::Configuration> parsed_config =
  79. ParseComponentConfigFromCommandLine();
  80. EXPECT_EQ(nullptr, parsed_config);
  81. }
  82. #endif
  83. } // namespace switches
  84. } // namespace optimization_guide