1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * PROJECT:         ReactOS api tests
 * LICENSE:         GPLv2+ - See COPYING in the top level directory
 * PURPOSE:         Test for ACListISF objects
 * PROGRAMMER:      Mark Jansen
 */

#define _UNICODE
#define UNICODE
#include <apitest.h>
#include <shlobj.h>
#include <atlbase.h>
#include <tchar.h>      //
#include <atlcom.h>     // These 3 includes only exist here to make gcc happy about (unused) templates..
#include <atlwin.h>     //

// Yes, gcc at it again, let's validate everything found inside unused templates!
ULONG DbgPrint(PCH Format,...);

#include <stdio.h>
#include <shellutils.h>
#include <shlwapi.h>
#include <strsafe.h>

static bool g_ShowHidden;
static DWORD g_WinVersion;
#define WINVER_VISTA   0x0600


#define ok_hr(status, expected)     ok_hex(status, expected)

// We do not want our results to originate from the helper functions, so have them originate from the calls to them
#define test_at_end                 (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_at_end_imp
#define test_ExpectDrives           (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_ExpectDrives_imp
#define test_ExpectFolders          (winetest_set_location(__FILE__, __LINE__), 0) ? (void)0 : test_ExpectFolders_imp
#define winetest_ok_hr(expression, expected) \
    do { \
        int _value = (expression); \
        winetest_ok(_value == (expected), "Wrong value for '%s', expected: " #expected " (0x%x), got: 0x%x\n", \
           #expression, (int)(expected), _value); \
    } while (0)




static void test_at_end_imp(CComPtr<IEnumString>& EnumStr)
{
    CComHeapPtr<OLECHAR> Result;
    ULONG Fetched = 12345;
    HRESULT hr = EnumStr->Next(1, &Result, &Fetched);
    winetest_ok(hr == S_FALSE, "Expected hr to be S_FALSE, was 0x%lx\n", hr);
    winetest_ok(Fetched == 0u, "Expected Fetched to be 0, was: %lu\n", Fetched);
    if (Fetched == 1u)
        winetest_ok(0, "Expected there not to be a result, got: %s\n", wine_dbgstr_w(Result));
}

static bool GetDisplayname(CComPtr<IShellFolder>& spDrives, CComHeapPtr<ITEMIDLIST>& pidl, CComHeapPtr<WCHAR>& DisplayName)
{
    STRRET StrRet;
    HRESULT hr;
    winetest_ok_hr(hr = spDrives->GetDisplayNameOf(pidl, SHGDN_INFOLDER | SHGDN_FORPARSING | SHGDN_FORADDRESSBAR, &StrRet), S_OK);
    if (!SUCCEEDED(hr))
        return false;

    winetest_ok_hr(hr = StrRetToStrW(&StrRet, NULL, &DisplayName), S_OK);
    if (!SUCCEEDED(hr))
        return false;
    return true;
}

enum ExpectOptions
{
    None = 0,
    IgnoreRoot = 1,
    CheckLast = 2,
    IgnoreHidden = 4,
    IgnoreFiles = 8,
};

// wtf c++
ExpectOptions operator | (const ExpectOptions& left, const ExpectOptions& right)
{
    return static_cast<ExpectOptions>(static_cast<int>(left) | static_cast<int>(right));
}


static void
test_ExpectFolders_imp(CComPtr<IEnumString>& EnumStr, LPITEMIDLIST pidlTarget, const WCHAR* Root, ExpectOptions options)
{
    CComPtr<IShellFolder> spDesktop;
    HRESULT hr = SHGetDesktopFolder(&spDesktop);

    CComPtr<IShellFolder> spTarget;
    if (pidlTarget)
    {
        winetest_ok_hr(hr = spDesktop->BindToObject(pidlTarget, NULL, IID_PPV_ARG(IShellFolder, &spTarget)), S_OK);
        if (!SUCCEEDED(hr))
            return;
    }
    else
    {
        spTarget = spDesktop;
    }

    SHCONTF EnumFlags = SHCONTF_FOLDERS | SHCONTF_INIT_ON_FIRST_NEXT;
    if (g_ShowHidden && !(options & IgnoreHidden))
        EnumFlags |= SHCONTF_INCLUDEHIDDEN;
    if (!(options & IgnoreFiles))
        EnumFlags |= SHCONTF_NONFOLDERS;

    CComPtr<IEnumIDList> spEnumIDList;
    winetest_ok_hr(hr = spTarget->EnumObjects(NULL, EnumFlags, &spEnumIDList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    WCHAR Buffer[512];
    CComHeapPtr<ITEMIDLIST> pidl;
    INT Count = 0;
    while (spEnumIDList->Next(1, &pidl, NULL) == S_OK)
    {
        CComHeapPtr<WCHAR> DisplayName;
        if (!GetDisplayname(spTarget, pidl, DisplayName))
            break;

        CComHeapPtr<OLECHAR> Result;
        ULONG Fetched;
        hr = EnumStr->Next(1, &Result, &Fetched);
        winetest_ok_hr(hr, S_OK);


        if (hr != S_OK)
            break;


        StringCchPrintfW(Buffer, _ARRAYSIZE(Buffer), L"%s%s", (options & IgnoreRoot) ? L"" : Root, (WCHAR*)DisplayName);

        winetest_ok(!wcscmp(Buffer, Result), "Expected %s, got %s\n", wine_dbgstr_w(Buffer), wine_dbgstr_w(Result));

        pidl.Free();
        Count++;
    }
    if (options & CheckLast)
    {
        test_at_end_imp(EnumStr);
    }
}

static void
test_ExpectDrives_imp(CComPtr<IEnumString>& EnumStr, CComHeapPtr<ITEMIDLIST>& pidlTarget)<--- Parameter 'pidlTarget' can be declared with const
{
    test_ExpectFolders_imp(EnumStr, pidlTarget, NULL, IgnoreRoot | CheckLast);
}

static void
test_ACListISF_NONE()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_NONE), S_OK);
    test_at_end(EnumStr);


    WCHAR Buffer[MAX_PATH];
    GetSystemWindowsDirectoryW(Buffer, _ARRAYSIZE(Buffer));
    Buffer[3] = '\0';

    CComHeapPtr<ITEMIDLIST> pidlDiskRoot;
    ok_hr(hr = SHParseDisplayName(Buffer, NULL, &pidlDiskRoot, NULL, NULL), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, CheckLast | IgnoreHidden);

    ok_hr(hr = EnumStr->Reset(), S_OK);
    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    ok_hr(hr = ACList->SetOptions(ACLO_NONE), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, CheckLast);
}

