| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "jingle/notifier/base/xmpp_connection.h" | 5 #include "jingle/notifier/base/xmpp_connection.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/message_loop/message_pump_default.h" |
| 14 #include "jingle/glue/mock_task.h" | 15 #include "jingle/glue/mock_task.h" |
| 15 #include "jingle/glue/task_pump.h" | 16 #include "jingle/glue/task_pump.h" |
| 16 #include "jingle/notifier/base/weak_xmpp_client.h" | 17 #include "jingle/notifier/base/weak_xmpp_client.h" |
| 17 #include "net/cert/cert_verifier.h" | 18 #include "net/cert/cert_verifier.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 20 #include "talk/xmpp/prexmppauth.h" | 21 #include "talk/xmpp/prexmppauth.h" |
| 21 #include "talk/xmpp/xmppclientsettings.h" | 22 #include "talk/xmpp/xmppclientsettings.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 virtual ~MockXmppConnectionDelegate() {} | 68 virtual ~MockXmppConnectionDelegate() {} |
| 68 | 69 |
| 69 MOCK_METHOD1(OnConnect, void(base::WeakPtr<buzz::XmppTaskParentInterface>)); | 70 MOCK_METHOD1(OnConnect, void(base::WeakPtr<buzz::XmppTaskParentInterface>)); |
| 70 MOCK_METHOD3(OnError, | 71 MOCK_METHOD3(OnError, |
| 71 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); | 72 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 class XmppConnectionTest : public testing::Test { | 75 class XmppConnectionTest : public testing::Test { |
| 75 protected: | 76 protected: |
| 76 XmppConnectionTest() | 77 XmppConnectionTest() |
| 77 : mock_pre_xmpp_auth_(new MockPreXmppAuth()), | 78 : mock_pre_xmpp_auth_(new MockPreXmppAuth()) { |
| 78 url_request_context_getter_(new net::TestURLRequestContextGetter( | 79 scoped_ptr<base::MessagePump> pump(new base::MessagePumpDefault()); |
| 79 message_loop_.message_loop_proxy())) {} | 80 message_loop_.reset(new base::MessageLoop(pump.Pass())); |
| 81 |
| 82 url_request_context_getter_ = new net::TestURLRequestContextGetter( |
| 83 message_loop_->message_loop_proxy()); |
| 84 } |
| 80 | 85 |
| 81 virtual ~XmppConnectionTest() {} | 86 virtual ~XmppConnectionTest() {} |
| 82 | 87 |
| 83 virtual void TearDown() { | 88 virtual void TearDown() { |
| 84 // Clear out any messages posted by XmppConnection's destructor. | 89 // Clear out any messages posted by XmppConnection's destructor. |
| 85 message_loop_.RunUntilIdle(); | 90 message_loop_->RunUntilIdle(); |
| 86 } | 91 } |
| 87 | 92 |
| 88 // Needed by XmppConnection. | 93 // Needed by XmppConnection. |
| 89 base::MessageLoop message_loop_; | 94 scoped_ptr<base::MessageLoop> message_loop_; |
| 90 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; | 95 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; |
| 91 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; | 96 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; |
| 92 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 97 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 93 }; | 98 }; |
| 94 | 99 |
| 95 TEST_F(XmppConnectionTest, CreateDestroy) { | 100 TEST_F(XmppConnectionTest, CreateDestroy) { |
| 96 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 101 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 97 url_request_context_getter_, | 102 url_request_context_getter_, |
| 98 &mock_xmpp_connection_delegate_, NULL); | 103 &mock_xmpp_connection_delegate_, NULL); |
| 99 } | 104 } |
| 100 | 105 |
| 101 #if !defined(_MSC_VER) || _MSC_VER < 1700 // http://crbug.com/158570 | 106 #if !defined(_MSC_VER) || _MSC_VER < 1700 // http://crbug.com/158570 |
| 102 TEST_F(XmppConnectionTest, ImmediateFailure) { | 107 TEST_F(XmppConnectionTest, ImmediateFailure) { |
| 103 // ChromeAsyncSocket::Connect() will always return false since we're | 108 // ChromeAsyncSocket::Connect() will always return false since we're |
| 104 // not setting a valid host, but this gets bubbled up as ERROR_NONE | 109 // not setting a valid host, but this gets bubbled up as ERROR_NONE |
| 105 // due to XmppClient's inconsistent error-handling. | 110 // due to XmppClient's inconsistent error-handling. |
| 106 EXPECT_CALL(mock_xmpp_connection_delegate_, | 111 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 107 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 112 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 108 | 113 |
| 109 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 114 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 110 url_request_context_getter_, | 115 url_request_context_getter_, |
| 111 &mock_xmpp_connection_delegate_, NULL); | 116 &mock_xmpp_connection_delegate_, NULL); |
| 112 | 117 |
| 113 // We need to do this *before* |xmpp_connection| gets destroyed or | 118 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 114 // our delegate won't be called. | 119 // our delegate won't be called. |
| 115 message_loop_.RunUntilIdle(); | 120 message_loop_->RunUntilIdle(); |
| 116 } | 121 } |
| 117 | 122 |
| 118 TEST_F(XmppConnectionTest, PreAuthFailure) { | 123 TEST_F(XmppConnectionTest, PreAuthFailure) { |
| 119 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); | 124 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); |
| 120 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 125 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 121 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); | 126 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); |
| 122 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); | 127 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); |
| 123 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); | 128 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); |
| 124 | 129 |
| 125 EXPECT_CALL(mock_xmpp_connection_delegate_, | 130 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 126 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); | 131 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); |
| 127 | 132 |
| 128 XmppConnection xmpp_connection( | 133 XmppConnection xmpp_connection( |
| 129 buzz::XmppClientSettings(), url_request_context_getter_, | 134 buzz::XmppClientSettings(), url_request_context_getter_, |
| 130 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 135 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 131 | 136 |
| 132 // We need to do this *before* |xmpp_connection| gets destroyed or | 137 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 133 // our delegate won't be called. | 138 // our delegate won't be called. |
| 134 message_loop_.RunUntilIdle(); | 139 message_loop_->RunUntilIdle(); |
| 135 } | 140 } |
| 136 | 141 |
| 137 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { | 142 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { |
| 138 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); | 143 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _,_)); |
| 139 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 144 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 140 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); | 145 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); |
| 141 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthMechanism()).WillOnce(Return("")); | 146 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthMechanism()).WillOnce(Return("")); |
| 142 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthToken()).WillOnce(Return("")); | 147 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthToken()).WillOnce(Return("")); |
| 143 | 148 |
| 144 EXPECT_CALL(mock_xmpp_connection_delegate_, | 149 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 145 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 150 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 146 | 151 |
| 147 XmppConnection xmpp_connection( | 152 XmppConnection xmpp_connection( |
| 148 buzz::XmppClientSettings(), url_request_context_getter_, | 153 buzz::XmppClientSettings(), url_request_context_getter_, |
| 149 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 154 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 150 | 155 |
| 151 // We need to do this *before* |xmpp_connection| gets destroyed or | 156 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 152 // our delegate won't be called. | 157 // our delegate won't be called. |
| 153 message_loop_.RunUntilIdle(); | 158 message_loop_->RunUntilIdle(); |
| 154 } | 159 } |
| 155 | 160 |
| 156 TEST_F(XmppConnectionTest, RaisedError) { | 161 TEST_F(XmppConnectionTest, RaisedError) { |
| 157 EXPECT_CALL(mock_xmpp_connection_delegate_, | 162 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 158 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 163 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 159 | 164 |
| 160 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 165 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 161 url_request_context_getter_, | 166 url_request_context_getter_, |
| 162 &mock_xmpp_connection_delegate_, NULL); | 167 &mock_xmpp_connection_delegate_, NULL); |
| 163 | 168 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 // We have to do this since the state enum is protected in | 245 // We have to do this since the state enum is protected in |
| 241 // talk_base::Task. | 246 // talk_base::Task. |
| 242 const int TASK_STATE_ERROR = 3; | 247 const int TASK_STATE_ERROR = 3; |
| 243 ON_CALL(*task, ProcessStart()) | 248 ON_CALL(*task, ProcessStart()) |
| 244 .WillByDefault(Return(TASK_STATE_ERROR)); | 249 .WillByDefault(Return(TASK_STATE_ERROR)); |
| 245 EXPECT_CALL(*task, ProcessStart()).Times(0); | 250 EXPECT_CALL(*task, ProcessStart()).Times(0); |
| 246 task->Start(); | 251 task->Start(); |
| 247 } | 252 } |
| 248 | 253 |
| 249 // This should destroy |task_pump|, but |task| still shouldn't run. | 254 // This should destroy |task_pump|, but |task| still shouldn't run. |
| 250 message_loop_.RunUntilIdle(); | 255 message_loop_->RunUntilIdle(); |
| 251 } | 256 } |
| 252 | 257 |
| 253 } // namespace notifier | 258 } // namespace notifier |
| OLD | NEW |