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/copresence/rpc/rpc_handler.h" | 5 #include "components/copresence/rpc/rpc_handler.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/memory/scoped_vector.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "components/copresence/handlers/directive_handler.h" | 15 #include "components/copresence/handlers/directive_handler.h" |
15 #include "components/copresence/mediums/audio/audio_manager.h" | 16 #include "components/copresence/mediums/audio/audio_manager.h" |
16 #include "components/copresence/proto/data.pb.h" | 17 #include "components/copresence/proto/data.pb.h" |
17 #include "components/copresence/proto/enums.pb.h" | 18 #include "components/copresence/proto/enums.pb.h" |
18 #include "components/copresence/proto/rpcs.pb.h" | 19 #include "components/copresence/proto/rpcs.pb.h" |
19 #include "net/http/http_status_code.h" | 20 #include "net/http/http_status_code.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 using google::protobuf::MessageLite; | 23 using google::protobuf::MessageLite; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 private: | 63 private: |
63 std::vector<Directive> added_directives_; | 64 std::vector<Directive> added_directives_; |
64 | 65 |
65 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); | 66 DISALLOW_COPY_AND_ASSIGN(FakeDirectiveHandler); |
66 }; | 67 }; |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { | 71 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { |
71 public: | 72 public: |
72 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS), api_key_("API key") { | 73 RpcHandlerTest() : rpc_handler_(this), status_(SUCCESS) { |
73 rpc_handler_.server_post_callback_ = | 74 rpc_handler_.server_post_callback_ = |
74 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); | 75 base::Bind(&RpcHandlerTest::CaptureHttpPost, base::Unretained(this)); |
75 rpc_handler_.device_id_ = "Device ID"; | |
76 } | 76 } |
77 | 77 |
78 void CaptureHttpPost( | 78 // CopresenceDelegate implementation |
79 net::URLRequestContextGetter* url_context_getter, | 79 |
80 const std::string& rpc_name, | 80 void HandleMessages(const std::string& app_id, |
81 scoped_ptr<MessageLite> request_proto, | 81 const std::string& subscription_id, |
82 const RpcHandler::PostCleanupCallback& response_callback) { | 82 const std::vector<Message>& messages) override { |
83 rpc_name_ = rpc_name; | 83 // app_id is unused for now, pending a server fix. |
84 request_proto_ = request_proto.Pass(); | 84 messages_by_subscription_[subscription_id] = messages; |
85 } | 85 } |
86 | 86 |
87 void CaptureStatus(CopresenceStatus status) { | 87 net::URLRequestContextGetter* GetRequestContext() const override { |
88 status_ = status; | 88 return NULL; |
89 } | 89 } |
90 | 90 |
91 inline const ReportRequest* GetReportSent() { | 91 const std::string GetPlatformVersionString() const override { |
92 return static_cast<ReportRequest*>(request_proto_.get()); | 92 return kChromeVersion; |
93 } | 93 } |
94 | 94 |
95 const TokenTechnology& GetTokenTechnologyFromReport() { | 95 const std::string GetAPIKey(const std::string& app_id) const override { |
96 return GetReportSent()->update_signals_request().state().capabilities() | 96 return app_id + " API Key"; |
97 .token_technology(0); | |
98 } | 97 } |
99 | 98 |
100 const RepeatedPtrField<PublishedMessage>& GetMessagesPublished() { | 99 const std::string GetAuthToken() const override { |
101 return GetReportSent()->manage_messages_request().message_to_publish(); | 100 return auth_token_; |
102 } | 101 } |
103 | 102 |
104 const RepeatedPtrField<Subscription>& GetSubscriptionsSent() { | 103 WhispernetClient* GetWhispernetClient() override { return NULL; } |
105 return GetReportSent()->manage_subscriptions_request().subscription(); | |
106 } | |
107 | 104 |
108 void SetDeviceId(const std::string& device_id) { | 105 protected: |
109 rpc_handler_.device_id_ = device_id; | |
110 } | |
111 | |
112 const std::string& GetDeviceId() { | |
113 return rpc_handler_.device_id_; | |
114 } | |
115 | |
116 void AddInvalidToken(const std::string& token) { | |
117 rpc_handler_.invalid_audio_token_cache_.Add(token, true); | |
118 } | |
119 | |
120 bool TokenIsInvalid(const std::string& token) { | |
121 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); | |
122 } | |
123 | |
124 FakeDirectiveHandler* InstallFakeDirectiveHandler() { | |
125 FakeDirectiveHandler* handler = new FakeDirectiveHandler; | |
126 rpc_handler_.directive_handler_.reset(handler); | |
127 return handler; | |
128 } | |
129 | |
130 void InvokeReportResponseHandler(int status_code, | 106 void InvokeReportResponseHandler(int status_code, |
131 const std::string& response) { | 107 const std::string& response) { |
132 rpc_handler_.ReportResponseHandler( | 108 rpc_handler_.ReportResponseHandler( |
133 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), | 109 base::Bind(&RpcHandlerTest::CaptureStatus, base::Unretained(this)), |
134 NULL, | 110 NULL, |
135 status_code, | 111 status_code, |
136 response); | 112 response); |
137 } | 113 } |
138 | 114 |
139 // CopresenceDelegate implementation | 115 FakeDirectiveHandler* InstallFakeDirectiveHandler() { |
140 | 116 FakeDirectiveHandler* handler = new FakeDirectiveHandler; |
141 void HandleMessages(const std::string& app_id, | 117 rpc_handler_.directive_handler_.reset(handler); |
142 const std::string& subscription_id, | 118 return handler; |
143 const std::vector<Message>& messages) override { | |
144 // app_id is unused for now, pending a server fix. | |
145 messages_by_subscription_[subscription_id] = messages; | |
146 } | 119 } |
147 | 120 |
148 net::URLRequestContextGetter* GetRequestContext() const override { | 121 void SetDeviceIdAndAuthToken(const std::string& device_id, |
149 return NULL; | 122 const std::string& auth_token) { |
| 123 rpc_handler_.device_id_by_auth_token_[auth_token] = device_id; |
| 124 auth_token_ = auth_token; |
150 } | 125 } |
151 | 126 |
152 const std::string GetPlatformVersionString() const override { | 127 void AddInvalidToken(const std::string& token) { |
153 return kChromeVersion; | 128 rpc_handler_.invalid_audio_token_cache_.Add(token, true); |
154 } | 129 } |
155 | 130 |
156 const std::string GetAPIKey() const override { return api_key_; } | 131 bool TokenIsInvalid(const std::string& token) { |
| 132 return rpc_handler_.invalid_audio_token_cache_.HasKey(token); |
| 133 } |
157 | 134 |
158 WhispernetClient* GetWhispernetClient() override { return NULL; } | |
159 | |
160 protected: | |
161 // For rpc_handler_.invalid_audio_token_cache_ | 135 // For rpc_handler_.invalid_audio_token_cache_ |
162 base::MessageLoop message_loop_; | 136 base::MessageLoop message_loop_; |
163 | 137 |
164 RpcHandler rpc_handler_; | 138 RpcHandler rpc_handler_; |
165 CopresenceStatus status_; | 139 CopresenceStatus status_; |
166 std::string api_key_; | |
167 | 140 |
168 std::string rpc_name_; | 141 std::string rpc_name_; |
169 scoped_ptr<MessageLite> request_proto_; | 142 std::string api_key_; |
| 143 std::string auth_token_; |
| 144 ScopedVector<MessageLite> request_protos_; |
170 std::map<std::string, std::vector<Message>> messages_by_subscription_; | 145 std::map<std::string, std::vector<Message>> messages_by_subscription_; |
| 146 |
| 147 private: |
| 148 void CaptureHttpPost( |
| 149 net::URLRequestContextGetter* url_context_getter, |
| 150 const std::string& rpc_name, |
| 151 const std::string& api_key, |
| 152 const std::string& auth_token, |
| 153 scoped_ptr<MessageLite> request_proto, |
| 154 const RpcHandler::PostCleanupCallback& response_callback) { |
| 155 rpc_name_ = rpc_name; |
| 156 api_key_ = api_key; |
| 157 auth_token_ = auth_token; |
| 158 request_protos_.push_back(request_proto.release()); |
| 159 } |
| 160 |
| 161 void CaptureStatus(CopresenceStatus status) { |
| 162 status_ = status; |
| 163 } |
171 }; | 164 }; |
172 | 165 |
173 TEST_F(RpcHandlerTest, Initialize) { | 166 TEST_F(RpcHandlerTest, RegisterDevice) { |
174 SetDeviceId(""); | 167 EXPECT_FALSE(rpc_handler_.IsRegisteredForToken("")); |
175 rpc_handler_.Initialize(RpcHandler::SuccessCallback()); | 168 rpc_handler_.RegisterForToken("", RpcHandler::SuccessCallback()); |
176 RegisterDeviceRequest* registration = | 169 EXPECT_EQ(1u, request_protos_.size()); |
177 static_cast<RegisterDeviceRequest*>(request_proto_.get()); | 170 const RegisterDeviceRequest* registration = |
| 171 static_cast<RegisterDeviceRequest*>(request_protos_[0]); |
178 Identity identity = registration->device_identifiers().registrant(); | 172 Identity identity = registration->device_identifiers().registrant(); |
179 EXPECT_EQ(CHROME, identity.type()); | 173 EXPECT_EQ(CHROME, identity.type()); |
180 EXPECT_FALSE(identity.chrome_id().empty()); | 174 EXPECT_FALSE(identity.chrome_id().empty()); |
| 175 |
| 176 EXPECT_FALSE(rpc_handler_.IsRegisteredForToken("abc")); |
| 177 rpc_handler_.RegisterForToken("abc", RpcHandler::SuccessCallback()); |
| 178 EXPECT_EQ(2u, request_protos_.size()); |
| 179 registration = static_cast<RegisterDeviceRequest*>(request_protos_[1]); |
| 180 EXPECT_FALSE(registration->has_device_identifiers()); |
181 } | 181 } |
182 | 182 |
| 183 // TODO(ckehoe): Add a test for RegisterResponseHandler. |
| 184 |
183 TEST_F(RpcHandlerTest, CreateRequestHeader) { | 185 TEST_F(RpcHandlerTest, CreateRequestHeader) { |
184 SetDeviceId("CreateRequestHeader Device ID"); | 186 SetDeviceIdAndAuthToken("CreateRequestHeader Device ID", |
| 187 "CreateRequestHeader Auth Token"); |
185 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), | 188 rpc_handler_.SendReportRequest(make_scoped_ptr(new ReportRequest), |
186 "CreateRequestHeader App ID", | 189 "CreateRequestHeader App", |
| 190 "CreateRequestHeader Auth Token", |
187 StatusCallback()); | 191 StatusCallback()); |
188 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 192 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
189 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 193 EXPECT_EQ("CreateRequestHeader App API Key", api_key_); |
| 194 EXPECT_EQ("CreateRequestHeader Auth Token", auth_token_); |
| 195 const ReportRequest* report = static_cast<ReportRequest*>(request_protos_[0]); |
190 EXPECT_EQ(kChromeVersion, | 196 EXPECT_EQ(kChromeVersion, |
191 report->header().framework_version().version_name()); | 197 report->header().framework_version().version_name()); |
192 EXPECT_EQ("CreateRequestHeader App ID", | 198 EXPECT_EQ("CreateRequestHeader App", |
193 report->header().client_version().client()); | 199 report->header().client_version().client()); |
194 EXPECT_EQ("CreateRequestHeader Device ID", | 200 EXPECT_EQ("CreateRequestHeader Device ID", |
195 report->header().registered_device_id()); | 201 report->header().registered_device_id()); |
196 EXPECT_EQ(CHROME_PLATFORM_TYPE, | 202 EXPECT_EQ(CHROME_PLATFORM_TYPE, |
197 report->header().device_fingerprint().type()); | 203 report->header().device_fingerprint().type()); |
198 } | 204 } |
199 | 205 |
200 TEST_F(RpcHandlerTest, ReportTokens) { | 206 TEST_F(RpcHandlerTest, ReportTokens) { |
201 std::vector<AudioToken> test_tokens; | 207 std::vector<AudioToken> test_tokens; |
202 test_tokens.push_back(AudioToken("token 1", false)); | 208 test_tokens.push_back(AudioToken("token 1", false)); |
203 test_tokens.push_back(AudioToken("token 2", true)); | 209 test_tokens.push_back(AudioToken("token 2", true)); |
204 test_tokens.push_back(AudioToken("token 3", false)); | 210 test_tokens.push_back(AudioToken("token 3", false)); |
205 AddInvalidToken("token 2"); | 211 AddInvalidToken("token 2"); |
206 | 212 |
| 213 SetDeviceIdAndAuthToken("ReportTokens Device 1", ""); |
| 214 SetDeviceIdAndAuthToken("ReportTokens Device 2", "ReportTokens Auth"); |
| 215 |
207 rpc_handler_.ReportTokens(test_tokens); | 216 rpc_handler_.ReportTokens(test_tokens); |
208 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); | 217 EXPECT_EQ(RpcHandler::kReportRequestRpcName, rpc_name_); |
209 ReportRequest* report = static_cast<ReportRequest*>(request_proto_.get()); | 218 EXPECT_EQ(" API Key", api_key_); |
| 219 EXPECT_EQ(2u, request_protos_.size()); |
| 220 const ReportRequest* report = static_cast<ReportRequest*>(request_protos_[0]); |
210 RepeatedPtrField<TokenObservation> tokens_sent = | 221 RepeatedPtrField<TokenObservation> tokens_sent = |
211 report->update_signals_request().token_observation(); | 222 report->update_signals_request().token_observation(); |
212 ASSERT_EQ(2, tokens_sent.size()); | 223 ASSERT_EQ(2, tokens_sent.size()); |
213 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); | 224 EXPECT_EQ("token 1", tokens_sent.Get(0).token_id()); |
214 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); | 225 EXPECT_EQ("token 3", tokens_sent.Get(1).token_id()); |
215 } | 226 } |
216 | 227 |
217 TEST_F(RpcHandlerTest, ReportResponseHandler) { | 228 TEST_F(RpcHandlerTest, ReportResponseHandler) { |
218 // Fail on HTTP status != 200. | 229 // Fail on HTTP status != 200. |
219 ReportResponse empty_response; | 230 ReportResponse empty_response; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 messages_by_subscription_["Subscription 2"][1].payload()); | 280 messages_by_subscription_["Subscription 2"][1].payload()); |
270 | 281 |
271 ASSERT_EQ(2U, directive_handler->added_directives().size()); | 282 ASSERT_EQ(2U, directive_handler->added_directives().size()); |
272 EXPECT_EQ("Subscription 1", | 283 EXPECT_EQ("Subscription 1", |
273 directive_handler->added_directives()[0].subscription_id()); | 284 directive_handler->added_directives()[0].subscription_id()); |
274 EXPECT_EQ("Subscription 2", | 285 EXPECT_EQ("Subscription 2", |
275 directive_handler->added_directives()[1].subscription_id()); | 286 directive_handler->added_directives()[1].subscription_id()); |
276 } | 287 } |
277 | 288 |
278 } // namespace copresence | 289 } // namespace copresence |
OLD | NEW |