FreeRDP
Loading...
Searching...
No Matches
TestHandleExportImportString.c
1
20#include <stdio.h>
21#include <winpr/crt.h>
22#include <winpr/handle.h>
23#include <winpr/pipe.h>
24
25/* exports hWrite, imports it back from the resulting string, then checks that writing through
26 * the imported HANDLE is visible on hRead - i.e. that the import genuinely reconstructs a
27 * working HANDLE, not just a value that happens to parse back. */
28static BOOL test_roundtrip(HANDLE hRead, HANDLE hWrite)
29{
30 char formatted[256] = WINPR_C_ARRAY_INIT;
31 if (!winpr_exportHandleToString(hWrite, "--fdIn={}", formatted, sizeof(formatted)))
32 {
33 printf("[roundtrip] winpr_exportHandleToString failed\n");
34 return FALSE;
35 }
36 printf("[roundtrip] exported as '%s'\n", formatted);
37
38 HANDLE imported = winpr_importHandleFromString(formatted, "--fdIn={}");
39 if (!imported || (imported == INVALID_HANDLE_VALUE))
40 {
41 printf("[roundtrip] winpr_importHandleFromString failed\n");
42 return FALSE;
43 }
44
45 const char payload[] = "PING";
46 DWORD written = 0;
47 if (!WriteFile(imported, payload, sizeof(payload) - 1, &written, nullptr) ||
48 (written != sizeof(payload) - 1))
49 {
50 printf("[roundtrip] WriteFile through the imported handle failed\n");
51 (void)CloseHandle(imported);
52 return FALSE;
53 }
54
55 char readBuf[16] = WINPR_C_ARRAY_INIT;
56 DWORD nread = 0;
57 if (!ReadFile(hRead, readBuf, sizeof(payload) - 1, &nread, nullptr) ||
58 (nread != sizeof(payload) - 1))
59 {
60 printf("[roundtrip] ReadFile on the original read end failed\n");
61 (void)CloseHandle(imported);
62 return FALSE;
63 }
64
65 if (memcmp(readBuf, payload, sizeof(payload) - 1) != 0)
66 {
67 printf("[roundtrip] data read back does not match what was written\n");
68 (void)CloseHandle(imported);
69 return FALSE;
70 }
71
72 (void)CloseHandle(imported);
73 return TRUE;
74}
75
76/* a format string without a "{}" placeholder is invalid input - must fail rather than silently
77 * dropping the handle value somewhere. */
78static BOOL test_export_rejects_missing_placeholder(HANDLE hWrite)
79{
80 char buf[64] = WINPR_C_ARRAY_INIT;
81 if (winpr_exportHandleToString(hWrite, "no-placeholder-here", buf, sizeof(buf)))
82 {
83 printf("[missing-placeholder] expected export to fail, but it succeeded\n");
84 return FALSE;
85 }
86 return TRUE;
87}
88
89/* an output buffer too small to hold "prefix + value + suffix + NUL" must fail cleanly instead
90 * of truncating or overflowing. */
91static BOOL test_export_rejects_short_buffer(HANDLE hWrite)
92{
93 char tiny[2] = WINPR_C_ARRAY_INIT;
94 if (winpr_exportHandleToString(hWrite, "--fdIn={}", tiny, sizeof(tiny)))
95 {
96 printf("[short-buffer] expected export to fail, but it succeeded\n");
97 return FALSE;
98 }
99 return TRUE;
100}
101
102/* an input string that does not match the given format's literal prefix/suffix must be rejected
103 * rather than mis-parsed. */
104static BOOL test_import_rejects_format_mismatch(void)
105{
106 HANDLE h = winpr_importHandleFromString("--other=5", "--fdIn={}");
107 if (h != INVALID_HANDLE_VALUE)
108 {
109 printf("[format-mismatch] expected INVALID_HANDLE_VALUE, got a handle\n");
110 (void)CloseHandle(h);
111 return FALSE;
112 }
113 return TRUE;
114}
115
116/* a non-numeric handle value between the matched prefix/suffix must be rejected. */
117static BOOL test_import_rejects_garbage_value(void)
118{
119 HANDLE h = winpr_importHandleFromString("--fdIn=notanumber", "--fdIn={}");
120 if (h != INVALID_HANDLE_VALUE)
121 {
122 printf("[garbage-value] expected INVALID_HANDLE_VALUE, got a handle\n");
123 (void)CloseHandle(h);
124 return FALSE;
125 }
126 return TRUE;
127}
128
129int TestHandleExportImportString(int argc, char* argv[])
130{
131 WINPR_UNUSED(argc);
132 WINPR_UNUSED(argv);
133
134 HANDLE hRead = nullptr;
135 HANDLE hWrite = nullptr;
136 if (!CreatePipe(&hRead, &hWrite, nullptr, 0))
137 {
138 printf("CreatePipe failed\n");
139 return -1;
140 }
141
142 int rc = 0;
143 if (!test_roundtrip(hRead, hWrite))
144 rc = -1;
145 if (!test_export_rejects_missing_placeholder(hWrite))
146 rc = -1;
147 if (!test_export_rejects_short_buffer(hWrite))
148 rc = -1;
149 if (!test_import_rejects_format_mismatch())
150 rc = -1;
151 if (!test_import_rejects_garbage_value())
152 rc = -1;
153
154 (void)CloseHandle(hRead);
155 (void)CloseHandle(hWrite);
156 return rc;
157}