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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
|
// Copyright 2014 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/browser/google/google_update_win.h"
#include <windows.h>
#include <wrl/client.h>
#include <memory>
#include <string>
#include "base/base_paths.h"
#include "base/containers/queue.h"
#include "base/functional/bind.h"
#include "base/i18n/string_search.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_path_override.h"
#include "base/test/test_reg_util_win.h"
#include "base/test/test_simple_task_runner.h"
#include "base/version.h"
#include "base/win/atl.h"
#include "base/win/registry.h"
#include "chrome/browser/google/switches.h"
#include "chrome/common/chrome_version.h"
#include "chrome/install_static/test/scoped_install_details.h"
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/updater/app/server/win/updater_legacy_idl.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/win/atl_module.h"
using ::testing::_;
using ::testing::AllOfArray;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::IsEmpty;
using ::testing::Return;
using ::testing::Sequence;
using ::testing::SetArgPointee;
using ::testing::StrEq;
using ::testing::StrictMock;
using ::testing::Values;
namespace {
// GMock's HasSubstr does not support std::u16string. Hence redefine it as a
// generic matcher.
MATCHER_P(HasSubstr, str, "") {
return arg.find(str) != arg.npos;
}
MATCHER_P(HasSubstrCaseInsensitive, str, "") {
return base::i18n::StringSearchIgnoringCaseAndAccents(str, arg, nullptr,
nullptr);
}
class MockUpdateCheckDelegate : public UpdateCheckDelegate {
public:
MockUpdateCheckDelegate() : weak_ptr_factory_(this) {}
MockUpdateCheckDelegate(const MockUpdateCheckDelegate&) = delete;
MockUpdateCheckDelegate& operator=(const MockUpdateCheckDelegate&) = delete;
base::WeakPtr<UpdateCheckDelegate> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
MOCK_METHOD1(OnUpdateCheckComplete, void(const std::u16string&));
MOCK_METHOD2(OnUpgradeProgress, void(int, const std::u16string&));
MOCK_METHOD1(OnUpgradeComplete, void(const std::u16string&));
MOCK_METHOD3(OnError,
void(GoogleUpdateErrorCode,
const std::u16string&,
const std::u16string&));
private:
base::WeakPtrFactory<UpdateCheckDelegate> weak_ptr_factory_;
};
// An interface that exposes a factory method for creating an IGoogleUpdate3Web
// instance.
class GoogleUpdateFactory {
public:
virtual ~GoogleUpdateFactory() = default;
virtual HRESULT Create(
Microsoft::WRL::ComPtr<IGoogleUpdate3Web>* google_update) = 0;
};
class MockCurrentState : public CComObjectRootEx<CComSingleThreadModel>,
public ICurrentState {
public:
BEGIN_COM_MAP(MockCurrentState)
COM_INTERFACE_ENTRY(ICurrentState)
END_COM_MAP()
MockCurrentState() = default;
MockCurrentState(const MockCurrentState&) = delete;
MockCurrentState& operator=(const MockCurrentState&) = delete;
// Adds an expectation for get_completionMessage that will return the given
// message any number of times.
void ExpectCompletionMessage(const std::u16string& completion_message) {
completion_message_ = completion_message;
EXPECT_CALL(*this, get_completionMessage(_))
.WillRepeatedly(
::testing::Invoke(this, &MockCurrentState::GetCompletionMessage));
}
HRESULT GetCompletionMessage(BSTR* completion_message) {
*completion_message = SysAllocString(base::as_wcstr(completion_message_));
return S_OK;
}
// Adds an expectation for get_availableVersion that will return the given
// version any number of times.
void ExpectAvailableVersion(const std::u16string& available_version) {
available_version_ = available_version;
EXPECT_CALL(*this, get_availableVersion(_))
.WillRepeatedly(
::testing::Invoke(this, &MockCurrentState::GetAvailableVersion));
}
HRESULT GetAvailableVersion(BSTR* available_version) {
*available_version = SysAllocString(base::as_wcstr(available_version_));
return S_OK;
}
// ICurrentState:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, get_stateValue, HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_availableVersion,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_bytesDownloaded,
HRESULT(ULONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_totalBytesToDownload,
HRESULT(ULONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_downloadTimeRemainingMs,
HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_nextRetryTime,
HRESULT(ULONGLONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_installProgress,
HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_installTimeRemainingMs,
HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_isCanceled,
HRESULT(VARIANT_BOOL*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, get_errorCode, HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, get_extraCode1, HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_completionMessage,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_installerResultCode,
HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_installerResultExtraCode1,
HRESULT(LONG*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_postInstallLaunchCommandLine,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_postInstallUrl,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_postInstallAction,
HRESULT(LONG*));
// IDispatch:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfoCount,
HRESULT(UINT*));
MOCK_METHOD3_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfo,
HRESULT(UINT, LCID, ITypeInfo**));
MOCK_METHOD5_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetIDsOfNames,
HRESULT(REFIID, LPOLESTR*, UINT, LCID, DISPID*));
MOCK_METHOD8_WITH_CALLTYPE(STDMETHODCALLTYPE,
Invoke,
HRESULT(DISPID,
REFIID,
LCID,
WORD,
DISPPARAMS*,
VARIANT*,
EXCEPINFO*,
UINT*));
private:
std::u16string completion_message_;
std::u16string available_version_;
};
// A mock IAppWeb that can run callers of get_currentState through a sequence of
// pre-programmed states registered via the various Push*State methods.
class MockApp : public CComObjectRootEx<CComSingleThreadModel>, public IAppWeb {
public:
BEGIN_COM_MAP(MockApp)
COM_INTERFACE_ENTRY(IAppWeb)
END_COM_MAP()
MockApp() {
// Connect get_currentState so that each call will go to GetNextState.
ON_CALL(*this, get_currentState(_))
.WillByDefault(::testing::Invoke(this, &MockApp::GetNextState));
}
MockApp(const MockApp&) = delete;
MockApp& operator=(const MockApp&) = delete;
// IAppWeb:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, get_appId, HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_currentVersionWeb,
HRESULT(IDispatch**));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_nextVersionWeb,
HRESULT(IDispatch**));
MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_command,
HRESULT(BSTR, IDispatch**));
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, cancel, HRESULT());
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_currentState,
HRESULT(IDispatch**));
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, launch, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, uninstall, HRESULT());
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_serverInstallDataIndex,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
put_serverInstallDataIndex,
HRESULT(BSTR));
// IDispatch:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfoCount,
HRESULT(UINT*));
MOCK_METHOD3_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfo,
HRESULT(UINT, LCID, ITypeInfo**));
MOCK_METHOD5_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetIDsOfNames,
HRESULT(REFIID, LPOLESTR*, UINT, LCID, DISPID*));
MOCK_METHOD8_WITH_CALLTYPE(STDMETHODCALLTYPE,
Invoke,
HRESULT(DISPID,
REFIID,
LCID,
WORD,
DISPPARAMS*,
VARIANT*,
EXCEPINFO*,
UINT*));
// Adds a MockCurrentState to the back of the sequence to be returned by the
// mock IAppWeb.
void PushState(CurrentState state) { MakeNextState(state); }
// Adds a MockCurrentState to the back of the sequence to be returned by the
// mock IAppWeb for an ERROR state.
void PushErrorState(LONG error_code,
const std::u16string& completion_message,
LONG installer_result_code) {
CComObject<MockCurrentState>* mock_state = MakeNextState(STATE_ERROR);
EXPECT_CALL(*mock_state, get_errorCode(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(error_code), Return(S_OK)));
mock_state->ExpectCompletionMessage(completion_message);
if (installer_result_code != -1) {
EXPECT_CALL(*mock_state, get_installerResultCode(_))
.WillRepeatedly(
DoAll(SetArgPointee<0>(installer_result_code), Return(S_OK)));
}
}
// Adds a MockCurrentState to the back of the sequence to be returned by the
// mock IAppWeb for an UPDATE_AVAILABLE state.
void PushUpdateAvailableState(const std::u16string& new_version) {
MakeNextState(STATE_UPDATE_AVAILABLE)->ExpectAvailableVersion(new_version);
}
// Adds a MockCurrentState to the back of the sequence to be returned by the
// mock IAppWeb for a DOWNLOADING or INSTALLING state.
void PushProgressiveState(CurrentState state, int progress) {
CComObject<MockCurrentState>* mock_state = MakeNextState(state);
if (state == STATE_DOWNLOADING) {
const ULONG kTotalBytes = 1024;
ULONG bytes_down = static_cast<double>(kTotalBytes) * progress / 100.0;
EXPECT_CALL(*mock_state, get_totalBytesToDownload(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(kTotalBytes), Return(S_OK)));
EXPECT_CALL(*mock_state, get_bytesDownloaded(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(bytes_down), Return(S_OK)));
} else if (state == STATE_INSTALLING) {
EXPECT_CALL(*mock_state, get_installProgress(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(progress), Return(S_OK)));
} else {
ADD_FAILURE() << "unsupported state " << state;
}
}
private:
// Returns a new MockCurrentState that will be returned by the mock IAppWeb's
// get_currentState method.
CComObject<MockCurrentState>* MakeNextState(CurrentState state) {
CComObject<MockCurrentState>* mock_state = nullptr;
// The new object's refcount is held at zero until it is released from the
// simulator in GetNextState.
EXPECT_EQ(S_OK, CComObject<MockCurrentState>::CreateInstance(&mock_state));
EXPECT_CALL(*mock_state, get_stateValue(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(state), Return(S_OK)));
states_.push(mock_state);
// Tell the app to expect this state.
EXPECT_CALL(*this, get_currentState(_)).InSequence(state_sequence_);
return mock_state;
}
// An implementation of IAppWeb::get_currentState that advances the
// IGoogleUpdate3Web simulator through a series of states.
HRESULT GetNextState(IDispatch** current_state) {
EXPECT_FALSE(states_.empty());
*current_state = states_.front();
// Give a reference to the caller.
(*current_state)->AddRef();
states_.pop();
return S_OK;
}
// The states returned by the MockApp when probed.
base::queue<raw_ptr<CComObject<MockCurrentState>, CtnExperimental>> states_;
// A gmock sequence under which a series of get_CurrentState expectations are
// evaluated.
Sequence state_sequence_;
};
// A mock IAppBundleWeb that can handle a single call to createInstalledApp
// followed by get_appWeb.
class MockAppBundle : public CComObjectRootEx<CComSingleThreadModel>,
public IAppBundleWeb {
public:
BEGIN_COM_MAP(MockAppBundle)
COM_INTERFACE_ENTRY(IAppBundleWeb)
END_COM_MAP()
MockAppBundle() = default;
MockAppBundle(const MockAppBundle&) = delete;
MockAppBundle& operator=(const MockAppBundle&) = delete;
// IAppBundleWeb:
MOCK_METHOD4_WITH_CALLTYPE(STDMETHODCALLTYPE,
createApp,
HRESULT(BSTR, BSTR, BSTR, BSTR));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
createInstalledApp,
HRESULT(BSTR));
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE,
createAllInstalledApps,
HRESULT());
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_displayLanguage,
HRESULT(BSTR*));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
put_displayLanguage,
HRESULT(BSTR));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
put_parentHWND,
HRESULT(ULONG_PTR));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, get_length, HRESULT(int*));
MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_appWeb,
HRESULT(int, IDispatch**));
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, initialize, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, checkForUpdate, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, download, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, install, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, pause, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, resume, HRESULT());
MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, cancel, HRESULT());
MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE,
downloadPackage,
HRESULT(BSTR, BSTR));
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
get_currentState,
HRESULT(VARIANT*));
// IDispatch:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfoCount,
HRESULT(UINT*));
MOCK_METHOD3_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfo,
HRESULT(UINT, LCID, ITypeInfo**));
MOCK_METHOD5_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetIDsOfNames,
HRESULT(REFIID, LPOLESTR*, UINT, LCID, DISPID*));
MOCK_METHOD8_WITH_CALLTYPE(STDMETHODCALLTYPE,
Invoke,
HRESULT(DISPID,
REFIID,
LCID,
WORD,
DISPPARAMS*,
VARIANT*,
EXCEPINFO*,
UINT*));
// Returns a MockApp for the given |app_guid| that will be returned by this
// instance's get_appWeb method. The returned instance is only valid for use
// in setting up expectations until a consumer obtains it via get_appWeb, at
// which time it is owned by the consumer.
CComObject<MockApp>* MakeApp(const wchar_t* app_guid) {
// The bundle will be called on to create the installed app.
EXPECT_CALL(*this, createInstalledApp(StrEq(app_guid)))
.WillOnce(Return(S_OK));
CComObject<MockApp>* mock_app = nullptr;
EXPECT_EQ(S_OK, CComObject<MockApp>::CreateInstance(&mock_app));
// Give mock_app_bundle a ref to the app which it will return when asked.
// Note: to support multiple apps, get_appWeb expectations should use
// successive indices.
mock_app->AddRef();
EXPECT_CALL(*this, get_appWeb(0, _))
.WillOnce(DoAll(SetArgPointee<1>(mock_app), Return(S_OK)));
return mock_app;
}
};
// A mock IGoogleUpdate3Web that can handle a call to initialize and
// createAppBundleWeb by consumers.
class MockGoogleUpdate : public CComObjectRootEx<CComSingleThreadModel>,
public IGoogleUpdate3Web {
public:
BEGIN_COM_MAP(MockGoogleUpdate)
COM_INTERFACE_ENTRY(IGoogleUpdate3Web)
END_COM_MAP()
MockGoogleUpdate() = default;
MockGoogleUpdate(const MockGoogleUpdate&) = delete;
MockGoogleUpdate& operator=(const MockGoogleUpdate&) = delete;
// IGoogleUpdate3Web:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
createAppBundleWeb,
HRESULT(IDispatch**));
// IDispatch:
MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfoCount,
HRESULT(UINT*));
MOCK_METHOD3_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetTypeInfo,
HRESULT(UINT, LCID, ITypeInfo**));
MOCK_METHOD5_WITH_CALLTYPE(STDMETHODCALLTYPE,
GetIDsOfNames,
HRESULT(REFIID, LPOLESTR*, UINT, LCID, DISPID*));
MOCK_METHOD8_WITH_CALLTYPE(STDMETHODCALLTYPE,
Invoke,
HRESULT(DISPID,
REFIID,
LCID,
WORD,
DISPPARAMS*,
VARIANT*,
EXCEPINFO*,
UINT*));
// Returns a MockAppBundle that will be returned by this instance's
// createAppBundleWeb method. The returned instance is only valid for use in
// setting up expectations until a consumer obtains it via createAppBundleWeb,
// at which time it is owned by the consumer.
CComObject<MockAppBundle>* MakeAppBundle() {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
EXPECT_EQ(S_OK,
CComObject<MockAppBundle>::CreateInstance(&mock_app_bundle));
EXPECT_CALL(*mock_app_bundle, initialize()).WillOnce(Return(S_OK));
// Give this instance a ref to the bundle which it will return when created.
mock_app_bundle->AddRef();
EXPECT_CALL(*this, createAppBundleWeb(_))
.WillOnce(DoAll(SetArgPointee<0>(mock_app_bundle), Return(S_OK)));
return mock_app_bundle;
}
};
// A mock factory for creating an IGoogleUpdate3Web instance.
class MockGoogleUpdateFactory : public GoogleUpdateFactory {
public:
MockGoogleUpdateFactory() = default;
MockGoogleUpdateFactory(const MockGoogleUpdateFactory&) = delete;
MockGoogleUpdateFactory& operator=(const MockGoogleUpdateFactory&) = delete;
MOCK_METHOD1(Create, HRESULT(Microsoft::WRL::ComPtr<IGoogleUpdate3Web>*));
// Returns a mock IGoogleUpdate3Web object that will be returned by the
// factory.
CComObject<MockGoogleUpdate>* MakeServerMock() {
CComObject<MockGoogleUpdate>* mock_google_update = nullptr;
EXPECT_EQ(S_OK, CComObject<MockGoogleUpdate>::CreateInstance(
&mock_google_update));
// Give the factory this updater. Do not add a ref, as the factory will add
// one when it hands out its instance.
EXPECT_CALL(*this, Create(_))
.InSequence(sequence_)
.WillOnce(DoAll(SetArgPointee<0>(mock_google_update), Return(S_OK)));
return mock_google_update;
}
private:
Sequence sequence_;
};
} // namespace
// A test fixture that can simulate the IGoogleUpdate3Web API via Google Mock.
// Individual tests must wire up the factories by a call to one of the
// PrepareSimulator methods. The family of Push*State methods are then used to
// configure the set of states to be simulated.
class GoogleUpdateWinTest : public ::testing::TestWithParam<bool> {
public:
GoogleUpdateWinTest(const GoogleUpdateWinTest&) = delete;
GoogleUpdateWinTest& operator=(const GoogleUpdateWinTest&) = delete;
static void SetUpTestCase() {
ui::win::CreateATLModuleIfNeeded();
// Configure all mock functions that return HRESULT to return failure.
::testing::DefaultValue<HRESULT>::Set(E_FAIL);
}
static void TearDownTestCase() { ::testing::DefaultValue<HRESULT>::Clear(); }
protected:
GoogleUpdateWinTest()
: task_runner_(new base::TestSimpleTaskRunner()),
task_runner_current_default_handle_(task_runner_),
system_level_install_(GetParam()),
scoped_install_details_(system_level_install_, 0) {}
void SetUp() override {
::testing::TestWithParam<bool>::SetUp();
// Override these paths so that they can be found after the registry
// override manager is in place.
base::FilePath temp;
base::PathService::Get(base::DIR_PROGRAM_FILES, &temp);
program_files_override_.reset(
new base::ScopedPathOverride(base::DIR_PROGRAM_FILES, temp));
base::PathService::Get(base::DIR_PROGRAM_FILESX86, &temp);
program_files_x86_override_.reset(
new base::ScopedPathOverride(base::DIR_PROGRAM_FILESX86, temp));
base::PathService::Get(base::DIR_LOCAL_APP_DATA, &temp);
local_app_data_override_.reset(
new base::ScopedPathOverride(base::DIR_LOCAL_APP_DATA, temp));
// Override the registry so that tests can freely push state to it.
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
ASSERT_NO_FATAL_FAILURE(
registry_override_manager_.OverrideRegistry(HKEY_LOCAL_MACHINE));
// Chrome is installed.
const HKEY root =
system_level_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
base::win::RegKey key(root, kClients, KEY_WRITE | KEY_WOW64_32KEY);
ASSERT_EQ(ERROR_SUCCESS,
key.CreateKey(kChromeGuid, KEY_WRITE | KEY_WOW64_32KEY));
ASSERT_EQ(ERROR_SUCCESS,
key.WriteValue(L"pv",
base::ASCIIToWide(CHROME_VERSION_STRING).c_str()));
ASSERT_EQ(ERROR_SUCCESS,
key.Create(root, kClientState, KEY_WRITE | KEY_WOW64_32KEY));
ASSERT_EQ(ERROR_SUCCESS,
key.CreateKey(kChromeGuid, KEY_WRITE | KEY_WOW64_32KEY));
base::FilePath dir_exe;
ASSERT_TRUE(base::PathService::Get(base::DIR_EXE, &dir_exe));
ASSERT_EQ(ERROR_SUCCESS,
key.WriteValue(L"UninstallString",
dir_exe.AppendASCII(CHROME_VERSION_STRING)
.Append(installer::kInstallerDir)
.Append(L"setup.exe")
.value()
.c_str()));
ASSERT_EQ(ERROR_SUCCESS,
key.WriteValue(L"UninstallArguments", L"--uninstall"));
// Provide an IGoogleUpdate3Web class factory so that this test can provide
// a mocked-out instance.
SetGoogleUpdateFactoryForTesting(
base::BindRepeating(&GoogleUpdateFactory::Create,
base::Unretained(&mock_google_update_factory_)));
// Compute a newer version.
base::Version current_version(CHROME_VERSION_STRING);
new_version_ = base::ASCIIToUTF16(base::StringPrintf(
"%u.%u.%u.%u", current_version.components()[0],
current_version.components()[1], current_version.components()[2] + 1,
current_version.components()[3]));
SetUpdateDriverTaskRunnerForTesting(task_runner_.get());
}
// Creates app bundle and app mocks that will be used to simulate Google
// Update.
void MakeGoogleUpdateMocks(CComObject<MockAppBundle>** mock_app_bundle,
CComObject<MockApp>** mock_app) {
CComObject<MockGoogleUpdate>* google_update =
mock_google_update_factory_.MakeServerMock();
CComObject<MockAppBundle>* app_bundle = google_update->MakeAppBundle();
CComObject<MockApp>* app = app_bundle->MakeApp(kChromeGuid);
if (mock_app_bundle) {
*mock_app_bundle = app_bundle;
}
if (mock_app) {
*mock_app = app;
}
}
void TearDown() override {
SetUpdateDriverTaskRunnerForTesting(nullptr);
// Remove the test's IGoogleUpdate on-demand update class factory.
SetGoogleUpdateFactoryForTesting(GoogleUpdate3ClassFactory());
::testing::TestWithParam<bool>::TearDown();
}
static const wchar_t kClients[];
static const wchar_t kClientState[];
static const wchar_t kChromeGuid[];
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::SingleThreadTaskRunner::CurrentDefaultHandle
task_runner_current_default_handle_;
bool system_level_install_;
install_static::ScopedInstallDetails scoped_install_details_;
std::unique_ptr<base::ScopedPathOverride> program_files_override_;
std::unique_ptr<base::ScopedPathOverride> program_files_x86_override_;
std::unique_ptr<base::ScopedPathOverride> local_app_data_override_;
registry_util::RegistryOverrideManager registry_override_manager_;
// A mock object, the OnUpdateCheckCallback method of which will be invoked
// each time the update check machinery invokes the given UpdateCheckCallback.
StrictMock<MockUpdateCheckDelegate> mock_update_check_delegate_;
// A mock object that provides a GoogleUpdate3ClassFactory by which the test
// fixture's IGoogleUpdate3Web simulator is provided to the update check
// machinery.
StrictMock<MockGoogleUpdateFactory> mock_google_update_factory_;
// The new version that the fixture will pretend is available.
std::u16string new_version_;
};
// static
const wchar_t GoogleUpdateWinTest::kClients[] =
L"Software\\Google\\Update\\Clients";
const wchar_t GoogleUpdateWinTest::kClientState[] =
L"Software\\Google\\Update\\ClientState";
const wchar_t GoogleUpdateWinTest::kChromeGuid[] =
L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
// Test that an update check fails with the proper error code if Chrome isn't in
// one of the expected install directories.
TEST_P(GoogleUpdateWinTest, InvalidInstallDirectory) {
// Override FILE_EXE so that it looks like the test is running from somewhere
// other than where Chrome is installed.
base::FilePath file_exe;
base::FilePath dir_temp;
ASSERT_TRUE(base::PathService::Get(base::FILE_EXE, &file_exe));
ASSERT_TRUE(base::PathService::Get(base::DIR_TEMP, &dir_temp));
base::ScopedPathOverride file_exe_override(
base::FILE_EXE, dir_temp.Append(file_exe.BaseName()),
/*is_absolute=*/true, /*create=*/false);
EXPECT_CALL(mock_update_check_delegate_,
OnError(CANNOT_UPGRADE_CHROME_IN_THIS_DIRECTORY, _, _));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
CANNOT_UPGRADE_CHROME_IN_THIS_DIRECTORY);
}
// Test the case where the GoogleUpdate class can't be created for an update
// check.
TEST_P(GoogleUpdateWinTest, NoGoogleUpdateForCheck) {
// The factory should be called upon: let it fail.
EXPECT_CALL(mock_google_update_factory_, Create(_));
// Expect the appropriate error when the on-demand class cannot be created.
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND, _, _));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND);
}
// Test the case where the GoogleUpdate class can't be created for an upgrade.
TEST_P(GoogleUpdateWinTest, NoGoogleUpdateForUpgrade) {
// The factory should be called upon: let it fail.
EXPECT_CALL(mock_google_update_factory_, Create(_));
// Expect the appropriate error when the on-demand class cannot be created.
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND, _, _));
BeginUpdateCheck(std::string(), true, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND);
}
// Test the case where the GoogleUpdate class returns an error when an update
// check is started.
TEST_P(GoogleUpdateWinTest, FailUpdateCheck) {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, nullptr);
// checkForUpdate will fail.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(E_FAIL));
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ONDEMAND_CLASS_REPORTED_ERROR, _, _));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
GOOGLE_UPDATE_ONDEMAND_CLASS_REPORTED_ERROR);
EXPECT_EQ(GetLastUpdateState()->hresult, E_FAIL);
}
// Test the case where the GoogleUpdate class reports that updates are disabled
// by Group Policy.
TEST_P(GoogleUpdateWinTest, UpdatesDisabledByPolicy) {
static const HRESULT GOOPDATE_E_APP_UPDATE_DISABLED_BY_POLICY = 0x80040813;
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushErrorState(GOOPDATE_E_APP_UPDATE_DISABLED_BY_POLICY,
u"disabled by policy", -1);
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_DISABLED_BY_POLICY, _, _));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_DISABLED_BY_POLICY);
}
// Test the case where the GoogleUpdate class reports that manual updates are
// disabled by Group Policy, but that automatic updates are enabled.
TEST_P(GoogleUpdateWinTest, ManualUpdatesDisabledByPolicy) {
static const HRESULT GOOPDATE_E_APP_UPDATE_DISABLED_BY_POLICY_MANUAL =
0x8004081f;
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushErrorState(GOOPDATE_E_APP_UPDATE_DISABLED_BY_POLICY_MANUAL,
u"manual updates disabled by policy", -1);
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_DISABLED_BY_POLICY_AUTO_ONLY, _, _));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
GOOGLE_UPDATE_DISABLED_BY_POLICY_AUTO_ONLY);
}
// Test an update check where no update is available.
TEST_P(GoogleUpdateWinTest, UpdateCheckNoUpdate) {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushState(STATE_NO_UPDATE);
EXPECT_CALL(mock_update_check_delegate_,
OnUpdateCheckComplete(IsEmpty())); // new_version
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_NO_ERROR);
EXPECT_EQ(GetLastUpdateState()->new_version, u"");
}
// Test an update check where an update is available.
TEST_P(GoogleUpdateWinTest, UpdateCheckUpdateAvailable) {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushUpdateAvailableState(new_version_);
EXPECT_CALL(mock_update_check_delegate_, OnUpdateCheckComplete(new_version_));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_NO_ERROR);
EXPECT_EQ(GetLastUpdateState()->new_version, new_version_);
}
// Test a successful upgrade.
TEST_P(GoogleUpdateWinTest, UpdateInstalled) {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
// Expect the bundle to be called on to start the install.
EXPECT_CALL(*mock_app_bundle, install()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushUpdateAvailableState(new_version_);
mock_app->PushState(STATE_WAITING_TO_DOWNLOAD);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 0);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 75);
mock_app->PushState(STATE_WAITING_TO_INSTALL);
mock_app->PushProgressiveState(STATE_INSTALLING, 50);
mock_app->PushState(STATE_INSTALL_COMPLETE);
{
InSequence callback_sequence;
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(0, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(12, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(37, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(50, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(75, new_version_));
EXPECT_CALL(mock_update_check_delegate_, OnUpgradeComplete(new_version_));
}
BeginUpdateCheck(std::string(), true, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_NO_ERROR);
EXPECT_EQ(GetLastUpdateState()->new_version, new_version_);
}
// Test a failed upgrade where Google Update reports that the installer failed.
TEST_P(GoogleUpdateWinTest, UpdateFailed) {
const std::u16string error(u"It didn't work.");
static const HRESULT GOOPDATEINSTALL_E_INSTALLER_FAILED = 0x80040902;
static const int kInstallerError = 12;
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
// Expect the bundle to be called on to start the install.
EXPECT_CALL(*mock_app_bundle, install()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushUpdateAvailableState(new_version_);
mock_app->PushState(STATE_WAITING_TO_DOWNLOAD);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 0);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 75);
mock_app->PushState(STATE_WAITING_TO_INSTALL);
mock_app->PushProgressiveState(STATE_INSTALLING, 50);
mock_app->PushErrorState(GOOPDATEINSTALL_E_INSTALLER_FAILED, error,
kInstallerError);
{
InSequence callback_sequence;
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(0, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(12, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(37, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(50, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(75, new_version_));
EXPECT_CALL(
mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ERROR_UPDATING, HasSubstr(error), new_version_));
}
BeginUpdateCheck(std::string(), true, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_ERROR_UPDATING);
EXPECT_EQ(GetLastUpdateState()->new_version, new_version_);
EXPECT_EQ(GetLastUpdateState()->hresult, GOOPDATEINSTALL_E_INSTALLER_FAILED);
ASSERT_TRUE(GetLastUpdateState()->installer_exit_code);
EXPECT_EQ(GetLastUpdateState()->installer_exit_code.value(), kInstallerError);
}
// Test that a retry after a USING_EXTERNAL_UPDATER failure succeeds.
TEST_P(GoogleUpdateWinTest, RetryAfterExternalUpdaterError) {
static const HRESULT GOOPDATE_E_APP_USING_EXTERNAL_UPDATER = 0xa043081d;
CComObject<MockAppBundle>* mock_app_bundle =
mock_google_update_factory_.MakeServerMock()->MakeAppBundle();
// The first attempt will fail in createInstalledApp indicating that an update
// is already in progress.
Sequence bundle_seq;
EXPECT_CALL(*mock_app_bundle, createInstalledApp(StrEq(kChromeGuid)))
.InSequence(bundle_seq)
.WillOnce(Return(GOOPDATE_E_APP_USING_EXTERNAL_UPDATER));
// Expect a retry on the same instance.
EXPECT_CALL(*mock_app_bundle, createInstalledApp(StrEq(kChromeGuid)))
.InSequence(bundle_seq)
.WillOnce(Return(S_OK));
// See MakeApp() for an explanation of this:
CComObject<MockApp>* mock_app = nullptr;
EXPECT_EQ(S_OK, CComObject<MockApp>::CreateInstance(&mock_app));
mock_app->AddRef();
EXPECT_CALL(*mock_app_bundle, get_appWeb(0, _))
.WillOnce(DoAll(SetArgPointee<1>(mock_app), Return(S_OK)));
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushState(STATE_NO_UPDATE);
// Expect the update check to succeed.
EXPECT_CALL(mock_update_check_delegate_,
OnUpdateCheckComplete(IsEmpty())); // new_version
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_NO_ERROR);
EXPECT_EQ(GetLastUpdateState()->new_version, u"");
}
TEST_P(GoogleUpdateWinTest, UpdateInstalledMultipleDelegates) {
CComObject<MockAppBundle>* mock_app_bundle = nullptr;
CComObject<MockApp>* mock_app = nullptr;
MakeGoogleUpdateMocks(&mock_app_bundle, &mock_app);
// Expect the bundle to be called on to start the update.
EXPECT_CALL(*mock_app_bundle, checkForUpdate()).WillOnce(Return(S_OK));
// Expect the bundle to be called on to start the install.
EXPECT_CALL(*mock_app_bundle, install()).WillOnce(Return(S_OK));
mock_app->PushState(STATE_INIT);
mock_app->PushState(STATE_CHECKING_FOR_UPDATE);
mock_app->PushUpdateAvailableState(new_version_);
mock_app->PushState(STATE_WAITING_TO_DOWNLOAD);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 0);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 25);
mock_app->PushProgressiveState(STATE_DOWNLOADING, 75);
mock_app->PushState(STATE_WAITING_TO_INSTALL);
mock_app->PushProgressiveState(STATE_INSTALLING, 50);
mock_app->PushState(STATE_INSTALL_COMPLETE);
StrictMock<MockUpdateCheckDelegate> mock_update_check_delegate_2;
{
InSequence callback_sequence;
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(0, new_version_));
EXPECT_CALL(mock_update_check_delegate_2,
OnUpgradeProgress(0, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(12, new_version_));
EXPECT_CALL(mock_update_check_delegate_2,
OnUpgradeProgress(12, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(37, new_version_));
EXPECT_CALL(mock_update_check_delegate_2,
OnUpgradeProgress(37, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(50, new_version_));
EXPECT_CALL(mock_update_check_delegate_2,
OnUpgradeProgress(50, new_version_));
EXPECT_CALL(mock_update_check_delegate_,
OnUpgradeProgress(75, new_version_));
EXPECT_CALL(mock_update_check_delegate_2,
OnUpgradeProgress(75, new_version_));
EXPECT_CALL(mock_update_check_delegate_, OnUpgradeComplete(new_version_));
EXPECT_CALL(mock_update_check_delegate_2, OnUpgradeComplete(new_version_));
}
BeginUpdateCheck(std::string(), true, 0,
mock_update_check_delegate_.AsWeakPtr());
BeginUpdateCheck(std::string(), true, 0,
mock_update_check_delegate_2.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_NO_ERROR);
EXPECT_EQ(GetLastUpdateState()->new_version, new_version_);
}
// Test the case where the GoogleUpdate class responds with error provided
// via the command line with specified hresult and GoogleUpdateErrorCode.
TEST_P(GoogleUpdateWinTest, SimulateHresultWithErrorCode) {
base::test::ScopedCommandLine commandline;
// Simulate GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND (3).
commandline.GetProcessCommandLine()->AppendSwitchASCII(
switches::kSimulateUpdateErrorCode, "3");
// Simulate WININET_E_INCORRECT_HANDLE_TYPE (0x80072ef2).
commandline.GetProcessCommandLine()->AppendSwitchASCII(
switches::kSimulateUpdateHresult, "0x80072ef2");
// Expect the appropriate error when the on-demand class cannot be created.
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND,
AllOfArray({HasSubstrCaseInsensitive(u"error code 3:"),
HasSubstrCaseInsensitive(u"0x80072EF2")}),
_));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code,
GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND);
}
// Test the case where the GoogleUpdate class responds with error provided
// via the command line with a specific hresult.
TEST_P(GoogleUpdateWinTest, SimulateHresultOnly) {
base::test::ScopedCommandLine commandline;
// Simulate WININET_E_INCORRECT_HANDLE_TYPE (0x80072ef2).
commandline.GetProcessCommandLine()->AppendSwitchASCII(
switches::kSimulateUpdateHresult, "0x80072ef2");
// Expect the appropriate error when the on-demand class cannot be created.
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ERROR_UPDATING,
AllOfArray({HasSubstrCaseInsensitive(u"error code 7:"),
HasSubstrCaseInsensitive(u"0x80072EF2")}),
_));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_ERROR_UPDATING);
}
// Test the case where the GoogleUpdate class responds with error provided
// via the command line without specific hresult.
TEST_P(GoogleUpdateWinTest, SimulateHresultDefault) {
base::test::ScopedCommandLine commandline;
commandline.GetProcessCommandLine()->AppendSwitch(
switches::kSimulateUpdateHresult);
// Expect the appropriate error when the on-demand class cannot be created.
EXPECT_CALL(mock_update_check_delegate_,
OnError(GOOGLE_UPDATE_ERROR_UPDATING,
AllOfArray({HasSubstrCaseInsensitive(u"error code 7:"),
HasSubstrCaseInsensitive(u"0x80004005")}),
_));
BeginUpdateCheck(std::string(), false, 0,
mock_update_check_delegate_.AsWeakPtr());
task_runner_->RunUntilIdle();
ASSERT_TRUE(GetLastUpdateState());
EXPECT_EQ(GetLastUpdateState()->error_code, GOOGLE_UPDATE_ERROR_UPDATING);
}
INSTANTIATE_TEST_SUITE_P(UserLevel, GoogleUpdateWinTest, Values(false));
INSTANTIATE_TEST_SUITE_P(SystemLevel, GoogleUpdateWinTest, Values(true));
|