chr
2026-04-08 53e656200368a983e563550e2cc1acbc6d86b729
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Linq;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Text;
using System.IO;
namespace OpenTap.Engine.UnitTests
{
    public class TapProcessContainer
    {
        public Process TapProcess;
        public string ConsoleOutput = "";
        Task consoleListener;
        void go()
        {
            TapProcess.Start();
            var consoleOutput = new StringBuilder();
            
            var procOutput = TapProcess.StandardOutput;
            var procOutput2 = TapProcess.StandardError;
 
            async Task asyncReader(StreamReader read)
            {
                char[] buffer = new char[100];
                while (!read.EndOfStream)
                {
                    int read2 = await read.ReadAsync(buffer, 0, buffer.Length);
                    if (read2 == -1)
                        break;
                    lock(consoleOutput)
                        consoleOutput.Append(buffer, 0, read2);
                }
            }
            
            async Task consoleOutputLoader()
            {
                await Task.WhenAll(asyncReader(procOutput), asyncReader(procOutput2));
                ConsoleOutput = consoleOutput.ToString();
            }
            consoleListener = consoleOutputLoader();
        }
 
        public static TapProcessContainer StartFromArgs(string args) => StartFromArgs(args, TimeSpan.FromMinutes(2));
            
        public static TapProcessContainer StartFromArgs(string args, TimeSpan timeOutAfter)
        {
            Process proc = new Process();
 
            var container = new TapProcessContainer { TapProcess = proc };
 
 
            var file = Path.GetDirectoryName(typeof(PluginManager).Assembly.Location);
            var files = new[] { Path.Combine(file, "tap.exe"), Path.Combine(file, "tap"), Path.Combine(file, "tap.dll") };
            global::OpenTap.Log.CreateSource("test").Debug($"location: {file}");
            var program = files.First(File.Exists);
            if (program.Contains(".dll"))
            {
                program = "dotnet";
                args = $"\"{file}/tap.dll\" " + args;
            }
 
            proc.StartInfo = new ProcessStartInfo(program, args)
            {
                UseShellExecute = true,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
            };
            proc.StartInfo.UseShellExecute = false;
 
            container.go();
            TapThread.Start(() =>
            {
                TapThread.Sleep(timeOutAfter);
                proc.Kill();
            });
            return container;
        }
 
        public void WaitForEnd()
        {
            consoleListener.Wait();
            TapProcess.WaitForExit();
        }
 
        public void WriteLine(string str)
        {
            TapProcess.StandardInput.WriteLine(str);
        }
 
        public void Kill()
        {
            try
            {
                TapProcess.Kill();
            }
            catch
            {
 
            }
        }
    }
}