File: dm_message_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (228 lines) | stat: -rw-r--r-- 10,985 bytes parent folder | download | duplicates (3)
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
224
225
226
227
228
// Copyright 2020 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/updater/device_management/dm_message.h"

#include <stdint.h>

#include <memory>
#include <string>
#include <utility>

#include "base/time/time.h"
#include "chrome/enterprise_companion/device_management_storage/dm_storage.h"
#include "chrome/updater/device_management/dm_policy_builder_for_testing.h"
#include "chrome/updater/device_management/dm_response_validator.h"
#include "chrome/updater/protos/omaha_settings.pb.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace updater {

TEST(DMMessage, GetRegisterBrowserRequestData) {
  std::string message = GetRegisterBrowserRequestData();
  EXPECT_FALSE(message.empty());
}

TEST(DMMessage, GetPolicyFetchRequestData) {
  std::string policy_type("google/machine-level-omaha");
  std::unique_ptr<
      ::wireless_android_enterprise_devicemanagement::OmahaSettingsClientProto>
      omaha_settings = GetDefaultTestingOmahaPolicyProto();
  std::unique_ptr<DMPolicyBuilderForTesting> policy_builder =
      DMPolicyBuilderForTesting::CreateInstanceWithOptions(
          true /* first request */, false /* rotate to new key */,
          DMPolicyBuilderForTesting::SigningOption::kSignNormally,
          "test-dm-token", "test-device-id");
  std::string policy_response_string(
      policy_builder->GetResponseBlobForPolicyPayload(
          policy_type, omaha_settings->SerializeAsString()));

  device_management_storage::CachedPolicyInfo policy_info;
  ASSERT_TRUE(policy_info.Populate(policy_response_string));
  const std::string request_data = GetPolicyFetchRequestData(
      policy::PolicyFetchReason::kScheduled, policy_type, policy_info);
  EXPECT_FALSE(request_data.empty());

  enterprise_management::DeviceManagementRequest dm_request;
  ASSERT_TRUE(dm_request.ParseFromString(request_data));
  ASSERT_TRUE(dm_request.has_policy_request());
  const enterprise_management::DevicePolicyRequest& device_policy_request =
      dm_request.policy_request();
  ASSERT_TRUE(device_policy_request.has_reason());
  EXPECT_EQ(device_policy_request.reason(),
            enterprise_management::DevicePolicyRequest::SCHEDULED);
}

TEST(DMMessage, ParseDeviceRegistrationResponse) {
  enterprise_management::DeviceManagementResponse dm_response;
  dm_response.mutable_register_response()->set_device_management_token(
      "test-dm-token-foo");
  EXPECT_EQ(ParseDeviceRegistrationResponse(dm_response.SerializeAsString()),
            "test-dm-token-foo");
}

TEST(DMMessage, ParsePolicyFetchResponse) {
  const std::string policy_type = "google/machine-level-omaha";
  const base::Time test_start_time = base::Time::Now() - base::Milliseconds(1);

  // Test DM response with first policy fetch request.
  std::unique_ptr<::enterprise_management::DeviceManagementResponse>
      dm_response = GetDefaultTestingPolicyFetchDMResponse(
          true /* first_request */, false /* rotate_to_new_key */,
          DMPolicyBuilderForTesting::SigningOption::kSignNormally);

  device_management_storage::CachedPolicyInfo initial_policy_info;
  std::vector<PolicyValidationResult> validation_results;
  DMPolicyMap policy_map = ParsePolicyFetchResponse(
      dm_response->SerializeAsString(), initial_policy_info, "test-dm-token",
      "test-device-id", validation_results);
  EXPECT_TRUE(validation_results.empty());
  EXPECT_EQ(policy_map.size(), size_t{1});
  EXPECT_NE(policy_map.find(policy_type), policy_map.end());
  std::string policy_data = policy_map[policy_type];

  device_management_storage::CachedPolicyInfo updated_policy_info;
  updated_policy_info.Populate(policy_data);
  EXPECT_FALSE(updated_policy_info.public_key().empty());
  EXPECT_GE(base::Time::UnixEpoch() +
                base::Milliseconds(updated_policy_info.timestamp()),
            test_start_time);

  // Test the case when public key is not rotated.
  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      false /* first_request */, false /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kSignNormally);
  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        updated_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_TRUE(validation_results.empty());
  EXPECT_EQ(policy_map.size(), size_t{1});
  EXPECT_NE(policy_map.find(policy_type), policy_map.end());

  device_management_storage::CachedPolicyInfo updated_policy_info2;
  updated_policy_info.Populate(policy_map[policy_type]);
  EXPECT_TRUE(updated_policy_info2.public_key().empty());

  // Test response that rotates to a new public key.
  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      false /* first_request */, true /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kSignNormally);

  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        updated_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_TRUE(validation_results.empty());
  EXPECT_EQ(policy_map.size(), size_t{1});
  EXPECT_NE(policy_map.find(policy_type), policy_map.end());

  // Verify that we got a new public key.
  device_management_storage::CachedPolicyInfo updated_policy_info3;
  updated_policy_info3.Populate(policy_map[policy_type]);
  std::string new_public_key = updated_policy_info3.public_key();
  EXPECT_FALSE(new_public_key.empty());
  EXPECT_NE(new_public_key, updated_policy_info.public_key());
  EXPECT_GE(updated_policy_info3.timestamp(), updated_policy_info.timestamp());
}

