| 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 "chrome/browser/autofill/wallet/encryption_escrow_client.h" | |
| 6 #include "chrome/browser/autofill/wallet/encryption_escrow_client_observer.h" | |
| 7 #include "chrome/browser/autofill/wallet/instrument.h" | |
| 8 #include "chrome/browser/autofill/wallet/wallet_test_util.h" | |
| 9 #include "chrome/test/base/testing_profile.h" | |
| 10 #include "content/public/test/test_browser_thread.h" | |
| 11 #include "googleurl/src/gurl.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 { | |
| 21 | |
| 22 const char kEncryptOtpRequest[] = "cvv=30:000102030405"; | |
| 23 const char kEncryptOtpResponse[] = "session_material|encrypted_one_time_pad"; | |
| 24 const char kEscrowInstrumentInformationRequest[] = | |
| 25 "gid=obfuscated_gaia_id&cardNumber=4444444444444448&cvv=123"; | |
| 26 const char kEscrowCardVerificationNumberRequest[] = | |
| 27 "gid=obfuscated_gaia_id&cvv=123"; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 namespace autofill { | |
| 32 namespace wallet { | |
| 33 | |
| 34 class MockEncryptionEscrowClientObserver : | |
| 35 public EncryptionEscrowClientObserver, | |
| 36 public base::SupportsWeakPtr<MockEncryptionEscrowClientObserver> { | |
| 37 public: | |
| 38 MockEncryptionEscrowClientObserver() {} | |
| 39 ~MockEncryptionEscrowClientObserver() {} | |
| 40 | |
| 41 MOCK_METHOD2(OnDidEncryptOneTimePad, | |
| 42 void(const std::string& encrypted_one_time_pad, | |
| 43 const std::string& session_material)); | |
| 44 MOCK_METHOD1(OnDidEscrowCardVerificationNumber, | |
| 45 void(const std::string& escrow_handle)); | |
| 46 MOCK_METHOD1(OnDidEscrowInstrumentInformation, | |
| 47 void(const std::string& escrow_handle)); | |
| 48 MOCK_METHOD0(OnMalformedResponse, void()); | |
| 49 MOCK_METHOD1(OnNetworkError, void(int response_code)); | |
| 50 }; | |
| 51 | |
| 52 class EncryptionEscrowClientTest : public testing::Test { | |
| 53 public: | |
| 54 EncryptionEscrowClientTest() : io_thread_(content::BrowserThread::IO) {} | |
| 55 | |
| 56 virtual void SetUp() { | |
| 57 io_thread_.StartIOThread(); | |
| 58 profile_.CreateRequestContext(); | |
| 59 } | |
| 60 | |
| 61 std::vector<uint8> MakeOneTimePad() { | |
| 62 std::vector<uint8> one_time_pad; | |
| 63 one_time_pad.push_back(0); | |
| 64 one_time_pad.push_back(1); | |
| 65 one_time_pad.push_back(2); | |
| 66 one_time_pad.push_back(3); | |
| 67 one_time_pad.push_back(4); | |
| 68 one_time_pad.push_back(5); | |
| 69 return one_time_pad; | |
| 70 } | |
| 71 | |
| 72 virtual void TearDown() { | |
| 73 profile_.ResetRequestContext(); | |
| 74 io_thread_.Stop(); | |
| 75 } | |
| 76 | |
| 77 void VerifyAndFinishRequest(const net::TestURLFetcherFactory& fetcher_factory, | |
| 78 net::HttpStatusCode response_code, | |
| 79 const std::string& request_body, | |
| 80 const std::string& response_body) { | |
| 81 net::TestURLFetcher* fetcher = fetcher_factory.GetFetcherByID(1); | |
| 82 ASSERT_TRUE(fetcher); | |
| 83 EXPECT_EQ(request_body, fetcher->upload_data()); | |
| 84 fetcher->set_response_code(response_code); | |
| 85 fetcher->SetResponseString(response_body); | |
| 86 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 87 } | |
| 88 | |
| 89 protected: | |
| 90 TestingProfile profile_; | |
| 91 | |
| 92 private: | |
| 93 // The profile's request context must be released on the IO thread. | |
| 94 content::TestBrowserThread io_thread_; | |
| 95 }; | |
| 96 | |
| 97 TEST_F(EncryptionEscrowClientTest, NetworkError) { | |
| 98 MockEncryptionEscrowClientObserver observer; | |
| 99 EXPECT_CALL(observer, OnNetworkError(net::HTTP_UNAUTHORIZED)).Times(1); | |
| 100 | |
| 101 net::TestURLFetcherFactory factory; | |
| 102 | |
| 103 scoped_ptr<Instrument> instrument = GetTestInstrument(); | |
| 104 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 105 &observer); | |
| 106 encryption_escrow_client.EscrowInstrumentInformation(*instrument, | |
| 107 "obfuscated_gaia_id"); | |
| 108 VerifyAndFinishRequest(factory, | |
| 109 net::HTTP_UNAUTHORIZED, | |
| 110 kEscrowInstrumentInformationRequest, | |
| 111 std::string()); | |
| 112 } | |
| 113 | |
| 114 TEST_F(EncryptionEscrowClientTest, EscrowInstrumentInformationSuccess) { | |
| 115 MockEncryptionEscrowClientObserver observer; | |
| 116 EXPECT_CALL(observer, OnDidEscrowInstrumentInformation("abc")).Times(1); | |
| 117 | |
| 118 net::TestURLFetcherFactory factory; | |
| 119 | |
| 120 scoped_ptr<Instrument> instrument = GetTestInstrument(); | |
| 121 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 122 &observer); | |
| 123 encryption_escrow_client.EscrowInstrumentInformation(*instrument, | |
| 124 "obfuscated_gaia_id"); | |
| 125 VerifyAndFinishRequest(factory, | |
| 126 net::HTTP_OK, | |
| 127 kEscrowInstrumentInformationRequest, | |
| 128 "abc"); | |
| 129 } | |
| 130 | |
| 131 TEST_F(EncryptionEscrowClientTest, EscrowInstrumentInformationFailure) { | |
| 132 MockEncryptionEscrowClientObserver observer; | |
| 133 EXPECT_CALL(observer, OnMalformedResponse()).Times(1); | |
| 134 | |
| 135 net::TestURLFetcherFactory factory; | |
| 136 scoped_ptr<Instrument> instrument = GetTestInstrument(); | |
| 137 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 138 &observer); | |
| 139 encryption_escrow_client.EscrowInstrumentInformation(*instrument, | |
| 140 "obfuscated_gaia_id"); | |
| 141 VerifyAndFinishRequest(factory, | |
| 142 net::HTTP_OK, | |
| 143 kEscrowInstrumentInformationRequest, | |
| 144 std::string()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(EncryptionEscrowClientTest, EscrowCardVerificationNumberSuccess) { | |
| 148 MockEncryptionEscrowClientObserver observer; | |
| 149 EXPECT_CALL(observer, OnDidEscrowCardVerificationNumber("abc")).Times(1); | |
| 150 | |
| 151 net::TestURLFetcherFactory factory; | |
| 152 | |
| 153 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 154 &observer); | |
| 155 encryption_escrow_client.EscrowCardVerificationNumber("123", | |
| 156 "obfuscated_gaia_id"); | |
| 157 VerifyAndFinishRequest(factory, | |
| 158 net::HTTP_OK, | |
| 159 kEscrowCardVerificationNumberRequest, | |
| 160 "abc"); | |
| 161 } | |
| 162 | |
| 163 TEST_F(EncryptionEscrowClientTest, EscrowCardVerificationNumberFailure) { | |
| 164 MockEncryptionEscrowClientObserver observer; | |
| 165 EXPECT_CALL(observer, OnMalformedResponse()).Times(1); | |
| 166 | |
| 167 net::TestURLFetcherFactory factory; | |
| 168 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 169 &observer); | |
| 170 encryption_escrow_client.EscrowCardVerificationNumber("123", | |
| 171 "obfuscated_gaia_id"); | |
| 172 VerifyAndFinishRequest(factory, | |
| 173 net::HTTP_OK, | |
| 174 kEscrowCardVerificationNumberRequest, | |
| 175 std::string()); | |
| 176 } | |
| 177 | |
| 178 TEST_F(EncryptionEscrowClientTest, EncryptOneTimePadSuccess) { | |
| 179 MockEncryptionEscrowClientObserver observer; | |
| 180 EXPECT_CALL(observer, | |
| 181 OnDidEncryptOneTimePad("encrypted_one_time_pad", | |
| 182 "session_material")).Times(1); | |
| 183 | |
| 184 net::TestURLFetcherFactory factory; | |
| 185 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 186 &observer); | |
| 187 encryption_escrow_client.EncryptOneTimePad(MakeOneTimePad()); | |
| 188 VerifyAndFinishRequest(factory, | |
| 189 net::HTTP_OK, | |
| 190 kEncryptOtpRequest, | |
| 191 kEncryptOtpResponse); | |
| 192 } | |
| 193 | |
| 194 TEST_F(EncryptionEscrowClientTest, EncryptOneTimePadFailure) { | |
| 195 MockEncryptionEscrowClientObserver observer; | |
| 196 EXPECT_CALL(observer, OnMalformedResponse()).Times(1); | |
| 197 | |
| 198 net::TestURLFetcherFactory factory; | |
| 199 EncryptionEscrowClient encryption_escrow_client(profile_.GetRequestContext(), | |
| 200 &observer); | |
| 201 encryption_escrow_client.EncryptOneTimePad(MakeOneTimePad()); | |
| 202 VerifyAndFinishRequest(factory, | |
| 203 net::HTTP_OK, | |
| 204 kEncryptOtpRequest, | |
| 205 std::string()); | |
| 206 } | |
| 207 | |
| 208 } // namespace wallet | |
| 209 } // namespace autofill | |
| OLD | NEW |