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
/*
 * PROJECT:         ReactOS API tests
 * LICENSE:         LGPLv2.1+ - See COPYING.LIB in the top level directory
 * PURPOSE:         Test for OpenAs_RunDLL
 * PROGRAMMERS:     Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
 */

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

// OpenAs_RunDLLA
typedef void (WINAPI *OPENAS_RUNDLLA)(HWND, HINSTANCE, LPCSTR, int);
// OpenAs_RunDLLW
typedef void (WINAPI *OPENAS_RUNDLLW)(HWND, HINSTANCE, LPCWSTR, int);

static OPENAS_RUNDLLA pOpenAs_RunDLLA = NULL;
static OPENAS_RUNDLLW pOpenAs_RunDLLW = NULL;

struct TEST_ENTRY
{
    int         nLine;
    BOOL        bWide;
    HINSTANCE   hInst;
    int         nRet;
    BOOL        bCreateFile;
    LPCSTR      pszFileA;
    LPCWSTR     pszFileW;
    DWORD       dwError;
};

#define COUNT       5
#define INTERVAL    200

#define DEATH       999
#define OPENED      1
#define NOT_OPENED  0

#define BAD_INST   ((HINSTANCE)0xDEAD)
#define BAD_SZ_A   ((LPCSTR)0xDEAD)
#define BAD_SZ_W   ((LPCWSTR)0xDEAD)

static const TEST_ENTRY s_TestEntries[] =
{
    // ANSI
    {__LINE__, FALSE, NULL, OPENED, FALSE, NULL, NULL, 0 },
    {__LINE__, FALSE, NULL, OPENED, FALSE, "", NULL, 0 },
    {__LINE__, FALSE, NULL, OPENED, FALSE, "invalid file name.txt", NULL, 0 },
    {__LINE__, FALSE, NULL, DEATH, FALSE, BAD_SZ_A, NULL, 0 },
    {__LINE__, FALSE, NULL, OPENED, TRUE, "created file.txt", NULL, 0 },
    {__LINE__, FALSE, BAD_INST, OPENED, FALSE, NULL, NULL, 0 },
    {__LINE__, FALSE, BAD_INST, OPENED, FALSE, "invalid file name.txt", NULL, 0 },
    {__LINE__, FALSE, BAD_INST, DEATH, FALSE, BAD_SZ_A, NULL, 0xDEADFACE },
    {__LINE__, FALSE, BAD_INST, OPENED, TRUE, "created file.txt", NULL, 0 },
    // WIDE
    {__LINE__, TRUE, NULL, OPENED, FALSE, NULL, NULL, 0x80070490 },
    {__LINE__, TRUE, NULL, OPENED, FALSE, NULL, L"", ERROR_NO_SCROLLBARS },
    {__LINE__, TRUE, NULL, OPENED, FALSE, NULL, L"invalid file name.txt", ERROR_NO_SCROLLBARS },
    {__LINE__, TRUE, NULL, OPENED, FALSE, NULL, BAD_SZ_W, 0xDEADFACE },
    {__LINE__, TRUE, NULL, OPENED, TRUE, NULL, L"created file.txt", ERROR_NO_SCROLLBARS },
    {__LINE__, TRUE, BAD_INST, OPENED, FALSE, NULL, NULL, 0x80070490 },
    {__LINE__, TRUE, BAD_INST, OPENED, FALSE, NULL, L"invalid file name.txt", ERROR_NO_SCROLLBARS },
    {__LINE__, TRUE, BAD_INST, OPENED, FALSE, NULL, BAD_SZ_W, 0xDEADFACE },
    {__LINE__, TRUE, BAD_INST, OPENED, TRUE, NULL, L"created file.txt", ERROR_NO_SCROLLBARS },
};

static HANDLE s_hThread = NULL;
static INT s_nRet = NOT_OPENED;

static unsigned __stdcall
watch_thread_proc(void *arg)
{
    for (int i = 0; i < COUNT; ++i)
    {
        Sleep(INTERVAL);

        HWND hwnd = FindWindowA("#32770", "Open With");
        if (hwnd == NULL)
            continue;

        if (!IsWindowVisible(hwnd))
        {
            trace("not visible\n");
            continue;
        }

        s_nRet = OPENED;
        PostMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDCANCEL, BN_CLICKED), 0);
        break;
    }
    return 0;
}

static void StartWatchGUI(const TEST_ENTRY *pEntry)
{
    s_nRet = NOT_OPENED;
    s_hThread = (HANDLE)_beginthreadex(NULL, 0, watch_thread_proc, NULL, 0, NULL);
}