TEST(DMMessage, ResponseValidation) {
  const std::string policy_type = "google/machine-level-omaha";

  // 1) Clients rejects DM responses if it is not intended for the
  // expected device ID or DM token.
  std::unique_ptr<::enterprise_management::DeviceManagementResponse>
      dm_response = GetDefaultTestingPolicyFetchDMResponse(
          true /* first_request */, false /* rotate_to_new_key */,
          DMPolicyBuilderForTesting::SigningOption::kSignNormally);
  const std::string dm_response_data = dm_response->SerializeAsString();

  device_management_storage::CachedPolicyInfo initial_policy_info;
  const std::string bad_dm_token = "bad-dm-token";
  std::vector<PolicyValidationResult> validation_results;
  DMPolicyMap policy_map = ParsePolicyFetchResponse(
      dm_response_data, initial_policy_info, bad_dm_token, "test-device-id",
      validation_results);
  EXPECT_EQ(validation_results.size(), size_t{1});
  EXPECT_EQ(validation_results[0].policy_type, policy_type);
  EXPECT_EQ(validation_results[0].status,
            PolicyValidationResult::Status::kValidationBadDMToken);
  EXPECT_TRUE(validation_results[0].issues.empty());
  EXPECT_TRUE(policy_map.empty());
  validation_results.clear();

  const std::string bad_devide_id = "unexpected-device-id";
  policy_map = ParsePolicyFetchResponse(dm_response_data, initial_policy_info,
                                        "test-dm-token", bad_devide_id,
                                        validation_results);
  EXPECT_EQ(validation_results.size(), size_t{1});
  EXPECT_EQ(validation_results[0].policy_type, policy_type);
  EXPECT_EQ(validation_results[0].status,
            PolicyValidationResult::Status::kValidationBadDeviceID);
  EXPECT_TRUE(validation_results[0].issues.empty());
  EXPECT_TRUE(policy_map.empty());
  validation_results.clear();

  // 2) Client must have a cached public key other than the first request.
  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      false /* first_request */, false /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kSignNormally);
  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        initial_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_EQ(validation_results.size(), size_t{1});
  EXPECT_EQ(validation_results[0].policy_type, policy_type);
  EXPECT_EQ(validation_results[0].status,
            PolicyValidationResult::Status::kValidationBadSignature);
  EXPECT_TRUE(validation_results[0].issues.empty());
  EXPECT_TRUE(policy_map.empty());
  validation_results.clear();

  // 3) Client should reject response if the public key is not signed properly.

  // First create a DM response to update cached policy info (gets a key
  // to sign the new key).
  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      true /* first_request */, false /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kSignNormally);
  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        initial_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_TRUE(validation_results.empty());
  device_management_storage::CachedPolicyInfo updated_policy_info;
  updated_policy_info.Populate(policy_map[policy_type]);
  EXPECT_FALSE(updated_policy_info.public_key().empty());

  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      true /* first_request */, false /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kTamperKeySignature);
  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        updated_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_EQ(validation_results.size(), size_t{1});
  EXPECT_EQ(validation_results[0].policy_type, policy_type);
  EXPECT_EQ(
      validation_results[0].status,
      PolicyValidationResult::Status::kValidationBadKeyVerificationSignature);
  EXPECT_TRUE(validation_results[0].issues.empty());
  EXPECT_TRUE(policy_map.empty());
  validation_results.clear();

  // 4) Client should reject response if policy data is not signed properly.
  dm_response = GetDefaultTestingPolicyFetchDMResponse(
      true /* first_request */, false /* rotate_to_new_key */,
      DMPolicyBuilderForTesting::SigningOption::kTamperDataSignature);
  policy_map = ParsePolicyFetchResponse(dm_response->SerializeAsString(),
                                        initial_policy_info, "test-dm-token",
                                        "test-device-id", validation_results);
  EXPECT_EQ(validation_results.size(), size_t{1});
  EXPECT_EQ(validation_results[0].policy_type, policy_type);
  EXPECT_EQ(validation_results[0].status,
            PolicyValidationResult::Status::kValidationBadSignature);
  EXPECT_TRUE(validation_results[0].issues.empty());
  EXPECT_TRUE(policy_map.empty());
  validation_results.clear();
}

}  // namespace updater