firmware_update_manager_unittest.cc 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. // Copyright 2021 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. #include "ash/components/fwupd/firmware_update_manager.h"
  5. #include <deque>
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "ash/components/fwupd/fake_fwupd_download_client.h"
  10. #include "ash/components/fwupd/histogram_util.h"
  11. #include "ash/constants/ash_features.h"
  12. #include "ash/shell.h"
  13. #include "ash/system/firmware_update/firmware_update_notification_controller.h"
  14. #include "ash/test/ash_test_base.h"
  15. #include "ash/webui/firmware_update_ui/mojom/firmware_update.mojom-test-utils.h"
  16. #include "ash/webui/firmware_update_ui/mojom/firmware_update.mojom.h"
  17. #include "base/files/file.h"
  18. #include "base/files/file_path.h"
  19. #include "base/files/file_util.h"
  20. #include "base/path_service.h"
  21. #include "base/strings/utf_string_conversions.h"
  22. #include "base/test/metrics/histogram_tester.h"
  23. #include "base/test/scoped_feature_list.h"
  24. #include "base/test/task_environment.h"
  25. #include "chromeos/ash/components/dbus/fwupd/fwupd_client.h"
  26. #include "components/user_manager/user_type.h"
  27. #include "dbus/message.h"
  28. #include "dbus/mock_bus.h"
  29. #include "dbus/mock_object_proxy.h"
  30. #include "mojo/public/cpp/bindings/pending_remote.h"
  31. #include "mojo/public/cpp/bindings/receiver.h"
  32. #include "services/network/public/cpp/shared_url_loader_factory.h"
  33. #include "services/network/test/test_url_loader_factory.h"
  34. #include "testing/gmock/include/gmock/gmock.h"
  35. #include "testing/gtest/include/gtest/gtest.h"
  36. #include "ui/message_center/fake_message_center.h"
  37. #include "ui/message_center/message_center.h"
  38. using message_center::MessageCenter;
  39. using message_center::Notification;
  40. using ::testing::_;
  41. using ::testing::Invoke;
  42. using ::testing::Return;
  43. namespace {
  44. const char kFakeDeviceIdForTesting[] = "Fake_Device_ID";
  45. const char kFakeInternalDeviceIdForTesting[] = "Fake_Internal_Device_ID";
  46. const char kFakeDeviceNameForTesting[] = "Fake Device Name";
  47. const char kFakeInternalDeviceNameForTesting[] = "Fake Internal Device Name";
  48. const char kFakeUpdateDescriptionForTesting[] =
  49. "This is a fake update for testing.";
  50. const uint32_t kFakeUpdatePriorityForTesting = 1;
  51. const uint32_t kFakeCriticalUpdatePriorityForTesting = 3;
  52. const char kFakeUpdateVersionForTesting[] = "1.0.0";
  53. const char kFakeUpdateUriForTesting[] =
  54. "file:///usr/share/fwupd/remotes.d/vendor/firmware/testFirmwarePath-V1.cab";
  55. const char kFakeUpdateFileNameForTesting[] = "testFirmwarePath-V1.cab";
  56. const char kEmptyFileSha256ForTesting[] =
  57. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
  58. const char kFilePathIdentifier[] = "file://";
  59. const char kFwupdServiceName[] = "org.freedesktop.fwupd";
  60. const char kFwupdServicePath[] = "/";
  61. const char kDescriptionKey[] = "Description";
  62. const char kIdKey[] = "DeviceId";
  63. const char kNameKey[] = "Name";
  64. const char kPriorityKey[] = "Urgency";
  65. const char kUriKey[] = "Uri";
  66. const char kVersionKey[] = "Version";
  67. const char kChecksumKey[] = "Checksum";
  68. const char kDownloadDir[] = "firmware-updates";
  69. const char kCacheDir[] = "cache";
  70. const char kCabExtension[] = ".cab";
  71. const char kFirmwareUpdateNotificationId[] =
  72. "cros_firmware_update_notification_id";
  73. const char kFlagsKey[] = "Flags";
  74. const uint64_t kFakeFlagForTesting = 1;
  75. void RunResponseCallback(dbus::ObjectProxy::ResponseOrErrorCallback callback,
  76. std::unique_ptr<dbus::Response> response) {
  77. std::move(callback).Run(response.get(), nullptr);
  78. }
  79. class FakeUpdateObserver : public ash::firmware_update::mojom::UpdateObserver {
  80. public:
  81. void OnUpdateListChanged(
  82. std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr>
  83. firmware_updates) override {
  84. updates_ = std::move(firmware_updates);
  85. ++num_times_notified_;
  86. }
  87. mojo::PendingRemote<ash::firmware_update::mojom::UpdateObserver>
  88. pending_remote() {
  89. return receiver_.BindNewPipeAndPassRemote();
  90. }
  91. const std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr>& updates()
  92. const {
  93. return updates_;
  94. }
  95. int num_times_notified() { return num_times_notified_; }
  96. private:
  97. std::vector<ash::firmware_update::mojom::FirmwareUpdatePtr> updates_;
  98. mojo::Receiver<ash::firmware_update::mojom::UpdateObserver> receiver_{this};
  99. int num_times_notified_ = 0;
  100. };
  101. class FakeUpdateProgressObserver
  102. : public ash::firmware_update::mojom::UpdateProgressObserver {
  103. public:
  104. void OnStatusChanged(
  105. ash::firmware_update::mojom::InstallationProgressPtr update) override {
  106. update_ = std::move(update);
  107. }
  108. mojo::PendingRemote<ash::firmware_update::mojom::UpdateProgressObserver>
  109. pending_remote() {
  110. return receiver_.BindNewPipeAndPassRemote();
  111. }
  112. const ash::firmware_update::mojom::InstallationProgressPtr& GetLatestUpdate()
  113. const {
  114. return update_;
  115. }
  116. private:
  117. ash::firmware_update::mojom::InstallationProgressPtr update_;
  118. mojo::Receiver<ash::firmware_update::mojom::UpdateProgressObserver> receiver_{
  119. this};
  120. };
  121. } // namespace
  122. namespace ash {
  123. class FirmwareUpdateManagerTest : public AshTestBase {
  124. public:
  125. FirmwareUpdateManagerTest() {
  126. scoped_feature_list_.InitAndEnableFeature(
  127. ::ash::features::kFirmwareUpdaterApp);
  128. dbus::Bus::Options options;
  129. options.bus_type = dbus::Bus::SYSTEM;
  130. bus_ = base::MakeRefCounted<dbus::MockBus>(options);
  131. dbus::ObjectPath fwupd_service_path(kFwupdServicePath);
  132. proxy_ = base::MakeRefCounted<dbus::MockObjectProxy>(
  133. bus_.get(), kFwupdServiceName, fwupd_service_path);
  134. EXPECT_CALL(*bus_.get(),
  135. GetObjectProxy(kFwupdServiceName, fwupd_service_path))
  136. .WillRepeatedly(testing::Return(proxy_.get()));
  137. EXPECT_CALL(*proxy_, DoConnectToSignal(_, _, _, _))
  138. .WillRepeatedly(Return());
  139. FwupdClient::Initialize(bus_.get());
  140. dbus_client_ = FwupdClient::Get();
  141. fake_fwupd_download_client_ = std::make_unique<FakeFwupdDownloadClient>();
  142. firmware_update_manager_ = std::make_unique<FirmwareUpdateManager>();
  143. firmware_update_manager_->BindInterface(
  144. update_provider_remote_.BindNewPipeAndPassReceiver());
  145. }
  146. FirmwareUpdateManagerTest(const FirmwareUpdateManagerTest&) = delete;
  147. FirmwareUpdateManagerTest& operator=(const FirmwareUpdateManagerTest&) =
  148. delete;
  149. ~FirmwareUpdateManagerTest() override {
  150. // Destructor depends on FirmwareUpdateManager.
  151. firmware_update_notification_controller_.reset();
  152. // Destructor depends on FwupdClient.
  153. firmware_update_manager_.reset();
  154. FwupdClient::Shutdown();
  155. }
  156. void OnMethodCalled(dbus::MethodCall* method_call,
  157. int timeout_ms,
  158. dbus::ObjectProxy::ResponseOrErrorCallback* callback) {
  159. ASSERT_FALSE(dbus_responses_.empty());
  160. auto response = std::move(dbus_responses_.front());
  161. task_environment()->GetMainThreadTaskRunner()->PostTask(
  162. FROM_HERE, base::BindOnce(&RunResponseCallback, std::move(*callback),
  163. std::move(response)));
  164. dbus_responses_.pop_front();
  165. }
  166. protected:
  167. void InitializeNotificationController() {
  168. firmware_update_notification_controller_ =
  169. std::make_unique<FirmwareUpdateNotificationController>(
  170. message_center());
  171. firmware_update_notification_controller_
  172. ->OnFirmwareUpdateManagerInitialized();
  173. }
  174. void StartInstall(const std::string& device_id,
  175. const base::FilePath& filepath) {
  176. base::RunLoop loop;
  177. firmware_update_manager_->StartInstall(
  178. device_id, filepath,
  179. base::BindOnce([](base::OnceClosure done) { std::move(done).Run(); },
  180. loop.QuitClosure()));
  181. loop.Run();
  182. }
  183. void RequestDevices() {
  184. firmware_update_manager_->RequestDevices();
  185. base::RunLoop().RunUntilIdle();
  186. }
  187. void SetFakeUrlForTesting(const std::string& fake_url) {
  188. firmware_update_manager_->SetFakeUrlForTesting(fake_url);
  189. }
  190. message_center::MessageCenter* message_center() const {
  191. return message_center::MessageCenter::Get();
  192. }
  193. std::unique_ptr<dbus::Response> CreateEmptyDeviceResponse() {
  194. auto response = dbus::Response::CreateEmpty();
  195. dbus::MessageWriter response_writer(response.get());
  196. dbus::MessageWriter response_array_writer(nullptr);
  197. dbus::MessageWriter device_array_writer(nullptr);
  198. // The response is an array of arrays of dictionaries. Each dictionary is
  199. // one device description.
  200. response_writer.OpenArray("a{sv}", &response_array_writer);
  201. response_array_writer.OpenArray("{sv}", &device_array_writer);
  202. response_array_writer.CloseContainer(&device_array_writer);
  203. response_writer.CloseContainer(&response_array_writer);
  204. return response;
  205. }
  206. std::unique_ptr<dbus::Response> CreateOneDeviceResponse() {
  207. auto response = dbus::Response::CreateEmpty();
  208. dbus::MessageWriter response_writer(response.get());
  209. dbus::MessageWriter response_array_writer(nullptr);
  210. dbus::MessageWriter device_array_writer(nullptr);
  211. dbus::MessageWriter dict_writer(nullptr);
  212. // The response is an array of arrays of dictionaries. Each dictionary is
  213. // one device description.
  214. response_writer.OpenArray("a{sv}", &response_array_writer);
  215. response_array_writer.OpenArray("{sv}", &device_array_writer);
  216. device_array_writer.OpenDictEntry(&dict_writer);
  217. dict_writer.AppendString(kNameKey);
  218. dict_writer.AppendVariantOfString(kFakeDeviceNameForTesting);
  219. device_array_writer.CloseContainer(&dict_writer);
  220. device_array_writer.OpenDictEntry(&dict_writer);
  221. dict_writer.AppendString(kIdKey);
  222. dict_writer.AppendVariantOfString(kFakeDeviceIdForTesting);
  223. device_array_writer.CloseContainer(&dict_writer);
  224. response_array_writer.CloseContainer(&device_array_writer);
  225. response_writer.CloseContainer(&response_array_writer);
  226. return response;
  227. }
  228. std::unique_ptr<dbus::Response> CreateInternalDeviceResponse() {
  229. auto response = dbus::Response::CreateEmpty();
  230. dbus::MessageWriter response_writer(response.get());
  231. dbus::MessageWriter response_array_writer(nullptr);
  232. dbus::MessageWriter device_array_writer(nullptr);
  233. dbus::MessageWriter dict_writer(nullptr);
  234. // The response is an array of arrays of dictionaries. Each dictionary is
  235. // one device description.
  236. response_writer.OpenArray("a{sv}", &response_array_writer);
  237. response_array_writer.OpenArray("{sv}", &device_array_writer);
  238. device_array_writer.OpenDictEntry(&dict_writer);
  239. dict_writer.AppendString(kNameKey);
  240. dict_writer.AppendVariantOfString(kFakeInternalDeviceNameForTesting);
  241. device_array_writer.CloseContainer(&dict_writer);
  242. device_array_writer.OpenDictEntry(&dict_writer);
  243. dict_writer.AppendString(kIdKey);
  244. dict_writer.AppendVariantOfString(kFakeInternalDeviceIdForTesting);
  245. device_array_writer.CloseContainer(&dict_writer);
  246. device_array_writer.OpenDictEntry(&dict_writer);
  247. dict_writer.AppendString(kFlagsKey);
  248. dict_writer.AppendVariantOfUint64(kFakeFlagForTesting);
  249. device_array_writer.CloseContainer(&dict_writer);
  250. response_array_writer.CloseContainer(&device_array_writer);
  251. response_writer.CloseContainer(&response_array_writer);
  252. return response;
  253. }
  254. std::unique_ptr<dbus::Response> CreateTwoDeviceResponse() {
  255. auto response = dbus::Response::CreateEmpty();
  256. dbus::MessageWriter response_writer(response.get());
  257. dbus::MessageWriter response_array_writer(nullptr);
  258. dbus::MessageWriter device_array_writer(nullptr);
  259. dbus::MessageWriter dict_writer(nullptr);
  260. // The response is an array of arrays of dictionaries. Each dictionary is
  261. // one device description.
  262. response_writer.OpenArray("a{sv}", &response_array_writer);
  263. response_array_writer.OpenArray("{sv}", &device_array_writer);
  264. device_array_writer.OpenDictEntry(&dict_writer);
  265. dict_writer.AppendString(kNameKey);
  266. dict_writer.AppendVariantOfString(std::string(kFakeDeviceNameForTesting) +
  267. "1");
  268. device_array_writer.CloseContainer(&dict_writer);
  269. device_array_writer.OpenDictEntry(&dict_writer);
  270. dict_writer.AppendString(kIdKey);
  271. dict_writer.AppendVariantOfString(std::string(kFakeDeviceIdForTesting) +
  272. "1");
  273. device_array_writer.CloseContainer(&dict_writer);
  274. // Prepare the next device entry.
  275. response_array_writer.CloseContainer(&device_array_writer);
  276. response_array_writer.OpenArray("{sv}", &device_array_writer);
  277. device_array_writer.OpenDictEntry(&dict_writer);
  278. dict_writer.AppendString(kNameKey);
  279. dict_writer.AppendVariantOfString(std::string(kFakeDeviceNameForTesting) +
  280. "2");
  281. device_array_writer.CloseContainer(&dict_writer);
  282. device_array_writer.OpenDictEntry(&dict_writer);
  283. dict_writer.AppendString(kIdKey);
  284. dict_writer.AppendVariantOfString(std::string(kFakeDeviceIdForTesting) +
  285. "2");
  286. device_array_writer.CloseContainer(&dict_writer);
  287. response_array_writer.CloseContainer(&device_array_writer);
  288. response_writer.CloseContainer(&response_array_writer);
  289. return response;
  290. }
  291. std::unique_ptr<dbus::Response> CreateOneUpdateResponse() {
  292. auto response = dbus::Response::CreateEmpty();
  293. dbus::MessageWriter response_writer(response.get());
  294. dbus::MessageWriter response_array_writer(nullptr);
  295. dbus::MessageWriter device_array_writer(nullptr);
  296. dbus::MessageWriter dict_writer(nullptr);
  297. // The response is an array of arrays of dictionaries. Each dictionary is
  298. // one device description.
  299. response_writer.OpenArray("a{sv}", &response_array_writer);
  300. response_array_writer.OpenArray("{sv}", &device_array_writer);
  301. device_array_writer.OpenDictEntry(&dict_writer);
  302. dict_writer.AppendString(kDescriptionKey);
  303. dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
  304. device_array_writer.CloseContainer(&dict_writer);
  305. device_array_writer.OpenDictEntry(&dict_writer);
  306. dict_writer.AppendString(kVersionKey);
  307. dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
  308. device_array_writer.CloseContainer(&dict_writer);
  309. device_array_writer.OpenDictEntry(&dict_writer);
  310. dict_writer.AppendString(kPriorityKey);
  311. dict_writer.AppendVariantOfUint32(kFakeUpdatePriorityForTesting);
  312. device_array_writer.CloseContainer(&dict_writer);
  313. device_array_writer.OpenDictEntry(&dict_writer);
  314. dict_writer.AppendString(kUriKey);
  315. dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
  316. device_array_writer.CloseContainer(&dict_writer);
  317. device_array_writer.OpenDictEntry(&dict_writer);
  318. dict_writer.AppendString(kChecksumKey);
  319. dict_writer.AppendVariantOfString(kEmptyFileSha256ForTesting);
  320. device_array_writer.CloseContainer(&dict_writer);
  321. response_array_writer.CloseContainer(&device_array_writer);
  322. response_writer.CloseContainer(&response_array_writer);
  323. return response;
  324. }
  325. std::unique_ptr<dbus::Response> CreateOneUpdateResponseWithChecksum(
  326. const std::string& checksum) {
  327. auto response = dbus::Response::CreateEmpty();
  328. dbus::MessageWriter response_writer(response.get());
  329. dbus::MessageWriter response_array_writer(nullptr);
  330. dbus::MessageWriter device_array_writer(nullptr);
  331. dbus::MessageWriter dict_writer(nullptr);
  332. // The response is an array of arrays of dictionaries. Each dictionary is
  333. // one device description.
  334. response_writer.OpenArray("a{sv}", &response_array_writer);
  335. response_array_writer.OpenArray("{sv}", &device_array_writer);
  336. device_array_writer.OpenDictEntry(&dict_writer);
  337. dict_writer.AppendString(kDescriptionKey);
  338. dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
  339. device_array_writer.CloseContainer(&dict_writer);
  340. device_array_writer.OpenDictEntry(&dict_writer);
  341. dict_writer.AppendString(kVersionKey);
  342. dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
  343. device_array_writer.CloseContainer(&dict_writer);
  344. device_array_writer.OpenDictEntry(&dict_writer);
  345. dict_writer.AppendString(kPriorityKey);
  346. dict_writer.AppendVariantOfUint32(kFakeUpdatePriorityForTesting);
  347. device_array_writer.CloseContainer(&dict_writer);
  348. device_array_writer.OpenDictEntry(&dict_writer);
  349. dict_writer.AppendString(kUriKey);
  350. dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
  351. device_array_writer.CloseContainer(&dict_writer);
  352. device_array_writer.OpenDictEntry(&dict_writer);
  353. dict_writer.AppendString(kChecksumKey);
  354. dict_writer.AppendVariantOfString(checksum);
  355. device_array_writer.CloseContainer(&dict_writer);
  356. response_array_writer.CloseContainer(&device_array_writer);
  357. response_writer.CloseContainer(&response_array_writer);
  358. return response;
  359. }
  360. std::unique_ptr<dbus::Response> CreateOneCriticalUpdateResponse() {
  361. auto response = dbus::Response::CreateEmpty();
  362. dbus::MessageWriter response_writer(response.get());
  363. dbus::MessageWriter response_array_writer(nullptr);
  364. dbus::MessageWriter device_array_writer(nullptr);
  365. dbus::MessageWriter dict_writer(nullptr);
  366. // The response is an array of arrays of dictionaries. Each dictionary is
  367. // one device description.
  368. response_writer.OpenArray("a{sv}", &response_array_writer);
  369. response_array_writer.OpenArray("{sv}", &device_array_writer);
  370. device_array_writer.OpenDictEntry(&dict_writer);
  371. dict_writer.AppendString(kDescriptionKey);
  372. dict_writer.AppendVariantOfString(kFakeUpdateDescriptionForTesting);
  373. device_array_writer.CloseContainer(&dict_writer);
  374. device_array_writer.OpenDictEntry(&dict_writer);
  375. dict_writer.AppendString(kVersionKey);
  376. dict_writer.AppendVariantOfString(kFakeUpdateVersionForTesting);
  377. device_array_writer.CloseContainer(&dict_writer);
  378. device_array_writer.OpenDictEntry(&dict_writer);
  379. dict_writer.AppendString(kPriorityKey);
  380. dict_writer.AppendVariantOfUint32(kFakeCriticalUpdatePriorityForTesting);
  381. device_array_writer.CloseContainer(&dict_writer);
  382. device_array_writer.OpenDictEntry(&dict_writer);
  383. dict_writer.AppendString(kUriKey);
  384. dict_writer.AppendVariantOfString(kFakeUpdateUriForTesting);
  385. device_array_writer.CloseContainer(&dict_writer);
  386. device_array_writer.OpenDictEntry(&dict_writer);
  387. dict_writer.AppendString(kChecksumKey);
  388. dict_writer.AppendVariantOfString(kEmptyFileSha256ForTesting);
  389. device_array_writer.CloseContainer(&dict_writer);
  390. response_array_writer.CloseContainer(&device_array_writer);
  391. response_writer.CloseContainer(&response_array_writer);
  392. return response;
  393. }
  394. std::unique_ptr<dbus::Response> CreateNoUpdateResponse() {
  395. auto response = dbus::Response::CreateEmpty();
  396. dbus::MessageWriter response_writer(response.get());
  397. dbus::MessageWriter response_array_writer(nullptr);
  398. dbus::MessageWriter device_array_writer(nullptr);
  399. // The response is an array of arrays of dictionaries. Each dictionary is
  400. // one device description.
  401. response_writer.OpenArray("a{sv}", &response_array_writer);
  402. response_array_writer.OpenArray("{sv}", &device_array_writer);
  403. response_array_writer.CloseContainer(&device_array_writer);
  404. response_writer.CloseContainer(&response_array_writer);
  405. return response;
  406. }
  407. std::unique_ptr<dbus::Response> CreateBoolResponse(bool success) {
  408. auto response = dbus::Response::CreateEmpty();
  409. dbus::MessageWriter response_writer(response.get());
  410. response_writer.AppendBool(success);
  411. return response;
  412. }
  413. void SetupObserver(FakeUpdateObserver* observer) {
  414. firmware_update_manager_->ObservePeripheralUpdates(
  415. observer->pending_remote());
  416. base::RunLoop().RunUntilIdle();
  417. }
  418. network::TestURLLoaderFactory& GetTestUrlLoaderFactory() {
  419. return fake_fwupd_download_client_->test_url_loader_factory();
  420. }
  421. void SetupProgressObserver(FakeUpdateProgressObserver* observer) {
  422. install_controller_remote_->AddObserver(observer->pending_remote());
  423. base::RunLoop().RunUntilIdle();
  424. }
  425. bool PrepareForUpdate(const std::string& device_id) {
  426. mojo::PendingRemote<ash::firmware_update::mojom::InstallController>
  427. pending_remote;
  428. ash::firmware_update::mojom::UpdateProviderAsyncWaiter(
  429. update_provider_remote_.get())
  430. .PrepareForUpdate(device_id, &pending_remote);
  431. if (!pending_remote.is_valid())
  432. return false;
  433. install_controller_remote_.Bind(std::move(pending_remote));
  434. base::RunLoop().RunUntilIdle();
  435. return true;
  436. }
  437. void SetProperties(uint32_t percentage, uint32_t status) {
  438. dbus_client_->SetPropertiesForTesting(percentage, status);
  439. base::RunLoop().RunUntilIdle();
  440. }
  441. void BeginUpdate(const std::string& device_id,
  442. const base::FilePath& filepath) {
  443. firmware_update_manager_->BeginUpdate(device_id, filepath);
  444. }
  445. void RequestAllUpdates() { firmware_update_manager_->RequestAllUpdates(); }
  446. // `FwupdClient` must be be before `FirmwareUpdateManager`.
  447. FwupdClient* dbus_client_ = nullptr;
  448. std::unique_ptr<FakeFwupdDownloadClient> fake_fwupd_download_client_;
  449. std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
  450. // `FirmwareUpdateNotificationController` must be be after
  451. // `FirmwareUpdateManager` to ensure that
  452. // `FirmwareUpdateNotificationController` is removed as an observer before
  453. // `FirmwareUpdateManager` is destroyed.
  454. std::unique_ptr<FirmwareUpdateNotificationController>
  455. firmware_update_notification_controller_;
  456. mojo::Remote<ash::firmware_update::mojom::UpdateProvider>
  457. update_provider_remote_;
  458. mojo::Remote<ash::firmware_update::mojom::InstallController>
  459. install_controller_remote_;
  460. // Mock bus for simulating calls.
  461. scoped_refptr<dbus::MockBus> bus_;
  462. scoped_refptr<dbus::MockObjectProxy> proxy_;
  463. // Fake responses.
  464. std::deque<std::unique_ptr<dbus::Response>> dbus_responses_;
  465. base::test::ScopedFeatureList scoped_feature_list_;
  466. };
  467. TEST_F(FirmwareUpdateManagerTest, CorrectMockInstance) {
  468. EXPECT_EQ(dbus_client_, FwupdClient::Get());
  469. }
  470. TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesNoDevices) {
  471. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  472. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  473. dbus_responses_.push_back(CreateEmptyDeviceResponse());
  474. FakeUpdateObserver update_observer;
  475. SetupObserver(&update_observer);
  476. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  477. update_observer.updates();
  478. base::RunLoop().RunUntilIdle();
  479. EXPECT_TRUE(updates.empty());
  480. ASSERT_EQ(0U, firmware_update_manager_->GetUpdateCount());
  481. }
  482. TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesOneDeviceNoUpdates) {
  483. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  484. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  485. dbus_responses_.push_back(CreateOneDeviceResponse());
  486. dbus_responses_.push_back(CreateNoUpdateResponse());
  487. FakeUpdateObserver update_observer;
  488. SetupObserver(&update_observer);
  489. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  490. update_observer.updates();
  491. base::RunLoop().RunUntilIdle();
  492. EXPECT_TRUE(updates.empty());
  493. ASSERT_EQ(0U, firmware_update_manager_->GetUpdateCount());
  494. }
  495. TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesOneDeviceOneUpdate) {
  496. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  497. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  498. dbus_responses_.push_back(CreateOneDeviceResponse());
  499. dbus_responses_.push_back(CreateOneUpdateResponse());
  500. FakeUpdateObserver update_observer;
  501. SetupObserver(&update_observer);
  502. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  503. update_observer.updates();
  504. base::RunLoop().RunUntilIdle();
  505. ASSERT_EQ(1U, updates.size());
  506. ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
  507. EXPECT_EQ(kFakeDeviceIdForTesting, updates[0]->device_id);
  508. EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeDeviceNameForTesting)),
  509. updates[0]->device_name);
  510. EXPECT_EQ(kFakeUpdateVersionForTesting, updates[0]->device_version);
  511. EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeUpdateDescriptionForTesting)),
  512. updates[0]->device_description);
  513. EXPECT_EQ(ash::firmware_update::mojom::UpdatePriority(
  514. kFakeUpdatePriorityForTesting),
  515. updates[0]->priority);
  516. EXPECT_EQ(kFakeUpdateUriForTesting, updates[0]->filepath.value());
  517. }
  518. TEST_F(FirmwareUpdateManagerTest, RequestUpdatesClearsCache) {
  519. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  520. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  521. dbus_responses_.push_back(CreateOneDeviceResponse());
  522. dbus_responses_.push_back(CreateOneUpdateResponse());
  523. FakeUpdateObserver update_observer;
  524. SetupObserver(&update_observer);
  525. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  526. update_observer.updates();
  527. base::RunLoop().RunUntilIdle();
  528. ASSERT_EQ(1U, updates.size());
  529. ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
  530. dbus_responses_.push_back(CreateOneDeviceResponse());
  531. dbus_responses_.push_back(CreateOneUpdateResponse());
  532. RequestDevices();
  533. // Expect cache to clear and only 1 updates now instead of 2.
  534. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& new_updates =
  535. update_observer.updates();
  536. ASSERT_EQ(1U, new_updates.size());
  537. ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
  538. }
  539. TEST_F(FirmwareUpdateManagerTest, RequestAllUpdatesTwoDeviceOneWithUpdate) {
  540. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  541. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  542. dbus_responses_.push_back(CreateTwoDeviceResponse());
  543. dbus_responses_.push_back(CreateNoUpdateResponse());
  544. dbus_responses_.push_back(CreateOneUpdateResponse());
  545. FakeUpdateObserver update_observer;
  546. SetupObserver(&update_observer);
  547. base::RunLoop().RunUntilIdle();
  548. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  549. update_observer.updates();
  550. base::RunLoop().RunUntilIdle();
  551. ASSERT_EQ(1U, updates.size());
  552. ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
  553. // The second device was the one with the update.
  554. EXPECT_EQ(std::string(kFakeDeviceIdForTesting) + "2", updates[0]->device_id);
  555. EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeDeviceNameForTesting) + "2"),
  556. updates[0]->device_name);
  557. EXPECT_EQ(kFakeUpdateVersionForTesting, updates[0]->device_version);
  558. EXPECT_EQ(base::UTF8ToUTF16(std::string(kFakeUpdateDescriptionForTesting)),
  559. updates[0]->device_description);
  560. EXPECT_EQ(ash::firmware_update::mojom::UpdatePriority(
  561. kFakeUpdatePriorityForTesting),
  562. updates[0]->priority);
  563. }
  564. TEST_F(FirmwareUpdateManagerTest, RequestUpdatesMutipleTimes) {
  565. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  566. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  567. dbus_responses_.push_back(CreateTwoDeviceResponse());
  568. dbus_responses_.push_back(CreateNoUpdateResponse());
  569. dbus_responses_.push_back(CreateOneUpdateResponse());
  570. FakeUpdateObserver update_observer;
  571. SetupObserver(&update_observer);
  572. base::RunLoop().RunUntilIdle();
  573. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  574. update_observer.updates();
  575. base::RunLoop().RunUntilIdle();
  576. ASSERT_EQ(1, update_observer.num_times_notified());
  577. ASSERT_EQ(1U, updates.size());
  578. ASSERT_EQ(1U, firmware_update_manager_->GetUpdateCount());
  579. // Request all updates multiple times, this time while a request is already
  580. // being made.
  581. dbus_responses_.push_back(CreateOneDeviceResponse());
  582. dbus_responses_.push_back(CreateOneUpdateResponse());
  583. RequestAllUpdates();
  584. RequestAllUpdates();
  585. base::RunLoop().RunUntilIdle();
  586. // Expect only one additional RequestAllUpdates() to go through.
  587. ASSERT_EQ(1U, updates.size());
  588. ASSERT_EQ(2, update_observer.num_times_notified());
  589. // Now request all updates again, this time after the previous request has
  590. // been completed.
  591. dbus_responses_.push_back(CreateOneDeviceResponse());
  592. dbus_responses_.push_back(CreateOneUpdateResponse());
  593. RequestAllUpdates();
  594. base::RunLoop().RunUntilIdle();
  595. // Expect another additional RequestAllUpdates() to go through.
  596. ASSERT_EQ(1U, updates.size());
  597. ASSERT_EQ(3, update_observer.num_times_notified());
  598. }
  599. TEST_F(FirmwareUpdateManagerTest, RequestInstall) {
  600. base::HistogramTester histogram_tester;
  601. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  602. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  603. dbus_responses_.push_back(CreateOneDeviceResponse());
  604. dbus_responses_.push_back(CreateOneUpdateResponse());
  605. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  606. // Add dbus response for RequestAllUpdates() call made after an install
  607. // is completed.
  608. dbus_responses_.push_back(CreateOneDeviceResponse());
  609. dbus_responses_.push_back(CreateOneUpdateResponse());
  610. FakeUpdateObserver update_observer;
  611. SetupObserver(&update_observer);
  612. ASSERT_EQ(1, update_observer.num_times_notified());
  613. const std::string fake_url =
  614. std::string("https://faketesturl/") + kFakeUpdateFileNameForTesting;
  615. std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
  616. SetFakeUrlForTesting(fake_url);
  617. GetTestUrlLoaderFactory().AddResponse(fake_url, "");
  618. EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
  619. FakeUpdateProgressObserver update_progress_observer;
  620. SetupProgressObserver(&update_progress_observer);
  621. StartInstall(std::string(kFakeDeviceIdForTesting), base::FilePath(fake_url));
  622. base::RunLoop().RunUntilIdle();
  623. base::FilePath root_dir;
  624. CHECK(base::PathService::Get(base::DIR_TEMP, &root_dir));
  625. const base::FilePath root_path =
  626. root_dir.Append(FILE_PATH_LITERAL(kDownloadDir))
  627. .Append(FILE_PATH_LITERAL(kCacheDir));
  628. const std::string test_filename =
  629. std::string(kFakeDeviceIdForTesting) + std::string(kCabExtension);
  630. base::FilePath full_path = root_path.Append(test_filename);
  631. // TODO(jimmyxgong): Check that the file was created. Tests are failing
  632. // because file isn't created initially.
  633. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kSuccess,
  634. update_progress_observer.GetLatestUpdate()->state);
  635. // Expect RequestAllUpdates() to have been called after an install to refresh
  636. // the update list.
  637. ASSERT_EQ(2, update_observer.num_times_notified());
  638. histogram_tester.ExpectUniqueSample(
  639. "ChromeOS.FirmwareUpdateUi.InstallResult",
  640. firmware_update::metrics::FirmwareUpdateInstallResult::kSuccess, 1);
  641. }
  642. TEST_F(FirmwareUpdateManagerTest, RequestInstallLocalPatch) {
  643. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  644. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  645. dbus_responses_.push_back(CreateOneDeviceResponse());
  646. dbus_responses_.push_back(CreateOneUpdateResponse());
  647. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  648. // Add dbus response for RequestAllUpdates() call made after an install
  649. // is completed.
  650. dbus_responses_.push_back(CreateOneDeviceResponse());
  651. dbus_responses_.push_back(CreateOneUpdateResponse());
  652. FakeUpdateObserver update_observer;
  653. SetupObserver(&update_observer);
  654. base::RunLoop().RunUntilIdle();
  655. base::FilePath root_dir;
  656. CHECK(base::PathService::Get(base::DIR_TEMP, &root_dir));
  657. const base::FilePath root_path =
  658. root_dir.Append(FILE_PATH_LITERAL(kDownloadDir))
  659. .Append(FILE_PATH_LITERAL(kCacheDir));
  660. const std::string test_filename =
  661. std::string(kFakeDeviceIdForTesting) + std::string(kCabExtension);
  662. base::FilePath full_path = root_path.Append(test_filename);
  663. // Create a temporary file to simulate a .cab available for install.
  664. base::WriteFile(full_path, "", 0);
  665. EXPECT_TRUE(base::PathExists(full_path));
  666. const std::string uri = kFilePathIdentifier + full_path.value();
  667. EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
  668. FakeUpdateProgressObserver update_progress_observer;
  669. SetupProgressObserver(&update_progress_observer);
  670. StartInstall(std::string(kFakeDeviceIdForTesting), base::FilePath(uri));
  671. base::RunLoop().RunUntilIdle();
  672. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kSuccess,
  673. update_progress_observer.GetLatestUpdate()->state);
  674. }
  675. TEST_F(FirmwareUpdateManagerTest, OnPropertiesChangedResponse) {
  676. EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
  677. FakeUpdateProgressObserver update_progress_observer;
  678. SetupProgressObserver(&update_progress_observer);
  679. // Initial state.
  680. SetProperties(/**percentage=*/0u, /**status=*/0u);
  681. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUnknown,
  682. update_progress_observer.GetLatestUpdate()->state);
  683. EXPECT_EQ(0u, update_progress_observer.GetLatestUpdate()->percentage);
  684. // Install in progress.
  685. SetProperties(/**percentage=*/1u, /**status=*/5u);
  686. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUpdating,
  687. update_progress_observer.GetLatestUpdate()->state);
  688. EXPECT_EQ(1u, update_progress_observer.GetLatestUpdate()->percentage);
  689. // Device restarting
  690. SetProperties(/**percentage=*/100u, /**status=*/4u);
  691. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kRestarting,
  692. update_progress_observer.GetLatestUpdate()->state);
  693. EXPECT_EQ(100u, update_progress_observer.GetLatestUpdate()->percentage);
  694. // Emitted once install is completed and device has been restarted.
  695. SetProperties(/**percentage=*/100u, /**status=*/0u);
  696. EXPECT_EQ(ash::firmware_update::mojom::UpdateState::kUnknown,
  697. update_progress_observer.GetLatestUpdate()->state);
  698. EXPECT_EQ(100u, update_progress_observer.GetLatestUpdate()->percentage);
  699. }
  700. TEST_F(FirmwareUpdateManagerTest, InvalidFile) {
  701. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  702. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  703. dbus_responses_.push_back(CreateOneDeviceResponse());
  704. dbus_responses_.push_back(CreateOneUpdateResponse());
  705. FakeUpdateObserver update_observer;
  706. SetupObserver(&update_observer);
  707. base::RunLoop().RunUntilIdle();
  708. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  709. std::string fake_url = "https://faketesturl/";
  710. std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
  711. SetFakeUrlForTesting(fake_url);
  712. GetTestUrlLoaderFactory().AddResponse(fake_url, "");
  713. EXPECT_TRUE(PrepareForUpdate(std::string(kFakeDeviceIdForTesting)));
  714. FakeUpdateProgressObserver update_progress_observer;
  715. SetupProgressObserver(&update_progress_observer);
  716. BeginUpdate(std::string(kFakeDeviceIdForTesting),
  717. base::FilePath("BadTestFilename@#.cab"));
  718. base::RunLoop().RunUntilIdle();
  719. // An invalid filepath will never trigger the install. Expect no updates
  720. // progress to be available.
  721. EXPECT_TRUE(!update_progress_observer.GetLatestUpdate());
  722. }
  723. TEST_F(FirmwareUpdateManagerTest, InvalidChecksum) {
  724. base::HistogramTester histogram_tester;
  725. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  726. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  727. dbus_responses_.push_back(CreateOneDeviceResponse());
  728. dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(
  729. "badbbadbad1ef97238fb24c5e40a979bc544bb2b0967b863e43e7d58e0d9a923f"));
  730. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  731. FakeUpdateObserver update_observer;
  732. SetupObserver(&update_observer);
  733. ASSERT_EQ(1, update_observer.num_times_notified());
  734. // No updates available since checksum is empty.
  735. const auto& updates = update_observer.updates();
  736. ASSERT_TRUE(updates.empty());
  737. }
  738. TEST_F(FirmwareUpdateManagerTest, EmptyChecksum) {
  739. base::HistogramTester histogram_tester;
  740. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  741. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  742. dbus_responses_.push_back(CreateOneDeviceResponse());
  743. dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(""));
  744. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  745. FakeUpdateObserver update_observer;
  746. SetupObserver(&update_observer);
  747. ASSERT_EQ(1, update_observer.num_times_notified());
  748. // No updates available since checksum is empty.
  749. const auto& updates = update_observer.updates();
  750. ASSERT_TRUE(updates.empty());
  751. }
  752. TEST_F(FirmwareUpdateManagerTest, WrongChecksumVariant) {
  753. base::HistogramTester histogram_tester;
  754. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  755. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  756. dbus_responses_.push_back(CreateOneDeviceResponse());
  757. dbus_responses_.push_back(CreateOneUpdateResponseWithChecksum(
  758. "badbbadbad1ef97238fb24c5e40a979bc544bb2b"));
  759. dbus_responses_.push_back(dbus::Response::CreateEmpty());
  760. FakeUpdateObserver update_observer;
  761. SetupObserver(&update_observer);
  762. ASSERT_EQ(1, update_observer.num_times_notified());
  763. // No updates available since checksum is empty.
  764. const auto& updates = update_observer.updates();
  765. ASSERT_TRUE(updates.empty());
  766. }
  767. TEST_F(FirmwareUpdateManagerTest, NotificationShownForCriticalUpdate) {
  768. InitializeNotificationController();
  769. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  770. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  771. dbus_responses_.push_back(CreateOneDeviceResponse());
  772. dbus_responses_.push_back(CreateOneCriticalUpdateResponse());
  773. FakeUpdateObserver update_observer;
  774. SetupObserver(&update_observer);
  775. EXPECT_TRUE(message_center()->FindVisibleNotificationById(
  776. kFirmwareUpdateNotificationId));
  777. message_center()->RemoveNotification(kFirmwareUpdateNotificationId,
  778. /*by_user=*/true);
  779. dbus_responses_.push_back(CreateOneDeviceResponse());
  780. dbus_responses_.push_back(CreateOneCriticalUpdateResponse());
  781. RequestDevices();
  782. base::RunLoop().RunUntilIdle();
  783. // Request updates again and verify that the notification is not being
  784. // shown multiple times for the same update.
  785. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  786. kFirmwareUpdateNotificationId));
  787. }
  788. TEST_F(FirmwareUpdateManagerTest, NotificationNotShownIfNoCriticalUpdates) {
  789. InitializeNotificationController();
  790. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  791. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  792. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  793. kFirmwareUpdateNotificationId));
  794. dbus_responses_.push_back(CreateOneDeviceResponse());
  795. dbus_responses_.push_back(CreateOneUpdateResponse());
  796. FakeUpdateObserver update_observer;
  797. SetupObserver(&update_observer);
  798. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  799. kFirmwareUpdateNotificationId));
  800. }
  801. TEST_F(FirmwareUpdateManagerTest, DeviceCountMetric) {
  802. base::HistogramTester histogram_tester;
  803. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  804. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  805. dbus_responses_.push_back(CreateOneDeviceResponse());
  806. dbus_responses_.push_back(CreateOneUpdateResponse());
  807. dbus_responses_.push_back(CreateOneDeviceResponse());
  808. dbus_responses_.push_back(CreateOneUpdateResponse());
  809. FakeUpdateObserver update_observer;
  810. SetupObserver(&update_observer);
  811. histogram_tester.ExpectUniqueSample(
  812. "ChromeOS.FirmwareUpdateUi.OnStartup.DeviceCount", 1, 1);
  813. RequestDevices();
  814. histogram_tester.ExpectUniqueSample(
  815. "ChromeOS.FirmwareUpdateUi.OnRefresh.DeviceCount", 1, 1);
  816. }
  817. TEST_F(FirmwareUpdateManagerTest, UpdateCountMetric) {
  818. base::HistogramTester histogram_tester;
  819. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  820. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  821. dbus_responses_.push_back(CreateOneDeviceResponse());
  822. dbus_responses_.push_back(CreateOneUpdateResponse());
  823. dbus_responses_.push_back(CreateOneDeviceResponse());
  824. dbus_responses_.push_back(CreateOneUpdateResponse());
  825. FakeUpdateObserver update_observer;
  826. SetupObserver(&update_observer);
  827. histogram_tester.ExpectBucketCount(
  828. "ChromeOS.FirmwareUpdateUi.OnStartup.CriticalUpdateCount", 0, 1);
  829. histogram_tester.ExpectBucketCount(
  830. "ChromeOS.FirmwareUpdateUi.OnStartup.UpdateCount", 1, 1);
  831. RequestDevices();
  832. histogram_tester.ExpectBucketCount(
  833. "ChromeOS.FirmwareUpdateUi.OnRefresh.CriticalUpdateCount", 0, 1);
  834. histogram_tester.ExpectBucketCount(
  835. "ChromeOS.FirmwareUpdateUi.OnRefresh.UpdateCount", 1, 1);
  836. }
  837. class FirmwareUpdateStartupNotificationTest : public NoSessionAshTestBase {
  838. public:
  839. FirmwareUpdateStartupNotificationTest() = default;
  840. ~FirmwareUpdateStartupNotificationTest() override = default;
  841. void SetUp() override {
  842. FwupdClient::InitializeFake();
  843. dbus_client_ = FwupdClient::Get();
  844. firmware_update_manager_ = std::make_unique<FirmwareUpdateManager>();
  845. EXPECT_TRUE(FirmwareUpdateManager::IsInitialized());
  846. SetShouldShowNotificationForTest(true);
  847. NoSessionAshTestBase::SetUp();
  848. }
  849. void TearDown() override {
  850. firmware_update_notification_controller_.reset();
  851. firmware_update_manager_.reset();
  852. FwupdClient::Shutdown();
  853. NoSessionAshTestBase::TearDown();
  854. }
  855. protected:
  856. void InitializeNotificationController() {
  857. firmware_update_notification_controller_ =
  858. std::make_unique<FirmwareUpdateNotificationController>(
  859. message_center());
  860. firmware_update_notification_controller_
  861. ->OnFirmwareUpdateManagerInitialized();
  862. }
  863. message_center::MessageCenter* message_center() const {
  864. return message_center::MessageCenter::Get();
  865. }
  866. message_center::Notification* FindShortcutsChangedNotification() {
  867. return message_center::MessageCenter::Get()->FindVisibleNotificationById(
  868. kFirmwareUpdateNotificationId);
  869. }
  870. void SetShouldShowNotificationForTest(bool show_notification) {
  871. FirmwareUpdateManager::Get()->set_should_show_notification_for_test(
  872. show_notification);
  873. }
  874. void SimulateFetchingUpdates() {
  875. FirmwareUpdateManager::Get()->RequestAllUpdates();
  876. }
  877. FwupdClient* dbus_client_ = nullptr;
  878. std::unique_ptr<FirmwareUpdateManager> firmware_update_manager_;
  879. std::unique_ptr<FirmwareUpdateNotificationController>
  880. firmware_update_notification_controller_;
  881. };
  882. TEST_F(FirmwareUpdateStartupNotificationTest,
  883. StartupNotificationShownRegularUser) {
  884. // Notification should be shown at login.
  885. SimulateUserLogin("user1@email.com");
  886. InitializeNotificationController();
  887. SimulateFetchingUpdates();
  888. EXPECT_TRUE(message_center()->FindVisibleNotificationById(
  889. kFirmwareUpdateNotificationId));
  890. }
  891. TEST_F(FirmwareUpdateStartupNotificationTest,
  892. StartupNotificationShownGuestUser) {
  893. // Notification should not be shown at login if the user is a guest.
  894. SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_GUEST);
  895. InitializeNotificationController();
  896. SimulateFetchingUpdates();
  897. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  898. kFirmwareUpdateNotificationId));
  899. }
  900. TEST_F(FirmwareUpdateStartupNotificationTest, StartupNotificationShownKiosk) {
  901. // Notification should not be shown at login if the user is in kiosk mode.
  902. SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_KIOSK_APP);
  903. InitializeNotificationController();
  904. SimulateFetchingUpdates();
  905. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  906. kFirmwareUpdateNotificationId));
  907. }
  908. TEST_F(FirmwareUpdateStartupNotificationTest,
  909. StartupNotificationShownKioskPWA) {
  910. // Notification should not be shown at login if the user is in kiosk mode.
  911. SimulateUserLogin("user1@email.com", user_manager::USER_TYPE_WEB_KIOSK_APP);
  912. InitializeNotificationController();
  913. SimulateFetchingUpdates();
  914. EXPECT_FALSE(message_center()->FindVisibleNotificationById(
  915. kFirmwareUpdateNotificationId));
  916. }
  917. TEST_F(FirmwareUpdateManagerTest, InternalDeviceFiltered) {
  918. EXPECT_CALL(*proxy_, DoCallMethodWithErrorResponse(_, _, _))
  919. .WillRepeatedly(Invoke(this, &FirmwareUpdateManagerTest::OnMethodCalled));
  920. dbus_responses_.push_back(CreateOneDeviceResponse());
  921. dbus_responses_.push_back(CreateOneUpdateResponse());
  922. dbus_responses_.push_back(CreateInternalDeviceResponse());
  923. dbus_responses_.push_back(CreateOneUpdateResponse());
  924. FakeUpdateObserver update_observer;
  925. SetupObserver(&update_observer);
  926. const std::vector<firmware_update::mojom::FirmwareUpdatePtr>& updates =
  927. update_observer.updates();
  928. base::RunLoop().RunUntilIdle();
  929. ASSERT_EQ(1U, updates.size());
  930. EXPECT_EQ(kFakeDeviceIdForTesting, updates[0]->device_id);
  931. }
  932. } // namespace ash