File: fake_fwupd_client.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 (148 lines) | stat: -rw-r--r-- 4,844 bytes parent folder | download | duplicates (7)
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
// 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 <string>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/threading/thread_restrictions.h"
#include "chromeos/ash/components/dbus/fwupd/dbus_constants.h"
#include "chromeos/ash/components/dbus/fwupd/fake_fwupd_client.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_device.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_properties.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_properties_fake.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_update.h"

namespace {

const char kFakeDeviceIdForTesting[] = "0123";
const char kFakeRemoteIdForTesting[] = "test-remote";

}  // namespace

namespace ash {

FakeFwupdClient::FakeFwupdClient() = default;
FakeFwupdClient::~FakeFwupdClient() {
  if (temp_directory_.IsValid()) {
    CHECK(temp_directory_.Delete());
  }
}
void FakeFwupdClient::Init(dbus::Bus* bus) {}

void FakeFwupdClient::RequestDevices() {
  FwupdDeviceList devices;

  // Add a fake device.
  if (update_with_reboot_) {
    devices.emplace_back(/*id=*/kFakeDeviceIdForTesting,
                         /*device_name=*/"fake_device",
                         /*needs_reboot*/ true);
  } else {
    devices.emplace_back(/*id=*/kFakeDeviceIdForTesting,
                         /*device_name=*/"fake_device",
                         /*needs_reboot*/ false);
  }

  for (auto& observer : observers_)
    observer.OnDeviceListResponse(&devices);
}

void FakeFwupdClient::RequestUpdates(const std::string& device_id) {
  // This matches the behavior of the real class. I.e. if you send an unknown
  // id, nothing happens.
  if (device_id != kFakeDeviceIdForTesting)
    return;

  FwupdUpdateList updates;

  // Add a fake update.
  updates.emplace_back(
      /*version=*/"1.2.3", /*description=*/"Fake update description.",
      /*priority=*/1,
      /*filepath=*/CreateUpdateFilePath(),
      /*checksum=*/
      "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

  for (auto& observer : observers_)
    observer.OnUpdateListResponse(device_id, &updates);
}

void FakeFwupdClient::InstallUpdate(
    const std::string& device_id,
    base::ScopedFD file_descriptor,
    FirmwareInstallOptions options,
    base::OnceCallback<void(FwupdDbusResult)> callback) {
  // This matches the behavior of the real class. I.e. if you send an unknown
  // id, nothing happens.
  if (device_id != kFakeDeviceIdForTesting) {
    std::move(callback).Run(FwupdDbusResult::kInternalError);
    return;
  }

  has_update_started_ = true;
  if (defer_install_update_callback_) {
    install_update_callback_ = std::move(callback);
  } else {
    std::move(callback).Run(FwupdDbusResult::kSuccess);
  }
}

void FakeFwupdClient::TriggerPropertiesChangeForTesting(uint32_t percentage,
                                                        uint32_t status) {
  FwupdPropertiesFake progress = FwupdPropertiesFake(percentage, status);
  for (auto& observer : observers_) {
    observer.OnPropertiesChangedResponse(&progress);
  }
}

void FakeFwupdClient::TriggerSuccessfulUpdateForTesting() {
  CHECK(install_update_callback_);
  has_update_started_ = false;
  std::move(install_update_callback_).Run(FwupdDbusResult::kSuccess);
}

void FakeFwupdClient::EmitDeviceRequestForTesting(uint32_t device_request_id) {
  for (auto& observer : observers_) {
    FwupdRequest request(/*id=*/device_request_id, /*kind=*/2);
    observer.OnDeviceRequestResponse(request);
  }
}

// Implement stub method to satisfy interface.
void FakeFwupdClient::SetFwupdFeatureFlags() {}

base::FilePath FakeFwupdClient::CreateUpdateFilePath() {
  base::ScopedAllowBlockingForTesting allow_blocking;
  if (!temp_directory_.IsValid()) {
    CHECK(temp_directory_.CreateUniqueTempDir());
  }

  const std::string fake_update_filename =
      base::StrCat({kFakeDeviceIdForTesting, ".cab"});

  // Create the file into the temp directory.
  base::FilePath full_path_to_fake_update =
      temp_directory_.GetPath().Append(fake_update_filename);
  // Write an empty file, since the contents of the cab file are not processed
  // upstream.
  base::WriteFile(full_path_to_fake_update, "");
  base::FilePath fake_update_file_with_URI(
      base::StrCat({"file://", full_path_to_fake_update.value()}));
  return fake_update_file_with_URI;
}

void FakeFwupdClient::UpdateMetadata(
    const std::string& remote_id,
    base::ScopedFD data_file_descriptor,
    base::ScopedFD sig_file_descriptor,
    base::OnceCallback<void(FwupdDbusResult)> callback) {
  if (remote_id != kFakeRemoteIdForTesting) {
    std::move(callback).Run(FwupdDbusResult::kInternalError);
    return;
  }
  std::move(callback).Run(FwupdDbusResult::kSuccess);
}

}  // namespace ash