1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for __getmainargs and __wgetmainargs
5  * PROGRAMMER:      Yaroslav Veremenko <yaroslav@veremenko.info>
6  */
7 
8 #include <apitest.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 
13 const char **__p__acmdln(void);
14 void __getmainargs(int* argc, char*** argv, char*** env, int expand_wildcards, int* new_mode);
15 const wchar_t **__p__wcmdln(void);
16 void __wgetmainargs(int* argc, wchar_t*** wargv, wchar_t*** wenv, int expand_wildcards, int* new_mode);
17 
18 
19 #define winetest_ok_str(x, y) \
20     winetest_ok(strcmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", y, x)
21 #define winetest_ok_wstr(x, y) \
22     winetest_ok(wcscmp(x, y) == 0, "Wrong string. Expected '%s', got '%s'\n", wine_dbgstr_w(y), wine_dbgstr_w(x))
23 #define ok_argsA  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsA_imp
24 #define ok_argsW  (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : ok_argsW_imp
25 
26 
27 void
ok_argsA_imp(const char * input_args,const char * arg1,const char * arg2,const char * arg3)28 ok_argsA_imp(const char* input_args, const char* arg1, const char* arg2, const char* arg3)
29 {
30     int argc = 0, mode = 0;
31     int expect_count = arg3 == NULL ? (arg2 == NULL ? 2 : 3) : 4;
32     char** argv, **env;
33 
34     /* Remove cached argv, setup our input as program argument. */
35     *__p___argv() = NULL;
36     *__p__acmdln() = input_args;
37 
38     /* Process the commandline stored in _acmdln */
39     __getmainargs(&argc, &argv, &env, 0, &mode);
40 
41     winetest_ok(argc == expect_count, "Wrong value for argc, expected: %d, got: %d\n", expect_count, argc);
42     if(argc != expect_count)
43         return;
44 
45     winetest_ok_str(argv[0], "test.exe");
46     winetest_ok_str(argv[1], arg1);
47     if (expect_count > 2)
48     {
49         winetest_ok_str(argv[2], arg2);
50         if (expect_count > 3)
51             winetest_ok_str(argv[3], arg3);
52     }
53 }
54 
55 void
ok_argsW_imp(const wchar_t * input_args,const wchar_t * arg1,const wchar_t * arg2,const wchar_t * arg3)56 ok_argsW_imp(const wchar_t* input_args, const wchar_t* arg1, const wchar_t* arg2, const wchar_t* arg3)
57 {
58     int argc = 0, mode = 0;
59     int expect_count = arg3 == NULL ? (arg2 == NULL ? 2 : 3) : 4;
60     wchar_t** argv, **env;
61 
62     /* Remove cached wargv, setup our input as program argument. */
63     *__p___wargv() = NULL;
64     *__p__wcmdln() = input_args;
65 
66     /* Process the commandline stored in _wcmdln */
67     __wgetmainargs(&argc, &argv, &env, 0, &mode);
68 
69     winetest_ok(argc == expect_count, "Wrong value for argc, expected: %d, got: %d\n", expect_count, argc);
70     if(argc != expect_count)
71         return;
72 
73     winetest_ok_wstr(argv[0], L"test.exe");
74     winetest_ok_wstr(argv[1], arg1);
75     if (expect_count > 2)
76     {
77         winetest_ok_wstr(argv[2], arg2);
78         if (expect_count > 3)
79             winetest_ok_wstr(argv[3], arg3);
80     }
81 }
82 
START_TEST(__getmainargs)83 START_TEST(__getmainargs)
84 {
85     ok_argsA("test.exe \"a b c\" d e", "a b c", "d", "e");
86     ok_argsA("test.exe \"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
87     ok_argsA("test.exe a\\\\\\b d\"e f\"g h", "a\\\\\\b", "de fg", "h");
88     ok_argsA("test.exe a\\\\\\\"b c d", "a\\\"b", "c", "d");
89     ok_argsA("test.exe a\\\\\\\\\"b c\" d e", "a\\\\b c", "d", "e");
90     ok_argsA("test.exe a b \"\"", "a", "b", "");
91     ok_argsA("test.exe a \"\" b", "a", "", "b");
92     ok_argsA("test.exe a \"b\"\" c", "a", "b\"", "c");
93     ok_argsA("test.exe a \"b\\\"\" c", "a", "b\"", "c");
94     ok_argsA("test.exe a  \"  b\\  \"\"  c", "a", "  b\\  \"", "c");
95     ok_argsA("test.exe a  \"b\\  \"\"\"  c\" d", "a", "b\\  \"  c", "d");
96     ok_argsA("test.exe a  \"b\\  \"\"\"  \"c  \"\"\"\" d", "a", "b\\  \"  c", "\" d");
97     ok_argsA("test.exe a b c  ", "a", "b", "c");
98     ok_argsA("test.exe \"a b c\"", "a b c", NULL, NULL);
99 
100     ok_argsW(L"test.exe \"a b c\" d e", L"a b c", L"d", L"e");
101     ok_argsW(L"test.exe \"ab\\\"c\" \"\\\\\" d", L"ab\"c", L"\\", L"d");
102     ok_argsW(L"test.exe a\\\\\\b d\"e f\"g h", L"a\\\\\\b", L"de fg", L"h");
103     ok_argsW(L"test.exe a\\\\\\\"b c d", L"a\\\"b", L"c", L"d");
104     ok_argsW(L"test.exe a\\\\\\\\\"b c\" d e", L"a\\\\b c", L"d", L"e");
105     ok_argsW(L"test.exe a b \"\"", L"a", L"b", L"");
106     ok_argsW(L"test.exe a \"\" b", L"a", L"", L"b");
107     ok_argsW(L"test.exe a \"b\"\" c", L"a", L"b\"", L"c");
108     ok_argsW(L"test.exe a \"b\\\"\" c", L"a", L"b\"", L"c");
109     ok_argsW(L"test.exe a  \"  b\\  \"\"  c", L"a", L"  b\\  \"", L"c");
110     ok_argsW(L"test.exe a  \"b\\  \"\"\"  c\" d", L"a", L"b\\  \"  c", L"d");
111     ok_argsW(L"test.exe a  \"b\\  \"\"\"  \"c  \"\"\"\" d", L"a", L"b\\  \"  c", L"\" d");
112     ok_argsW(L"test.exe a b c  ", L"a", L"b", L"c");
113     ok_argsW(L"test.exe \"a b c\"", L"a b c", NULL, NULL);
114 }
115