| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/weak_ptr.h" | 12 #include "base/weak_ptr.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 "talk/xmpp/prexmppauth.h" | 15 #include "talk/xmpp/prexmppauth.h" |
| 15 #include "talk/xmpp/xmppclientsettings.h" | 16 #include "talk/xmpp/xmppclientsettings.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace buzz { | 20 namespace buzz { |
| 20 class CaptchaChallenge; | 21 class CaptchaChallenge; |
| 21 class Jid; | 22 class Jid; |
| 22 } // namespace buzz | 23 } // namespace buzz |
| 23 | 24 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 70 |
| 70 virtual ~XmppConnectionTest() {} | 71 virtual ~XmppConnectionTest() {} |
| 71 | 72 |
| 72 virtual void TearDown() { | 73 virtual void TearDown() { |
| 73 // Clear out any messages posted by XmppConnection's destructor. | 74 // Clear out any messages posted by XmppConnection's destructor. |
| 74 message_loop_.RunAllPending(); | 75 message_loop_.RunAllPending(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 // Needed by XmppConnection. | 78 // Needed by XmppConnection. |
| 78 MessageLoop message_loop_; | 79 MessageLoop message_loop_; |
| 80 net::CertVerifier cert_verifier_; |
| 79 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; | 81 MockXmppConnectionDelegate mock_xmpp_connection_delegate_; |
| 80 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; | 82 scoped_ptr<MockPreXmppAuth> mock_pre_xmpp_auth_; |
| 81 }; | 83 }; |
| 82 | 84 |
| 83 TEST_F(XmppConnectionTest, CreateDestroy) { | 85 TEST_F(XmppConnectionTest, CreateDestroy) { |
| 84 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 86 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 85 &mock_xmpp_connection_delegate_, NULL); | 87 &mock_xmpp_connection_delegate_, NULL); |
| 86 } | 88 } |
| 87 | 89 |
| 88 TEST_F(XmppConnectionTest, ImmediateFailure) { | 90 TEST_F(XmppConnectionTest, ImmediateFailure) { |
| 89 // ChromeAsyncSocket::Connect() will always return false since we're | 91 // ChromeAsyncSocket::Connect() will always return false since we're |
| 90 // not setting a valid host, but this gets bubbled up as ERROR_NONE | 92 // not setting a valid host, but this gets bubbled up as ERROR_NONE |
| 91 // due to XmppClient's inconsistent error-handling. | 93 // due to XmppClient's inconsistent error-handling. |
| 92 EXPECT_CALL(mock_xmpp_connection_delegate_, | 94 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 93 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 95 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 94 | 96 |
| 95 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 97 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 96 &mock_xmpp_connection_delegate_, NULL); | 98 &mock_xmpp_connection_delegate_, NULL); |
| 97 | 99 |
| 98 // We need to do this *before* |xmpp_connection| gets destroyed or | 100 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 99 // our delegate won't be called. | 101 // our delegate won't be called. |
| 100 message_loop_.RunAllPending(); | 102 message_loop_.RunAllPending(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 TEST_F(XmppConnectionTest, PreAuthFailure) { | 105 TEST_F(XmppConnectionTest, PreAuthFailure) { |
| 104 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); | 106 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); |
| 105 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 107 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 106 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); | 108 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(false)); |
| 107 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); | 109 EXPECT_CALL(*mock_pre_xmpp_auth_, HadError()).WillOnce(Return(true)); |
| 108 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); | 110 EXPECT_CALL(*mock_pre_xmpp_auth_, GetError()).WillOnce(Return(5)); |
| 109 | 111 |
| 110 EXPECT_CALL(mock_xmpp_connection_delegate_, | 112 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 111 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); | 113 OnError(buzz::XmppEngine::ERROR_AUTH, 5, NULL)); |
| 112 | 114 |
| 113 XmppConnection xmpp_connection( | 115 XmppConnection xmpp_connection( |
| 114 buzz::XmppClientSettings(), &mock_xmpp_connection_delegate_, | 116 buzz::XmppClientSettings(), &cert_verifier_, |
| 115 mock_pre_xmpp_auth_.release()); | 117 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 116 | 118 |
| 117 // We need to do this *before* |xmpp_connection| gets destroyed or | 119 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 118 // our delegate won't be called. | 120 // our delegate won't be called. |
| 119 message_loop_.RunAllPending(); | 121 message_loop_.RunAllPending(); |
| 120 } | 122 } |
| 121 | 123 |
| 122 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { | 124 TEST_F(XmppConnectionTest, FailureAfterPreAuth) { |
| 123 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); | 125 EXPECT_CALL(*mock_pre_xmpp_auth_, StartPreXmppAuth(_, _, _, _)); |
| 124 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); | 126 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthDone()).WillOnce(Return(true)); |
| 125 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); | 127 EXPECT_CALL(*mock_pre_xmpp_auth_, IsAuthorized()).WillOnce(Return(true)); |
| 126 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthCookie()).WillOnce(Return("")); | 128 EXPECT_CALL(*mock_pre_xmpp_auth_, GetAuthCookie()).WillOnce(Return("")); |
| 127 | 129 |
| 128 EXPECT_CALL(mock_xmpp_connection_delegate_, | 130 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 129 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 131 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 130 | 132 |
| 131 XmppConnection xmpp_connection( | 133 XmppConnection xmpp_connection( |
| 132 buzz::XmppClientSettings(), &mock_xmpp_connection_delegate_, | 134 buzz::XmppClientSettings(), &cert_verifier_, |
| 133 mock_pre_xmpp_auth_.release()); | 135 &mock_xmpp_connection_delegate_, mock_pre_xmpp_auth_.release()); |
| 134 | 136 |
| 135 // We need to do this *before* |xmpp_connection| gets destroyed or | 137 // We need to do this *before* |xmpp_connection| gets destroyed or |
| 136 // our delegate won't be called. | 138 // our delegate won't be called. |
| 137 message_loop_.RunAllPending(); | 139 message_loop_.RunAllPending(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 TEST_F(XmppConnectionTest, RaisedError) { | 142 TEST_F(XmppConnectionTest, RaisedError) { |
| 141 EXPECT_CALL(mock_xmpp_connection_delegate_, | 143 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 142 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 144 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 143 | 145 |
| 144 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 146 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 145 &mock_xmpp_connection_delegate_, NULL); | 147 &mock_xmpp_connection_delegate_, NULL); |
| 146 | 148 |
| 147 xmpp_connection.weak_xmpp_client_-> | 149 xmpp_connection.weak_xmpp_client_-> |
| 148 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); | 150 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); |
| 149 } | 151 } |
| 150 | 152 |
| 151 TEST_F(XmppConnectionTest, Connect) { | 153 TEST_F(XmppConnectionTest, Connect) { |
| 152 base::WeakPtr<talk_base::Task> weak_ptr; | 154 base::WeakPtr<talk_base::Task> weak_ptr; |
| 153 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 155 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 154 WillOnce(SaveArg<0>(&weak_ptr)); | 156 WillOnce(SaveArg<0>(&weak_ptr)); |
| 155 | 157 |
| 156 { | 158 { |
| 157 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 159 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 158 &mock_xmpp_connection_delegate_, NULL); | 160 &mock_xmpp_connection_delegate_, NULL); |
| 159 | 161 |
| 160 xmpp_connection.weak_xmpp_client_-> | 162 xmpp_connection.weak_xmpp_client_-> |
| 161 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 163 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 162 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 164 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 163 } | 165 } |
| 164 | 166 |
| 165 EXPECT_EQ(NULL, weak_ptr.get()); | 167 EXPECT_EQ(NULL, weak_ptr.get()); |
| 166 } | 168 } |
| 167 | 169 |
| 168 TEST_F(XmppConnectionTest, MultipleConnect) { | 170 TEST_F(XmppConnectionTest, MultipleConnect) { |
| 169 EXPECT_DEBUG_DEATH({ | 171 EXPECT_DEBUG_DEATH({ |
| 170 base::WeakPtr<talk_base::Task> weak_ptr; | 172 base::WeakPtr<talk_base::Task> weak_ptr; |
| 171 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 173 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 172 WillOnce(SaveArg<0>(&weak_ptr)); | 174 WillOnce(SaveArg<0>(&weak_ptr)); |
| 173 | 175 |
| 174 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 176 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 175 &mock_xmpp_connection_delegate_, NULL); | 177 &mock_xmpp_connection_delegate_, NULL); |
| 176 | 178 |
| 177 xmpp_connection.weak_xmpp_client_-> | 179 xmpp_connection.weak_xmpp_client_-> |
| 178 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 180 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 179 for (int i = 0; i < 3; ++i) { | 181 for (int i = 0; i < 3; ++i) { |
| 180 xmpp_connection.weak_xmpp_client_-> | 182 xmpp_connection.weak_xmpp_client_-> |
| 181 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 183 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 182 } | 184 } |
| 183 | 185 |
| 184 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 186 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 185 }, "more than once"); | 187 }, "more than once"); |
| 186 } | 188 } |
| 187 | 189 |
| 188 TEST_F(XmppConnectionTest, ConnectThenError) { | 190 TEST_F(XmppConnectionTest, ConnectThenError) { |
| 189 base::WeakPtr<talk_base::Task> weak_ptr; | 191 base::WeakPtr<talk_base::Task> weak_ptr; |
| 190 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). | 192 EXPECT_CALL(mock_xmpp_connection_delegate_, OnConnect(_)). |
| 191 WillOnce(SaveArg<0>(&weak_ptr)); | 193 WillOnce(SaveArg<0>(&weak_ptr)); |
| 192 EXPECT_CALL(mock_xmpp_connection_delegate_, | 194 EXPECT_CALL(mock_xmpp_connection_delegate_, |
| 193 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); | 195 OnError(buzz::XmppEngine::ERROR_NONE, 0, NULL)); |
| 194 | 196 |
| 195 XmppConnection xmpp_connection(buzz::XmppClientSettings(), | 197 XmppConnection xmpp_connection(buzz::XmppClientSettings(), &cert_verifier_, |
| 196 &mock_xmpp_connection_delegate_, NULL); | 198 &mock_xmpp_connection_delegate_, NULL); |
| 197 | 199 |
| 198 xmpp_connection.weak_xmpp_client_-> | 200 xmpp_connection.weak_xmpp_client_-> |
| 199 SignalStateChange(buzz::XmppEngine::STATE_OPEN); | 201 SignalStateChange(buzz::XmppEngine::STATE_OPEN); |
| 200 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); | 202 EXPECT_EQ(xmpp_connection.weak_xmpp_client_.get(), weak_ptr.get()); |
| 201 | 203 |
| 202 xmpp_connection.weak_xmpp_client_-> | 204 xmpp_connection.weak_xmpp_client_-> |
| 203 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); | 205 SignalStateChange(buzz::XmppEngine::STATE_CLOSED); |
| 204 EXPECT_EQ(NULL, weak_ptr.get()); | 206 EXPECT_EQ(NULL, weak_ptr.get()); |
| 205 } | 207 } |
| 206 | 208 |
| 207 } // namespace notifier | 209 } // namespace notifier |
| OLD | NEW |