Ejemplo n.º 1
0
        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");
            }
        }
Ejemplo n.º 2
0
        public void can_get_exit_code_from_process()
        {
            using (var subject = new ProcessHost("./ExampleNoninteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start("return 1729");

                Assert.That(subject.WaitForExit(one_second), "process did not exit");
                var code = subject.ExitCode();

                Assert.That(code, Is.EqualTo(1729));
            }
        }
Ejemplo n.º 3
0
        public void can_write_strings_from_a_processes_pipes()
        {
            using (var subject = new ProcessHost("./ExampleInteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start();

                subject.StdIn.WriteAllText(Encoding.Default, "bye\r\n");

                int exitCode;
                var ok = subject.WaitForExit(one_second, out exitCode);

                Assert.That(ok, Is.True);
                Assert.That(exitCode, Is.EqualTo(0));
            }
        }
Ejemplo n.º 4
0
        public void can_call_environment_executables()
        {
            using (var subject = new ProcessHost("net", null))
            {
                subject.Start();
                subject.WaitForExit(TimeSpan.FromSeconds(5));

                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");
            }
        }
Ejemplo n.º 5
0
        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");
            }
        }
Ejemplo n.º 6
0
        public void Writing_to_an_OUT_pipe_throws_an_exception()
        {
            using (var subject = new ProcessHost("./ExampleNoninteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start();

                var dummy = new byte[2];

                Assert.Throws <Exception>(() => subject.StdOut.Write(dummy, 0, 1));

                int exitCode;
                var exited = subject.WaitForExit(one_second, out exitCode);

                Assert.That(exited, Is.True);
                Assert.That(exitCode, Is.EqualTo(0));
            }
        }
Ejemplo n.º 7
0
        public void can_read_and_write_single_lines_on_a_processes_pipes()
        {
            using (var subject = new ProcessHost("./ExampleInteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start();

                var read = subject.StdOut.ReadLine(Encoding.Default, one_second);
                Assert.That(read, Is.EqualTo(ExampleProcess.Program.Intro));

                subject.StdIn.WriteLine(Encoding.Default, "bye");

                int exitCode;
                var ok = subject.WaitForExit(one_second, out exitCode);

                Assert.That(ok, Is.True);
                Assert.That(exitCode, Is.EqualTo(0));
            }
        }
Ejemplo n.º 8
0
        public void can_get_process_id_and_use_with_existing_dotnet_libraries()
        {
            using (var subject = new ProcessHost("./ExampleInteractiveProcess.exe", Directory.GetCurrentDirectory()))
            {
                subject.Start();

                uint id = subject.ProcessId();

                var process = Process.GetProcessById((int)id);
                Assert.That(process.HasExited, Is.False, "Exited");

                process.Kill();

                int exitCode;
                var exited = subject.WaitForExit(one_second, out exitCode);

                Assert.That(exited, Is.True, "Exited after kill");
                Assert.That(exitCode, Is.EqualTo(0), "Exit code");
            }
        }