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

Side by Side Diff: chrome/browser/local_discovery/privetv3_session_unittest.cc

Issue 1106183006: Added gcdPrivate.getDeviceInfo function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no_establish
Patch Set: Mon Apr 27 22:56:49 PDT 2015 Created 5 years, 7 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
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 "chrome/browser/local_discovery/privetv3_session.h" 5 #include "chrome/browser/local_discovery/privetv3_session.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/local_discovery/privet_http.h" 9 #include "chrome/browser/local_discovery/privet_http.h"
10 #include "content/public/test/test_utils.h" 10 #include "content/public/test/test_utils.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 } // namespace 73 } // namespace
74 74
75 class PrivetV3SessionTest : public testing::Test { 75 class PrivetV3SessionTest : public testing::Test {
76 public: 76 public:
77 PrivetV3SessionTest() 77 PrivetV3SessionTest()
78 : fetcher_factory_(nullptr), 78 : fetcher_factory_(nullptr),
79 session_(make_scoped_ptr(new MockPrivetHTTPClient())) {} 79 session_(make_scoped_ptr(new MockPrivetHTTPClient())) {}
80 80
81 ~PrivetV3SessionTest() override {} 81 ~PrivetV3SessionTest() override {}
82 82
83 MOCK_METHOD2(OnInitialized, void(Result, const std::vector<PairingType>&)); 83 void OnInitialized(Result result, const base::DictionaryValue& info) {
84 info_.MergeDictionary(&info);
85 OnInitializedMock(result, info);
86 }
87
88 MOCK_METHOD2(OnInitializedMock, void(Result, const base::DictionaryValue&));
84 MOCK_METHOD1(OnPairingStarted, void(Result)); 89 MOCK_METHOD1(OnPairingStarted, void(Result));
85 MOCK_METHOD1(OnCodeConfirmed, void(Result)); 90 MOCK_METHOD1(OnCodeConfirmed, void(Result));
86 MOCK_METHOD2(OnMessageSend, void(Result, const base::DictionaryValue& value)); 91 MOCK_METHOD2(OnMessageSend, void(Result, const base::DictionaryValue&));
87 MOCK_METHOD1(OnPostData, void(const base::DictionaryValue& data)); 92 MOCK_METHOD1(OnPostData, void(const base::DictionaryValue&));
88 93
89 protected: 94 protected:
90 void SetUp() override { 95 void SetUp() override {
91 EXPECT_CALL(*this, OnInitialized(_, _)).Times(0); 96 EXPECT_CALL(*this, OnInitializedMock(_, _)).Times(0);
92 EXPECT_CALL(*this, OnPairingStarted(_)).Times(0); 97 EXPECT_CALL(*this, OnPairingStarted(_)).Times(0);
93 EXPECT_CALL(*this, OnCodeConfirmed(_)).Times(0); 98 EXPECT_CALL(*this, OnCodeConfirmed(_)).Times(0);
94 EXPECT_CALL(*this, OnMessageSend(_, _)).Times(0); 99 EXPECT_CALL(*this, OnMessageSend(_, _)).Times(0);
95 EXPECT_CALL(*this, OnPostData(_)).Times(0); 100 EXPECT_CALL(*this, OnPostData(_)).Times(0);
96 session_.on_post_data_ = 101 session_.on_post_data_ =
97 base::Bind(&PrivetV3SessionTest::OnPostData, base::Unretained(this)); 102 base::Bind(&PrivetV3SessionTest::OnPostData, base::Unretained(this));
98 } 103 }
99 104
105 base::DictionaryValue info_;
100 base::MessageLoop loop_; 106 base::MessageLoop loop_;
101 base::Closure quit_closure_; 107 base::Closure quit_closure_;
102 net::FakeURLFetcherFactory fetcher_factory_; 108 net::FakeURLFetcherFactory fetcher_factory_;
103 PrivetV3Session session_; 109 PrivetV3Session session_;
104 }; 110 };
105 111
106 TEST_F(PrivetV3SessionTest, InitError) { 112 TEST_F(PrivetV3SessionTest, InitError) {
107 EXPECT_CALL(*this, OnInitialized(Result::STATUS_CONNECTIONERROR, _)).Times(1); 113 EXPECT_CALL(*this, OnInitializedMock(Result::STATUS_CONNECTIONERROR, _))
114 .Times(1);
108 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), "", 115 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), "",
109 net::HTTP_OK, net::URLRequestStatus::FAILED); 116 net::HTTP_OK, net::URLRequestStatus::FAILED);
110 session_.Init( 117 session_.Init(
111 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this))); 118 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this)));
112 base::RunLoop().RunUntilIdle(); 119 base::RunLoop().RunUntilIdle();
113 } 120 }
114 121
115 TEST_F(PrivetV3SessionTest, VersionError) { 122 TEST_F(PrivetV3SessionTest, VersionError) {
116 std::string response(kInfoResponse); 123 std::string response(kInfoResponse);
117 ReplaceFirstSubstringAfterOffset(&response, 0, "3.0", "4.1"); 124 ReplaceFirstSubstringAfterOffset(&response, 0, "3.0", "4.1");
118 125
119 EXPECT_CALL(*this, OnInitialized(Result::STATUS_SESSIONERROR, _)).Times(1); 126 EXPECT_CALL(*this, OnInitializedMock(Result::STATUS_SESSIONERROR, _))
127 .Times(1);
120 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), response, 128 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), response,
121 net::HTTP_OK, 129 net::HTTP_OK,
122 net::URLRequestStatus::SUCCESS); 130 net::URLRequestStatus::SUCCESS);
123 session_.Init( 131 session_.Init(
124 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this))); 132 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this)));
125 base::RunLoop().RunUntilIdle(); 133 base::RunLoop().RunUntilIdle();
126 } 134 }
127 135
128 TEST_F(PrivetV3SessionTest, ModeError) { 136 TEST_F(PrivetV3SessionTest, ModeError) {
129 std::string response(kInfoResponse); 137 std::string response(kInfoResponse);
130 ReplaceFirstSubstringAfterOffset(&response, 0, "mode", "mode_"); 138 ReplaceFirstSubstringAfterOffset(&response, 0, "mode", "mode_");
131 139
132 EXPECT_CALL(*this, OnInitialized(Result::STATUS_SESSIONERROR, _)).Times(1); 140 EXPECT_CALL(*this, OnInitializedMock(Result::STATUS_SESSIONERROR, _))
141 .Times(1);
133 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), response, 142 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), response,
134 net::HTTP_OK, 143 net::HTTP_OK,
135 net::URLRequestStatus::SUCCESS); 144 net::URLRequestStatus::SUCCESS);
136 session_.Init( 145 session_.Init(
137 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this))); 146 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this)));
138 base::RunLoop().RunUntilIdle(); 147 base::RunLoop().RunUntilIdle();
139 } 148 }
140 149
141 TEST_F(PrivetV3SessionTest, Pairing) { 150 TEST_F(PrivetV3SessionTest, Pairing) {
142 std::vector<PairingType> pairings; 151 EXPECT_CALL(*this, OnInitializedMock(Result::STATUS_SUCCESS, _))
143 EXPECT_CALL(*this, OnInitialized(Result::STATUS_SUCCESS, _)) 152 .Times(1);
144 .WillOnce(SaveArg<1>(&pairings));
145 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), 153 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"),
146 kInfoResponse, net::HTTP_OK, 154 kInfoResponse, net::HTTP_OK,
147 net::URLRequestStatus::SUCCESS); 155 net::URLRequestStatus::SUCCESS);
148 156
149 session_.Init( 157 session_.Init(
150 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this))); 158 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this)));
151 base::RunLoop().RunUntilIdle(); 159 base::RunLoop().RunUntilIdle();
152 160
153 EXPECT_EQ(2u, pairings.size()); 161 const base::ListValue* pairing = nullptr;
154 EXPECT_EQ(PairingType::PAIRING_TYPE_PINCODE, pairings[0]); 162 ASSERT_TRUE(info_.GetList("authentication.pairing", &pairing));
155 EXPECT_EQ(PairingType::PAIRING_TYPE_EMBEDDEDCODE, pairings[1]); 163
164 std::string pairing_string;
165 ASSERT_TRUE(pairing->GetString(0, &pairing_string));
166 EXPECT_EQ("pinCode", pairing_string);
167
168 ASSERT_TRUE(pairing->GetString(1, &pairing_string));
169 EXPECT_EQ("embeddedCode", pairing_string);
156 170
157 crypto::P224EncryptedKeyExchange spake( 171 crypto::P224EncryptedKeyExchange spake(
158 crypto::P224EncryptedKeyExchange::kPeerTypeServer, "testPin"); 172 crypto::P224EncryptedKeyExchange::kPeerTypeServer, "testPin");
159 173
160 EXPECT_CALL(*this, OnPairingStarted(Result::STATUS_SUCCESS)).Times(1); 174 EXPECT_CALL(*this, OnPairingStarted(Result::STATUS_SUCCESS)).Times(1);
161 EXPECT_CALL(*this, OnPostData(_)) 175 EXPECT_CALL(*this, OnPostData(_))
162 .WillOnce( 176 .WillOnce(
163 testing::Invoke([this, &spake](const base::DictionaryValue& data) { 177 testing::Invoke([this, &spake](const base::DictionaryValue& data) {
164 std::string pairing_type; 178 std::string pairing_type;
165 EXPECT_TRUE(data.GetString("pairing", &pairing_type)); 179 EXPECT_TRUE(data.GetString("pairing", &pairing_type));
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 session_.ConfirmCode("testPin", 261 session_.ConfirmCode("testPin",
248 base::Bind(&PrivetV3SessionTest::OnCodeConfirmed, 262 base::Bind(&PrivetV3SessionTest::OnCodeConfirmed,
249 base::Unretained(this))); 263 base::Unretained(this)));
250 base::RunLoop().RunUntilIdle(); 264 base::RunLoop().RunUntilIdle();
251 265
252 EXPECT_FALSE(session_.fingerprint_.empty()); 266 EXPECT_FALSE(session_.fingerprint_.empty());
253 EXPECT_EQ("testType 567", session_.privet_auth_token_); 267 EXPECT_EQ("testType 567", session_.privet_auth_token_);
254 } 268 }
255 269
256 TEST_F(PrivetV3SessionTest, Cancel) { 270 TEST_F(PrivetV3SessionTest, Cancel) {
257 EXPECT_CALL(*this, OnInitialized(Result::STATUS_SUCCESS, _)); 271 EXPECT_CALL(*this, OnInitializedMock(Result::STATUS_SUCCESS, _)).Times(1);;
258 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"), 272 fetcher_factory_.SetFakeResponse(GURL("http://host/privet/info"),
259 kInfoResponse, net::HTTP_OK, 273 kInfoResponse, net::HTTP_OK,
260 net::URLRequestStatus::SUCCESS); 274 net::URLRequestStatus::SUCCESS);
261 275
262 session_.Init( 276 session_.Init(
263 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this))); 277 base::Bind(&PrivetV3SessionTest::OnInitialized, base::Unretained(this)));
264 base::RunLoop().RunUntilIdle(); 278 base::RunLoop().RunUntilIdle();
265 279
266 EXPECT_CALL(*this, OnPairingStarted(Result::STATUS_SUCCESS)).Times(1); 280 EXPECT_CALL(*this, OnPairingStarted(Result::STATUS_SUCCESS)).Times(1);
267 EXPECT_CALL(*this, OnPostData(_)) 281 EXPECT_CALL(*this, OnPostData(_))
(...skipping 19 matching lines...) Expand all
287 .WillOnce(testing::Invoke([this](const base::DictionaryValue& data) { 301 .WillOnce(testing::Invoke([this](const base::DictionaryValue& data) {
288 std::string session_id; 302 std::string session_id;
289 EXPECT_TRUE(data.GetString("sessionId", &session_id)); 303 EXPECT_TRUE(data.GetString("sessionId", &session_id));
290 })); 304 }));
291 } 305 }
292 306
293 // TODO(vitalybuka): replace PrivetHTTPClient with regular URL fetcher and 307 // TODO(vitalybuka): replace PrivetHTTPClient with regular URL fetcher and
294 // implement SendMessage test. 308 // implement SendMessage test.
295 309
296 } // namespace local_discovery 310 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privetv3_session.cc ('k') | chrome/common/extensions/api/gcd_private.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698