static void
test_ACListISF_CURRENTDIR()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<ICurrentWorkingDirectory> CurrentWorkingDir;
    ok_hr(hr = EnumStr->QueryInterface(IID_ICurrentWorkingDirectory, (void**)&CurrentWorkingDir), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_CURRENTDIR), S_OK);
    test_at_end(EnumStr);


    WCHAR Buffer[MAX_PATH] = { 1, 1, 1, 1, 1, 0 }, Buffer2[MAX_PATH];
    if (g_WinVersion < WINVER_VISTA)
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), E_NOTIMPL);
    else
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), E_UNEXPECTED);
    ok(!wcscmp(L"\x1\x1\x1\x1\x1", Buffer), "Expected %s, got %s\n", wine_dbgstr_w(L"\x1\x1\x1\x1\x1"), wine_dbgstr_w(Buffer));

    GetSystemWindowsDirectoryW(Buffer2, _ARRAYSIZE(Buffer2));
    // Windows 2k3 does not parse it without the trailing '\\'
    Buffer2[3] = '\0';
    CComHeapPtr<ITEMIDLIST> pidlDiskRoot;
    ok_hr(hr = SHParseDisplayName(Buffer2, NULL, &pidlDiskRoot, NULL, NULL), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = CurrentWorkingDir->SetDirectory(Buffer2), S_OK);
    test_at_end(EnumStr);

    Buffer[0] = '\0';
    if (g_WinVersion < WINVER_VISTA)
    {
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), E_NOTIMPL);
    }
    else
    {
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), S_OK);
        ok(!wcscmp(Buffer2, Buffer), "Expected %s, got %s\n", wine_dbgstr_w(Buffer2), wine_dbgstr_w(Buffer));
    }

    Buffer2[2] = '\0';
    ok_hr(hr = CurrentWorkingDir->SetDirectory(Buffer2), S_OK);
    test_at_end(EnumStr);

    Buffer[0] = '\0';
    Buffer2[2] = '\\';
    if (g_WinVersion < WINVER_VISTA)
    {
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), E_NOTIMPL);
    }
    else
    {
        ok_hr(hr = CurrentWorkingDir->GetDirectory(Buffer, _ARRAYSIZE(Buffer)), S_OK);
        ok(!wcscmp(Buffer2, Buffer), "Expected %s, got %s\n", wine_dbgstr_w(Buffer2), wine_dbgstr_w(Buffer));
    }

    ok_hr(hr = ACList->Expand(Buffer2), S_OK);
    // The first set of results are absolute paths, without hidden files?!
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer2, IgnoreHidden);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer2, IgnoreHidden | IgnoreRoot | CheckLast);
}

