installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / tests / trivial_test.cc
1 // Copyright (c) 2015 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #include <windows.h>
22
23 #include <cassert>
24 #include <cctype>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cwchar>
28 #include <vector>
29
30 #include "../include/winpty.h"
31 #include "../shared/DebugClient.h"
32
33 static std::vector<unsigned char> filterContent(
34         const std::vector<unsigned char> &content) {
35     std::vector<unsigned char> result;
36     auto it = content.begin();
37     const auto itEnd = content.end();
38     while (it < itEnd) {
39         if (*it == '\r') {
40             // Filter out carriage returns.  Sometimes the output starts with
41             // a single CR; other times, it has multiple CRs.
42             it++;
43         } else if (*it == '\x1b' && (it + 1) < itEnd && *(it + 1) == '[') {
44             // Filter out escape sequences.  They have no interior letters and
45             // end with a single letter.
46             it += 2;
47             while (it < itEnd && !isalpha(*it)) {
48                 it++;
49             }
50             it++;
51         } else {
52             // Let everything else through.
53             result.push_back(*it);
54             it++;
55         }
56     }
57     return result;
58 }
59
60 // Read bytes from the non-overlapped file handle until the file is closed or
61 // until an I/O error occurs.
62 static std::vector<unsigned char> readAll(HANDLE handle) {
63     unsigned char buf[1024];
64     std::vector<unsigned char> result;
65     while (true) {
66         DWORD amount = 0;
67         BOOL ret = ReadFile(handle, buf, sizeof(buf), &amount, nullptr);
68         if (!ret || amount == 0) {
69             break;
70         }
71         result.insert(result.end(), buf, buf + amount);
72     }
73     return result;
74 }
75
76 static void parentTest() {
77     wchar_t program[1024];
78     wchar_t cmdline[1024];
79     GetModuleFileNameW(nullptr, program, 1024);
80
81     {
82         // XXX: We'd like to use swprintf, which is part of C99 and takes a
83         // size_t maxlen argument.  MinGW-w64 has this function, as does MSVC.
84         // The old MinGW doesn't, though -- instead, it apparently provides an
85         // swprintf taking no maxlen argument.  This *might* be a regression?
86         // (There is also no swnprintf, but that function is obsolescent with a
87         // correct swprintf, and it isn't in POSIX or ISO C.)
88         //
89         // Visual C++ 6 also provided this non-conformant swprintf, and I'm
90         // guessing MSVCRT.DLL does too.  (My impression is that the old MinGW
91         // prefers to rely on MSVCRT.DLL for convenience?)
92         //
93         // I could compile differently for old MinGW, but what if it fixes its
94         // function later?  Instead, use a workaround.  It's starting to make
95         // sense to drop MinGW support in favor of MinGW-w64.  This is too
96         // annoying.
97         //
98         // grepbait: OLD-MINGW / WINPTY_TARGET_MSYS1
99         cmdline[0] = L'\0';
100         wcscat(cmdline, L"\"");
101         wcscat(cmdline, program);
102         wcscat(cmdline, L"\" CHILD");
103     }
104     // swnprintf(cmdline, sizeof(cmdline) / sizeof(cmdline[0]),
105     //           L"\"%ls\" CHILD", program);
106
107     auto agentCfg = winpty_config_new(0, nullptr);
108     assert(agentCfg != nullptr);
109     auto pty = winpty_open(agentCfg, nullptr);
110     assert(pty != nullptr);
111     winpty_config_free(agentCfg);
112
113     HANDLE conin = CreateFileW(
114         winpty_conin_name(pty),
115         GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
116     HANDLE conout = CreateFileW(
117         winpty_conout_name(pty),
118         GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
119     assert(conin != INVALID_HANDLE_VALUE);
120     assert(conout != INVALID_HANDLE_VALUE);
121
122     auto spawnCfg = winpty_spawn_config_new(
123             WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN, program, cmdline,
124             nullptr, nullptr, nullptr);
125     assert(spawnCfg != nullptr);
126     HANDLE process = nullptr;
127     BOOL spawnSuccess = winpty_spawn(
128         pty, spawnCfg, &process, nullptr, nullptr, nullptr);
129     assert(spawnSuccess && process != nullptr);
130
131     auto content = readAll(conout);
132     content = filterContent(content);
133
134     std::vector<unsigned char> expectedContent = {
135         'H', 'I', '\n', 'X', 'Y', '\n'
136     };
137     DWORD exitCode = 0;
138     assert(GetExitCodeProcess(process, &exitCode) && exitCode == 42);
139     CloseHandle(process);
140     CloseHandle(conin);
141     CloseHandle(conout);
142     assert(content == expectedContent);
143     winpty_free(pty);
144 }
145
146 static void childTest() {
147     printf("HI\nXY\n");
148     exit(42);
149 }
150
151 int main(int argc, char *argv[]) {
152     if (argc == 1) {
153         parentTest();
154     } else {
155         childTest();
156     }
157     return 0;
158 }