1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Tests msvcrt/data.c
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2006 Andrew Ziem
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
211c9ac7a2SAmine Khaldi #include "wine/test.h"
221c9ac7a2SAmine Khaldi #include <stdarg.h>
231c9ac7a2SAmine Khaldi #include <stdlib.h>
241c9ac7a2SAmine Khaldi #include <stdio.h>
251c9ac7a2SAmine Khaldi #include <fcntl.h>
261c9ac7a2SAmine Khaldi #include <sys/stat.h>
271c9ac7a2SAmine Khaldi #include <io.h>
281c9ac7a2SAmine Khaldi #include <windef.h>
291c9ac7a2SAmine Khaldi #include <winbase.h>
301c9ac7a2SAmine Khaldi #include <winnls.h>
311c9ac7a2SAmine Khaldi #include <process.h>
321c9ac7a2SAmine Khaldi #include <errno.h>
33c2c66affSColin Finck #include <direct.h>
34c2c66affSColin Finck 
35c2c66affSColin Finck void __cdecl __getmainargs(int *, char ***, char ***, int, int *);
36c2c66affSColin Finck static int* (__cdecl *p___p___argc)(void);
37c2c66affSColin Finck static char*** (__cdecl *p___p___argv)(void);
38c2c66affSColin Finck 
39c2c66affSColin Finck typedef void (__cdecl *_INITTERMFUN)(void);
40c2c66affSColin Finck static void (__cdecl *p_initterm)(_INITTERMFUN *start, _INITTERMFUN *end);
41c2c66affSColin Finck 
42c2c66affSColin Finck static int (__cdecl *p_get_pgmptr)(char **p);
43c2c66affSColin Finck 
44c2c66affSColin Finck static int callbacked;
45c2c66affSColin Finck 
initcallback(void)46c2c66affSColin Finck static void __cdecl initcallback(void)
47c2c66affSColin Finck {
48c2c66affSColin Finck    callbacked++;
49c2c66affSColin Finck }
50c2c66affSColin Finck 
51c2c66affSColin Finck #define initterm_test(start, end, expected) \
52c2c66affSColin Finck     callbacked = 0; \
53c2c66affSColin Finck     p_initterm(start, end); \
54c2c66affSColin Finck     ok(expected == callbacked,"_initterm: callbacks count mismatch: got %i, expected %i\n", callbacked, expected);
55c2c66affSColin Finck 
test_initterm(void)56c2c66affSColin Finck static void test_initterm(void)
57c2c66affSColin Finck {
58c2c66affSColin Finck     int i;
59c2c66affSColin Finck     static _INITTERMFUN callbacks[4];
60c2c66affSColin Finck 
61c2c66affSColin Finck     if (!p_initterm)
62c2c66affSColin Finck         return;
63c2c66affSColin Finck 
64c2c66affSColin Finck     for (i = 0; i < 4; i++)
65c2c66affSColin Finck     {
66c2c66affSColin Finck         callbacks[i] = initcallback;
67c2c66affSColin Finck     }
68c2c66affSColin Finck 
69c2c66affSColin Finck     initterm_test(&callbacks[0], &callbacks[1], 1);
70c2c66affSColin Finck     initterm_test(&callbacks[0], &callbacks[2], 2);
71c2c66affSColin Finck     initterm_test(&callbacks[0], &callbacks[3], 3);
72c2c66affSColin Finck 
73c2c66affSColin Finck     callbacks[1] = NULL;
74c2c66affSColin Finck     initterm_test(&callbacks[0], &callbacks[3], 2);
75c2c66affSColin Finck }
76c2c66affSColin Finck 
test_initvar(HMODULE hmsvcrt)77c2c66affSColin Finck static void test_initvar( HMODULE hmsvcrt )
78c2c66affSColin Finck {
79c2c66affSColin Finck     OSVERSIONINFOA osvi = { sizeof(OSVERSIONINFOA) };
80c2c66affSColin Finck     int *pp_winver   = (int*)GetProcAddress(hmsvcrt, "_winver");
81c2c66affSColin Finck     int *pp_winmajor = (int*)GetProcAddress(hmsvcrt, "_winmajor");
82c2c66affSColin Finck     int *pp_winminor = (int*)GetProcAddress(hmsvcrt, "_winminor");
83c2c66affSColin Finck     int *pp_osver    = (int*)GetProcAddress(hmsvcrt, "_osver");
84c2c66affSColin Finck     int *pp_osplatform = (int*)GetProcAddress(hmsvcrt, "_osplatform");
85c2c66affSColin Finck     unsigned int winver, winmajor, winminor, osver, osplatform;
86c2c66affSColin Finck 
87c2c66affSColin Finck     if( !( pp_winmajor && pp_winminor && pp_winver)) {
88c2c66affSColin Finck         win_skip("_winver variables are not available\n");
89c2c66affSColin Finck         return;
90c2c66affSColin Finck     }
91c2c66affSColin Finck     winver = *pp_winver;
92c2c66affSColin Finck     winminor = *pp_winminor;
93c2c66affSColin Finck     winmajor = *pp_winmajor;
94c2c66affSColin Finck     GetVersionExA( &osvi);
95c2c66affSColin Finck     ok( winminor == osvi.dwMinorVersion, "Wrong value for _winminor %02x expected %02x\n",
96c2c66affSColin Finck             winminor, osvi.dwMinorVersion);
97c2c66affSColin Finck     ok( winmajor == osvi.dwMajorVersion, "Wrong value for _winmajor %02x expected %02x\n",
98c2c66affSColin Finck             winmajor, osvi.dwMajorVersion);
99c2c66affSColin Finck     ok( winver == ((osvi.dwMajorVersion << 8) | osvi.dwMinorVersion),
100c2c66affSColin Finck             "Wrong value for _winver %02x expected %02x\n",
101c2c66affSColin Finck             winver, ((osvi.dwMajorVersion << 8) | osvi.dwMinorVersion));
102c2c66affSColin Finck     if( !pp_osver || !pp_osplatform ) {
103c2c66affSColin Finck         win_skip("_osver variables are not available\n");
104c2c66affSColin Finck         return;
105c2c66affSColin Finck     }
106c2c66affSColin Finck     osver = *pp_osver;
107c2c66affSColin Finck     osplatform = *pp_osplatform;
108c2c66affSColin Finck     ok( osver == (osvi.dwBuildNumber & 0xffff) ||
109c2c66affSColin Finck             ((osvi.dwBuildNumber >> 24) == osvi.dwMajorVersion &&
110c2c66affSColin Finck                  ((osvi.dwBuildNumber >> 16) & 0xff) == osvi.dwMinorVersion), /* 95/98/ME */
111c2c66affSColin Finck             "Wrong value for _osver %04x expected %04x\n",
112c2c66affSColin Finck             osver, osvi.dwBuildNumber);
113c2c66affSColin Finck     ok(osplatform == osvi.dwPlatformId,
114c2c66affSColin Finck             "Wrong value for _osplatform %x expected %x\n",
115c2c66affSColin Finck             osplatform, osvi.dwPlatformId);
116c2c66affSColin Finck }
117c2c66affSColin Finck 
test_get_pgmptr(void)118c2c66affSColin Finck static void test_get_pgmptr(void)
119c2c66affSColin Finck {
120c2c66affSColin Finck     char *pgm = NULL;
121c2c66affSColin Finck     int res;
122c2c66affSColin Finck 
123c2c66affSColin Finck     if (!p_get_pgmptr)
124c2c66affSColin Finck         return;
125c2c66affSColin Finck 
126c2c66affSColin Finck     res = p_get_pgmptr(&pgm);
127c2c66affSColin Finck 
128c2c66affSColin Finck     ok( res == 0, "Wrong _get_pgmptr return value %d expected 0\n", res);
129c2c66affSColin Finck     ok( pgm != NULL, "_get_pgmptr returned a NULL pointer\n" );
130c2c66affSColin Finck }
131c2c66affSColin Finck 
test___getmainargs(void)132c2c66affSColin Finck static void test___getmainargs(void)
133c2c66affSColin Finck {
134c2c66affSColin Finck     int argc, new_argc, mode;
135c2c66affSColin Finck     char **argv, **new_argv, **envp;
136c2c66affSColin Finck     char tmppath[MAX_PATH], filepath[MAX_PATH];
137c2c66affSColin Finck     FILE *f;
138c2c66affSColin Finck 
139c2c66affSColin Finck     ok(GetTempPathA(MAX_PATH, tmppath) != 0, "GetTempPath failed\n");
140c2c66affSColin Finck 
141c2c66affSColin Finck     mode = 0;
142c2c66affSColin Finck     __getmainargs(&argc, &argv, &envp, 0, &mode);
143c2c66affSColin Finck     ok(argc == 4, "argc = %d\n", argc);
144c2c66affSColin Finck     ok(!strcmp(argv[1], "data"), "argv[1] = %s\n", argv[1]);
145c2c66affSColin Finck     sprintf(filepath, "%s*\\*", tmppath);
146c2c66affSColin Finck     ok(!strcmp(argv[2], filepath), "argv[2] = %s\n", argv[2]);
147c2c66affSColin Finck     sprintf(filepath, "%swine_test/*", tmppath);
148c2c66affSColin Finck     ok(!strcmp(argv[3], filepath), "argv[3] = %s\n", argv[3]);
149c2c66affSColin Finck     ok(!argv[4], "argv[4] != NULL\n");
150c2c66affSColin Finck 
151c2c66affSColin Finck     if(p___p___argc && p___p___argv) {
152c2c66affSColin Finck         new_argc = *p___p___argc();
153c2c66affSColin Finck         new_argv = *p___p___argv();
154c2c66affSColin Finck         ok(new_argc == 4, "*__p___argc() = %d\n", new_argc);
155c2c66affSColin Finck         ok(new_argv == argv, "*__p___argv() = %p, expected %p\n", new_argv, argv);
156c2c66affSColin Finck     }
157*ce2df64aSAmine Khaldi     else skip("__p___argc or __p___argv is not available\n");
158c2c66affSColin Finck 
159c2c66affSColin Finck     mode = 0;
160c2c66affSColin Finck     __getmainargs(&argc, &argv, &envp, 1, &mode);
161c2c66affSColin Finck     ok(argc == 5, "argc = %d\n", argc);
162c2c66affSColin Finck     ok(!strcmp(argv[1], "data"), "argv[1] = %s\n", argv[1]);
163c2c66affSColin Finck     sprintf(filepath, "%s*\\*", tmppath);
164c2c66affSColin Finck     ok(!strcmp(argv[2], filepath), "argv[2] = %s\n", argv[2]);
165c2c66affSColin Finck     sprintf(filepath, "%swine_test/a", tmppath);
166c2c66affSColin Finck     if(argv[3][strlen(argv[3])-1] == 'a') {
167c2c66affSColin Finck         ok(!strcmp(argv[3], filepath), "argv[3] = %s\n", argv[3]);
168c2c66affSColin Finck         sprintf(filepath, "%swine_test/test", tmppath);
169c2c66affSColin Finck         ok(!strcmp(argv[4], filepath), "argv[4] = %s\n", argv[4]);
170c2c66affSColin Finck     }else {
171c2c66affSColin Finck         ok(!strcmp(argv[4], filepath), "argv[4] = %s\n", argv[4]);
172c2c66affSColin Finck         sprintf(filepath, "%swine_test/test", tmppath);
173c2c66affSColin Finck         ok(!strcmp(argv[3], filepath), "argv[3] = %s\n", argv[3]);
174c2c66affSColin Finck     }
175c2c66affSColin Finck     ok(!argv[5], "argv[5] != NULL\n");
176c2c66affSColin Finck 
177c2c66affSColin Finck     if(p___p___argc && p___p___argv) {
178c2c66affSColin Finck         new_argc = *p___p___argc();
179c2c66affSColin Finck         new_argv = *p___p___argv();
180c2c66affSColin Finck         ok(new_argc == argc, "*__p___argc() = %d, expected %d\n", new_argc, argc);
181c2c66affSColin Finck         ok(new_argv == argv, "*__p___argv() = %p, expected %p\n", new_argv, argv);
182c2c66affSColin Finck     }
183c2c66affSColin Finck 
184c2c66affSColin Finck     sprintf(filepath, "%swine_test/b", tmppath);
185c2c66affSColin Finck     f = fopen(filepath, "w");
186c2c66affSColin Finck     ok(f != NULL, "fopen(%s) failed: %d\n", filepath, errno);
187c2c66affSColin Finck     fclose(f);
188c2c66affSColin Finck     mode = 0;
189c2c66affSColin Finck     __getmainargs(&new_argc, &new_argv, &envp, 1, &mode);
190c2c66affSColin Finck     ok(new_argc == argc+1, "new_argc = %d, expected %d\n", new_argc, argc+1);
191c2c66affSColin Finck     _unlink(filepath);
192c2c66affSColin Finck }
193c2c66affSColin Finck 
test___getmainargs_parent(char * name)194c2c66affSColin Finck static void test___getmainargs_parent(char *name)
195c2c66affSColin Finck {
196c2c66affSColin Finck     char cmdline[3*MAX_PATH];
197c2c66affSColin Finck     char tmppath[MAX_PATH], filepath[MAX_PATH];
198c2c66affSColin Finck     STARTUPINFOA startup;
199c2c66affSColin Finck     PROCESS_INFORMATION proc;
200c2c66affSColin Finck     FILE *f;
201c2c66affSColin Finck     int ret;
202c2c66affSColin Finck 
203c2c66affSColin Finck     ok(GetTempPathA(MAX_PATH, tmppath) != 0, "GetTempPath failed\n");
204c2c66affSColin Finck     sprintf(cmdline, "%s data %s*\\* %swine_test/*", name, tmppath, tmppath);
205c2c66affSColin Finck 
206c2c66affSColin Finck     sprintf(filepath, "%swine_test", tmppath);
207c2c66affSColin Finck     ret = _mkdir(filepath);
208c2c66affSColin Finck     ok(!ret, "_mkdir failed: %d\n", errno);
209c2c66affSColin Finck     sprintf(filepath, "%swine_test\\a", tmppath);
210c2c66affSColin Finck     f = fopen(filepath, "w");
211c2c66affSColin Finck     ok(f != NULL, "fopen(%s) failed: %d\n", filepath, errno);
212c2c66affSColin Finck     fclose(f);
213c2c66affSColin Finck     sprintf(filepath, "%swine_test\\test", tmppath);
214c2c66affSColin Finck     f = fopen(filepath, "w");
215c2c66affSColin Finck     ok(f != NULL, "fopen(%s) failed: %d\n", filepath, errno);
216c2c66affSColin Finck     fclose(f);
217c2c66affSColin Finck 
218c2c66affSColin Finck     memset(&startup, 0, sizeof(startup));
219c2c66affSColin Finck     startup.cb = sizeof(startup);
220c2c66affSColin Finck     CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE|NORMAL_PRIORITY_CLASS, NULL, NULL, &startup, &proc);
221c2c66affSColin Finck     winetest_wait_child_process(proc.hProcess);
222c2c66affSColin Finck 
223c2c66affSColin Finck     _unlink(filepath);
224c2c66affSColin Finck     sprintf(filepath, "%swine_test\\a", tmppath);
225c2c66affSColin Finck     _unlink(filepath);
226c2c66affSColin Finck     sprintf(filepath, "%swine_test", tmppath);
227c2c66affSColin Finck     _rmdir(filepath);
228c2c66affSColin Finck }
229c2c66affSColin Finck 
START_TEST(data)230c2c66affSColin Finck START_TEST(data)
231c2c66affSColin Finck {
232c2c66affSColin Finck     HMODULE hmsvcrt;
233c2c66affSColin Finck     int arg_c;
234c2c66affSColin Finck     char** arg_v;
235c2c66affSColin Finck 
236c2c66affSColin Finck     hmsvcrt = GetModuleHandleA("msvcrt.dll");
237c2c66affSColin Finck     if (!hmsvcrt)
238c2c66affSColin Finck         hmsvcrt = GetModuleHandleA("msvcrtd.dll");
239c2c66affSColin Finck     if (hmsvcrt)
240c2c66affSColin Finck     {
241c2c66affSColin Finck         p_initterm=(void*)GetProcAddress(hmsvcrt, "_initterm");
242c2c66affSColin Finck         p_get_pgmptr=(void*)GetProcAddress(hmsvcrt, "_get_pgmptr");
243c2c66affSColin Finck         p___p___argc=(void*)GetProcAddress(hmsvcrt, "__p___argc");
244c2c66affSColin Finck         p___p___argv=(void*)GetProcAddress(hmsvcrt, "__p___argv");
245c2c66affSColin Finck     }
246c2c66affSColin Finck 
247c2c66affSColin Finck     arg_c = winetest_get_mainargs(&arg_v);
248c2c66affSColin Finck     if(arg_c >= 3) {
249c2c66affSColin Finck         test___getmainargs();
250c2c66affSColin Finck         return;
251c2c66affSColin Finck     }
252c2c66affSColin Finck 
253c2c66affSColin Finck     test_initterm();
254c2c66affSColin Finck     test_initvar(hmsvcrt);
255c2c66affSColin Finck     test_get_pgmptr();
256c2c66affSColin Finck     test___getmainargs_parent(arg_v[0]);
257c2c66affSColin Finck }
258