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 using setenvfn_t
= void (*)(const ACE_TCHAR
*, const ACE_TCHAR
*, void *);
19 #if defined (ACE_WIN32) && !defined (ACE_USES_WCHAR) && !defined (ACE_HAS_WINCE)
21 void create_large_env (setenvfn_t setenv
, void *ctx
)
23 static const size_t varsize
= 1600;
24 for (int i
= 0; i
< 26; i
++)
26 char name
[2] = { 'A', '\0' };
29 ACE_OS::memset (value
, 'R', varsize
);
30 value
[varsize
- 1] = '\0';
31 setenv (ACE_TEXT_CHAR_TO_TCHAR (name
),
32 ACE_TEXT_CHAR_TO_TCHAR (value
),
37 void apo_setenv (const ACE_TCHAR
*name
, const ACE_TCHAR
*value
, void *ctx
)
39 ACE_Process_Options
*apo
= static_cast<ACE_Process_Options
*> (ctx
);
40 apo
->setenv (name
, value
);
43 void thisproc_setenv (const ACE_TCHAR
*name
, const ACE_TCHAR
*value
, void *)
45 ACE_TString
putstr (name
);
46 putstr
+= ACE_TEXT ('=');
48 ACE_OS::putenv (putstr
.c_str ());
53 run_main (int, ACE_TCHAR
*[])
56 ACE_START_TEST (ACE_TEXT ("Process_Env_Test"));
58 #if defined (ACE_WIN32) && !defined (ACE_USES_WCHAR) && !defined (ACE_HAS_WINCE)
59 ACE_Process_Options
options (
61 ACE_Process_Options::DEFAULT_COMMAND_LINE_BUF_LEN
,
63 options
.command_line (ACE_TEXT ("attrib.exe /?"));
64 create_large_env (apo_setenv
, &options
);
66 ACE_OS::fclose(stdout
);
68 if (process
.spawn (options
) != -1)
71 * In Windows versions < Vista the ENTIRE environment block could be a
72 * maximum of 32,767 bytes long
74 * In Windows versions > Vista it's 32,767 bytes per environment variable
76 #if (_WIN32_WINNT < 0x0600)
78 "ERROR: This should have failed due to the large "
79 "environment buffer\n"));
84 "Using large environment buffer works as expected "
85 "on Windows vista or newer\n"));
86 #endif /* _WIN32_WINNT < 0x0600 */
89 options
.enable_unicode_environment ();
90 if (process
.spawn (options
) == -1)
93 "ERROR: This should have succeeded\n"));
97 // Test inheriting a large env block with enable_unicode_environment
98 ACE_Process_Options
opts2 (1,
99 ACE_Process_Options::DEFAULT_COMMAND_LINE_BUF_LEN
,
101 create_large_env (thisproc_setenv
, 0);
102 opts2
.enable_unicode_environment ();
103 opts2
.setenv (ACE_TEXT ("ZZ"), ACE_TEXT ("1"));
104 opts2
.command_line (ACE_TEXT ("cmd.exe /d /c ")
105 ACE_TEXT ("\"if defined Z (exit 1) else (exit 2)\""));
106 ACE_Process process2
;
107 if (process2
.spawn (opts2
) == -1)
109 ACE_ERROR ((LM_ERROR
,
110 "ERROR: Failed to spawn process2.\n"));
114 process2
.wait (&status
);
117 ACE_ERROR ((LM_ERROR
,
118 "ERROR: process2 did not inherit env var Z, wait returned %d.\n", status
));
123 ACE_DEBUG ((LM_INFO
, "This test is for Win32 without ACE_USES_WCHAR\n"));
124 #endif /* ACE_WIN32 && !ACE_USES_WCHAR && !ACE_HAS_WINCE */