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