Example #1
0
        private async Task <IRemoteDesktopSession> Connect(
            RdpTunnel tunnel,
            InstanceLocator vmInstanceReference)
        {
            using (var gceAdapter = new ComputeEngineAdapter(this.serviceProvider.GetService <IAuthorizationAdapter>()))
            {
                var credentials = await gceAdapter.ResetWindowsUserAsync(
                    vmInstanceReference,
                    CreateRandomUsername(),
                    TimeSpan.FromSeconds(60),
                    CancellationToken.None);

                var settings = VmInstanceConnectionSettings.CreateNew(vmInstanceReference);
                settings.Username.Value            = credentials.UserName;
                settings.Password.Value            = credentials.SecurePassword;
                settings.AuthenticationLevel.Value = RdpAuthenticationLevel.NoServerAuthentication;
                settings.BitmapPersistence.Value   = RdpBitmapPersistence.Disabled;
                settings.DesktopSize.Value         = RdpDesktopSize.ClientSize;

                var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);
                return(rdpService.Connect(
                           vmInstanceReference,
                           "localhost",
                           (ushort)tunnel.LocalPort,
                           settings));
            }
        }
        public void WhenNoSuggestedUserNameProvided_ThenSuggestionIsDerivedFromSigninName()
        {
            var serviceRegistry = new ServiceRegistry();

            var auth = new Mock <IAuthorization>();

            auth.SetupGet(a => a.Email).Returns("*****@*****.**");

            serviceRegistry.AddMock <IAuthorizationAdapter>()
            .SetupGet(a => a.Authorization).Returns(auth.Object);

            var credDialog = serviceRegistry.AddMock <IGenerateCredentialsDialog>();

            credDialog
            .Setup(d => d.PromptForUsername(
                       It.IsAny <IWin32Window>(),
                       It.IsAny <string>()))
            .Returns <string>(null);    // Cancel dialog


            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            var credentialsService = new CredentialsService(serviceRegistry);

            AssertEx.ThrowsAggregateException <TaskCanceledException>(
                () => credentialsService.GenerateCredentialsAsync(
                    null,
                    SampleInstance,
                    settings,
                    false).Wait());

            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.Is <string>(u => u == "bobsemail")), Times.Once);
        }
        public async Task WhenCredentialsInvalid_ThenErrorIsShownAndWindowIsClosed(
            [WindowsInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var tunnel = RdpTunnel.Create(
                       locator,
                       await credential))
            {
                var settings = VmInstanceConnectionSettings.CreateNew(
                    locator.ProjectId,
                    locator.Name);
                settings.Username.StringValue                 = "wrong";
                settings.Password.Value                       = SecureStringExtensions.FromClearText("wrong");
                settings.AuthenticationLevel.EnumValue        = RdpAuthenticationLevel.NoServerAuthentication;
                settings.UserAuthenticationBehavior.EnumValue = RdpUserAuthenticationBehavior.AbortOnFailure;
                settings.DesktopSize.EnumValue                = RdpDesktopSize.ClientSize;

                var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);
                var session    = rdpService.Connect(
                    locator,
                    "localhost",
                    (ushort)tunnel.LocalPort,
                    settings);

                AwaitEvent <ConnectionFailedEvent>();
                Assert.IsNotNull(this.ExceptionShown);
                Assert.IsInstanceOf(typeof(RdpDisconnectedException), this.ExceptionShown);
                Assert.AreEqual(2055, ((RdpDisconnectedException)this.ExceptionShown).DisconnectReason);
            }
        }
        public void WhenParseStringCreatedByToString_ResultIsSame()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("project-1", "instance-1");

            settings.Username.Value                     = "user";
            settings.Domain.Value                       = "domain";
            settings.ConnectionBar.Value                = RdpConnectionBarState.Off;
            settings.DesktopSize.Value                  = RdpDesktopSize.ScreenSize;
            settings.AuthenticationLevel.Value          = RdpAuthenticationLevel.RequireServerAuthentication;
            settings.ColorDepth.Value                   = RdpColorDepth.TrueColor;
            settings.AudioMode.Value                    = RdpAudioMode.PlayOnServer;
            settings.RedirectClipboard.Value            = RdpRedirectClipboard.Disabled;
            settings.CredentialGenerationBehavior.Value = RdpCredentialGenerationBehavior.Disallow;

            var url = new IapRdpUrl(
                new InstanceLocator("project-1", "us-central1-a", "instance-1"),
                settings.ToUrlQuery());

            var copy = VmInstanceConnectionSettings.FromUrl(url);

            Assert.AreEqual("user", copy.Username.Value);
            Assert.AreEqual("domain", copy.Domain.Value);
            Assert.AreEqual(RdpConnectionBarState.Off, copy.ConnectionBar.Value);
            Assert.AreEqual(RdpDesktopSize.ScreenSize, copy.DesktopSize.Value);
            Assert.AreEqual(RdpAuthenticationLevel.RequireServerAuthentication, copy.AuthenticationLevel.Value);
            Assert.AreEqual(RdpColorDepth.TrueColor, copy.ColorDepth.Value);
            Assert.AreEqual(RdpAudioMode.PlayOnServer, copy.AudioMode.Value);
            Assert.AreEqual(RdpRedirectClipboard.Disabled, copy.RedirectClipboard.Value);
            Assert.AreEqual(RdpCredentialGenerationBehavior.Disallow, copy.CredentialGenerationBehavior.Value);
        }
        public void WhenSuggestedUserNameProvided_ThenSuggestionIsUsed()
        {
            var serviceRegistry = new ServiceRegistry();
            var credDialog      = serviceRegistry.AddMock <IGenerateCredentialsDialog>();

            credDialog
            .Setup(d => d.PromptForUsername(
                       It.IsAny <IWin32Window>(),
                       It.IsAny <string>()))
            .Returns <string>(null);    // Cancel dialog

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.Username.Value = "alice";

            var credentialsService = new CredentialsService(serviceRegistry);

            AssertEx.ThrowsAggregateException <TaskCanceledException>(
                () => credentialsService.GenerateCredentialsAsync(
                    null,
                    SampleInstance,
                    settings,
                    false).Wait());

            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.Is <string>(u => u == "alice")), Times.Once);
        }
