public async Task WhenNonWhitelistedEnvironmentVariablePassed_ThenOpenShellChannelAsyncThrowsRequestDenied(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate("testuser", key))
                        {
                            SshAssert.ThrowsNativeExceptionWithError(
                                session,
                                LIBSSH2_ERROR.CHANNEL_REQUEST_DENIED,
                                () => authSession.OpenShellChannel(
                                    LIBSSH2_CHANNEL_EXTENDED_DATA.MERGE,
                                    DefaultTerminal,
                                    80,
                                    24,
                                    new[]
                            {
                                new EnvironmentVariable("FOO", "foo", true),
                                new EnvironmentVariable("BAR", "bar", true)
                            }));
                        }
            }
        }
        public async Task WhenPublicKeyValidAndKnownFromMetadata_ThenAuthenticationSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                    {
                        var authSession = connection.Authenticate(
                            "testuser",
                            key,
                            this.UnexpectedAuthenticationCallback);
                        Assert.IsNotNull(authSession);
                    }
            }
        }
        public async Task When2faRequiredAndPromptThrowsException_ThenAuthenticationFailsWithoutRetry(
            [LinuxInstance(InitializeScript = RequireSshPassword)] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                    {
                        var callbackCount = 0;

                        Assert.Throws <OperationCanceledException>(
                            () => connection.Authenticate(
                                "testuser",
                                key,
                                (name, instruction, prompt, echo) =>
                        {
                            callbackCount++;
                            throw new OperationCanceledException();
                        }));
                        Assert.AreEqual(1, callbackCount);
                    }
            }
        }
Esempio n. 4
0
        public async Task WhenDisconnected_ThenOpenExecChannelAsyncThrowsSocketSend(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var instanceLocator = await instanceLocatorTask;
            var endpoint        = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    instanceLocator,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate("testuser", key))
                        {
                            connection.Dispose();
                            SshAssert.ThrowsNativeExceptionWithError(
                                session,
                                LIBSSH2_ERROR.SOCKET_SEND,
                                () => authSession.OpenExecChannel(
                                    "whoami",
                                    LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL));
                        }
            }
        }
Esempio n. 5
0
        public async Task WhenConnected_ThenOpenShellChannelAsyncChannelSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var instanceLocator = await instanceLocatorTask;
            var endpoint        = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    instanceLocator,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate("testuser", key))
                            using (var channel = authSession.OpenExecChannel(
                                       "whoami",
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL))
                            {
                                channel.Close();
                            }
            }
        }
Esempio n. 6
0
        public async Task WhenNoMoreDataToRead_ThenReadReturnZero(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate(
                                   "testuser",
                                   key,
                                   UnexpectedAuthenticationCallback))
                            using (var channel = authSession.OpenExecChannel(
                                       "whoami",
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL))
                            {
                                channel.WaitForEndOfStream();

                                Assert.AreNotEqual(0, channel.Read(new byte[1024]));
                                Assert.AreEqual(0, channel.Read(new byte[1024]));
                                channel.Close();
                            }
            }
        }
        public async Task WhenSessionDisconnected_ThenAuthenticateThrowsSocketSend(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                    {
                        connection.Dispose();

                        SshAssert.ThrowsNativeExceptionWithError(
                            session,
                            LIBSSH2_ERROR.SOCKET_SEND,
                            () => connection.Authenticate("testuser", key));
                    }
            }
        }
        public async Task WhenCommandIsValid_ThenOpenExecChannelAsyncSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate("testuser", key))
                            using (var channel = authSession.OpenExecChannel(
                                       "whoami",
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL))
                            {
                                channel.WaitForEndOfStream();

                                var buffer    = new byte[1024];
                                var bytesRead = channel.Read(buffer);
                                Assert.AreNotEqual(0, bytesRead);

                                Assert.AreEqual("testuser\n", Encoding.ASCII.GetString(buffer, 0, (int)bytesRead));

                                Assert.AreEqual(0, channel.ExitCode);
                                Assert.IsNull(channel.ExitSignal);
                                channel.Close();
                            }
            }
        }
Esempio n. 9
0
        public async Task WhenClosingSessionBeforeChannel_ThenDoubleFreeIsPrevented(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var instanceLocator = await instanceLocatorTask;
            var endpoint        = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    instanceLocator,
                    "testuser",
                    key).ConfigureAwait(true);

                var session     = CreateSession();
                var connection  = session.Connect(endpoint);
                var authSession = connection.Authenticate(
                    "testuser",
                    key,
                    UnexpectedAuthenticationCallback);
                var channel = authSession.OpenExecChannel(
                    "whoami",
                    LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL);

                session.Dispose();

                // Free channel after session - note that this causes an assertion
                // when debugging.
                channel.Dispose();
            }
        }
 private static void SaveInstanceIfDirty()
 {
     if (_instanceDirtied)
     {
         InstanceUtil.Save();
         _instanceDirtied = false;
     }
 }
