2 //=============================================================================
4 * @file Process_Env_Test.cpp
6 * This program tests the limits of the Windows CreateProcess
9 * @author Chad Elliott <elliott_c@ociweb.com>
11 //=============================================================================
13 #include "test_config.h"
14 #include "ace/Process.h"
15 #include "ace/SString.h"
17 #if defined (ACE_WIN32) && !defined (ACE_USES_WCHAR)
18 using setenvfn_t
= void (*)(const ACE_TCHAR
*, const ACE_TCHAR
*, void *);
20 void create_large_env (setenvfn_t setenv
, void *ctx
)
22 static const size_t varsize
= 1600;
23 for (int i
= 0; i
< 26; i
++)
25 char name
[2] = { 'A', '\0' };
28 ACE_OS::memset (value
, 'R', varsize
);
29 value
[varsize
- 1] = '\0';
30 setenv (ACE_TEXT_CHAR_TO_TCHAR (name
),
31 ACE_TEXT_CHAR_TO_TCHAR (value
),
36 void apo_setenv (const ACE_TCHAR
*name
, const ACE_TCHAR
*value
, void *ctx
)
38 ACE_Process_Options
*apo
= static_cast<ACE_Process_Options
*> (ctx
);
39 apo
->setenv (name
, value
);
42 void thisproc_setenv (const ACE_TCHAR
*name
, const ACE_TCHAR
*value
, void *)
44 ACE_TString
putstr (name
);
45 putstr
+= ACE_TEXT ('=');
47 ACE_OS::putenv (putstr
.c_str ());
52 run_main (int, ACE_TCHAR
*[])
55 ACE_START_TEST (ACE_TEXT ("Process_Env_Test"));
57 #if defined (ACE_WIN32) && !defined (ACE_USES_WCHAR)
58 ACE_Process_Options
options (
60 ACE_Process_Options::DEFAULT_COMMAND_LINE_BUF_LEN
,
62 options
.command_line (ACE_TEXT ("attrib.exe /?"));
63 create_large_env (apo_setenv
, &options
);
65 ACE_OS::fclose(stdout
);
67 if (process
.spawn (options
) != -1)
70 * In Windows versions < Vista the ENTIRE environment block could be a
71 * maximum of 32,767 bytes long
73 * In Windows versions > Vista it's 32,767 bytes per environment variable
75 #if (_WIN32_WINNT < 0x0600)
77 "ERROR: This should have failed due to the large "
78 "environment buffer\n"));
83 "Using large environment buffer works as expected "
84 "on Windows vista or newer\n"));
85 #endif /* _WIN32_WINNT < 0x0600 */
88 options
.enable_unicode_environment ();
89 if (process
.spawn (options
) == -1)
92 "ERROR: This should have succeeded\n"));
96 // Test inheriting a large env block with enable_unicode_environment
97 ACE_Process_Options
opts2 (1,
98 ACE_Process_Options::DEFAULT_COMMAND_LINE_BUF_LEN
,
100 create_large_env (thisproc_setenv
, 0);
101 opts2
.enable_unicode_environment ();
102 opts2
.setenv (ACE_TEXT ("Z"), ACE_TEXT ("1"));
103 opts2
.command_line (ACE_TEXT ("cmd.exe /d /c ")
104 ACE_TEXT ("\"if defined Z (exit 1) else (exit 2)\""));
105 ACE_Process process2
;
106 if (process2
.spawn (opts2
) == -1)
108 ACE_ERROR ((LM_ERROR
,
109 "ERROR: Failed to spawn process2.\n"));
113 process2
.wait (&status
);
116 ACE_ERROR ((LM_ERROR
,
117 "ERROR: process2 did not inherit env var Z, wait returned %d.\n", status
));
122 ACE_DEBUG ((LM_INFO
, "This test is for Win32 without ACE_USES_WCHAR\n"));
123 #endif /* ACE_WIN32 && !ACE_USES_WCHAR */