| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "components/autofill/content/browser/wallet/encryption_escrow_client.h" | |
| 7 #include "components/autofill/content/browser/wallet/encryption_escrow_client_ob
server.h" | |
| 8 #include "components/autofill/content/browser/wallet/instrument.h" | |
| 9 #include "components/autofill/content/browser/wallet/wallet_test_util.h" | |
| 10 #include "content/public/test/test_browser_context.h" | |
| 11 #include "content/public/test/test_browser_thread.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/test_url_fetcher_factory.h" | |
| 15 #include "net/url_request/url_fetcher_delegate.h" | |
| 16 #include "net/url_request/url_request_status.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace autofill { | |
| 21 namespace wallet { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kEncryptOtpRequest[] = "cvv=30:000102030405"; | |
| 26 const char kEncryptOtpResponse[] = "session_material|encrypted_one_time_pad"; | |
| 27 const char kEscrowInstrumentInformationRequest[] = | |
| 28 "gid=obfuscated_gaia_id&cardNumber=4444444444444448&cvv=123"; | |
| 29 const char kEscrowCardVerificationNumberRequest[] = | |
| 30 "gid=obfuscated_gaia_id&cvv=123"; | |
| 31 | |
| 32 class MockEncryptionEscrowClientObserver : | |
| 33 public EncryptionEscrowClientObserver, | |
| 34 public base::SupportsWeakPtr<MockEncryptionEscrowClientObserver> { | |
| 35 public: | |
| 36 MockEncryptionEscrowClientObserver() {} | |
| 37 ~MockEncryptionEscrowClientObserver() {} | |
| 38 | |
| 39 MOCK_METHOD2(OnDidEncryptOneTimePad, | |
| 40 void(const std::string& encrypted_one_time_pad, | |
| 41 const std::string& session_material)); | |
| 42 MOCK_METHOD1(OnDidEscrowCardVerificationNumber, | |
| 43 void(const std::string& escrow_handle)); | |
| 44 MOCK_METHOD1(OnDidEscrowInstrumentInformation, | |
| 45 void(const std::string& escrow_handle)); | |
| 46 MOCK_METHOD0(OnDidMakeRequest, void()); | |
| 47 MOCK_METHOD0(OnMalformedResponse, void()); | |
| 48 MOCK_METHOD0(OnNetworkError, void()); | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class TestEncryptionEscrowClient : public EncryptionEscrowClient { | |
| 54 public: | |
| 55 TestEncryptionEscrowClient( | |
| 56 net::URLRequestContextGetter* context_getter, | |
| 57 EncryptionEscrowClientObserver* observer) | |
| 58 : EncryptionEscrowClient(context_getter, observer) {} | |
| 59 | |
| 60 bool HasRequestInProgress() const { | |
| 61 return request() != NULL; | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 class EncryptionEscrowClientTest : public testing::Test { | |
| 66 public: | |
| 67 EncryptionEscrowClientTest() | |
| 68 : instrument_(GetTestInstrument()) {} | |
| 69 | |
| 70 virtual void SetUp() OVERRIDE { | |
| 71 encryption_escrow_client_.reset(new TestEncryptionEscrowClient( | |
| 72 browser_context_.GetRequestContext(), &observer_)); | |
| 73 } | |
| 74 | |
| 75 virtual void TearDown() OVERRIDE { | |
| 76 encryption_escrow_client_.reset(); | |
| 77 } | |
| 78 | |
| 79 std::vector<uint8> MakeOneTimePad() { | |
| 80 std::vector<uint8> one_time_pad; | |
| 81 one_time_pad.push_back(0); | |
| 82 one_time_pad.push_back(1); | |
| 83 one_time_pad.push_back(2); | |
| 84 one_time_pad.push_back(3); | |
| 85 one_time_pad.push_back(4); | |
| 86 one_time_pad.push_back(5); | |
| 87 return one_time_pad; | |
| 88 } | |
| 89 | |
| 90 void VerifyAndFinishRequest(net::HttpStatusCode response_code, | |
| 91 const std::string& request_body, | |
| 92 const std::string& response_body) { | |
| 93 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(1); | |
| 94 ASSERT_TRUE(fetcher); | |
| 95 EXPECT_EQ(request_body, fetcher->upload_data()); | |
| 96 fetcher->set_response_code(response_code); | |
| 97 fetcher->SetResponseString(response_body); | |
| 98 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 99 } | |
| 100 | |
| 101 protected: | |
| 102 scoped_ptr<TestEncryptionEscrowClient> encryption_escrow_client_; | |
| 103 testing::StrictMock<MockEncryptionEscrowClientObserver> observer_; | |
| 104 scoped_ptr<Instrument> instrument_; | |
| 105 | |
| 106 | |
| 107 private: | |
| 108 content::TestBrowserContext browser_context_; | |
| 109 net::TestURLFetcherFactory factory_; | |
| 110 }; | |
| 111 | |
| 112 TEST_F(EncryptionEscrowClientTest, NetworkError) { | |
| 113 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 114 EXPECT_CALL(observer_, OnNetworkError()).Times(1); | |
| 115 | |
| 116 encryption_escrow_client_->EscrowInstrumentInformation(*instrument_, | |
| 117 "obfuscated_gaia_id"); | |
| 118 VerifyAndFinishRequest(net::HTTP_UNAUTHORIZED, | |
| 119 kEscrowInstrumentInformationRequest, | |
| 120 std::string()); | |
| 121 } | |
| 122 | |
| 123 TEST_F(EncryptionEscrowClientTest, EscrowInstrumentInformationSuccess) { | |
| 124 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 125 EXPECT_CALL(observer_, OnDidEscrowInstrumentInformation("abc")).Times(1); | |
| 126 | |
| 127 encryption_escrow_client_->EscrowInstrumentInformation(*instrument_, | |
| 128 "obfuscated_gaia_id"); | |
| 129 VerifyAndFinishRequest(net::HTTP_OK, | |
| 130 kEscrowInstrumentInformationRequest, | |
| 131 "abc"); | |
| 132 } | |
| 133 | |
| 134 TEST_F(EncryptionEscrowClientTest, EscrowInstrumentInformationFailure) { | |
| 135 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 136 EXPECT_CALL(observer_, OnMalformedResponse()).Times(1); | |
| 137 | |
| 138 encryption_escrow_client_->EscrowInstrumentInformation(*instrument_, | |
| 139 "obfuscated_gaia_id"); | |
| 140 VerifyAndFinishRequest(net::HTTP_OK, | |
| 141 kEscrowInstrumentInformationRequest, | |
| 142 std::string()); | |
| 143 } | |
| 144 | |
| 145 TEST_F(EncryptionEscrowClientTest, EscrowCardVerificationNumberSuccess) { | |
| 146 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 147 EXPECT_CALL(observer_, OnDidEscrowCardVerificationNumber("abc")).Times(1); | |
| 148 | |
| 149 encryption_escrow_client_->EscrowCardVerificationNumber("123", | |
| 150 "obfuscated_gaia_id"); | |
| 151 VerifyAndFinishRequest(net::HTTP_OK, | |
| 152 kEscrowCardVerificationNumberRequest, | |
| 153 "abc"); | |
| 154 } | |
| 155 | |
| 156 TEST_F(EncryptionEscrowClientTest, EscrowCardVerificationNumberFailure) { | |
| 157 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 158 EXPECT_CALL(observer_, OnMalformedResponse()).Times(1); | |
| 159 | |
| 160 encryption_escrow_client_->EscrowCardVerificationNumber("123", | |
| 161 "obfuscated_gaia_id"); | |
| 162 VerifyAndFinishRequest(net::HTTP_OK, | |
| 163 kEscrowCardVerificationNumberRequest, | |
| 164 std::string()); | |
| 165 } | |
| 166 | |
| 167 TEST_F(EncryptionEscrowClientTest, EncryptOneTimePadSuccess) { | |
| 168 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 169 EXPECT_CALL(observer_, | |
| 170 OnDidEncryptOneTimePad("encrypted_one_time_pad", | |
| 171 "session_material")).Times(1); | |
| 172 | |
| 173 encryption_escrow_client_->EncryptOneTimePad(MakeOneTimePad()); | |
| 174 VerifyAndFinishRequest(net::HTTP_OK, kEncryptOtpRequest, kEncryptOtpResponse); | |
| 175 } | |
| 176 | |
| 177 TEST_F(EncryptionEscrowClientTest, EncryptOneTimePadFailure) { | |
| 178 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 179 EXPECT_CALL(observer_, OnMalformedResponse()).Times(1); | |
| 180 | |
| 181 encryption_escrow_client_->EncryptOneTimePad(MakeOneTimePad()); | |
| 182 VerifyAndFinishRequest(net::HTTP_OK, kEncryptOtpRequest, std::string()); | |
| 183 } | |
| 184 | |
| 185 TEST_F(EncryptionEscrowClientTest, CancelRequest) { | |
| 186 EXPECT_CALL(observer_, OnDidMakeRequest()).Times(1); | |
| 187 | |
| 188 encryption_escrow_client_->EncryptOneTimePad(MakeOneTimePad()); | |
| 189 EXPECT_TRUE(encryption_escrow_client_->HasRequestInProgress()); | |
| 190 | |
| 191 encryption_escrow_client_->CancelRequest(); | |
| 192 EXPECT_FALSE(encryption_escrow_client_->HasRequestInProgress()); | |
| 193 } | |
| 194 | |
| 195 } // namespace wallet | |
| 196 } // namespace autofill | |
| OLD | NEW |