Esempio n. 11
0
 public override void StaticObjectUpdate()
 {
     if (audioPlayer != null)
     {
         float scale = InstanceUtil.GetStaticInstanceForGameObject(gameObject).ModelScale;
         audioPlayer.minDistance = minDistance * scale;
         audioPlayer.maxDistance = maxDistance * scale;
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Spawns an Instance of an defined StaticModel
        /// </summary>
        /// <param name="model"></param>
        /// <param name="fOffset"></param>
        /// <param name="vPosition"></param>
        /// <param name="fAngle"></param>
        /// <returns></returns>
        public string SpawnInstance(StaticModel model, float fOffset, Vector3 vPosition)
        {
            StaticInstance instance = new StaticInstance();

            instance.Orientation     = Vector3.up;
            instance.heighReference  = HeightReference.Terrain;
            instance.VisibilityRange = (PhysicsGlobals.Instance.VesselRangesDefault.flying.unload + 3000);

            instance.Group          = "SaveGame";
            instance.RadialPosition = vPosition;

            instance.model = model;

            if (instance.model == null)
            {
                Log.UserError("LoadFromSave: Canot find model named: " + model.name);
                instance = null;
            }
            //instance.mesh = UnityEngine.Object.Instantiate(instance.model.prefab);


            instance.CelestialBody = KerbalKonstructs.instance.GetCurrentBody();

            instance.RadiusOffset  = fOffset;
            instance.RotationAngle = 0;

            instance.RefLatitude  = KKMath.GetLatitudeInDeg(instance.RadialPosition);
            instance.RefLongitude = KKMath.GetLongitudeInDeg(instance.RadialPosition);

            InstanceUtil.CreateGroupCenterIfMissing(instance);


            bool oldLegacySpawn = KerbalKonstructs.convertLegacyConfigs;

            instance.Orientate();
            instance.Activate();

            KerbalKonstructs.convertLegacyConfigs = oldLegacySpawn;

            KerbalKonstructs.SelectInstance(instance, true);

            selectedInstance = instance;
            startPosition    = selectedInstance.position;

            instance.Update();
            instance.isInSavegame = true;

            instance.HighlightObject(XKCDColors.FreshGreen);

            this.Open();

            return(instance.UUID);
        }
        public async Task WhenConnected_ThenIsAuthenticatedIsFalse(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    Assert.IsFalse(connection.IsAuthenticated);
                }
        }
        public async Task WhenPortNotListening_ThenHandshakeThrowsSocketException(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                12);

            using (var session = CreateSession())
            {
                AssertEx.ThrowsAggregateException <SocketException>(
                    () => session.Connect(endpoint));
            }
        }
        public async Task WhenPortIsCorrect_ThenHandshakeSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    Assert.AreEqual(LIBSSH2_ERROR.NONE, session.LastError);
                }
        }
