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