Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: blimp/client/session/assignment_source_unittest.cc

Issue 1687393002: Add assigner support to Blimp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added tests and addressed comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "blimp/client/session/assignment_source.h"
6
7 #include "base/command_line.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/test/test_simple_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/values.h"
13 #include "blimp/client/app/blimp_client_switches.h"
14 #include "blimp/common/protocol_version.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using testing::_;
20 using testing::InSequence;
21
22 namespace blimp {
23 namespace client {
24 namespace {
25
26 MATCHER_P(AssignmentMatcher, assignment, "") {
Kevin M 2016/02/17 22:00:15 AssignmentEquals
David Trainor- moved to gerrit 2016/02/17 23:02:31 Done.
27 return arg.ip_endpoint == assignment.ip_endpoint &&
28 arg.client_token == assignment.client_token &&
29 arg.certificate == assignment.certificate &&
30 arg.certificate_fingerprint == assignment.certificate_fingerprint;
31 }
32
33 net::IPEndPoint BuildIPEndPoint(const std::string& ip, int port) {
34 net::IPAddress ip_address;
35 EXPECT_TRUE(ip_address.AssignFromIPLiteral(ip));
36
37 return net::IPEndPoint(ip_address, port);
38 }
39
40 Assignment BuildValidAssignment() {
41 Assignment assignment;
42 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500);
43 assignment.client_token = "SecretT0kenz";
44 assignment.certificate_fingerprint = "WhaleWhaleWhale";
45 assignment.certificate = "whaaaaaaaaaaaaale";
46 return assignment;
47 }
48
49 std::string BuildResponseFromAssignment(const Assignment& assignment) {
50 base::DictionaryValue dict;
51 dict.SetString("clientToken", assignment.client_token);
52 dict.SetString("host", assignment.ip_endpoint.address().ToString());
53 dict.SetInteger("port", assignment.ip_endpoint.port());
54 dict.SetString("certificateFingerprint", assignment.certificate_fingerprint);
55 dict.SetString("certificate", assignment.certificate);
56
57 std::string json;
58 base::JSONWriter::Write(dict, &json);
59 return json;
60 }
61
62 class AssignmentSourceTest : public testing::Test {
63 public:
64 AssignmentSourceTest()
65 : task_runner_(new base::TestSimpleTaskRunner),
66 task_runner_handle_(task_runner_),
67 source_(task_runner_, task_runner_) {}
68
69 void GetCustomAssignment() {
Kevin M 2016/02/17 22:00:15 Comment? What does custom mean here?
David Trainor- moved to gerrit 2016/02/17 23:02:32 Done.
70 source_.GetAssignment("",
71 base::Bind(&AssignmentSourceTest::AssignmentResponse,
72 base::Unretained(this)));
73 EXPECT_EQ(nullptr, factory_.GetFetcherByID(0));
74 task_runner_->RunUntilIdle();
75 }
76
77 // See net/base/net_errors.h for possible status errors.
78 void GetAssignment(net::HttpStatusCode response_code,
79 int status,
80 const std::string& response,
81 const std::string& client_auth_token,
82 const std::string& protocol_version) {
83 source_.GetAssignment(client_auth_token,
84 base::Bind(&AssignmentSourceTest::AssignmentResponse,
85 base::Unretained(this)));
86
87 net::TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
88
89 task_runner_->RunUntilIdle();
90
91 EXPECT_NE(nullptr, fetcher);
92 EXPECT_EQ(kDefaultAssignerURL, fetcher->GetOriginalURL().spec());
93
94 // Check that the request has a valid protocol_version.
95 scoped_ptr<base::Value> json =
96 base::JSONReader::Read(fetcher->upload_data());
97 EXPECT_NE(nullptr, json.get());
98
99 const base::DictionaryValue* dict;
100 EXPECT_TRUE(json->GetAsDictionary(&dict));
101
102 std::string uploaded_protocol_version;
103 EXPECT_TRUE(
104 dict->GetString("protocol_version", &uploaded_protocol_version));
105 EXPECT_EQ(protocol_version, uploaded_protocol_version);
106
107 // Check that the request has a valid authentication header.
108 net::HttpRequestHeaders headers;
109 fetcher->GetExtraRequestHeaders(&headers);
110
111 std::string authorization;
112 EXPECT_TRUE(headers.GetHeader("Authorization", &authorization));
113 EXPECT_EQ("Bearer " + client_auth_token, authorization);
114
115 // Send the fake response back.
116 fetcher->set_response_code(response_code);
117 fetcher->set_status(net::URLRequestStatus::FromError(status));
118 fetcher->SetResponseString(response);
119 fetcher->delegate()->OnURLFetchComplete(fetcher);
120
121 task_runner_->RunUntilIdle();
122 }
123
124 MOCK_METHOD2(AssignmentResponse,
125 void(AssignmentSource::Result, const Assignment&));
126
127 protected:
128 // Used to drive all AssignmentSource tasks.
129 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
130 base::ThreadTaskRunnerHandle task_runner_handle_;
131
132 net::TestURLFetcherFactory factory_;
133
134 AssignmentSource source_;
135 };
136
137 TEST_F(AssignmentSourceTest, TestCustomSuccess) {
Kevin M 2016/02/17 22:00:15 Custom...? How about TestAlternateEndpointSuccess
David Trainor- moved to gerrit 2016/02/17 23:02:31 Done.
138 Assignment assignment;
139 assignment.ip_endpoint = BuildIPEndPoint("100.150.200.250", 500);
140 assignment.client_token = kDummyClientToken;
141
142 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
143 switches::kBlimpletEndpoint, "100.150.200.250:500");
144
145 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK,
146 AssignmentMatcher(assignment)))
147 .Times(1);
148
149 GetCustomAssignment();
150 }
151
152 TEST_F(AssignmentSourceTest, TestSuccess) {
153 Assignment assignment = BuildValidAssignment();
154
155 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK,
156 AssignmentMatcher(assignment)))
157 .Times(1);
158
159 GetAssignment(net::HTTP_OK, net::Error::OK,
160 BuildResponseFromAssignment(assignment), "UserAuthT0kenz",
161 kEngineVersion);
162 }
163
164 TEST_F(AssignmentSourceTest, TestRequestInterruption) {
Kevin M 2016/02/17 22:00:15 Hmmm... ValidAfterError?
David Trainor- moved to gerrit 2016/02/17 23:02:31 I'll add that test. This is testing that two subs
165 InSequence sequence;
166 Assignment assignment = BuildValidAssignment();
167
168 source_.GetAssignment("",
169 base::Bind(&AssignmentSourceTest::AssignmentResponse,
170 base::Unretained(this)));
171
172 EXPECT_CALL(*this, AssignmentResponse(
173 AssignmentSource::Result::RESULT_SERVER_INTERRUPTED,
174 AssignmentMatcher(Assignment())))
175 .Times(1)
176 .RetiresOnSaturation();
177
178 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK,
179 AssignmentMatcher(assignment)))
180 .Times(1)
181 .RetiresOnSaturation();
182
183 GetAssignment(net::HTTP_OK, net::Error::OK,
184 BuildResponseFromAssignment(assignment), "UserAuthT0kenz",
185 kEngineVersion);
186 }
187
188 TEST_F(AssignmentSourceTest, TestNetworkFailure) {
189 EXPECT_CALL(*this, AssignmentResponse(
190 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _));
191 GetAssignment(net::HTTP_OK, net::Error::ERR_INSUFFICIENT_RESOURCES, "",
192 "UserAuthT0kenz", kEngineVersion);
193 }
194
195 TEST_F(AssignmentSourceTest, TestBadRequest) {
196 EXPECT_CALL(*this, AssignmentResponse(
197 AssignmentSource::Result::RESULT_BAD_REQUEST, _));
198 GetAssignment(net::HTTP_BAD_REQUEST, net::Error::OK, "", "UserAuthT0kenz",
199 kEngineVersion);
200 }
201
202 TEST_F(AssignmentSourceTest, TestUnauthorized) {
203 EXPECT_CALL(*this,
204 AssignmentResponse(
205 AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, _));
206 GetAssignment(net::HTTP_UNAUTHORIZED, net::Error::OK, "", "UserAuthT0kenz",
207 kEngineVersion);
208 }
209
210 TEST_F(AssignmentSourceTest, TestForbidden) {
211 EXPECT_CALL(*this, AssignmentResponse(
212 AssignmentSource::Result::RESULT_USER_INVALID, _));
213 GetAssignment(net::HTTP_FORBIDDEN, net::Error::OK, "", "UserAuthT0kenz",
214 kEngineVersion);
215 }
216
217 TEST_F(AssignmentSourceTest, TestTooManyRequests) {
218 EXPECT_CALL(*this, AssignmentResponse(
219 AssignmentSource::Result::RESULT_OUT_OF_VMS, _));
220 GetAssignment(static_cast<net::HttpStatusCode>(429), net::Error::OK, "",
221 "UserAuthT0kenz", kEngineVersion);
222 }
223
224 TEST_F(AssignmentSourceTest, TestInternalServerError) {
225 EXPECT_CALL(*this, AssignmentResponse(
226 AssignmentSource::Result::RESULT_SERVER_ERROR, _));
227 GetAssignment(net::HTTP_INTERNAL_SERVER_ERROR, net::Error::OK, "",
228 "UserAuthT0kenz", kEngineVersion);
229 }
230
231 TEST_F(AssignmentSourceTest, TestServerNetBadResposne) {
Kevin M 2016/02/17 22:00:15 TestUnexpectedNetCodeFallback?
David Trainor- moved to gerrit 2016/02/17 23:02:31 Done.
232 EXPECT_CALL(*this, AssignmentResponse(
233 AssignmentSource::Result::RESULT_BAD_RESPONSE, _));
234 GetAssignment(net::HTTP_NOT_IMPLEMENTED, net::Error::OK, "", "UserAuthT0kenz",
235 kEngineVersion);
236 }
237
238 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) {
239 Assignment assignment = BuildValidAssignment();
240
241 // Remove half the response.
242 std::string response = BuildResponseFromAssignment(assignment);
243 response = response.substr(response.size() / 2);
244
245 EXPECT_CALL(*this, AssignmentResponse(
246 AssignmentSource::Result::RESULT_BAD_RESPONSE, _));
247 GetAssignment(net::HTTP_OK, net::Error::OK, response, "UserAuthT0kenz",
248 kEngineVersion);
249 }
250
Kevin M 2016/02/17 22:00:15 Test cases for: missing response field? unparseab
David Trainor- moved to gerrit 2016/02/17 23:02:31 Done.
251 } // namespace
252 } // namespace client
253 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698