static void
test_ACListISF_MYCOMPUTER()
{
    CComPtr<IACList2> ACList;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IACList2, &ACList));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    // Check the default
    DWORD CurrentOption = 0xdeadbeef;
    ok_hr(ACList->GetOptions(&CurrentOption), S_OK);
    ok(CurrentOption == (ACLO_CURRENTDIR|ACLO_MYCOMPUTER), "Expected the default to be %x, was %lx\n",
        (ACLO_CURRENTDIR|ACLO_MYCOMPUTER), CurrentOption);


    CComPtr<IEnumString> EnumStr;
    ok_hr(hr = ACList->QueryInterface(IID_IEnumString, (void**)&EnumStr), S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IPersistFolder> PersistFolder;
    ok_hr(hr = EnumStr->QueryInterface(IID_IPersistFolder, (void**)&PersistFolder), S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComHeapPtr<ITEMIDLIST> pidlMyComputer;
    ok_hr(hr = SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidlMyComputer), S_OK);
    if (!SUCCEEDED(hr))
        return;


    hr = EnumStr->Reset();
    if (g_WinVersion < WINVER_VISTA)
        ok_hr(hr, S_FALSE);
    else
        ok_hr(hr, S_OK);
    test_ExpectDrives(EnumStr, pidlMyComputer);

    ok_hr(hr = ACList->SetOptions(ACLO_MYCOMPUTER), S_OK);
    ok_hr(EnumStr->Reset(), S_OK);
    test_ExpectDrives(EnumStr, pidlMyComputer);

    WCHAR Buffer[MAX_PATH];
    GetSystemWindowsDirectoryW(Buffer, _ARRAYSIZE(Buffer));
    // Windows 2k3 does not parse it without the trailing '\\'
    Buffer[3] = '\0';
    CComHeapPtr<ITEMIDLIST> pidlDiskRoot;
    ok_hr(hr = SHParseDisplayName(Buffer, NULL, &pidlDiskRoot, NULL, NULL), S_OK);
    if (!SUCCEEDED(hr))
        return;
    Buffer[2] = '\0';

    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, None);
    test_ExpectDrives(EnumStr, pidlMyComputer);

    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    ok_hr(EnumStr->Reset(), S_OK);
    // Pre vista does not remove the expanded data from the enumeration, it changes it to relative paths???
    if (g_WinVersion < WINVER_VISTA)
        test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, IgnoreRoot);
    test_ExpectDrives(EnumStr, pidlMyComputer);

    ok_hr(EnumStr->Reset(), S_OK);
    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, None);
    test_ExpectDrives(EnumStr, pidlMyComputer);
}

static void
test_ACListISF_DESKTOP()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_DESKTOP), S_OK);
    test_ExpectFolders(EnumStr, NULL, NULL, IgnoreRoot | CheckLast | IgnoreHidden);
}

