ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Process_Env_Test.cpp
blob559ef8fef196f5ea39aa2910ce62808d6fdcce5a
2 //=============================================================================
3 /**
4 * @file Process_Env_Test.cpp
6 * This program tests the limits of the Windows CreateProcess
7 * environment buffer.
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' };
27 name[0] += i;
28 char value[varsize];
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),
33 ctx);
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 ('=');
47 putstr += value;
48 ACE_OS::putenv (putstr.c_str ());
50 #endif
52 int
53 run_main (int, ACE_TCHAR*[])
55 int test_status = 0;
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,
62 32 * 1024);
63 options.command_line (ACE_TEXT ("attrib.exe /?"));
64 create_large_env (apo_setenv, &options);
66 ACE_OS::fclose(stdout);
67 ACE_Process process;
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)
77 ACE_ERROR ((LM_ERROR,
78 "ERROR: This should have failed due to the large "
79 "environment buffer\n"));
81 test_status = 1;
82 #else
83 ACE_DEBUG ((LM_DEBUG,
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)
92 ACE_ERROR ((LM_ERROR,
93 "ERROR: This should have succeeded\n"));
94 test_status = 1;
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,
100 128 * 1024);
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"));
111 test_status = 1;
113 ACE_exitcode status;
114 process2.wait (&status);
115 if (status != 1)
117 ACE_ERROR ((LM_ERROR,
118 "ERROR: process2 did not inherit env var Z, wait returned %d.\n", status));
119 test_status = 1;
122 #else
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 */
126 ACE_END_TEST;
127 return test_status;