public void can_wait_for_process_and_kill_if_required()
        {
            using (var subject = new ProcessHost("./ExampleNoninteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start("wait");

                var ended = subject.WaitForExit(one_second);

                Assert.That(ended, Is.False, "Ended");
                Assert.That(subject.IsAlive(), Is.True, "Alive");

                subject.Kill();
                var endedAfterKill = subject.WaitForExit(one_second);

                Assert.That(endedAfterKill, Is.True, "ended after kill");
                Assert.That(subject.IsAlive(), Is.False, "Alive after kill");
                Assert.That(subject.ExitCode(), Is.EqualTo(127), "standard killed code");
            }
        }
        public void can_run_and_read_from_a_non_interactive_process()
        {
            using (var subject = new ProcessHost("./ExampleNoninteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start();
                Thread.Sleep(250);

                Assert.That(subject.IsAlive(), Is.False);

                var output = subject.StdOut.ReadAllText(Encoding.Default);
                Assert.That(output, Is.StringStarting(ExampleNoninteractiveProcess.Program.StdOutMsg), "Standard Out");

                var err = subject.StdErr.ReadAllText(Encoding.Default);
                Assert.That(err, Is.StringStarting(ExampleNoninteractiveProcess.Program.StdErrMsg), "Standard Error");
            }
        }
        public void can_call_environment_executables()
        {
            using (var subject = new ProcessHost("net", null))
            {
                subject.Start();
                Thread.Sleep(250);

                Assert.That(subject.IsAlive(), Is.False);

                var output = subject.StdOut.ReadAllText(Encoding.Default);
                Assert.That(output, Is.EqualTo(""), "Standard Out");

                var err = subject.StdErr.ReadAllText(Encoding.Default);
                Assert.That(err, Is.StringStarting("The syntax of this command is:"), "Standard Error");
            }
        }
        public void can_impersonate_another_user(string domain, string user, string password)
        {
            var expected = domain + "\\" + user + "\r\n";

            using (var subject = new ProcessHost("whoami", null))
            {
                subject.StartAsAnotherUser(domain, user, password, "");
                subject.WaitForExit(TimeSpan.FromSeconds(2));
                Assert.That(subject.IsAlive(), Is.False);

                var output = subject.StdOut.ReadAllText(Encoding.Default);
                Assert.That(output.ToLower(), Is.EqualTo(expected.ToLower()), "Standard Out");

                var err = subject.StdErr.ReadAllText(Encoding.Default);
                Assert.That(err, Is.Empty, "Standard Error");
            }
        }
 static bool IsOk(ProcessHost proc)
 {
     return proc != null && proc.IsAlive();
 }