static void
test_ACListISF_FAVORITES()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComHeapPtr<ITEMIDLIST> pidlFavorites;
    ok_hr(hr = SHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES, &pidlFavorites), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_FAVORITES), S_OK);
    test_ExpectFolders(EnumStr, pidlFavorites, NULL, IgnoreRoot | CheckLast | IgnoreHidden);
}

static void
test_ACListISF_FILESYSONLY()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    WCHAR Buffer[MAX_PATH];
    GetSystemWindowsDirectoryW(Buffer, _ARRAYSIZE(Buffer));
    // Windows 2k3 does not parse it without the trailing '\\'
    Buffer[3] = '\0';
    CComHeapPtr<ITEMIDLIST> pidlDiskRoot;
    ok_hr(hr = SHParseDisplayName(Buffer, NULL, &pidlDiskRoot, NULL, NULL), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_FILESYSONLY), S_OK);
    test_at_end(EnumStr);

    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, CheckLast | IgnoreHidden);
}

static void
test_ACListISF_FILESYSDIRS()
{
    CComPtr<IEnumString> EnumStr;
    HRESULT hr = CoCreateInstance(CLSID_ACListISF, NULL, CLSCTX_ALL, IID_PPV_ARG(IEnumString, &EnumStr));
    ok_hr(hr, S_OK);
    if (!SUCCEEDED(hr))
        return;

    CComPtr<IACList2> ACList;
    ok_hr(hr = EnumStr->QueryInterface(IID_IACList2, (void**)&ACList), S_OK);
    if (!SUCCEEDED(hr))
        return;

    WCHAR Buffer[MAX_PATH];
    GetSystemWindowsDirectoryW(Buffer, _ARRAYSIZE(Buffer));
    // Windows 2k3 does not parse it without the trailing '\\'
    Buffer[3] = '\0';
    CComHeapPtr<ITEMIDLIST> pidlDiskRoot;
    ok_hr(hr = SHParseDisplayName(Buffer, NULL, &pidlDiskRoot, NULL, NULL), S_OK);
    if (!SUCCEEDED(hr))
        return;

    ok_hr(hr = ACList->SetOptions(ACLO_FILESYSDIRS), S_OK);
    test_at_end(EnumStr);

    ok_hr(hr = ACList->Expand(Buffer), S_OK);
    test_ExpectFolders(EnumStr, pidlDiskRoot, Buffer, CheckLast | IgnoreFiles | IgnoreHidden);
}

static void GetEnvStatus()
{
    RTL_OSVERSIONINFOEXW rtlinfo = {0};
    void (__stdcall* pRtlGetVersion)(RTL_OSVERSIONINFOEXW*);
    pRtlGetVersion = (void (__stdcall*)(RTL_OSVERSIONINFOEXW*))GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");

    rtlinfo.dwOSVersionInfoSize = sizeof(rtlinfo);
    pRtlGetVersion(&rtlinfo);
    g_WinVersion = (rtlinfo.dwMajorVersion << 8) | rtlinfo.dwMinorVersion;

    SHELLFLAGSTATE sfs = {0};
    SHGetSettings(&sfs, SSF_SHOWALLOBJECTS);
    g_ShowHidden = !!sfs.fShowAllObjects;
    trace("Show hidden folders: %s\n", g_ShowHidden ? "yes" : "no");
}

struct CCoInit
{
    CCoInit() { hres = CoInitialize(NULL); }
    ~CCoInit() { if (SUCCEEDED(hres)) { CoUninitialize(); } }
    HRESULT hres;
};

START_TEST(ACListISF)
{
    GetEnvStatus();
    CCoInit init;
    ok_hr(init.hres, S_OK);
    if (!SUCCEEDED(init.hres))
        return;

    test_ACListISF_NONE();
    test_ACListISF_CURRENTDIR();
    test_ACListISF_MYCOMPUTER();
    test_ACListISF_DESKTOP();
    test_ACListISF_FAVORITES();
    test_ACListISF_FILESYSONLY();
    test_ACListISF_FILESYSDIRS();
}