File: app_inventory_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (223 lines) | stat: -rw-r--r-- 8,564 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/credential_provider/extension/app_inventory_manager.h"

#include <windows.h>

#include <memory>

#include "base/base_paths_win.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_path_override.h"
#include "chrome/credential_provider/extension/user_device_context.h"
#include "chrome/credential_provider/gaiacp/gcpw_strings.h"
#include "chrome/credential_provider/gaiacp/mdm_utils.h"
#include "chrome/credential_provider/gaiacp/reg_utils.h"
#include "chrome/credential_provider/test/gls_runner_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace credential_provider {

namespace testing {

class AppInventoryManagerBaseTest : public GlsRunnerTestBase {
 protected:
  void SetUp() override;
  std::wstring CreateUser();
};

void AppInventoryManagerBaseTest::SetUp() {
  GlsRunnerTestBase::SetUp();

  FakesForTesting fakes;
  fakes.fake_win_http_url_fetcher_creator =
      fake_http_url_fetcher_factory()->GetCreatorCallback();
  AppInventoryManager::Get()->SetFakesForTesting(&fakes);
}

std::wstring AppInventoryManagerBaseTest::CreateUser() {
  // Create a fake user associated to a gaia id.
  CComBSTR sid_str;
  EXPECT_EQ(S_OK, fake_os_user_manager()->CreateTestOSUser(
                      kDefaultUsername, L"password", L"Full Name", L"comment",
                      kDefaultGaiaId, L"user@company.com", &sid_str));
  return OLE2W(sid_str);
}

// Tests user policy fetch by ESA service.
// Params:
// string : The specified device resource ID.
// bool : Whether a valid user sid is present.
// bool : Whether app data is present or not.
// string : The specified DM token.
class AppInventoryManagerTest
    : public AppInventoryManagerBaseTest,
      public ::testing::WithParamInterface<
          std::tuple<const wchar_t*, bool, bool, const wchar_t*>> {
 public:
  AppInventoryManagerTest();

 protected:
  extension::TaskCreator app_inventory_task_creator_;
};

AppInventoryManagerTest::AppInventoryManagerTest() {
  app_inventory_task_creator_ =
      AppInventoryManager::UploadAppInventoryTaskCreator();
}

TEST_P(AppInventoryManagerTest, uploadAppInventory) {
  const std::wstring device_resource_id(std::get<0>(GetParam()));
  bool has_valid_sid = std::get<1>(GetParam());
  bool has_app_data = std::get<2>(GetParam());
  const std::wstring dm_token(std::get<3>(GetParam()));

  const char kAppDisplayName[] = "name";
  const char kAppDisplayVersion[] = "version";
  const char kAppPublisher[] = "publisher";
  const char kAppType[] = "app_type";

  const wchar_t kApp1[] = L"app1";
  const wchar_t kAppDisplayName1[] = L"appName1";
  const wchar_t kAppDisplayVersion1[] = L"appVersion1";
  const wchar_t kAppPublisher1[] = L"appPublisher1";

  const wchar_t kApp2[] = L"app2";
  const wchar_t kAppDisplayName2[] = L"appName2";
  const wchar_t kAppDisplayVersion2[] = L"appVersion2";

  const wchar_t kApp3[] = L"app3";
  const wchar_t kAppDisplayVersion3[] = L"appVersion3";

  const wchar_t kInstalledWin32AppsRegistryPath[] =
      L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
  const wchar_t kInstalledWin32AppsRegistryPathWOW6432[] =
      L"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
  const wchar_t kDelimiter[] = L"\\";
  const wchar_t kAppDisplayNameRegistryKey[] = L"DisplayName";
  const wchar_t kAppDisplayVersionRegistryKey[] = L"DisplayVersion";
  const wchar_t kAppPublisherRegistryKey[] = L"Publisher";

  std::wstring user_sid = L"invalid-user-sid";
  if (has_valid_sid) {
    // Create a fake user associated to a gaia id.
    CComBSTR sid_str;
    ASSERT_EQ(S_OK, fake_os_user_manager()->CreateTestOSUser(
                        kDefaultUsername, L"password", L"Full Name", L"comment",
                        kDefaultGaiaId, L"user@company.com", &sid_str));
    user_sid = OLE2W(sid_str);
  }

  if (has_app_data) {
    // Set valid app data at
    // SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.
    std::wstring app_path_1 = std::wstring(kInstalledWin32AppsRegistryPath)
                                  .append(std::wstring(kDelimiter))
                                  .append(kApp1);
    SetMachineRegString(app_path_1, kAppDisplayNameRegistryKey,
                        kAppDisplayName1);
    SetMachineRegString(app_path_1, kAppDisplayVersionRegistryKey,
                        kAppDisplayVersion1);
    SetMachineRegString(app_path_1, kAppPublisherRegistryKey, kAppPublisher1);

    // Set valid app data at
    // SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall.
    std::wstring app_path_2 =
        std::wstring(kInstalledWin32AppsRegistryPathWOW6432)
            .append(std::wstring(kDelimiter))
            .append(kApp2);
    SetMachineRegString(app_path_2, kAppDisplayNameRegistryKey,
                        kAppDisplayName2);
    SetMachineRegString(app_path_2, kAppDisplayVersionRegistryKey,
                        kAppDisplayVersion2);

    // Set app data without display name.
    std::wstring app_path_3 = std::wstring(kInstalledWin32AppsRegistryPath)
                                  .append(std::wstring(kDelimiter))
                                  .append(kApp3);
    SetMachineRegString(app_path_3, kAppDisplayVersionRegistryKey,
                        kAppDisplayVersion3);
  }

  GURL app_inventory_url =
      AppInventoryManager::Get()->GetGemServiceUploadAppInventoryUrl();
  ASSERT_TRUE(app_inventory_url.is_valid());

  auto expected_response_value = base::Value::Dict().Set(
      "deviceResourceId", base::WideToUTF8(device_resource_id));
  std::string expected_response;
  base::JSONWriter::Write(expected_response_value, &expected_response);

  fake_http_url_fetcher_factory()->SetCollectRequestData(true);
  // Set upload device details server response.
  fake_http_url_fetcher_factory()->SetFakeResponse(
      app_inventory_url, FakeWinHttpUrlFetcher::Headers(), expected_response);

  extension::UserDeviceContext context(device_resource_id, L"", L"", user_sid,
                                       dm_token);

  auto task(app_inventory_task_creator_.Run());
  ASSERT_TRUE(task);

  ASSERT_TRUE(SUCCEEDED(task->SetContext({context})));
  HRESULT status = task->Execute();

  if (!has_valid_sid || device_resource_id.empty() || dm_token.empty()) {
    ASSERT_TRUE(FAILED(status));
    ASSERT_EQ(fake_http_url_fetcher_factory()->requests_created(), 0uLL);
  } else {
    ASSERT_TRUE(SUCCEEDED(status));
    ASSERT_EQ(fake_http_url_fetcher_factory()->requests_created(), 1uLL);
    FakeWinHttpUrlFetcherFactory::RequestData request_data =
        fake_http_url_fetcher_factory()->GetRequestData(0);

    std::optional<base::Value> body_value =
        base::JSONReader::Read(request_data.body);

    base::Value::Dict request;

    request.Set("device_resource_id", "valid-device-resource-id");
    request.Set("dm_token", "valid-dm-token");
    request.Set("obfuscated_gaia_id", "test-gaia-id");
    request.Set("user_sid", "S-1-4-2");
    base::Value::List app_info_value_list;

    if (has_app_data) {
      base::Value::Dict request_dict_1;
      request_dict_1.Set(kAppDisplayName, base::WideToUTF8(kAppDisplayName1));
      request_dict_1.Set(kAppDisplayVersion,
                         base::WideToUTF8(kAppDisplayVersion1));
      request_dict_1.Set(kAppPublisher, base::WideToUTF8(kAppPublisher1));
      // WIN_32
      request_dict_1.Set(kAppType, 1);
      app_info_value_list.Append(std::move(request_dict_1));

      base::Value::Dict request_dict_2;
      request_dict_2.Set(kAppDisplayName, base::WideToUTF8(kAppDisplayName2));
      request_dict_2.Set(kAppDisplayVersion,
                         base::WideToUTF8(kAppDisplayVersion2));
      request_dict_2.Set(kAppType, 1);
      app_info_value_list.Append(std::move(request_dict_2));
    }

    request.Set("windows_gpcw_app_info", std::move(app_info_value_list));
    ASSERT_EQ(body_value.value(), request);
  }
}

INSTANTIATE_TEST_SUITE_P(
    All,
    AppInventoryManagerTest,
    ::testing::Combine(::testing::Values(L"", L"valid-device-resource-id"),
                       ::testing::Bool(),
                       ::testing::Bool(),
                       ::testing::Values(L"", L"valid-dm-token")));

}  // namespace testing
}  // namespace credential_provider