File size: 5,241 Bytes
44dc2da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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.");
    }
}