Esempio n. 16
0
 internal override void OnPostSetup()
 {
     if (customSpaceCenter == null)
     {
         StaticInstance staticInstance = InstanceUtil.GetStaticInstanceForGameObject(this.gameObject);
         customSpaceCenter = new CustomSpaceCenter();
         customSpaceCenter.isFromFacility  = true;
         customSpaceCenter.SpaceCenterName = FacilityName;
         customSpaceCenter.staticInstance  = staticInstance;
         customSpaceCenter.gameObject      = staticInstance.gameObject;
         SpaceCenterManager.AddSpaceCenter(customSpaceCenter);
         Log.Normal("SpaceCenter created: " + FacilityName);
     }
 }
        public async Task WhenRequestedAlgorithmInvalid_ThenActiveAlgorithmsThrowsArgumentException(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    Assert.Throws <ArgumentException>(
                        () => connection.GetActiveAlgorithms((LIBSSH2_METHOD)9999999));
                }
        }
        public async Task WhenPseudoterminalResized_ThenShellReflectsNewSize(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate(
                                   "testuser",
                                   key,
                                   UnexpectedAuthenticationCallback))
                            using (var channel = authSession.OpenShellChannel(
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.MERGE,
                                       DefaultTerminal,
                                       80,
                                       24))
                            {
                                var welcome = ReadUntil(channel, "~$", Encoding.ASCII);

                                // Read initial terminal size.
                                channel.Write(Encoding.ASCII.GetBytes("echo $COLUMNS $LINES\n"));
                                ReadUntil(channel, "\n", Encoding.ASCII);

                                var terminalSize = ReadUntil(channel, "\n", Encoding.ASCII);
                                Assert.AreEqual("80 24\r\n", terminalSize);

                                // Resize terminal.
                                channel.ResizePseudoTerminal(100, 30);

                                // Read terminal size again.
                                channel.Write(Encoding.ASCII.GetBytes("echo $COLUMNS $LINES\n"));
                                ReadUntil(channel, "\n", Encoding.ASCII);

                                terminalSize = ReadUntil(channel, "\n", Encoding.ASCII);
                                Assert.AreEqual("100 30\r\n", terminalSize);

                                channel.Close();
                            }
            }
        }
        //---------------------------------------------------------------------
        // Banner.
        //---------------------------------------------------------------------

        public async Task WhenConnected_ThenGetRemoteBannerReturnsBanner(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    var banner = connection.GetRemoteBanner();
                    Assert.AreEqual(LIBSSH2_ERROR.NONE, session.LastError);
                    Assert.IsNotNull(banner);
                }
        }
        public async Task WhenConnected_GetRemoteBannerReturnsBanner(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
            {
                using (var connection = session.Connect(endpoint))
                {
                    StringAssert.StartsWith("SSH", connection.GetRemoteBanner());
                }
            }
        }
        public async Task WhenConnected_ThenGetAuthenticationMethodsReturnsPublicKey(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    var methods = connection.GetAuthenticationMethods(string.Empty);
                    Assert.IsNotNull(methods);
                    Assert.AreEqual(1, methods.Length);
                    Assert.AreEqual("publickey", methods.First());
                }
        }
        public async Task WhenCustomBannerSet_ThenConnectionSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
            {
                session.SetLocalBanner("SSH-2.0-test-123");
                using (var connection = session.Connect(endpoint))
                {
                    Assert.IsFalse(connection.IsAuthenticated);
                }
            }
        }
        public async Task WhenWhitelistedEnvironmentVariablePassed_ThenShellCanAccessVariable(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate(
                                   "testuser",
                                   key,
                                   UnexpectedAuthenticationCallback))
                            using (var channel = authSession.OpenShellChannel(
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.MERGE,
                                       DefaultTerminal,
                                       80,
                                       24,
                                       new[]
                            {
                                new EnvironmentVariable(
                                    "LANG",
                                    "LC_ALL",
                                    true) // LANG is whitelisted by sshd by default.
                            }))
                            {
                                var bytesWritten = channel.Write(Encoding.ASCII.GetBytes("echo $LANG;exit\n"));
                                Assert.AreEqual(16, bytesWritten);

                                var output = ReadToEnd(channel, Encoding.ASCII);
                                channel.Close();

                                StringAssert.Contains(
                                    "en_US.UTF-8",
                                    output);

                                Assert.AreEqual(0, channel.ExitCode);
                            }
            }
        }
