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
/*
 * PROJECT:     ReactOS api tests
 * LICENSE:     LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
 * PURPOSE:     Test for SHChangeNotify
 * COPYRIGHT:   Copyright 2020 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
 */

#include "shelltest.h"
#include <shlwapi.h>
#include <stdio.h>

#define WM_SHELL_NOTIFY (WM_USER + 100)
#define WM_GET_NOTIFY_FLAGS (WM_USER + 101)
#define WM_CLEAR_FLAGS (WM_USER + 102)
#define WM_SET_PATHS (WM_USER + 103)

static WCHAR s_dir1[MAX_PATH];  // "%TEMP%\\WatchDir1"
static WCHAR s_dir2[MAX_PATH];  // "%TEMP%\\WatchDir1\\Dir2"
static WCHAR s_dir3[MAX_PATH];  // "%TEMP%\\WatchDir1\\Dir3"
static WCHAR s_file1[MAX_PATH]; // "%TEMP%\\WatchDir1\\File1.txt"
static WCHAR s_file2[MAX_PATH]; // "%TEMP%\\WatchDir1\\File2.txt"

static HWND s_hwnd = NULL;
static const WCHAR s_szName[] = L"SHChangeNotify testcase";

typedef enum TYPE
{
    TYPE_RENAMEITEM,
    TYPE_CREATE,
    TYPE_DELETE,
    TYPE_MKDIR,
    TYPE_RMDIR,
    TYPE_UPDATEDIR,
    TYPE_UPDATEITEM,
    TYPE_RENAMEFOLDER,
    TYPE_FREESPACE
} TYPE;

typedef void (*ACTION)(void);

typedef struct TEST_ENTRY
{
    INT line;
    LONG event;
    LPCVOID item1;
    LPCVOID item2;
    LPCSTR pattern;
    ACTION action;
    LPCWSTR path1;
    LPCWSTR path2;
} TEST_ENTRY;

static BOOL
DoCreateEmptyFile(LPCWSTR pszFileName)
{
    FILE *fp = _wfopen(pszFileName, L"wb");
    fclose(fp);<--- Null pointer dereference
    return fp != NULL;<--- Assuming that condition 'fp!=NULL' is not redundant
}

static void
DoAction1(void)
{
    ok_int(CreateDirectoryW(s_dir2, NULL), TRUE);
}

static void
DoAction2(void)
{
    ok_int(RemoveDirectoryW(s_dir2), TRUE);
}

static void
DoAction3(void)
{
    ok_int(MoveFileExW(s_dir2, s_dir3, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING), TRUE);
}

static void
DoAction4(void)
{
    ok_int(DoCreateEmptyFile(s_file1), TRUE);
}

static void
DoAction5(void)
{
    ok_int(MoveFileExW(s_file1, s_file2, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING), TRUE);
}

static void
DoAction6(void)
{
    ok_int(DeleteFileW(s_file2), TRUE);
}

static void
DoAction7(void)
{
    DeleteFileW(s_file1);
    DeleteFileW(s_file2);
    ok_int(RemoveDirectoryW(s_dir3), TRUE);
}

static void
DoAction8(void)
{
    ok_int(RemoveDirectoryW(s_dir1), TRUE);
}

