| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/client/ios/bridge/client_instance.h" | |
| 6 | |
| 7 #import "remoting/client/ios/host_preferences.h" | |
| 8 #import "testing/gtest_mac.h" | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/mac/scoped_nsobject.h" | |
| 12 #include "base/run_loop.h" | |
| 13 #include "base/strings/sys_string_conversions.h" | |
| 14 #include "base/synchronization/waitable_event.h" | |
| 15 #include "remoting/base/constants.h" | |
| 16 #include "remoting/client/ios/bridge/client_proxy.h" | |
| 17 #include "remoting/client/ios/bridge/client_proxy_delegate_wrapper.h" | |
| 18 #include "remoting/proto/event.pb.h" | |
| 19 #include "remoting/protocol/clipboard_stub.h" | |
| 20 | |
| 21 @interface ClientProxyDelegateForClientInstanceTester | |
| 22 : NSObject<ClientProxyDelegate> | |
| 23 | |
| 24 - (void)resetDidReceiveSomething; | |
| 25 | |
| 26 // Validating what was received is outside of the scope for this test unit. See | |
| 27 // ClientProxyUnittest for those tests. | |
| 28 @property(nonatomic, assign) BOOL didReceiveSomething; | |
| 29 | |
| 30 @end | |
| 31 | |
| 32 @implementation ClientProxyDelegateForClientInstanceTester | |
| 33 | |
| 34 @synthesize didReceiveSomething = _didReceiveSomething; | |
| 35 | |
| 36 - (void)resetDidReceiveSomething { | |
| 37 _didReceiveSomething = false; | |
| 38 } | |
| 39 | |
| 40 - (void)requestHostPin:(BOOL)pairingSupported { | |
| 41 _didReceiveSomething = true; | |
| 42 } | |
| 43 | |
| 44 - (void)connected { | |
| 45 _didReceiveSomething = true; | |
| 46 } | |
| 47 | |
| 48 - (void)connectionStatus:(NSString*)statusMessage { | |
| 49 _didReceiveSomething = true; | |
| 50 } | |
| 51 | |
| 52 - (void)connectionFailed:(NSString*)errorMessage { | |
| 53 _didReceiveSomething = true; | |
| 54 } | |
| 55 | |
| 56 - (void)applyFrame:(const webrtc::DesktopSize&)size | |
| 57 stride:(NSInteger)stride | |
| 58 data:(uint8_t*)data | |
| 59 rects:(const std::vector<webrtc::DesktopRect>&)rects { | |
| 60 _didReceiveSomething = true; | |
| 61 } | |
| 62 | |
| 63 - (void)applyCursor:(const webrtc::DesktopSize&)size | |
| 64 hotspot:(const webrtc::DesktopVector&)hotspot | |
| 65 cursorData:(uint8_t*)data { | |
| 66 _didReceiveSomething = true; | |
| 67 } | |
| 68 | |
| 69 @end | |
| 70 | |
| 71 namespace remoting { | |
| 72 | |
| 73 namespace { | |
| 74 | |
| 75 NSString* const kHostId = @"HostIdTest"; | |
| 76 NSString* const kPairingId = @"PairingIdTest"; | |
| 77 NSString* const kPairingSecret = @"PairingSecretTest"; | |
| 78 NSString* const kSecretPin = @"SecretPinTest"; | |
| 79 NSString* const kDeviceId = @"TestingDevice"; | |
| 80 | |
| 81 // TODO(aboone) should be able to call RunLoop().RunUntilIdle() instead but | |
| 82 // MessagePumpUIApplication::DoRun is marked NOTREACHED() | |
| 83 void RunCFMessageLoop() { | |
| 84 int result; | |
| 85 do { // Repeat until no messages remain | |
| 86 result = CFRunLoopRunInMode( | |
| 87 kCFRunLoopDefaultMode, | |
| 88 0, // Execute queued messages, do not wait for additional messages | |
| 89 YES); // Do only one message at a time | |
| 90 } while (result != kCFRunLoopRunStopped && result != kCFRunLoopRunFinished && | |
| 91 result != kCFRunLoopRunTimedOut); | |
| 92 } | |
| 93 | |
| 94 void SecretPinCallBack(const std::string& secret) { | |
| 95 ASSERT_STREQ(base::SysNSStringToUTF8(kSecretPin).c_str(), secret.c_str()); | |
| 96 } | |
| 97 | |
| 98 } // namespace | |
| 99 | |
| 100 class ClientInstanceTest : public ::testing::Test { | |
| 101 protected: | |
| 102 void SetUp() override { | |
| 103 testDelegate_.reset( | |
| 104 [[ClientProxyDelegateForClientInstanceTester alloc] init]); | |
| 105 proxy_.reset(new ClientProxy( | |
| 106 [ClientProxyDelegateWrapper wrapDelegate:testDelegate_])); | |
| 107 instance_ = new ClientInstance(proxy_->AsWeakPtr(), "", "", "", "", ""); | |
| 108 } | |
| 109 | |
| 110 void TearDown() override { | |
| 111 // Ensure memory is not leaking | |
| 112 // Notice Cleanup is safe to call, regardless of if Start() was ever called. | |
| 113 instance_->Cleanup(); | |
| 114 RunCFMessageLoop(); | |
| 115 // An object on the network thread which owns a reference to |instance_| may | |
| 116 // be cleaned up 'soon', but not immediately. Lets wait it out, up to 1 | |
| 117 // second. | |
| 118 for (int i = 0; i < 100; i++) { | |
| 119 if (!instance_->HasOneRef()) { | |
| 120 [NSThread sleepForTimeInterval:.01]; | |
| 121 } else { | |
| 122 break; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // Remove the last reference from |instance_|, and destructor is called. | |
| 127 ASSERT_TRUE(instance_->HasOneRef()); | |
| 128 instance_ = NULL; | |
| 129 } | |
| 130 | |
| 131 void AssertAcknowledged(BOOL wasAcknowledged) { | |
| 132 ASSERT_EQ(wasAcknowledged, [testDelegate_ didReceiveSomething]); | |
| 133 // Reset for the next test | |
| 134 [testDelegate_ resetDidReceiveSomething]; | |
| 135 } | |
| 136 | |
| 137 void TestStatusAndError(protocol::ConnectionToHost::State state, | |
| 138 protocol::ErrorCode error) { | |
| 139 instance_->OnConnectionState(state, error); | |
| 140 AssertAcknowledged(true); | |
| 141 } | |
| 142 | |
| 143 void TestConnectionStatus(protocol::ConnectionToHost::State state) { | |
| 144 TestStatusAndError(state, protocol::ErrorCode::OK); | |
| 145 TestStatusAndError(state, protocol::ErrorCode::PEER_IS_OFFLINE); | |
| 146 TestStatusAndError(state, protocol::ErrorCode::SESSION_REJECTED); | |
| 147 TestStatusAndError(state, protocol::ErrorCode::INCOMPATIBLE_PROTOCOL); | |
| 148 TestStatusAndError(state, protocol::ErrorCode::AUTHENTICATION_FAILED); | |
| 149 TestStatusAndError(state, protocol::ErrorCode::CHANNEL_CONNECTION_ERROR); | |
| 150 TestStatusAndError(state, protocol::ErrorCode::SIGNALING_ERROR); | |
| 151 TestStatusAndError(state, protocol::ErrorCode::SIGNALING_TIMEOUT); | |
| 152 TestStatusAndError(state, protocol::ErrorCode::HOST_OVERLOAD); | |
| 153 TestStatusAndError(state, protocol::ErrorCode::UNKNOWN_ERROR); | |
| 154 } | |
| 155 | |
| 156 base::scoped_nsobject<ClientProxyDelegateForClientInstanceTester> | |
| 157 testDelegate_; | |
| 158 std::unique_ptr<ClientProxy> proxy_; | |
| 159 scoped_refptr<ClientInstance> instance_; | |
| 160 }; | |
| 161 | |
| 162 TEST_F(ClientInstanceTest, Create) { | |
| 163 // This is a test for memory leaking. Ensure a completely unused instance of | |
| 164 // ClientInstance is destructed. | |
| 165 | |
| 166 ASSERT_TRUE(instance_.get() != NULL); | |
| 167 ASSERT_TRUE(instance_->HasOneRef()); | |
| 168 } | |
| 169 | |
| 170 TEST_F(ClientInstanceTest, CreateAndStart) { | |
| 171 // This is a test for memory leaking. Ensure a properly used instance of | |
| 172 // ClientInstance is destructed. | |
| 173 | |
| 174 ASSERT_TRUE(instance_.get() != NULL); | |
| 175 ASSERT_TRUE(instance_->HasOneRef()); | |
| 176 | |
| 177 instance_->Start("", ""); | |
| 178 RunCFMessageLoop(); | |
| 179 ASSERT_TRUE(!instance_->HasOneRef()); // more than one | |
| 180 } | |
| 181 | |
| 182 TEST_F(ClientInstanceTest, SecretPin) { | |
| 183 NSString* hostId = kHostId; | |
| 184 NSString* pairingId = kPairingId; | |
| 185 NSString* pairingSecret = kPairingSecret; | |
| 186 | |
| 187 HostPreferences* newHost = [HostPreferences hostForId:hostId]; | |
| 188 newHost.pairId = pairingId; | |
| 189 newHost.pairSecret = pairingSecret; | |
| 190 [newHost saveToKeychain]; | |
| 191 | |
| 192 // Suggesting that our pairing Id is known, but since it is not the correct | |
| 193 // credentials expect the stored value to be discarded before requesting the | |
| 194 // PIN. | |
| 195 instance_ = new ClientInstance(proxy_->AsWeakPtr(), "", "", "", | |
| 196 base::SysNSStringToUTF8(kHostId), ""); | |
| 197 | |
| 198 instance_->Start(base::SysNSStringToUTF8(kPairingId), | |
| 199 base::SysNSStringToUTF8(kPairingSecret)); | |
| 200 RunCFMessageLoop(); | |
| 201 | |
| 202 instance_->FetchSecret(false, base::Bind(&SecretPinCallBack)); | |
| 203 RunCFMessageLoop(); | |
| 204 AssertAcknowledged(true); | |
| 205 | |
| 206 HostPreferences* host = [HostPreferences hostForId:hostId]; | |
| 207 | |
| 208 // The pairing information was discarded. | |
| 209 ASSERT_TRUE([host.pairId isEqualToString:@""]); | |
| 210 ASSERT_TRUE([host.pairSecret isEqualToString:@""]); | |
| 211 | |
| 212 instance_->ProvideSecret(base::SysNSStringToUTF8(kSecretPin), false, | |
| 213 base::SysNSStringToUTF8(kDeviceId)); | |
| 214 RunCFMessageLoop(); | |
| 215 } | |
| 216 | |
| 217 TEST_F(ClientInstanceTest, NoProxy) { | |
| 218 // After the proxy is released, we still expect quite a few functions to be | |
| 219 // able to run, but not produce any output. Some of these are just being | |
| 220 // executed for code coverage, the outputs are not pertinent to this test | |
| 221 // unit. | |
| 222 proxy_.reset(); | |
| 223 | |
| 224 instance_->Start("", ""); | |
| 225 RunCFMessageLoop(); | |
| 226 | |
| 227 instance_->FetchSecret(false, base::Bind(&SecretPinCallBack)); | |
| 228 AssertAcknowledged(false); | |
| 229 | |
| 230 instance_->ProvideSecret(base::SysNSStringToUTF8(kSecretPin), false, | |
| 231 base::SysNSStringToUTF8(kDeviceId)); | |
| 232 AssertAcknowledged(false); | |
| 233 | |
| 234 instance_->PerformMouseAction(webrtc::DesktopVector(0, 0), | |
| 235 webrtc::DesktopVector(0, 0), | |
| 236 (protocol::MouseEvent_MouseButton)0, false); | |
| 237 AssertAcknowledged(false); | |
| 238 | |
| 239 instance_->PerformKeyboardAction(0, false); | |
| 240 AssertAcknowledged(false); | |
| 241 | |
| 242 instance_->OnConnectionState(protocol::ConnectionToHost::State::CONNECTED, | |
| 243 protocol::ErrorCode::OK); | |
| 244 AssertAcknowledged(false); | |
| 245 | |
| 246 instance_->OnConnectionReady(false); | |
| 247 AssertAcknowledged(false); | |
| 248 | |
| 249 instance_->OnRouteChanged("", protocol::TransportRoute()); | |
| 250 AssertAcknowledged(false); | |
| 251 | |
| 252 // SetCapabilities requires a host connection to be established | |
| 253 // instance_->SetCapabilities(""); | |
| 254 // AssertAcknowledged(false); | |
| 255 | |
| 256 instance_->SetPairingResponse(protocol::PairingResponse()); | |
| 257 AssertAcknowledged(false); | |
| 258 | |
| 259 instance_->DeliverHostMessage(protocol::ExtensionMessage()); | |
| 260 AssertAcknowledged(false); | |
| 261 | |
| 262 ASSERT_TRUE(instance_->GetClipboardStub() != NULL); | |
| 263 ASSERT_TRUE(instance_->GetCursorShapeStub() != NULL); | |
| 264 | |
| 265 protocol::ClipboardEvent event; | |
| 266 event.set_mime_type(kMimeTypeTextUtf8); | |
| 267 event.set_data("Test data."); | |
| 268 instance_->InjectClipboardEvent(event); | |
| 269 AssertAcknowledged(false); | |
| 270 | |
| 271 instance_->SetCursorShape(protocol::CursorShapeInfo()); | |
| 272 AssertAcknowledged(false); | |
| 273 } | |
| 274 | |
| 275 TEST_F(ClientInstanceTest, OnConnectionStateINITIALIZING) { | |
| 276 TestConnectionStatus(protocol::ConnectionToHost::State::INITIALIZING); | |
| 277 } | |
| 278 | |
| 279 TEST_F(ClientInstanceTest, OnConnectionStateCONNECTING) { | |
| 280 TestConnectionStatus(protocol::ConnectionToHost::State::CONNECTING); | |
| 281 } | |
| 282 | |
| 283 TEST_F(ClientInstanceTest, OnConnectionStateAUTHENTICATED) { | |
| 284 TestConnectionStatus(protocol::ConnectionToHost::State::AUTHENTICATED); | |
| 285 } | |
| 286 | |
| 287 TEST_F(ClientInstanceTest, OnConnectionStateCONNECTED) { | |
| 288 TestConnectionStatus(protocol::ConnectionToHost::State::CONNECTED); | |
| 289 } | |
| 290 | |
| 291 TEST_F(ClientInstanceTest, OnConnectionStateFAILED) { | |
| 292 TestConnectionStatus(protocol::ConnectionToHost::State::FAILED); | |
| 293 } | |
| 294 | |
| 295 TEST_F(ClientInstanceTest, OnConnectionStateCLOSED) { | |
| 296 TestConnectionStatus(protocol::ConnectionToHost::State::CLOSED); | |
| 297 } | |
| 298 | |
| 299 TEST_F(ClientInstanceTest, OnConnectionReady) { | |
| 300 instance_->OnConnectionReady(true); | |
| 301 AssertAcknowledged(false); | |
| 302 instance_->OnConnectionReady(false); | |
| 303 AssertAcknowledged(false); | |
| 304 } | |
| 305 | |
| 306 TEST_F(ClientInstanceTest, OnRouteChanged) { | |
| 307 // Not expecting anything to happen | |
| 308 protocol::TransportRoute route; | |
| 309 | |
| 310 route.type = protocol::TransportRoute::DIRECT; | |
| 311 instance_->OnRouteChanged("", route); | |
| 312 AssertAcknowledged(false); | |
| 313 | |
| 314 route.type = protocol::TransportRoute::STUN; | |
| 315 instance_->OnRouteChanged("", route); | |
| 316 AssertAcknowledged(false); | |
| 317 | |
| 318 route.type = protocol::TransportRoute::RELAY; | |
| 319 instance_->OnRouteChanged("", route); | |
| 320 AssertAcknowledged(false); | |
| 321 } | |
| 322 | |
| 323 TEST_F(ClientInstanceTest, SetCursorShape) { | |
| 324 instance_->SetCursorShape(protocol::CursorShapeInfo()); | |
| 325 AssertAcknowledged(true); | |
| 326 } | |
| 327 | |
| 328 } // namespace remoting | |
| OLD | NEW |