Example #6
0
        public async Task WhenBehaviorSetToForceAndPermissionsGranted_ThenDialogIsSkippedAndCredentialsAreGenerated()
        {
            var taskDialog = new Mock <ITaskDialog>();

            var credentialPrompt = CreateCredentialsPrompt(true, true, taskDialog);

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.CredentialGenerationBehavior.EnumValue = RdpCredentialGenerationBehavior.Force;

            await credentialPrompt.ShowCredentialsPromptAsync(
                null,
                SampleInstance,
                settings,
                true);

            Assert.AreEqual("bob", settings.Username.StringValue);
            Assert.AreEqual("secret", settings.Password.ClearTextValue);
            Assert.IsNull(settings.Domain.Value);

            // No dialog shown.
            taskDialog.Verify(t => t.ShowOptionsTaskDialog(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <IntPtr>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <IList <string> >(),
                                  It.IsAny <string>(),
                                  out It.Ref <bool> .IsAny), Times.Never);
        }
        public async Task WhenNoSuggestedUserNameProvidedAndSilentIsTrue_ThenSuggestionIsDerivedFromSigninNameWithoutPrompting()
        {
            var serviceRegistry = new ServiceRegistry();

            var auth = new Mock <IAuthorization>();

            auth.SetupGet(a => a.Email).Returns("*****@*****.**");
            serviceRegistry.AddMock <IAuthorizationAdapter>()
            .SetupGet(a => a.Authorization).Returns(auth.Object);

            serviceRegistry.AddSingleton <IJobService, SynchronousJobService>();
            serviceRegistry.AddMock <IComputeEngineAdapter>()
            .Setup(a => a.ResetWindowsUserAsync(
                       It.IsAny <InstanceLocator>(),
                       It.Is <string>(user => user == "bobsemail"),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(new NetworkCredential("bobsemail", "password"));

            var credDialog = serviceRegistry.AddMock <IGenerateCredentialsDialog>();
            var settings   = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            var credentialsService = new CredentialsService(serviceRegistry);
            await credentialsService.GenerateCredentialsAsync(
                null,
                SampleInstance,
                settings,
                true);

            Assert.AreEqual("bobsemail", settings.Username.Value);
            Assert.AreEqual("password", settings.Password.ClearTextValue);
            credDialog.Verify(d => d.PromptForUsername(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <string>()), Times.Never);
        }
        public async Task WhenConnected_ThenActiveSessionIsSetAndTryActivateReturnsTrue(
            [WindowsInstance(MachineType = MachineTypeForRdp)] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var tunnel = RdpTunnel.Create(
                       locator,
                       await credential))
                using (var gceAdapter = new ComputeEngineAdapter(this.serviceProvider.GetService <IAuthorizationAdapter>()))
                {
                    var credentials = await gceAdapter.ResetWindowsUserAsync(
                        locator,
                        CreateRandomUsername(),
                        TimeSpan.FromSeconds(60),
                        CancellationToken.None);

                    var settings = VmInstanceConnectionSettings.CreateNew(
                        locator.ProjectId,
                        locator.Name);
                    settings.Username.StringValue = credentials.UserName;
                    settings.Password.Value       = credentials.SecurePassword;

                    var broker  = new RemoteDesktopConnectionBroker(this.serviceProvider);
                    var session = broker.Connect(
                        locator,
                        "localhost",
                        (ushort)tunnel.LocalPort,
                        settings);

                    AwaitEvent <ConnectionSuceededEvent>();
                    Assert.IsNull(this.ExceptionShown);

                    Assert.IsTrue(broker.IsConnected(locator));
                    Assert.AreSame(session, broker.ActiveSession);
                    Assert.IsTrue(broker.TryActivate(locator));

                    Assert.IsFalse(broker.TryActivate(SampleLocator));
                    Assert.IsFalse(broker.IsConnected(SampleLocator));

                    ConnectionClosedEvent expectedEvent = null;

                    this.serviceProvider.GetService <IEventService>()
                    .BindHandler <ConnectionClosedEvent>(e =>
                    {
                        expectedEvent = e;
                    });
                    session.Close();

                    Assert.IsNotNull(expectedEvent);
                }
        }
Example #9
0
        public async Task WhenConnectingByUrlWithUsernameAndCredentialsExist_ThenConnectionIsMadeWithUsernameFromUrl()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("project", "instance-1");

            settings.Username.Value = "existinguser";
            settings.Password.Value = SecureStringExtensions.FromClearText("password");

            var settingsService = this.serviceRegistry.AddMock <IConnectionSettingsService>();

            settingsService.Setup(s => s.GetConnectionSettings(
                                      It.IsAny <IProjectExplorerNode>()))
            .Returns(settings);

            var vmNode = new Mock <IProjectExplorerVmInstanceNode>();

            vmNode.SetupGet(n => n.Reference)
            .Returns(new InstanceLocator("project-1", "zone-1", "instance-1"));

            this.serviceRegistry.AddMock <ICredentialPrompt>()
            .Setup(p => p.ShowCredentialsPromptAsync(
                       It.IsAny <IWin32Window>(),
                       It.IsAny <InstanceLocator>(),
                       It.IsAny <ConnectionSettingsBase>(),
                       It.IsAny <bool>()));
            this.serviceRegistry.AddMock <IProjectExplorer>()
            .Setup(p => p.TryFindNode(
                       It.IsAny <InstanceLocator>()))
            .Returns(vmNode.Object);

            var remoteDesktopService = new Mock <IRemoteDesktopConnectionBroker>();

            remoteDesktopService.Setup(s => s.Connect(
                                           It.IsAny <InstanceLocator>(),
                                           "localhost",
                                           It.IsAny <ushort>(),
                                           It.IsAny <VmInstanceConnectionSettings>())).Returns <IRemoteDesktopSession>(null);

            this.serviceRegistry.AddSingleton <IRemoteDesktopConnectionBroker>(remoteDesktopService.Object);

            var service = new IapRdpConnectionService(this.serviceRegistry);
            await service.ActivateOrConnectInstanceAsync(
                IapRdpUrl.FromString("iap-rdp:///project/us-central-1/instance-1?username=john%20doe"));

            remoteDesktopService.Verify(s => s.Connect(
                                            It.IsAny <InstanceLocator>(),
                                            "localhost",
                                            It.IsAny <ushort>(),
                                            It.Is <VmInstanceConnectionSettings>(i => i.Username.StringValue == "john doe")), Times.Once);
            settingsService.Verify(s => s.GetConnectionSettings(
                                       It.IsAny <IProjectExplorerNode>()), Times.Once);
        }
