1 // Copyright 2016 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 "chrome/install_static/install_details.h"
6 
7 #include <string>
8 
9 #include "base/macros.h"
10 #include "chrome/install_static/buildflags.h"
11 #include "components/version_info/version_info_values.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 using ::testing::StrEq;
16 
17 namespace install_static {
18 
19 class FakeInstallDetails : public InstallDetails {
20  public:
FakeInstallDetails()21   FakeInstallDetails() : InstallDetails(&payload) {
22     constants.size = sizeof(constants);
23     constants.install_suffix = L"";
24     constants.default_channel_name = L"";
25 #if BUILDFLAG(USE_GOOGLE_UPDATE_INTEGRATION)
26     constants.app_guid = L"testguid";
27     constants.channel_strategy = ChannelStrategy::FIXED;
28 #else
29     constants.app_guid = L"";
30     constants.channel_strategy = ChannelStrategy::UNSUPPORTED;
31 #endif
32     payload.size = sizeof(payload);
33     payload.product_version = product_version.c_str();
34     payload.mode = &constants;
35     payload.channel = channel.c_str();
36     payload.channel_length = channel.length();
37   }
38 
set_product_version(const char * version)39   void set_product_version(const char* version) {
40     product_version.assign(version);
41     payload.product_version = product_version.c_str();
42   }
43 
set_payload_size(size_t size)44   void set_payload_size(size_t size) { payload.size = size; }
45 
set_mode_size(size_t size)46   void set_mode_size(size_t size) { constants.size = size; }
47 
48   InstallConstants constants = InstallConstants();
49   std::wstring channel = std::wstring(L"testchannel");
50   std::string product_version = std::string(PRODUCT_VERSION);
51   Payload payload = Payload();
52 
53   DISALLOW_COPY_AND_ASSIGN(FakeInstallDetails);
54 };
55 
TEST(InstallDetailsTest,GetClientStateKeyPath)56 TEST(InstallDetailsTest, GetClientStateKeyPath) {
57   FakeInstallDetails details;
58 #if BUILDFLAG(USE_GOOGLE_UPDATE_INTEGRATION)
59   EXPECT_THAT(details.GetClientStateKeyPath(),
60               StrEq(L"Software\\Google\\Update\\ClientState\\testguid"));
61 #else
62   EXPECT_THAT(details.GetClientStateKeyPath(),
63               StrEq(std::wstring(L"Software\\").append(kProductPathName)));
64 #endif
65 }
66 
TEST(InstallDetailsTest,GetClientStateMediumKeyPath)67 TEST(InstallDetailsTest, GetClientStateMediumKeyPath) {
68   FakeInstallDetails details;
69 #if BUILDFLAG(USE_GOOGLE_UPDATE_INTEGRATION)
70   EXPECT_THAT(details.GetClientStateMediumKeyPath(),
71               StrEq(L"Software\\Google\\Update\\ClientStateMedium\\testguid"));
72 #else
73   EXPECT_THAT(details.GetClientStateKeyPath(),
74               StrEq(std::wstring(L"Software\\").append(kProductPathName)));
75 #endif
76 }
77 
TEST(InstallDetailsTest,VersionMismatch)78 TEST(InstallDetailsTest, VersionMismatch) {
79   // All is well to begin with.
80   EXPECT_FALSE(FakeInstallDetails().VersionMismatch());
81 
82   // Bad product version.
83   {
84     FakeInstallDetails details;
85     details.set_product_version("0.1.2.3");
86     EXPECT_TRUE(details.VersionMismatch());
87   }
88 
89   // Bad Payload size.
90   {
91     FakeInstallDetails details;
92     details.set_payload_size(sizeof(InstallDetails::Payload) + 1);
93     EXPECT_TRUE(details.VersionMismatch());
94   }
95 
96   // Bad InstallConstants size.
97   {
98     FakeInstallDetails details;
99     details.set_mode_size(sizeof(InstallConstants) + 1);
100     EXPECT_TRUE(details.VersionMismatch());
101   }
102 }
103 
104 }  // namespace install_static
105