static const TEST_ENTRY s_TestEntries[] = {
    {__LINE__, SHCNE_MKDIR, s_dir1, NULL, "000100000", NULL, s_dir1, L""},
    {__LINE__, SHCNE_MKDIR, s_dir2, NULL, "000100000", NULL, s_dir2, L""},
    {__LINE__, SHCNE_RMDIR, s_dir2, NULL, "000010000", NULL, s_dir2, L""},
    {__LINE__, SHCNE_MKDIR, s_dir2, NULL, "000100000", DoAction1, s_dir2, L""},
    {__LINE__, SHCNE_RMDIR, s_dir2, NULL, "000010000", NULL, s_dir2, L""},
    {__LINE__, SHCNE_RMDIR, s_dir2, NULL, "000010000", DoAction2, s_dir2, L""},
    {__LINE__, SHCNE_MKDIR, s_dir2, NULL, "000100000", DoAction1, s_dir2, L""},
    {__LINE__, SHCNE_RENAMEFOLDER, s_dir2, s_dir3, "000000010", NULL, s_dir2, s_dir3},
    {__LINE__, SHCNE_RENAMEFOLDER, s_dir2, s_dir3, "000000010", DoAction3, s_dir2, s_dir3},
    {__LINE__, SHCNE_CREATE, s_file1, NULL, "010000000", NULL, s_file1, L""},
    {__LINE__, SHCNE_CREATE, s_file1, s_file2, "010000000", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_CREATE, s_file1, NULL, "010000000", DoAction4, s_file1, L""},
    {__LINE__, SHCNE_RENAMEITEM, s_file1, s_file2, "100000000", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_RENAMEITEM, s_file1, s_file2, "100000000", DoAction5, s_file1, s_file2},
    {__LINE__, SHCNE_RENAMEITEM, s_file1, s_file2, "100000000", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_UPDATEITEM, s_file1, NULL, "000000100", NULL, s_file1, L""},
    {__LINE__, SHCNE_UPDATEITEM, s_file2, NULL, "000000100", NULL, s_file2, L""},
    {__LINE__, SHCNE_UPDATEITEM, s_file1, s_file2, "000000100", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_UPDATEITEM, s_file2, s_file1, "000000100", NULL, s_file2, s_file1},
    {__LINE__, SHCNE_DELETE, s_file1, NULL, "001000000", NULL, s_file1, L""},
    {__LINE__, SHCNE_DELETE, s_file2, NULL, "001000000", NULL, s_file2, L""},
    {__LINE__, SHCNE_DELETE, s_file2, s_file1, "001000000", NULL, s_file2, s_file1},
    {__LINE__, SHCNE_DELETE, s_file1, s_file2, "001000000", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_DELETE, s_file2, NULL, "001000000", DoAction6, s_file2, L""},
    {__LINE__, SHCNE_DELETE, s_file2, NULL, "001000000", NULL, s_file2, L""},
    {__LINE__, SHCNE_DELETE, s_file1, NULL, "001000000", NULL, s_file1, L""},
    {__LINE__, SHCNE_UPDATEDIR, s_file1, NULL, "000001000", NULL, s_file1, L""},
    {__LINE__, SHCNE_UPDATEDIR, s_file2, NULL, "000001000", NULL, s_file2, L""},
    {__LINE__, SHCNE_UPDATEDIR, s_file1, s_file2, "000001000", NULL, s_file1, s_file2},
    {__LINE__, SHCNE_UPDATEDIR, s_file2, s_file1, "000001000", NULL, s_file2, s_file1},
    {__LINE__, SHCNE_UPDATEDIR, s_dir1, NULL, "000001000", NULL, s_dir1, L""},
    {__LINE__, SHCNE_UPDATEDIR, s_dir2, NULL, "000001000", NULL, s_dir2, L""},
    {__LINE__, SHCNE_UPDATEDIR, s_dir1, s_dir2, "000001000", NULL, s_dir1, s_dir2},
    {__LINE__, SHCNE_UPDATEDIR, s_dir2, s_dir1, "000001000", NULL, s_dir2, s_dir1},
    {__LINE__, SHCNE_RMDIR, s_dir1, NULL, "000010000", NULL, s_dir1, L""},
    {__LINE__, SHCNE_RMDIR, s_dir2, NULL, "000010000", NULL, s_dir2, L""},
    {__LINE__, SHCNE_RMDIR, s_dir3, NULL, "000010000", NULL, s_dir3, L""},
    {__LINE__, SHCNE_RMDIR, s_dir1, s_dir2, "000010000", NULL, s_dir1, s_dir2},
    {__LINE__, SHCNE_RMDIR, s_dir1, s_dir3, "000010000", NULL, s_dir1, s_dir3},
    {__LINE__, SHCNE_RMDIR, s_dir2, s_dir1, "000010000", NULL, s_dir2, s_dir1},
    {__LINE__, SHCNE_RMDIR, s_dir2, s_dir3, "000010000", NULL, s_dir2, s_dir3},
    {__LINE__, SHCNE_RMDIR, s_dir3, NULL, "000010000", NULL, s_dir3, L""},
    {__LINE__, SHCNE_RMDIR, s_dir3, NULL, "000010000", DoAction7, s_dir3, L""},
    {__LINE__, SHCNE_RMDIR, s_dir1, NULL, "000010000", NULL, s_dir1, L""},
    {__LINE__, SHCNE_RMDIR, s_dir1, NULL, "000010000", DoAction8, s_dir1, L""},
};

LPCSTR PatternFromFlags(DWORD flags)
{
    static char s_buf[TYPE_FREESPACE + 1 + 1];
    DWORD i;
    for (i = 0; i <= TYPE_FREESPACE; ++i)
    {
        s_buf[i] = (char)('0' + !!(flags & (1 << i)));
    }
    s_buf[i] = 0;
    return s_buf;
}

static BOOL
DoGetClipText(LPWSTR pszPath1, LPWSTR pszPath2)
{
    pszPath1[0] = pszPath2[0] = 0;

    if (!OpenClipboard(NULL) || !IsClipboardFormatAvailable(CF_UNICODETEXT))
        return FALSE;

    WCHAR szText[MAX_PATH * 2];
    HGLOBAL hGlobal = GetClipboardData(CF_UNICODETEXT);
    LPWSTR psz = (LPWSTR)GlobalLock(hGlobal);
    lstrcpynW(szText, psz, _countof(szText));
    GlobalUnlock(hGlobal);
    CloseClipboard();

    LPWSTR pch = wcschr(szText, L'|');
    if (pch == NULL)
        return FALSE;

    *pch = 0;
    lstrcpynW(pszPath1, szText, MAX_PATH);
    lstrcpynW(pszPath2, pch + 1, MAX_PATH);
    return TRUE;
}