Example #10
0
        public async Task WhenCredentialsFoundAndBehaviorSetToAllowIfNoCredentialsFound_ThenDialogIsSkipped(
            [Values(true, false)] bool isGrantedPermissionToGenerateCredentials)
        {
            var taskDialog = new Mock <ITaskDialog>();

            taskDialog.Setup(t => t.ShowOptionsTaskDialog(
                                 It.IsAny <IWin32Window>(),
                                 It.IsAny <IntPtr>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <IList <string> >(),
                                 It.IsAny <string>(),
                                 out It.Ref <bool> .IsAny)).Returns(0);

            var credentialPrompt = CreateCredentialsPrompt(
                isGrantedPermissionToGenerateCredentials,
                false,
                taskDialog);

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.CredentialGenerationBehavior.EnumValue = RdpCredentialGenerationBehavior.AllowIfNoCredentialsFound;
            settings.Username.StringValue    = "alice";
            settings.Password.ClearTextValue = "alicespassword";

            await credentialPrompt.ShowCredentialsPromptAsync(
                null,
                SampleInstance,
                settings,
                true);

            Assert.AreEqual("alice", settings.Username.Value);
            Assert.AreEqual("alicespassword", settings.Password.ClearTextValue);
            Assert.IsNull(settings.Domain.Value);

            taskDialog.Verify(t => t.ShowOptionsTaskDialog(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <IntPtr>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <IList <string> >(),
                                  It.IsAny <string>(),
                                  out It.Ref <bool> .IsAny), Times.Never);
        }
        public void WhenSettingsContainsEscapableChars_ThenToStringEscapesThem()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("project-1", "instance-1");

            settings.Username.Value = "Tom & Jerry?";
            settings.Domain.Value   = "\"?\"";

            var url = new IapRdpUrl(
                new InstanceLocator("project-1", "us-central1-a", "instance-1"),
                settings.ToUrlQuery());

            Assert.AreEqual(
                "iap-rdp:///project-1/us-central1-a/instance-1?" +
                "Username=Tom+%26+Jerry%3f&Domain=%22%3f%22",
                url.ToString());
        }
        public void WhenVmInstanceSettingsArePopulated_ThenToUrlQueryExcludesPassword()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("pro-1", "instance-1");

            settings.Username.Value              = "bob";
            settings.Password.ClearTextValue     = "secret";
            settings.RedirectClipboard.EnumValue = RdpRedirectClipboard.Disabled;
            settings.ConnectionTimeout.IntValue  = 123;

            var query = settings.ToUrlQuery();

            Assert.AreEqual(3, query.Count);
            Assert.AreEqual("bob", query["Username"]);
            Assert.AreEqual("0", query["RedirectClipboard"]);
            Assert.AreEqual("123", query["ConnectionTimeout"]);
        }
