agent-phone-use / tests /TestFixture.cs
LucioLiu's picture
Initial release - mirrors github.com/LucioLiu/agent-phone-use
44dc2da verified
Raw
History Blame Contribute Delete
5.24 kB
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
internal static class TestFixture
{
private static int Main(string[] args)
{
string exe = Path.GetFileName(Environment.GetCommandLineArgs()[0]).ToLowerInvariant();
if (exe == "adb.exe")
{
return RunAdb(args);
}
if (exe == "scrcpy.exe")
{
return RunScrcpy(args);
}
Console.Error.WriteLine("Unexpected fixture executable name.");
return 90;
}
private static int RunAdb(string[] args)
{
Log("ADB " + string.Join(" ", args));
string mode = Environment.GetEnvironmentVariable("APU_FIXTURE_MODE") ?? "single";
if (args.SequenceEqual(new[] { "version" }))
{
Console.WriteLine("Android Debug Bridge version 1.0.41");
return 0;
}
if (args.SequenceEqual(new[] { "mdns", "services" }))
{
if (mode == "none")
{
return 0;
}
if (mode == "pair")
{
Console.WriteLine("adb-EXAMPLE-PAIR _adb-tls-pairing._tcp 192.0.2.10:37123");
return 0;
}
Console.WriteLine("adb-EXAMPLE-A _adb-tls-connect._tcp 192.0.2.10:12345");
if (mode == "multi")
{
Console.WriteLine("adb-EXAMPLE-B _adb-tls-connect._tcp 198.51.100.20:23456");
}
return 0;
}
if (args.Length == 2 && args[0] == "connect")
{
Console.WriteLine("connected to " + args[1]);
return 0;
}
if (args.Length == 2 && args[0] == "pair")
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APU_PAIRING_CODE")) ||
!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWC_PAIRING_CODE")))
{
Console.Error.WriteLine("Pairing code leaked into the ADB child environment.");
return 94;
}
string expectedCode = Environment.GetEnvironmentVariable("APU_FIXTURE_EXPECT_PAIRING_CODE");
if (!string.IsNullOrWhiteSpace(expectedCode))
{
string suppliedCode = Console.In.ReadLine();
if (!string.Equals(suppliedCode, expectedCode, StringComparison.Ordinal))
{
Console.Error.WriteLine("Pairing code was not received through standard input.");
return 93;
}
}
Console.WriteLine("Successfully paired to " + args[1]);
return 0;
}
if (args.Length == 3 && args[0] == "-s" && args[2] == "get-state")
{
Console.WriteLine("device");
return 0;
}
if (args.SequenceEqual(new[] { "devices" }))
{
Console.WriteLine("List of devices attached");
Console.WriteLine("192.0.2.10:12345\tdevice");
if (mode == "alias")
{
Console.WriteLine("adb-EXAMPLE-A._adb-tls-connect._tcp\tdevice");
}
return 0;
}
if (args.Length > 0 && args[0] == "shell")
{
Console.Error.WriteLine("Bare adb shell is forbidden by the fixture.");
return 91;
}
Console.Error.WriteLine("Unexpected adb arguments: " + string.Join(" ", args));
return 92;
}
private static int RunScrcpy(string[] args)
{
if (args.SequenceEqual(new[] { "--version" }))
{
Console.WriteLine("scrcpy 4.0 <https://github.com/Genymobile/scrcpy>");
return 0;
}
Log("SCRCPY " + string.Join(" ", args));
int sleepMs;
if (int.TryParse(Environment.GetEnvironmentVariable("APU_FIXTURE_SCRCPY_SLEEP_MS"), out sleepMs) && sleepMs > 0)
{
Thread.Sleep(sleepMs);
}
int exitCode;
if (int.TryParse(Environment.GetEnvironmentVariable("APU_FIXTURE_SCRCPY_EXIT"), out exitCode))
{
return exitCode;
}
return 0;
}
private static void Log(string line)
{
string path = Environment.GetEnvironmentVariable("APU_FIXTURE_LOG");
if (string.IsNullOrWhiteSpace(path))
{
return;
}
byte[] bytes = Encoding.UTF8.GetBytes(line + Environment.NewLine);
for (int attempt = 0; attempt < 20; attempt++)
{
try
{
using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
stream.Seek(0, SeekOrigin.End);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
return;
}
catch (IOException)
{
Thread.Sleep(25);
}
}
throw new IOException("Fixture invocation log remained locked.");
}
}