Esempio n. 24
0
        public static void LoadBuilding(ConfigNode cfgNode)
        {
            StaticInstance instance = new StaticInstance();

            instance.isInSavegame = true;

            instance.Orientation     = Vector3.up;
            instance.heighReference  = HeightReference.Terrain;
            instance.VisibilityRange = (PhysicsGlobals.Instance.VesselRangesDefault.flying.unload + 3000);

            instance.Group = "SaveGame";

            instance.RadialPosition = ConfigNode.ParseVector3(cfgNode.GetValue("Position"));

            instance.model = StaticDatabase.GetModelByName(cfgNode.GetValue("ModelName"));

            if (instance.model == null)
            {
                Log.UserError("LoadFromSave: Canot find model named: " + cfgNode.GetValue("ModelName"));
                instance = null;
                return;
            }
            //instance.mesh = UnityEngine.Object.Instantiate(instance.model.prefab);

            instance.UUID = cfgNode.GetValue("UUID");

            instance.CelestialBody = ConfigUtil.GetCelestialBody(cfgNode.GetValue("Body"));

            instance.RadiusOffset  = float.Parse(cfgNode.GetValue("Altitude"));
            instance.RotationAngle = float.Parse(cfgNode.GetValue("Rotation"));

            instance.RefLatitude  = KKMath.GetLatitudeInDeg(instance.RadialPosition);
            instance.RefLongitude = KKMath.GetLongitudeInDeg(instance.RadialPosition);

            InstanceUtil.CreateGroupCenterIfMissing(instance);

            if (cfgNode.HasValue("IsScanable"))
            {
                bool.TryParse(cfgNode.GetValue("IsScanable"), out instance.isScanable);
            }

            bool oldLegacySpawn = KerbalKonstructs.convertLegacyConfigs;

            instance.Orientate();

            KerbalKonstructs.convertLegacyConfigs = oldLegacySpawn;
        }
        public async Task WhenPublicKeyValidButUnrecognized_ThenAuthenticateThrowsAuthenticationFailed(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                    using (var key = new RsaSshKey(new RSACng()))
                    {
                        SshAssert.ThrowsNativeExceptionWithError(
                            session,
                            LIBSSH2_ERROR.AUTHENTICATION_FAILED,
                            () => connection.Authenticate("invaliduser", key));
                    }
        }
        public async Task WhenConnected_ThenGetRemoteHostKeyHashReturnsKeyHash(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    Assert.IsNotNull(connection.GetRemoteHostKeyHash(LIBSSH2_HOSTKEY_HASH.MD5), "MD5");
                    Assert.IsNotNull(connection.GetRemoteHostKeyHash(LIBSSH2_HOSTKEY_HASH.SHA1), "SHA1");

                    // SHA256 is not always available.
                    // Assert.IsNotNull(connection.GetRemoteHostKeyHash(LIBSSH2_HOSTKEY_HASH.SHA256), "SHA256");
                }
        }
        public async Task WhenConnected_ThenGetRemoteHostKeyTypeReturnsEcdsa256(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
                using (var connection = session.Connect(endpoint))
                {
                    var keyType = connection.GetRemoteHostKeyType();
                    Assert.AreEqual(LIBSSH2_ERROR.NONE, session.LastError);
                    Assert.IsTrue(
                        keyType == LIBSSH2_HOSTKEY_TYPE.ECDSA_256 ||
                        keyType == LIBSSH2_HOSTKEY_TYPE.RSA);
                }
        }
        public async Task WhenPreferringIncompatibleAlgorithm_ThenConnectFails(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var session = CreateSession())
            {
                session.SetPreferredMethods(
                    LIBSSH2_METHOD.KEX,
                    new[] { "diffie-hellman-group-exchange-sha1" });

                SshAssert.ThrowsNativeExceptionWithError(
                    session,
                    LIBSSH2_ERROR.KEX_FAILURE,
                    () => session.Connect(endpoint));
            }
        }
        public async Task WhenConnected_ThenOpenShellChannelAsyncSucceeds(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate(
                                   "testuser",
                                   key,
                                   UnexpectedAuthenticationCallback))
                            using (var channel = authSession.OpenShellChannel(
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.MERGE,
                                       DefaultTerminal,
                                       80,
                                       24))
                            {
                                // Run command.
                                var bytesWritten = channel.Write(Encoding.ASCII.GetBytes("whoami;exit\n"));
                                Assert.AreEqual(12, bytesWritten);

                                // Read command output.
                                var output = ReadToEnd(channel, Encoding.ASCII);
                                channel.Close();

                                StringAssert.Contains(
                                    "whoami;exit\r\ntestuser\r\nlogout\r\n",
                                    output);

                                Assert.AreEqual(0, channel.ExitCode);
                                Assert.AreEqual(null, channel.ExitSignal);
                            }
            }
        }
Esempio n. 30
0
        public async Task WhenCommandInvalidAndExtendedDataModeIsNormal_ThenExecuteSucceedsAndStderrContainsError(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key).ConfigureAwait(true);

                using (var session = CreateSession())
                    using (var connection = session.Connect(endpoint))
                        using (var authSession = connection.Authenticate(
                                   "testuser",
                                   key,
                                   UnexpectedAuthenticationCallback))
                            using (var channel = authSession.OpenExecChannel(
                                       "invalidcommand",
                                       LIBSSH2_CHANNEL_EXTENDED_DATA.NORMAL))
                            {
                                channel.WaitForEndOfStream();

                                var buffer    = new byte[1024];
                                var bytesRead = channel.Read(
                                    buffer,
                                    LIBSSH2_STREAM.EXTENDED_DATA_STDERR);
                                Assert.AreNotEqual(0, bytesRead);

                                Assert.AreEqual(
                                    "bash: invalidcommand: command not found\n",
                                    Encoding.ASCII.GetString(buffer, 0, (int)bytesRead));

                                Assert.AreEqual(127, channel.ExitCode);
                                Assert.IsNull(channel.ExitSignal);
                                channel.Close();
                            }
            }
        }