Example #13
0
        public void WhenServerInvalid_ThenErrorIsShownAndWindowIsClosed()
        {
            var settings = VmInstanceConnectionSettings.CreateNew(this.SampleLocator);

            var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);

            rdpService.Connect(
                this.SampleLocator,
                "invalid.corp",
                3389,
                settings);

            AwaitEvent <ConnectionFailedEvent>();
            Assert.IsInstanceOf(typeof(RdpDisconnectedException), this.ExceptionShown);
            Assert.AreEqual(260, ((RdpDisconnectedException)this.ExceptionShown).DisconnectReason);
        }
Example #14
0
        public void WhenWrongPort_ThenErrorIsShownAndWindowIsClosed()
        {
            var settings = VmInstanceConnectionSettings.CreateNew(this.SampleLocator);

            var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);

            rdpService.Connect(
                this.SampleLocator,
                "localhost",
                135,    // That one will be listening, but it is RPC, not RDP.
                settings);

            AwaitEvent <ConnectionFailedEvent>();
            Assert.IsInstanceOf(typeof(RdpDisconnectedException), this.ExceptionShown);
            Assert.AreEqual(2308, ((RdpDisconnectedException)this.ExceptionShown).DisconnectReason);
        }
        public async Task WhenConnectingByNodeAndPersistentCredentialsDisallowed_ThenPasswordIsClear()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("project", "instance-1");

            settings.Username.Value = "existinguser";
            settings.Password.Value = SecureStringExtensions.FromClearText("password");

            var settingsService = this.serviceRegistry.AddMock <IConnectionSettingsService>();

            settingsService.Setup(s => s.GetConnectionSettings(
                                      It.IsAny <IProjectExplorerNode>()))
            .Returns(settings);

            var vmNode = new Mock <IProjectExplorerVmInstanceNode>();

            vmNode.SetupGet(n => n.Reference)
            .Returns(new InstanceLocator("project-1", "zone-1", "instance-1"));

            this.serviceRegistry.AddMock <IProjectExplorer>()
            .Setup(p => p.TryFindNode(
                       It.IsAny <InstanceLocator>()))
            .Returns(vmNode.Object);

            var remoteDesktopService = new Mock <IRemoteDesktopSessionBroker>();

            remoteDesktopService.Setup(s => s.Connect(
                                           It.IsAny <InstanceLocator>(),
                                           "localhost",
                                           It.IsAny <ushort>(),
                                           It.IsAny <VmInstanceConnectionSettings>())).Returns <IRemoteDesktopSession>(null);

            this.serviceRegistry.AddSingleton <IRemoteDesktopSessionBroker>(remoteDesktopService.Object);

            var service = new IapRdpConnectionService(this.serviceRegistry);
            await service.ActivateOrConnectInstanceAsync(vmNode.Object, false);

            remoteDesktopService.Verify(s => s.Connect(
                                            It.IsAny <InstanceLocator>(),
                                            "localhost",
                                            It.IsAny <ushort>(),
                                            It.Is <VmInstanceConnectionSettings>(i =>
                                                                                 i.Username.StringValue == "existinguser" &&
                                                                                 i.Password.ClearTextValue == "")), Times.Once);

            settingsService.Verify(s => s.SaveConnectionSettings(
                                       It.IsAny <ConnectionSettingsBase>()), Times.Never);
        }
        public async Task WhenSigningOutPerSendKeys_ThenWindowIsClosed(
            [WindowsInstance(ImageFamily = WindowsInstanceAttribute.WindowsServer2019)]
            ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var tunnel = RdpTunnel.Create(
                       locator,
                       await credential))
                using (var gceAdapter = new ComputeEngineAdapter(this.serviceProvider.GetService <IAuthorizationAdapter>()))
                {
                    var credentials = await gceAdapter.ResetWindowsUserAsync(
                        locator,
                        CreateRandomUsername(),
                        TimeSpan.FromSeconds(60),
                        CancellationToken.None);

                    var settings = VmInstanceConnectionSettings.CreateNew(
                        locator.ProjectId,
                        locator.Name);
                    settings.Username.StringValue          = credentials.UserName;
                    settings.Password.Value                = credentials.SecurePassword;
                    settings.AuthenticationLevel.EnumValue = RdpAuthenticationLevel.NoServerAuthentication;
                    settings.BitmapPersistence.EnumValue   = RdpBitmapPersistence.Disabled;
                    settings.DesktopSize.EnumValue         = RdpDesktopSize.ClientSize;

                    var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);
                    var session    = (RemoteDesktopPane)rdpService.Connect(
                        locator,
                        "localhost",
                        (ushort)tunnel.LocalPort,
                        settings);

                    AwaitEvent <ConnectionSuceededEvent>();

                    Thread.Sleep(5000);
                    session.ShowSecurityScreen();
                    Thread.Sleep(1000);
                    session.SendKeys(Keys.Menu, Keys.S); // Sign out.

                    AwaitEvent <ConnectionClosedEvent>();
                    Assert.IsNull(this.ExceptionShown);
                }
        }