static void
DoTestEntry(const TEST_ENTRY *entry)
{
    if (entry->action)
    {
        (*entry->action)();
    }

    SHChangeNotify(entry->event, SHCNF_PATHW | SHCNF_FLUSH, entry->item1, entry->item2);

    DWORD flags = SendMessageW(s_hwnd, WM_GET_NOTIFY_FLAGS, 0, 0);
    LPCSTR pattern = PatternFromFlags(flags);

    ok(lstrcmpA(pattern, entry->pattern) == 0, "Line %d: pattern mismatch '%s'\n", entry->line, pattern);

    SendMessageW(s_hwnd, WM_SET_PATHS, 0, 0);

    WCHAR szPath1[MAX_PATH], szPath2[MAX_PATH];
    BOOL bOK = DoGetClipText(szPath1, szPath2);
    if (entry->path1)
        ok(bOK && lstrcmpiW(entry->path1, szPath1) == 0,
           "Line %d: path1 mismatch '%S' (%d)\n", entry->line, szPath1, bOK);
    if (entry->path2)
        ok(bOK && lstrcmpiW(entry->path2, szPath2) == 0,
           "Line %d: path2 mismatch '%S' (%d)\n", entry->line, szPath2, bOK);

    SendMessageW(s_hwnd, WM_CLEAR_FLAGS, 0, 0);
}

static BOOL
DoInit(HWND hwnd)
{
    WCHAR szTemp[MAX_PATH], szPath[MAX_PATH];

    GetTempPathW(_countof(szTemp), szTemp);
    GetLongPathNameW(szTemp, szPath, _countof(szPath));

    lstrcpyW(s_dir1, szPath);
    PathAppendW(s_dir1, L"WatchDir1");
    CreateDirectoryW(s_dir1, NULL);

    lstrcpyW(s_dir2, s_dir1);
    PathAppendW(s_dir2, L"Dir2");

    lstrcpyW(s_dir3, s_dir1);
    PathAppendW(s_dir3, L"Dir3");

    lstrcpyW(s_file1, s_dir1);
    PathAppendW(s_file1, L"File1.txt");

    lstrcpyW(s_file2, s_dir1);
    PathAppendW(s_file2, L"File2.txt");

    return TRUE;
}

static void
DoEnd(HWND hwnd)
{
    DeleteFileW(s_file1);
    DeleteFileW(s_file2);
    RemoveDirectoryW(s_dir3);
    RemoveDirectoryW(s_dir2);
    RemoveDirectoryW(s_dir1);

    SendMessageW(s_hwnd, WM_COMMAND, IDOK, 0);
}

static BOOL CALLBACK
PropEnumProcEx(HWND hwnd, LPWSTR lpszString, HANDLE hData, ULONG_PTR dwData)
{
    if (HIWORD(lpszString))
        trace("Prop: '%S' --> %p\n", lpszString, hData);
    else
        trace("Prop: '%u' --> %p\n", LOWORD(lpszString), hData);
    return TRUE;
}

static void
DoPropTest(HWND hwnd)
{
    EnumPropsExW(hwnd, PropEnumProcEx, 0);
}

START_TEST(SHChangeNotify)
{
    WCHAR szPath[MAX_PATH];
    GetModuleFileNameW(NULL, szPath, _countof(szPath));
    PathRemoveFileSpecW(szPath);
    PathAppendW(szPath, L"shell-notify.exe");

    if (!PathFileExistsW(szPath))
    {
        trace("szPath: %S\n", szPath);
        PathRemoveFileSpecW(szPath);
        PathAppendW(szPath, L"testdata\\shell-notify.exe");

        if (!PathFileExistsW(szPath))
        {
            trace("szPath: %S\n", szPath);
            skip("shell-notify.exe not found\n");
            return;
        }
    }

    HINSTANCE hinst = ShellExecuteW(NULL, NULL, szPath, NULL, NULL, SW_SHOWNORMAL);
    if ((INT_PTR)hinst <= 32)
    {
        skip("Unable to run shell-notify.exe.\n");
        return;
    }

    for (int i = 0; i < 15; ++i)
    {
        s_hwnd = FindWindowW(s_szName, s_szName);
        if (s_hwnd)
            break;

        Sleep(50);
    }

    if (!s_hwnd)
    {
        skip("Unable to find window.\n");
        return;
    }

    if (!DoInit(s_hwnd))
    {
        skip("Unable to initialize.\n");
        return;
    }

    DoPropTest(s_hwnd);

    for (size_t i = 0; i < _countof(s_TestEntries); ++i)
    {
        DoTestEntry(&s_TestEntries[i]);
    }

    DoEnd(s_hwnd);
}