| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "jingle/notifier/base/weak_xmpp_client.h" | 13 #include "jingle/notifier/base/weak_xmpp_client.h" |
| 14 #include "net/base/cert_verifier.h" | 14 #include "net/base/cert_verifier.h" |
| 15 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "net/url_request/url_request_test_util.h" |
| 15 #include "talk/xmpp/prexmppauth.h" | 17 #include "talk/xmpp/prexmppauth.h" |
| 16 #include "talk/xmpp/xmppclientsettings.h" | 18 #include "talk/xmpp/xmppclientsettings.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 namespace buzz { | 22 namespace buzz { |
| 21 class CaptchaChallenge; | 23 class CaptchaChallenge; |
| 22 class Jid; | 24 class Jid; |
| 23 } // namespace buzz | 25 } // namespace buzz |
| 24 | 26 |
| 25 namespace talk_base { | 27 namespace talk_base { |
| 26 class CryptString; | 28 class CryptString; |
| 27 class SocketAddress; | 29 class SocketAddress; |
| 28 class Task; | 30 class Task; |
| 29 } // namespace talk_base | 31 } // namespace talk_base |
| 30 | 32 |
| 33 namespace { |
| 34 // Used to return a dummy context. |
| 35 class TestURLRequestContextGetter : public net::URLRequestContextGetter { |
| 36 public: |
| 37 TestURLRequestContextGetter() |
| 38 : message_loop_proxy_(base::MessageLoopProxy::CreateForCurrentThread()) { |
| 39 } |
| 40 virtual ~TestURLRequestContextGetter() { } |
| 41 |
| 42 // net::URLRequestContextGetter: |
| 43 virtual net::URLRequestContext* GetURLRequestContext() { |
| 44 if (!context_) |
| 45 context_ = new TestURLRequestContext(); |
| 46 return context_.get(); |
| 47 } |
| 48 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
| 49 return message_loop_proxy_; |
| 50 } |
| 51 |
| 52 private: |
| 53 scoped_refptr<net::URLRequestContext> context_; |
| 54 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 55 }; |
| 56 } |
| 57 |
| 31 namespace notifier { | 58 namespace notifier { |
| 32 | 59 |
| 33 using ::testing::_; | 60 using ::testing::_; |
| 34 using ::testing::Return; | 61 using ::testing::Return; |
| 35 using ::testing::SaveArg; | 62 using ::testing::SaveArg; |
| 36 | 63 |
| 37 class MockPreXmppAuth : public buzz::PreXmppAuth { | 64 class MockPreXmppAuth : public buzz::PreXmppAuth { |
| 38 public: | 65 public: |
| 39 virtual ~MockPreXmppAuth() {} | 66 virtual ~MockPreXmppAuth() {} |
| 40 | 67 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 59 public: | 86 public: |
| 60 virtual ~MockXmppConnectionDelegate() {} | 87 virtual ~MockXmppConnectionDelegate() {} |
| 61 | 88 |
| 62 MOCK_METHOD1(OnConnect, void(base::WeakPtr<talk_base::Task>)); | 89 MOCK_METHOD1(OnConnect, void(base::WeakPtr<talk_base::Task>)); |
| 63 MOCK_METHOD3(OnError, | 90 MOCK_METHOD3(OnError, |
| 64 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); | 91 void(buzz::XmppEngine::Error, int, const buzz::XmlElement*)); |
| 65 }; | 92 }; |
| 66 | 93 |
| 67 class XmppConnectionTest : public testing::Test { | 94 class XmppConnectionTest : public testing::Test { |
| 68 protected: | 95 protected: |
| 69 XmppConnectionTest() : mock_pre_xmpp_auth_(new MockPreXmppAuth()) {} | 96 XmppConnectionTest() |
| 97 : mock_pre_xmpp_auth_(new MockPreXmppAuth()), |
| 98 url_request_context_getter_(new TestURLRequestContextGetter()) {} |
| 70 | 99 |
| 71 virtual ~XmppConnectionTest() {} | 100 virtual ~XmppConnectionTest() {} |
| 72 | 101 |
| 73 virtual void TearDown() { | 102 virtual void TearDown() { |
| 74 // Clear out any messages posted by XmppConnection's destructor. | 103 // Clear out any messages posted by XmppConnection's destructor. |
| 75 message_loop_.RunAllPending(); | 104 message_loop_.RunAllPending(); |
| 76 } | 105 } |
| 77 | 106 |
| 78 // Needed by XmppConnection. | 107 // Needed by XmppConnection. |
| 79 MessageLoop message_loop_; | 108 MessageLoop message_loop_; |
| 80 net::CertVerifier cert_verifier_; | |
| 81 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; | 109 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; |
| 82 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; | 110 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; |
| 111 scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_; |
| 83 }; | 112 }; |
| 84 | 113 |
| 85 TEST_F(XmppConnectionTest, CreateDestroy) { | 114 TEST_F(XmppConnectionTest, CreateDestroy) { |
| 86 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 115 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 116 url_request_context_getter_, |
| 87 &mock_xmpp_connection_delegate_, NULL); | 117 &mock_xmpp_connection_delegate_, NULL); |
| 88 } | 118 } |
| 89 | 119 |
| 90 TEST_F(XmppConnectionTest, ImmediateFailure) { | 120 TEST_F(XmppConnectionTest, ImmediateFailure) { |
| 91 // ChromeAsyncSocket::Connect() will always return false since we're | 121 // ChromeAsyncSocket::Connect() will always return false since we're |
| 92 // not setting a valid host, but this gets bubbled up as ERROR_NONE | 122 // not setting a valid host, but this gets bubbled up as ERROR_NONE |
| 93 // due to XmppClient's inconsistent error-handling. | 123 // due to XmppClient's inconsistent error-handling. |
| 94 EXPECT_CALL(mock_xmpp_connection_delegate_, | 124 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 95 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 125 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 96 | 126 |
| 97 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 127 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 128 url_request_context_getter_, |
| 98 &mock_xmpp_connection_delegate_, NULL); | 129 &mock_xmpp_connection_delegate_, NULL); |
| 99 | 130 |
| 100 // We need to do this *before* |xmpp_connection| gets destroyed or | 131 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 101 // our delegate won't be called. | 132 // our delegate won't be called. |
| 102 message_loop_.RunAllPending(); | 133 message_loop_.RunAllPending(); |
| 103 } | 134 } |
| 104 | 135 |
| 105 TEST_F(XmppConnectionTest, PreAuthFailure) { | 136 TEST_F(XmppConnectionTest, PreAuthFailure) { |
| 106 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); | 137 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); |
| 107 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 138 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 108 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); | 139 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); |
| 109 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); | 140 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); |
| 110 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); | 141 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); |
| 111 | 142 |
| 112 EXPECT_CALL(mock_xmpp_connection_delegate_, | 143 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 113 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); | 144 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); |
| 114 | 145 |
| 115 XmppConnection xmpp_connection( | 146 XmppConnection xmpp_connection( |
| 116 buzz::XmppClientSettings(), &cert_verifier_, | 147 buzz::XmppClientSettings(), url_request_context_getter_, |
| 117 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 148 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 118 | 149 |
| 119 // We need to do this *before* |xmpp_connection| gets destroyed or | 150 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 120 // our delegate won't be called. | 151 // our delegate won't be called. |
| 121 message_loop_.RunAllPending(); | 152 message_loop_.RunAllPending(); |
| 122 } | 153 } |
| 123 | 154 |
| 124 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { | 155 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { |
| 125 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); | 156 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); |
| 126 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 157 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 127 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); | 158 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); |
| 128 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthCookie()).WillOnce(Return("")); | 159 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthCookie()).WillOnce(Return("")); |
| 129 | 160 |
| 130 EXPECT_CALL(mock_xmpp_connection_delegate_, | 161 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 131 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 162 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 132 | 163 |
| 133 XmppConnection xmpp_connection( | 164 XmppConnection xmpp_connection( |
| 134 buzz::XmppClientSettings(), &cert_verifier_, | 165 buzz::XmppClientSettings(), url_request_context_getter_, |
| 135 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); | 166 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 136 | 167 |
| 137 // We need to do this *before* |xmpp_connection| gets destroyed or | 168 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 138 // our delegate won't be called. | 169 // our delegate won't be called. |
| 139 message_loop_.RunAllPending(); | 170 message_loop_.RunAllPending(); |
| 140 } | 171 } |
| 141 | 172 |
| 142 TEST_F(XmppConnectionTest, RaisedError) { | 173 TEST_F(XmppConnectionTest, RaisedError) { |
| 143 EXPECT_CALL(mock_xmpp_connection_delegate_, | 174 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 144 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 175 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 145 | 176 |
| 146 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 177 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 178 url_request_context_getter_, |
| 147 &mock_xmpp_connection_delegate_, NULL); | 179 &mock_xmpp_connection_delegate_, NULL); |
| 148 | 180 |
| 149 xmpp_connection.weak_xmpp_client_-> | 181 xmpp_connection.weak_xmpp_client_-> |
| 150 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); | 182 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); |
| 151 } | 183 } |
| 152 | 184 |
| 153 TEST_F(XmppConnectionTest, Connect) { | 185 TEST_F(XmppConnectionTest, Connect) { |
| 154 base::WeakPtr<talk_base::Task> weak_ptr; | 186 base::WeakPtr<talk_base::Task> weak_ptr; |
| 155 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 187 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 156 WillOnce(SaveArg<0>(&weak_ptr)); | 188 WillOnce(SaveArg<0>(&weak_ptr)); |
| 157 | 189 |
| 158 { | 190 { |
| 159 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 191 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 192 url_request_context_getter_, |
| 160 &mock_xmpp_connection_delegate_, NULL); | 193 &mock_xmpp_connection_delegate_, NULL); |
| 161 | 194 |
| 162 xmpp_connection.weak_xmpp_client_-> | 195 xmpp_connection.weak_xmpp_client_-> |
| 163 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 196 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 164 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 197 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 165 } | 198 } |
| 166 | 199 |
| 167 EXPECT_EQ(NULL, weak_ptr.get()); | 200 EXPECT_EQ(NULL, weak_ptr.get()); |
| 168 } | 201 } |
| 169 | 202 |
| 170 TEST_F(XmppConnectionTest, MultipleConnect) { | 203 TEST_F(XmppConnectionTest, MultipleConnect) { |
| 171 EXPECT_DEBUG_DEATH({ | 204 EXPECT_DEBUG_DEATH({ |
| 172 base::WeakPtr<talk_base::Task> weak_ptr; | 205 base::WeakPtr<talk_base::Task> weak_ptr; |
| 173 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 206 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 174 WillOnce(SaveArg<0>(&weak_ptr)); | 207 WillOnce(SaveArg<0>(&weak_ptr)); |
| 175 | 208 |
| 176 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 209 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 210 url_request_context_getter_, |
| 177 &mock_xmpp_connection_delegate_, NULL); | 211 &mock_xmpp_connection_delegate_, NULL); |
| 178 | 212 |
| 179 xmpp_connection.weak_xmpp_client_-> | 213 xmpp_connection.weak_xmpp_client_-> |
| 180 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 214 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 181 for (int i = 0; i < 3; ++i) { | 215 for (int i = 0; i < 3; ++i) { |
| 182 xmpp_connection.weak_xmpp_client_-> | 216 xmpp_connection.weak_xmpp_client_-> |
| 183 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 217 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 184 } | 218 } |
| 185 | 219 |
| 186 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 220 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 187 }, "more than once"); | 221 }, "more than once"); |
| 188 } | 222 } |
| 189 | 223 |
| 190 TEST_F(XmppConnectionTest, ConnectThenError) { | 224 TEST_F(XmppConnectionTest, ConnectThenError) { |
| 191 base::WeakPtr<talk_base::Task> weak_ptr; | 225 base::WeakPtr<talk_base::Task> weak_ptr; |
| 192 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 226 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 193 WillOnce(SaveArg<0>(&weak_ptr)); | 227 WillOnce(SaveArg<0>(&weak_ptr)); |
| 194 EXPECT_CALL(mock_xmpp_connection_delegate_, | 228 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 195 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 229 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 196 | 230 |
| 197 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, | 231 XmppConnection xmpp_connection(buzz::XmppClientSettings(), |
| 232 url_request_context_getter_, |
| 198 &mock_xmpp_connection_delegate_, NULL); | 233 &mock_xmpp_connection_delegate_, NULL); |
| 199 | 234 |
| 200 xmpp_connection.weak_xmpp_client_-> | 235 xmpp_connection.weak_xmpp_client_-> |
| 201 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 236 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 202 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 237 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 203 | 238 |
| 204 xmpp_connection.weak_xmpp_client_-> | 239 xmpp_connection.weak_xmpp_client_-> |
| 205 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); | 240 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); |
| 206 EXPECT_EQ(NULL, weak_ptr.get()); | 241 EXPECT_EQ(NULL, weak_ptr.get()); |
| 207 } | 242 } |
| 208 | 243 |
| 209 } // namespace notifier | 244 } // namespace notifier |
| OLD | NEW |