| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | 5 #include "components/cryptauth/cryptauth_api_call_flow.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/test_simple_task_runner.h" | 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace proximity_auth { | 17 namespace cryptauth { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char kSerializedRequestProto[] = "serialized_request_proto"; | 21 const char kSerializedRequestProto[] = "serialized_request_proto"; |
| 22 const char kSerializedResponseProto[] = "result_proto"; | 22 const char kSerializedResponseProto[] = "result_proto"; |
| 23 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test"; | 23 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test"; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 class ProximityAuthCryptAuthApiCallFlowTest | 27 class CryptAuthApiCallFlowTest |
| 28 : public testing::Test, | 28 : public testing::Test, |
| 29 public net::TestURLFetcherDelegateForTests { | 29 public net::TestURLFetcherDelegateForTests { |
| 30 protected: | 30 protected: |
| 31 ProximityAuthCryptAuthApiCallFlowTest() | 31 CryptAuthApiCallFlowTest() |
| 32 : url_request_context_getter_(new net::TestURLRequestContextGetter( | 32 : url_request_context_getter_(new net::TestURLRequestContextGetter( |
| 33 new base::TestSimpleTaskRunner())) {} | 33 new base::TestSimpleTaskRunner())) {} |
| 34 | 34 |
| 35 void SetUp() override { | 35 void SetUp() override { |
| 36 // The TestURLFetcherFactory will override the global URLFetcherFactory for | 36 // The TestURLFetcherFactory will override the global URLFetcherFactory for |
| 37 // the entire test. | 37 // the entire test. |
| 38 url_fetcher_factory_.reset(new net::TestURLFetcherFactory()); | 38 url_fetcher_factory_.reset(new net::TestURLFetcherFactory()); |
| 39 url_fetcher_factory_->SetDelegateForTests(this); | 39 url_fetcher_factory_->SetDelegateForTests(this); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void StartApiCallFlow() { | 42 void StartApiCallFlow() { |
| 43 StartApiCallFlowWithRequest(kSerializedRequestProto); | 43 StartApiCallFlowWithRequest(kSerializedRequestProto); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void StartApiCallFlowWithRequest(const std::string& serialized_request) { | 46 void StartApiCallFlowWithRequest(const std::string& serialized_request) { |
| 47 flow_.Start(GURL(kRequestUrl), url_request_context_getter_.get(), | 47 flow_.Start(GURL(kRequestUrl), url_request_context_getter_.get(), |
| 48 "access_token", serialized_request, | 48 "access_token", serialized_request, |
| 49 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnResult, | 49 base::Bind(&CryptAuthApiCallFlowTest::OnResult, |
| 50 base::Unretained(this)), | 50 base::Unretained(this)), |
| 51 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnError, | 51 base::Bind(&CryptAuthApiCallFlowTest::OnError, |
| 52 base::Unretained(this))); | 52 base::Unretained(this))); |
| 53 // URLFetcher object for the API request should be created. | 53 // URLFetcher object for the API request should be created. |
| 54 CheckCryptAuthHttpRequest(serialized_request); | 54 CheckCryptAuthHttpRequest(serialized_request); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void OnResult(const std::string& result) { | 57 void OnResult(const std::string& result) { |
| 58 EXPECT_FALSE(result_); | 58 EXPECT_FALSE(result_); |
| 59 result_.reset(new std::string(result)); | 59 result_.reset(new std::string(result)); |
| 60 } | 60 } |
| 61 | 61 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 net::TestURLFetcher* url_fetcher_; | 103 net::TestURLFetcher* url_fetcher_; |
| 104 std::unique_ptr<std::string> result_; | 104 std::unique_ptr<std::string> result_; |
| 105 std::unique_ptr<std::string> error_message_; | 105 std::unique_ptr<std::string> error_message_; |
| 106 | 106 |
| 107 private: | 107 private: |
| 108 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 108 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 109 std::unique_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | 109 std::unique_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; |
| 110 CryptAuthApiCallFlow flow_; | 110 CryptAuthApiCallFlow flow_; |
| 111 | 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest); | 112 DISALLOW_COPY_AND_ASSIGN(CryptAuthApiCallFlowTest); |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestSuccess) { | 115 TEST_F(CryptAuthApiCallFlowTest, RequestSuccess) { |
| 116 StartApiCallFlow(); | 116 StartApiCallFlow(); |
| 117 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto); | 117 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto); |
| 118 EXPECT_EQ(kSerializedResponseProto, *result_); | 118 EXPECT_EQ(kSerializedResponseProto, *result_); |
| 119 EXPECT_FALSE(error_message_); | 119 EXPECT_FALSE(error_message_); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestFailure) { | 122 TEST_F(CryptAuthApiCallFlowTest, RequestFailure) { |
| 123 StartApiCallFlow(); | 123 StartApiCallFlow(); |
| 124 CompleteCurrentRequest(net::ERR_FAILED, 0, std::string()); | 124 CompleteCurrentRequest(net::ERR_FAILED, 0, std::string()); |
| 125 EXPECT_FALSE(result_); | 125 EXPECT_FALSE(result_); |
| 126 EXPECT_EQ("Request failed", *error_message_); | 126 EXPECT_EQ("Request failed", *error_message_); |
| 127 } | 127 } |
| 128 | 128 |
| 129 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestStatus500) { | 129 TEST_F(CryptAuthApiCallFlowTest, RequestStatus500) { |
| 130 StartApiCallFlow(); | 130 StartApiCallFlow(); |
| 131 CompleteCurrentRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR, | 131 CompleteCurrentRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR, |
| 132 "CryptAuth Meltdown."); | 132 "CryptAuth Meltdown."); |
| 133 EXPECT_FALSE(result_); | 133 EXPECT_FALSE(result_); |
| 134 EXPECT_EQ("HTTP status: 500", *error_message_); | 134 EXPECT_EQ("HTTP status: 500", *error_message_); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // The empty string is a valid protocol buffer message serialization. | 137 // The empty string is a valid protocol buffer message serialization. |
| 138 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestWithNoBody) { | 138 TEST_F(CryptAuthApiCallFlowTest, RequestWithNoBody) { |
| 139 StartApiCallFlowWithRequest(std::string()); | 139 StartApiCallFlowWithRequest(std::string()); |
| 140 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto); | 140 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto); |
| 141 EXPECT_EQ(kSerializedResponseProto, *result_); | 141 EXPECT_EQ(kSerializedResponseProto, *result_); |
| 142 EXPECT_FALSE(error_message_); | 142 EXPECT_FALSE(error_message_); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // The empty string is a valid protocol buffer message serialization. | 145 // The empty string is a valid protocol buffer message serialization. |
| 146 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, ResponseWithNoBody) { | 146 TEST_F(CryptAuthApiCallFlowTest, ResponseWithNoBody) { |
| 147 StartApiCallFlow(); | 147 StartApiCallFlow(); |
| 148 CompleteCurrentRequest(net::OK, net::HTTP_OK, std::string()); | 148 CompleteCurrentRequest(net::OK, net::HTTP_OK, std::string()); |
| 149 EXPECT_EQ(std::string(), *result_); | 149 EXPECT_EQ(std::string(), *result_); |
| 150 EXPECT_FALSE(error_message_); | 150 EXPECT_FALSE(error_message_); |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace proximity_auth | 153 } // namespace cryptauth |
| OLD | NEW |