File: feedback_service_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (492 lines) | stat: -rw-r--r-- 20,927 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// 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 "extensions/browser/api/feedback_private/feedback_service.h"

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/mock_callback.h"
#include "build/build_config.h"
#include "components/feedback/feedback_data.h"
#include "components/feedback/feedback_report.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/feedback_private/mock_feedback_service.h"
#include "extensions/browser/api_unittest.h"
#include "extensions/shell/browser/api/feedback_private/shell_feedback_private_delegate.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "base/base64.h"
#include "chromeos/ash/components/dbus/debug_daemon/debug_daemon_client.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "third_party/boringssl/src/include/openssl/hpke.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace extensions {

using feedback::FeedbackData;
using feedback::FeedbackUploader;
using testing::_;
using testing::StrictMock;

namespace {

const std::string kFakeKey = "fake key";
const std::string kFakeValue = "fake value";
const std::string kTabTitleValue = "some sensitive info";
#if BUILDFLAG(IS_CHROMEOS)
constexpr char kVariationsFetchHpkeKey[] =
    "https://www.gstatic.com/chromeos-feedback-variations-encryption-key/"
    "public_keyset.json";
const std::string kTestPublicKeyResponseBody =
    "{\"primaryKeyId\":123,\"key\":[{\"keyData\":{\"typeUrl\":\"type."
    "googleapis.com/"
    "google.crypto.tink.HpkePublicKey\",\"value\":"
    "\"EgYIARABGAIaIKeVK4N3icUhM5YF+Pp5S6PWAg9OlY8zP9oLL9qv4IYS\","
    "\"keyMaterialType\":\"ASYMMETRIC_PUBLIC\"},\"status\":\"ENABLED\","
    "\"keyId\":456,\"outputPrefixType\":\"RAW\"}]}";
const std::string kTestBase64HpkePrivateKey =
    "IHbZB+CCrEXra2WGQx/jFQ+a0NSpVCauqy2uC9NH8Hs=";
// Command line variation string returned during testing.
const std::string kTestCommandLineVariations =
    " --enable-features=\"*TestBlinkFeatureDefault,"
    "TestFeatureForBrowserTest1\" "
    "--disable-features=\"TestFeatureForBrowserTest2\" "
    "--disable-field-trial-config";
#endif  // BUILDFLAG(IS_CHROMEOS)

class MockFeedbackUploader : public FeedbackUploader {
 public:
  MockFeedbackUploader(
      bool is_off_the_record,
      const base::FilePath& state_path,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
      : FeedbackUploader(is_off_the_record, state_path, url_loader_factory) {}

  MOCK_METHOD(void,
              QueueReport,
              (std::unique_ptr<std::string>, bool, int),
              (override));

  base::WeakPtr<FeedbackUploader> AsWeakPtr() override {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  base::WeakPtrFactory<MockFeedbackUploader> weak_ptr_factory_{this};
};

class MockFeedbackPrivateDelegate : public ShellFeedbackPrivateDelegate {
 public:
  MockFeedbackPrivateDelegate() {
    ON_CALL(*this, FetchSystemInformation)
        .WillByDefault([](content::BrowserContext* context,
                          system_logs::SysLogsFetcherCallback callback) {
          auto sys_info = std::make_unique<system_logs::SystemLogsResponse>();
          sys_info->emplace(kFakeKey, kFakeValue);
          sys_info->emplace(feedback::FeedbackReport::kMemUsageWithTabTitlesKey,
                            kTabTitleValue);
          std::move(callback).Run(std::move(sys_info));
        });
#if BUILDFLAG(IS_CHROMEOS)
    ON_CALL(*this, FetchExtraLogs)
        .WillByDefault([](scoped_refptr<FeedbackData> feedback_data,
                          FetchExtraLogsCallback callback) {
          std::move(callback).Run(feedback_data);
        });
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  ~MockFeedbackPrivateDelegate() override = default;

  MOCK_METHOD(void,
              FetchSystemInformation,
              (content::BrowserContext*, system_logs::SysLogsFetcherCallback),
              (const, override));
#if BUILDFLAG(IS_CHROMEOS)
  MOCK_METHOD(void,
              FetchExtraLogs,
              (scoped_refptr<feedback::FeedbackData>, FetchExtraLogsCallback),
              (const, override));
#endif  // BUILDFLAG(IS_CHROMEOS)
};

#if BUILDFLAG(IS_CHROMEOS)
const FeedbackCommon::AttachedFile* FindAttachment(
    std::string_view name,
    const scoped_refptr<FeedbackData>& feedback_data) {
  size_t num_attachments = feedback_data->attachments();
  for (size_t i = 0; i < num_attachments; i++) {
    const FeedbackCommon::AttachedFile* file = feedback_data->attachment(i);
    if (file->name == name) {
      return file;
    }
  }
  return nullptr;
}

void VerifyAttachment(std::string_view name,
                      std::string_view data,
                      const scoped_refptr<FeedbackData>& feedback_data) {
  const auto* attachment = FindAttachment(name, feedback_data);
  ASSERT_TRUE(attachment);
  EXPECT_EQ(name, attachment->name);
  EXPECT_EQ(data, attachment->data);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

class FeedbackServiceTest : public ApiUnitTest {
 protected:
  FeedbackServiceTest() {
#if BUILDFLAG(IS_CHROMEOS)
    test_url_loader_factory_.AddResponse(
        kVariationsFetchHpkeKey, kTestPublicKeyResponseBody, net::HTTP_OK);
#endif  // BUILDFLAG(IS_CHROMEOS)
    test_shared_loader_factory_ =
        base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
            &test_url_loader_factory_);
    CHECK(scoped_temp_dir_.CreateUniqueTempDir());
    mock_uploader_ = std::make_unique<StrictMock<MockFeedbackUploader>>(
        /*is_off_the_record=*/false, scoped_temp_dir_.GetPath(),
        test_shared_loader_factory_);
    feedback_data_ = base::MakeRefCounted<FeedbackData>(
        mock_uploader_->AsWeakPtr(), nullptr);
#if BUILDFLAG(IS_CHROMEOS)
    auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
    scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
        std::move(fake_user_manager));
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  ~FeedbackServiceTest() override = default;

  void TestSendFeedbackConcerningTabTitles(bool send_tab_titles) {
    feedback_data_->AddLog(kFakeKey, kFakeValue);
    feedback_data_->AddLog(feedback::FeedbackReport::kMemUsageWithTabTitlesKey,
                           kTabTitleValue);
    const FeedbackParams params{/*is_internal_email=*/false,
                                /*load_system_info=*/false,
                                /*send_tab_titles=*/send_tab_titles,
                                /*send_histograms=*/true,
                                /*send_bluetooth_logs=*/false,
                                /*send_wifi_debug_logs=*/false,
                                /*send_autofill_metadata=*/false};

    EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
    base::MockCallback<SendFeedbackCallback> mock_callback;
    EXPECT_CALL(mock_callback, Run(true));

    auto mock_delegate = std::make_unique<MockFeedbackPrivateDelegate>();
#if BUILDFLAG(IS_CHROMEOS)
    EXPECT_CALL(*mock_delegate, FetchExtraLogs(_, _)).Times(1);
#endif  // BUILDFLAG(IS_CHROMEOS)

    auto feedback_service = base::MakeRefCounted<FeedbackService>(
        browser_context(), mock_delegate.get());
    RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
    EXPECT_EQ(1u, feedback_data_->sys_info()->count(kFakeKey));
  }

#if BUILDFLAG(IS_CHROMEOS)
  void TestSendFeedbackConcerningWifiDebugLogs(bool send_wifi_debug_logs) {
    const FeedbackParams params{/*is_internal_email=*/false,
                                /*load_system_info=*/true,
                                /*send_tab_titles=*/false,
                                /*send_histograms=*/false,
                                /*send_bluetooth_logs=*/false,
                                /*send_wifi_debug_logs=*/send_wifi_debug_logs,
                                /*send_autofill_metadata=*/false};

    // Create a test file in sub directory "wifi".
    const base::FilePath test_file_dir =
        scoped_temp_dir_.GetPath().Append("wifi");
    ASSERT_TRUE(base::CreateDirectory(test_file_dir));

    const base::FilePath test_file =
        test_file_dir.Append("wifi_firmware_dumps.tar.zst");
    ASSERT_TRUE(base::WriteFile(test_file, "Test file content"));

    EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
    base::MockCallback<SendFeedbackCallback> mock_callback;
    EXPECT_CALL(mock_callback, Run(true));

    auto mock_delegate = std::make_unique<MockFeedbackPrivateDelegate>();
    EXPECT_CALL(*mock_delegate, FetchSystemInformation(_, _)).Times(1);
    EXPECT_CALL(*mock_delegate, FetchExtraLogs(_, _)).Times(1);

    if (send_wifi_debug_logs) {
      ash::DebugDaemonClient::InitializeFake();
    }
    auto feedback_service = base::MakeRefCounted<FeedbackService>(
        browser_context(), mock_delegate.get());
    feedback_service->SetLogFilesRootPathForTesting(scoped_temp_dir_.GetPath());

    RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
    if (ash::DebugDaemonClient::Get()) {
      ash::DebugDaemonClient::Shutdown();
    }
    EXPECT_EQ(1u, feedback_data_->sys_info()->count(kFakeKey));

    // Verify the attachment is added if and only if send_wifi_debug_logs is
    // true.
    constexpr char kWifiDumpName[] = "wifi_firmware_dumps.tar.zst";
    if (send_wifi_debug_logs) {
      VerifyAttachment(kWifiDumpName, "TestData", feedback_data_);
    } else {
      EXPECT_FALSE(FindAttachment(kWifiDumpName, feedback_data_));
    }
  }

  void TestSendFeedbackConcerningBluetoothDebugLogs(bool send_bluetooth_logs) {
    const FeedbackParams params{/*is_internal_email=*/false,
                                /*load_system_info=*/true,
                                /*send_tab_titles=*/false,
                                /*send_histograms=*/false,
                                /*send_bluetooth_logs=*/send_bluetooth_logs,
                                /*send_wifi_debug_logs=*/false,
                                /*send_autofill_metadata=*/false};

    EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
    base::MockCallback<SendFeedbackCallback> mock_callback;
    EXPECT_CALL(mock_callback, Run(true));

    auto mock_delegate = std::make_unique<MockFeedbackPrivateDelegate>();
    EXPECT_CALL(*mock_delegate, FetchSystemInformation(_, _)).Times(1);
    EXPECT_CALL(*mock_delegate, FetchExtraLogs(_, _)).Times(1);

    if (send_bluetooth_logs) {
      ash::DebugDaemonClient::InitializeFake();
    }
    auto feedback_service = base::MakeRefCounted<FeedbackService>(
        browser_context(), mock_delegate.get());
    feedback_service->SetLogFilesRootPathForTesting(scoped_temp_dir_.GetPath());

    RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
    if (ash::DebugDaemonClient::Get()) {
      ash::DebugDaemonClient::Shutdown();
    }
    EXPECT_EQ(1u, feedback_data_->sys_info()->count(kFakeKey));

    // Verify the attachment is added if and only if send_bluetooth_logs is
    // true.
    constexpr char kBluetoothDumpName[] = "bluetooth_firmware_dumps.tar.zst";
    if (send_bluetooth_logs) {
      VerifyAttachment(kBluetoothDumpName, "TestData", feedback_data_);
    } else {
      EXPECT_FALSE(FindAttachment(kBluetoothDumpName, feedback_data_));
    }
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  void RunUntilFeedbackIsSent(scoped_refptr<FeedbackService> feedback_service,
                              const FeedbackParams& params,
                              SendFeedbackCallback mock_callback) {
    feedback_service->RedactThenSendFeedback(params, feedback_data_,
                                             std::move(mock_callback));
    base::ThreadPoolInstance::Get()->FlushForTesting();
    task_environment()->RunUntilIdle();
  }

#if BUILDFLAG(IS_CHROMEOS)
  std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
  data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
#endif  // BUILDFLAG(IS_CHROMEOS)

  base::ScopedTempDir scoped_temp_dir_;
  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
  std::unique_ptr<StrictMock<MockFeedbackUploader>> mock_uploader_;
  scoped_refptr<FeedbackData> feedback_data_;
};

TEST_F(FeedbackServiceTest, SendFeedbackWithoutSysInfo) {
  const FeedbackParams params{/*is_internal_email=*/false,
                              /*load_system_info=*/false,
                              /*send_tab_titles=*/true,
                              /*send_histograms=*/true,
                              /*send_bluetooth_logs=*/false,
                              /*send_wifi_debug_logs=*/false,
                              /*send_autofill_metadata=*/false};

  EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
  base::MockCallback<SendFeedbackCallback> mock_callback;
  EXPECT_CALL(mock_callback, Run(true));

  auto shell_delegate = std::make_unique<ShellFeedbackPrivateDelegate>();
  auto feedback_service = base::MakeRefCounted<FeedbackService>(
      browser_context(), shell_delegate.get());

  RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
}

TEST_F(FeedbackServiceTest, SendFeedbackLoadSysInfo) {
  const FeedbackParams params{/*is_internal_email=*/false,
                              /*load_system_info=*/true,
                              /*send_tab_titles=*/true,
                              /*send_histograms=*/true,
                              /*send_bluetooth_logs=*/false,
                              /*send_wifi_debug_logs=*/false,
                              /*send_autofill_metadata=*/false};

  EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
  base::MockCallback<SendFeedbackCallback> mock_callback;
  EXPECT_CALL(mock_callback, Run(true));

  auto mock_delegate = std::make_unique<MockFeedbackPrivateDelegate>();
  EXPECT_CALL(*mock_delegate, FetchSystemInformation(_, _)).Times(1);
#if BUILDFLAG(IS_CHROMEOS)
  EXPECT_CALL(*mock_delegate, FetchExtraLogs(_, _)).Times(1);
#endif  // BUILDFLAG(IS_CHROMEOS)

  auto feedback_service = base::MakeRefCounted<FeedbackService>(
      browser_context(), mock_delegate.get());

  RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
  EXPECT_EQ(1u, feedback_data_->sys_info()->count(kFakeKey));
  EXPECT_EQ(1u, feedback_data_->sys_info()->count(
                    feedback::FeedbackReport::kMemUsageWithTabTitlesKey));
}

// TODO(crbug.com/40908623): Re-enable this test
TEST_F(FeedbackServiceTest, DISABLED_SendFeedbackDoNotSendTabTitles) {
  TestSendFeedbackConcerningTabTitles(false);
  EXPECT_EQ(0u, feedback_data_->sys_info()->count(
                    feedback::FeedbackReport::kMemUsageWithTabTitlesKey));
}

TEST_F(FeedbackServiceTest, SendFeedbackDoSendTabTitles) {
  TestSendFeedbackConcerningTabTitles(true);
  EXPECT_EQ(1u, feedback_data_->sys_info()->count(
                    feedback::FeedbackReport::kMemUsageWithTabTitlesKey));
}

TEST_F(FeedbackServiceTest, SendFeedbackAutofillMetadata) {
  const FeedbackParams params{/*is_internal_email=*/true,
                              /*load_system_info=*/false,
                              /*send_tab_titles=*/false,
                              /*send_histograms=*/true,
                              /*send_bluetooth_logs=*/false,
                              /*send_wifi_debug_logs=*/false,
                              /*send_autofill_metadata=*/true};
  feedback_data_->set_autofill_metadata("Autofill Metadata");
  EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
  base::MockCallback<SendFeedbackCallback> mock_callback;
  EXPECT_CALL(mock_callback, Run(true));

  auto feedback_service =
      base::MakeRefCounted<FeedbackService>(browser_context(), nullptr);

  RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
}

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(FeedbackServiceTest, SendFeedbackWithWifiDebugLogs) {
  TestSendFeedbackConcerningWifiDebugLogs(/*send_wifi_debug_logs=*/true);
}

TEST_F(FeedbackServiceTest, SendFeedbackWithoutWifiDebugLogs) {
  TestSendFeedbackConcerningWifiDebugLogs(/*send_wifi_debug_logs=*/false);
}

TEST_F(FeedbackServiceTest, SendFeedbackWithBluetoothDebugLogs) {
  TestSendFeedbackConcerningBluetoothDebugLogs(/*send_bluetooth_logs=*/true);
}

TEST_F(FeedbackServiceTest, SendFeedbackWithoutBluetoothDebugLogs) {
  TestSendFeedbackConcerningBluetoothDebugLogs(/*send_bluetooth_logs=*/false);
}

// Test that the feedback report contains the variations.binary encrypted
// properly.
TEST_F(FeedbackServiceTest, TestSendFeedbackWithVariationsBinary) {
  const FeedbackParams params{/*is_internal_email=*/false,
                              /*load_system_info=*/true,
                              /*send_tab_titles=*/false,
                              /*send_histograms=*/false,
                              /*send_bluetooth_logs=*/false,
                              /*send_wifi_debug_logs=*/false,
                              /*send_autofill_metadata=*/false};

  EXPECT_CALL(*mock_uploader_, QueueReport).Times(1);
  base::MockCallback<SendFeedbackCallback> mock_callback;
  EXPECT_CALL(mock_callback, Run(true));

  auto mock_delegate = std::make_unique<MockFeedbackPrivateDelegate>();
  EXPECT_CALL(*mock_delegate, FetchSystemInformation(_, _)).Times(1);
  EXPECT_CALL(*mock_delegate, FetchExtraLogs(_, _)).Times(1);

  auto feedback_service = base::MakeRefCounted<FeedbackService>(
      browser_context(), mock_delegate.get());
  feedback_service->SetUrlLoaderFactory(test_shared_loader_factory_);

  RunUntilFeedbackIsSent(feedback_service, params, mock_callback.Get());
  EXPECT_EQ(1u, feedback_data_->sys_info()->count(kFakeKey));

  // Initialize Hpke private key.
  bssl::ScopedEVP_HPKE_KEY base_key;
  std::string decoded_hpke_private_key;
  base::Base64Decode(kTestBase64HpkePrivateKey, &decoded_hpke_private_key);
  std::vector<uint8_t> hpke_private_key;
  hpke_private_key.assign(decoded_hpke_private_key.begin(),
                          decoded_hpke_private_key.end());
  ASSERT_TRUE(EVP_HPKE_KEY_init(/*key=*/base_key.get(),
                                /*kem=*/EVP_hpke_x25519_hkdf_sha256(),
                                /*priv_key=*/hpke_private_key.data(),
                                /*priv_key_len=*/hpke_private_key.size()));

  // Get the encrypted file.
  constexpr char kVariationsBinary[] = "variations.binary";
  const FeedbackCommon::AttachedFile* variatons_binary =
      FindAttachment(kVariationsBinary, feedback_data_);
  ASSERT_TRUE(variatons_binary);
  std::vector<uint8_t> encrypted_data(variatons_binary->data.begin(),
                                      variatons_binary->data.end());

  // Setup recipient context.
  bssl::ScopedEVP_HPKE_CTX recipient_ctx;
  ASSERT_TRUE(EVP_HPKE_CTX_setup_recipient(/*ctx=*/recipient_ctx.get(),
                                           /*key=*/base_key.get(),
                                           /*kdf=*/EVP_hpke_hkdf_sha256(),
                                           /*aead=*/EVP_hpke_aes_256_gcm(),
                                           /*enc=*/encrypted_data.data(),
                                           /*enc_len=*/X25519_PUBLIC_VALUE_LEN,
                                           /*info=*/nullptr,
                                           /*info_len=*/0));

  // Decryption.
  auto ciphertext =
      base::span(encrypted_data).subspan<X25519_PUBLIC_VALUE_LEN>();
  std::vector<uint8_t> plaintext(ciphertext.size());
  size_t plaintext_len;
  ASSERT_TRUE(EVP_HPKE_CTX_open(/*ctx=*/recipient_ctx.get(),
                                /*out=*/plaintext.data(),
                                /*out_len=*/&plaintext_len,
                                /*max_out_len=*/plaintext.size(),
                                /*in=*/ciphertext.data(),
                                /*in_len=*/ciphertext.size(),
                                /*ad=*/nullptr,
                                /*ad_len=*/0));
  plaintext.resize(plaintext_len);
  std::string decrypted_string(plaintext.begin(), plaintext.end());

  // Final check.
  EXPECT_EQ(kTestCommandLineVariations, decrypted_string);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace extensions