Example #17
0
        public void WhenPortNotListening_ThenErrorIsShownAndWindowIsClosed()
        {
            var settings = VmInstanceConnectionSettings.CreateNew(this.SampleLocator);

            settings.ConnectionTimeout.IntValue = 5;

            var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);

            rdpService.Connect(
                this.SampleLocator,
                "localhost",
                1,
                settings);

            AwaitEvent <ConnectionFailedEvent>();
            Assert.IsInstanceOf(typeof(RdpDisconnectedException), this.ExceptionShown);
            Assert.AreEqual(516, ((RdpDisconnectedException)this.ExceptionShown).DisconnectReason);
        }
Example #18
0
        public async Task WhenNoCredentialsFoundAndPermissionGrantedAndBehaviorSetToAllowIfNoCredentialsFound_ThenGenerateOptionIsShown()
        {
            var taskDialog = new Mock <ITaskDialog>();

            taskDialog.Setup(t => t.ShowOptionsTaskDialog(
                                 It.IsAny <IWin32Window>(),
                                 It.IsAny <IntPtr>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <IList <string> >(),
                                 It.IsAny <string>(),
                                 out It.Ref <bool> .IsAny)).Returns(0);

            var credentialPrompt = CreateCredentialsPrompt(true, false, taskDialog);

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.CredentialGenerationBehavior.EnumValue = RdpCredentialGenerationBehavior.AllowIfNoCredentialsFound;

            await credentialPrompt.ShowCredentialsPromptAsync(
                null,
                SampleInstance,
                settings,
                true);

            Assert.AreEqual("bob", settings.Username.Value);
            Assert.AreEqual("secret", settings.Password.ClearTextValue);
            Assert.IsNull(settings.Domain.Value);

            taskDialog.Verify(t => t.ShowOptionsTaskDialog(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <IntPtr>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.Is <IList <string> >(options => options.Count == 3),
                                  It.IsAny <string>(),
                                  out It.Ref <bool> .IsAny), Times.Once);
        }