static void DoEntry(const TEST_ENTRY *pEntry)
{
    if (pEntry->bWide)
    {
        if (pEntry->bCreateFile)
        {
            FILE *fp = _wfopen(pEntry->pszFileW, L"wb");<--- Assignment 'fp=_wfopen(pEntry->pszFileW,L"wb")', assigned value is 0
            ok(fp != NULL, "Line %d: Unable to create file '%s'\n", pEntry->nLine, wine_dbgstr_w(pEntry->pszFileW));<--- Assuming that condition 'fp!=NULL' is not redundant
            fclose(fp);<--- Null pointer dereference
        }

        StartWatchGUI(pEntry);
        SetLastError(0xDEADFACE);

        _SEH2_TRY
        {
            (*pOpenAs_RunDLLW)(NULL, pEntry->hInst, pEntry->pszFileW, SW_SHOWDEFAULT);
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            s_nRet = DEATH;
        }
        _SEH2_END;

        DWORD dwError = GetLastError();
        ok(pEntry->dwError == dwError, "Line %d: error expected %ld, was %ld\n", pEntry->nLine, pEntry->dwError, dwError);

        WaitForSingleObject(s_hThread, INFINITE);
        CloseHandle(s_hThread);

        if (pEntry->bCreateFile)
        {
            ok(DeleteFileW(pEntry->pszFileW), "Line %d: DeleteFileA failed\n", pEntry->nLine);
        }
    }
    else
    {
        if (pEntry->bCreateFile)
        {
            FILE *fp = fopen(pEntry->pszFileA, "wb");<--- Assignment 'fp=fopen(pEntry->pszFileA,"wb")', assigned value is 0
            ok(fp != NULL, "Line %d: Unable to create file '%s'\n", pEntry->nLine, pEntry->pszFileA);<--- Assuming that condition 'fp!=NULL' is not redundant
            fclose(fp);<--- Null pointer dereference
        }

        StartWatchGUI(pEntry);
        SetLastError(0xDEADFACE);

        _SEH2_TRY
        {
            (*pOpenAs_RunDLLA)(NULL, pEntry->hInst, pEntry->pszFileA, SW_SHOWDEFAULT);
        }
        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
        {
            s_nRet = DEATH;
        }
        _SEH2_END;

        DWORD dwError = GetLastError();
        ok(pEntry->dwError == dwError, "Line %d: error expected %ld, was %ld\n", pEntry->nLine, pEntry->dwError, dwError);

        WaitForSingleObject(s_hThread, INFINITE);
        CloseHandle(s_hThread);

        if (pEntry->bCreateFile)
        {
            ok(DeleteFileA(pEntry->pszFileA), "Line %d: DeleteFileA failed\n", pEntry->nLine);
        }
    }

    // FIXME: This function probably returns void
    //ok(pEntry->nRet == s_nRet, "Line %d: s_nRet expected %d, was %d\n", pEntry->nLine, pEntry->nRet, s_nRet);
}

START_TEST(OpenAs_RunDLL)
{
    HINSTANCE hShell32 = LoadLibraryA("shell32.dll");
    if (hShell32 == NULL)
    {
        skip("Unable to load shell32.dll\n");
        return;
    }

    pOpenAs_RunDLLA = (OPENAS_RUNDLLA)GetProcAddress(hShell32, "OpenAs_RunDLLA");
    if (pOpenAs_RunDLLA == NULL)
    {
        skip("Unable to get OpenAs_RunDLLA\n");
        return;
    }

    pOpenAs_RunDLLW = (OPENAS_RUNDLLW)GetProcAddress(hShell32, "OpenAs_RunDLLW");
    if (pOpenAs_RunDLLW == NULL)
    {
        skip("Unable to get OpenAs_RunDLLW\n");
        return;
    }

    TCHAR szCurDir[MAX_PATH], szTempDir[MAX_PATH];

    ok(GetCurrentDirectory(_countof(szCurDir), szCurDir), "GetCurrentDirectory failed\n");
    ok(GetTempPath(_countof(szTempDir), szTempDir), "GetTempPath failed\n");

    if (!SetCurrentDirectory(szTempDir))
    {
        skip("SetCurrentDirectory failed\n");
    }
    else
    {
        for (size_t i = 0; i < _countof(s_TestEntries); ++i)
        {
            const TEST_ENTRY *pEntry = &s_TestEntries[i];
            DoEntry(pEntry);
        }

        ok(SetCurrentDirectory(szCurDir), "SetCurrentDirectory failed\n");
    }

    FreeLibrary(hShell32);
}