1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
4  * PURPOSE:         Tests for PathUnExpandEnvStringsForUser
5  * PROGRAMMERS:     Katayama Hirofumi MZ
6  */
7 
8 #include <apitest.h>
9 #include <shlwapi.h>
10 #include <strsafe.h>
11 
12 #define DO_TEST(Res, hToken, TestStr, ExpStr, Len) \
13 do { \
14     BOOL ret = PathUnExpandEnvStringsForUserW((hToken), (TestStr), OutStr, Len); \
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 // PathUnExpandEnvStringsForUserW
23 typedef BOOL (WINAPI *PATHUNEXPANDENVSTRINGSFORUSERW)(HANDLE hToken, LPCWSTR pszPath, LPWSTR pszUnExpanded, INT cchUnExpanded);
24 PATHUNEXPANDENVSTRINGSFORUSERW pPathUnExpandEnvStringsForUserW = NULL;
25 
26 START_TEST(PathUnExpandEnvStringsForUser)
27 {
28     DWORD ret;
29     WCHAR OutStr[MAX_PATH], TestStr[MAX_PATH];
30     HINSTANCE hShlwapi;
31 
32     hShlwapi = GetModuleHandleW(L"shlwapi");
33     if (hShlwapi == NULL)
34     {
35         skip("shlwapi.dll was not loaded\n");
36         return;
37     }
38 
39     pPathUnExpandEnvStringsForUserW =
40         (PATHUNEXPANDENVSTRINGSFORUSERW)GetProcAddress(hShlwapi, "PathUnExpandEnvStringsForUserW");
41 
42     if (pPathUnExpandEnvStringsForUserW == NULL)
43     {
44         trace("PathUnExpandEnvStringsForUserW is not public\n");
45         pPathUnExpandEnvStringsForUserW =
46             (PATHUNEXPANDENVSTRINGSFORUSERW)GetProcAddress(hShlwapi, (LPCSTR)(LONG_PTR)466);
47     }
48     if (pPathUnExpandEnvStringsForUserW == NULL)
49     {
50         skip("PathUnExpandEnvStringsForUserW was not found\n");
51         return;
52     }
53 
54 #define PathUnExpandEnvStringsForUserW  (*pPathUnExpandEnvStringsForUserW)
55 
56     /* empty string */
57     DO_TEST(FALSE, NULL, L"", L"", 0);
58     DO_TEST(FALSE, NULL, L"", L"", -1);
59     DO_TEST(FALSE, NULL, L"", L"", 2);
60     DO_TEST(FALSE, NULL, L"", L"", MAX_PATH);
61 
62     /* No unexpansion possible */
63     DO_TEST(FALSE, NULL, L"ZZ:\\foobar\\directory", L"", 0);
64     DO_TEST(FALSE, NULL, L"ZZ:\\foobar\\directory", L"", -1);
65     DO_TEST(FALSE, NULL, L"ZZ:\\foobar\\directory", L"", 2);
66     DO_TEST(FALSE, NULL, L"ZZ:\\foobar\\directory", L"", MAX_PATH);
67 
68     /* %APPDATA% */
69     ret = GetEnvironmentVariableW(L"APPDATA", TestStr, _countof(TestStr));
70     ok(ret, "got %lu\n", ret);
71     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%", 0);
72     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%", -1);
73     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%", 2);
74     DO_TEST(TRUE, NULL, TestStr, L"%APPDATA%", MAX_PATH);
75     StringCbCatW(TestStr, sizeof(TestStr), L"\\TEST");
76     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%\\TEST", 0);
77     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%\\TEST", -1);
78     DO_TEST(FALSE, NULL, TestStr, L"%APPDATA%\\TEST", 2);
79     DO_TEST(TRUE, NULL, TestStr, L"%APPDATA%\\TEST", MAX_PATH);
80 
81     /* %USERPROFILE% */
82     ret = GetEnvironmentVariableW(L"USERPROFILE", TestStr, _countof(TestStr));
83     ok(ret, "got %lu\n", ret);
84     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%", 0);
85     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%", -1);
86     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%", 2);
87     DO_TEST(TRUE, NULL, TestStr, L"%USERPROFILE%", MAX_PATH);
88     StringCbCatW(TestStr, sizeof(TestStr), L"\\TEST");
89     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%\\TEST", 0);
90     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%\\TEST", -1);
91     DO_TEST(FALSE, NULL, TestStr, L"%USERPROFILE%\\TEST", 2);
92     DO_TEST(TRUE, NULL, TestStr, L"%USERPROFILE%\\TEST", MAX_PATH);
93 
94     /* %ALLUSERSPROFILE% */
95     ret = GetEnvironmentVariableW(L"ALLUSERSPROFILE", TestStr, _countof(TestStr));
96     ok(ret, "got %lu\n", ret);
97     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%", 0);
98     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%", -1);
99     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%", 2);
100     DO_TEST(TRUE, NULL, TestStr, L"%ALLUSERSPROFILE%", MAX_PATH);
101     StringCbCatW(TestStr, sizeof(TestStr), L"\\TEST");
102     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%\\TEST", 0);
103     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%\\TEST", -1);
104     DO_TEST(FALSE, NULL, TestStr, L"%ALLUSERSPROFILE%\\TEST", 2);
105     DO_TEST(TRUE, NULL, TestStr, L"%ALLUSERSPROFILE%\\TEST", MAX_PATH);
106 
107     /* %ProgramFiles% */
108     ret = GetEnvironmentVariableW(L"ProgramFiles", TestStr, _countof(TestStr));
109     ok(ret, "got %lu\n", ret);
110     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%", 0);
111     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%", -1);
112     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%", 2);
113     DO_TEST(TRUE, NULL, TestStr, L"%ProgramFiles%", MAX_PATH);
114     StringCbCatW(TestStr, sizeof(TestStr), L"\\TEST");
115     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%\\TEST", 0);
116     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%\\TEST", -1);
117     DO_TEST(FALSE, NULL, TestStr, L"%ProgramFiles%\\TEST", 2);
118     DO_TEST(TRUE, NULL, TestStr, L"%ProgramFiles%\\TEST", MAX_PATH);
119 }
120