Example #19
0
        public async Task WhenNoCredentialsFoundAndBehaviorSetToDisallowAndJumpToSettingsNotAllowed_ThenDialogIsSkipped()
        {
            var taskDialog = new Mock <ITaskDialog>();

            taskDialog.Setup(t => t.ShowOptionsTaskDialog(
                                 It.IsAny <IWin32Window>(),
                                 It.IsAny <IntPtr>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <IList <string> >(),
                                 It.IsAny <string>(),
                                 out It.Ref <bool> .IsAny)).Returns(1);

            var credentialPrompt = CreateCredentialsPrompt(true, false, taskDialog);

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.CredentialGenerationBehavior.EnumValue = RdpCredentialGenerationBehavior.Disallow;

            await credentialPrompt.ShowCredentialsPromptAsync(
                null,
                SampleInstance,
                settings,
                false);

            Assert.IsNull(settings.Username.Value);
            Assert.IsNull(settings.Password.Value);
            Assert.IsNull(settings.Domain.Value);

            taskDialog.Verify(t => t.ShowOptionsTaskDialog(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <IntPtr>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <IList <string> >(),
                                  It.IsAny <string>(),
                                  out It.Ref <bool> .IsAny), Times.Never);
        }
Example #20
0
        public void WhenBehaviorSetToForceAndPermissionsNotGranted_ThenJumpToSettingsOptionIsShown()
        {
            var taskDialog = new Mock <ITaskDialog>();

            taskDialog.Setup(t => t.ShowOptionsTaskDialog(
                                 It.IsAny <IWin32Window>(),
                                 It.IsAny <IntPtr>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <string>(),
                                 It.IsAny <IList <string> >(),
                                 It.IsAny <string>(),
                                 out It.Ref <bool> .IsAny)).Returns(0);

            var credentialPrompt = CreateCredentialsPrompt(false, false, taskDialog);
            var window           = this.serviceRegistry.AddMock <IConnectionSettingsWindow>();

            var settings = VmInstanceConnectionSettings.CreateNew(SampleInstance);

            settings.CredentialGenerationBehavior.EnumValue = RdpCredentialGenerationBehavior.Force;

            AssertEx.ThrowsAggregateException <TaskCanceledException>(
                () => credentialPrompt.ShowCredentialsPromptAsync(
                    null,
                    SampleInstance,
                    settings,
                    true).Wait());

            window.Verify(w => w.ShowWindow(), Times.Once);
            taskDialog.Verify(t => t.ShowOptionsTaskDialog(
                                  It.IsAny <IWin32Window>(),
                                  It.IsAny <IntPtr>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.IsAny <string>(),
                                  It.Is <IList <string> >(options => options.Count == 2),
                                  It.IsAny <string>(),
                                  out It.Ref <bool> .IsAny), Times.Once);
        }
        public void WhenVmInstanceSettingsAreAllDefaults_ThenToUrlQueryReturnsEmptyCollection()
        {
            var settings = VmInstanceConnectionSettings.CreateNew("pro-1", "instance-1");

            Assert.AreEqual(0, settings.ToUrlQuery().Count);
        }
        public async Task WhenCredentialsValid_ThenConnectingSucceeds(
            [Values(RdpConnectionBarState.AutoHide, RdpConnectionBarState.Off, RdpConnectionBarState.Pinned)]
            RdpConnectionBarState connectionBarState,

            [Values(RdpDesktopSize.ClientSize, RdpDesktopSize.ScreenSize)]
            RdpDesktopSize desktopSize,

            [Values(RdpAudioMode.DoNotPlay, RdpAudioMode.PlayLocally, RdpAudioMode.PlayOnServer)]
            RdpAudioMode audioMode,

            [Values(RdpRedirectClipboard.Disabled, RdpRedirectClipboard.Enabled)]
            RdpRedirectClipboard redirectClipboard,

            // Use a slightly larger machine type as all this RDP'ing consumes a fair
            // amount of memory.
            [WindowsInstance(MachineType = "n1-standard-2")] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.IapTunnelUser)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var tunnel = RdpTunnel.Create(
                       locator,
                       await credential))
                using (var gceAdapter = new ComputeEngineAdapter(this.serviceProvider.GetService <IAuthorizationAdapter>()))
                {
                    var credentials = await gceAdapter.ResetWindowsUserAsync(
                        locator,
                        CreateRandomUsername(),
                        TimeSpan.FromSeconds(60),
                        CancellationToken.None);

                    var settings = VmInstanceConnectionSettings.CreateNew(
                        locator.ProjectId,
                        locator.Name);
                    settings.Username.StringValue          = credentials.UserName;
                    settings.Password.Value                = credentials.SecurePassword;
                    settings.ConnectionBar.EnumValue       = connectionBarState;
                    settings.DesktopSize.EnumValue         = desktopSize;
                    settings.AudioMode.EnumValue           = audioMode;
                    settings.RedirectClipboard.EnumValue   = redirectClipboard;
                    settings.AuthenticationLevel.EnumValue = RdpAuthenticationLevel.NoServerAuthentication;
                    settings.BitmapPersistence.EnumValue   = RdpBitmapPersistence.Disabled;

                    var rdpService = new RemoteDesktopConnectionBroker(this.serviceProvider);
                    var session    = rdpService.Connect(
                        locator,
                        "localhost",
                        (ushort)tunnel.LocalPort,
                        settings);

                    AwaitEvent <ConnectionSuceededEvent>();
                    Assert.IsNull(this.ExceptionShown);


                    ConnectionClosedEvent expectedEvent = null;

                    this.serviceProvider.GetService <IEventService>()
                    .BindHandler <ConnectionClosedEvent>(e =>
                    {
                        expectedEvent = e;
                    });
                    session.Close();

                    Assert.IsNotNull(expectedEvent);
                }
        }