1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Tests for PathUnExpandEnvStrings
5  * PROGRAMMERS:     Hermes Belusca-Maito
6  */
7 
8 #include <apitest.h>
9 #include <shlwapi.h>
10 #include <strsafe.h>
11 
12 #define DO_TEST(Res, TestStr, ExpStr) \
13 do { \
14     BOOL ret = PathUnExpandEnvStringsW((TestStr), OutStr, _countof(OutStr)); \
15     ok(ret == (Res), "Tested %s, expected returned value %d, got %d\n", \
16        wine_dbgstr_w((TestStr)), (Res), ret); \
17     if (ret) \
18         ok(_wcsicmp(OutStr, (ExpStr)) == 0, "Tested %s, expected %s, got %s\n", \
19            wine_dbgstr_w((TestStr)), wine_dbgstr_w((ExpStr)), wine_dbgstr_w(OutStr)); \
20 } while (0)
21 
22 START_TEST(PathUnExpandEnvStrings)
23 {
24     INT len;
25     DWORD ret;
26     WCHAR TestStr[MAX_PATH];
27     WCHAR ExpStr[MAX_PATH];
28     WCHAR OutStr[MAX_PATH];
29 
30     /*
31      * We expect here that the following standard environment variables:
32      * %COMPUTERNAME%, %ProgramFiles%, %SystemRoot% and %SystemDrive%
33      * are correctly defined.
34      */
35 
36     /* No unexpansion possible */
37     DO_TEST(FALSE, L"ZZ:\\foobar\\directory", L"");
38 
39     /* Contrary to what MSDN says, %COMPUTERNAME% does not seeem to be unexpanded... */
40     ret = GetEnvironmentVariableW(L"COMPUTERNAME", TestStr, _countof(TestStr));
41     ok(ret, "got %lu\n", ret);
42     DO_TEST(FALSE, TestStr, L"%COMPUTERNAME%");
43 #if 0
44     StringCchCopyW(TestStr, _countof(TestStr), L"ZZ:\\foobar\\");
45     len = wcslen(TestStr);
46     ret = GetEnvironmentVariableW(L"COMPUTERNAME", TestStr + len, _countof(TestStr) - len);
47     ok(ret, "got %lu\n", ret);
48     StringCchCatW(TestStr, _countof(TestStr), L"\\directory");
49     DO_TEST(TRUE, TestStr, L"ZZ:\\foobar\\%COMPUTERNAME%\\directory");
50 #endif
51 
52     /*
53      * L"%SystemRoot%\\%SystemRoot%" to L"%SystemRoot%\\%SystemRoot%" (no expansion)
54      * Unexpansion fails.
55      * This shows that given a path string, if PathUnExpandEnvStrings fails,
56      * the string could have been already unexpanded...
57      */
58     DO_TEST(FALSE, L"%SystemRoot%\\%SystemRoot%", L"%SystemRoot%\\%SystemRoot%");
59 
60     /*
61      * L"<real_SystemRoot><real_SystemRoot>" to L"%SystemRoot%<real_SystemRoot>"
62      * example: L"C:\\WindowsC:\\Windows"
63      * Unexpansion succeeds only on the first path.
64      */
65     ret = GetEnvironmentVariableW(L"SystemRoot", TestStr, _countof(TestStr));
66     ok(ret, "got %lu\n", ret);
67     len = wcslen(TestStr);
68     ret = GetEnvironmentVariableW(L"SystemRoot", TestStr + len, _countof(TestStr) - len);
69     ok(ret, "got %lu\n", ret);
70 
71     StringCchCopyW(ExpStr, _countof(ExpStr), L"%SystemRoot%");
72     len = wcslen(ExpStr);
73     ret = GetEnvironmentVariableW(L"SystemRoot", ExpStr + len, _countof(ExpStr) - len);
74     ok(ret, "got %lu\n", ret);
75     DO_TEST(TRUE, TestStr, ExpStr);
76 
77     /*
78      * L"%SystemRoot%\\<real_Program_Files>" to L"%SystemRoot%\\%ProgramFiles%"
79      * Unexpansion fails.
80      */
81     StringCchCopyW(TestStr, _countof(TestStr), L"%SystemRoot%\\");
82     len = wcslen(TestStr);
83     ret = GetEnvironmentVariableW(L"ProgramFiles", TestStr + len, _countof(TestStr) - len);
84     ok(ret, "got %lu\n", ret);
85     DO_TEST(FALSE, TestStr, L"%SystemRoot%\\%ProgramFiles%");
86 
87     /*
88      * L"<real_SystemRoot>\\%ProgramFiles%" to L"%SystemRoot%\\%ProgramFiles%"
89      * Unexpansion succeeds.
90      */
91     ret = GetEnvironmentVariableW(L"SystemRoot", TestStr, _countof(TestStr));
92     ok(ret, "got %lu\n", ret);
93     StringCchCatW(TestStr, _countof(TestStr), L"\\%ProgramFiles%");
94     DO_TEST(TRUE, TestStr, L"%SystemRoot%\\%ProgramFiles%");
95 
96     /*
97      * L"<real_SystemRoot>\\notepad.exe <real_SystemRoot>\\file.txt" to L"%SystemRoot%\\notepad.exe %SystemRoot%\\file.txt"
98      * Unexpansion succeeds only on the first path, therefore the obtained string is not the one naively expected.
99      */
100     ret = GetEnvironmentVariableW(L"SystemRoot", TestStr, _countof(TestStr));
101     ok(ret, "got %lu\n", ret);
102     StringCchCatW(TestStr, _countof(TestStr), L"\\notepad.exe ");
103     len = wcslen(TestStr);
104     ret = GetEnvironmentVariableW(L"SystemRoot", TestStr + len, _countof(TestStr) - len);
105     ok(ret, "got %lu\n", ret);
106     StringCchCatW(TestStr, _countof(TestStr), L"\\file.txt");
107     // DO_TEST(TRUE, TestStr, L"%SystemRoot%\\notepad.exe %SystemRoot%\\file.txt");
108 
109     /*
110      * L"<real_SystemRoot>\\notepad.exe <real_SystemRoot>\\file.txt" to L"%SystemRoot%\\notepad.exe <real_SystemRoot>\\file.txt"
111      * Unexpansion succeeds only on the first path.
112      */
113     StringCchCopyW(ExpStr, _countof(ExpStr), L"%SystemRoot%\\notepad.exe ");
114     len = wcslen(ExpStr);
115     ret = GetEnvironmentVariableW(L"SystemRoot", ExpStr + len, _countof(ExpStr) - len);
116     ok(ret, "got %lu\n", ret);
117     StringCchCatW(ExpStr, _countof(ExpStr), L"\\file.txt");
118     DO_TEST(TRUE, TestStr, ExpStr);
119 }
120