Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Process_Env_Test.cpp
blob4b9342c85574a87cb6da3a571afcbb9a2715a963
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 #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' };
26 name[0] += i;
27 char value[varsize];
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),
32 ctx);
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 ('=');
46 putstr += value;
47 ACE_OS::putenv (putstr.c_str ());
49 #endif
51 int
52 run_main (int, ACE_TCHAR*[])
54 int test_status = 0;
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,
61 32 * 1024);
62 options.command_line (ACE_TEXT ("attrib.exe /?"));
63 create_large_env (apo_setenv, &options);
65 ACE_OS::fclose(stdout);
66 ACE_Process process;
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)
76 ACE_ERROR ((LM_ERROR,
77 "ERROR: This should have failed due to the large "
78 "environment buffer\n"));
80 test_status = 1;
81 #else
82 ACE_DEBUG ((LM_DEBUG,
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)
91 ACE_ERROR ((LM_ERROR,
92 "ERROR: This should have succeeded\n"));
93 test_status = 1;
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,
99 128 * 1024);
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"));
110 test_status = 1;
112 ACE_exitcode status;
113 process2.wait (&status);
114 if (status != 1)
116 ACE_ERROR ((LM_ERROR,
117 "ERROR: process2 did not inherit env var Z, wait returned %d.\n", status));
118 test_status = 1;
121 #else
122 ACE_DEBUG ((LM_INFO, "This test is for Win32 without ACE_USES_WCHAR\n"));
123 #endif /* ACE_WIN32 && !ACE_USES_WCHAR */
125 ACE_END_TEST;
126 return test_status;