summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CreateMutexW_ReleaseMutex/test2/CreateMutexW.cpp
blob: 41b7798a6e245a1a5d8ddebe4dc7965c68c280fd (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
** Source: CreateMutexW_ReleaseMutex/test2/CreateMutexW.c
**
** Purpose: This test case tests the following things
**          - Creation of named Mutexes
**          - Creating multiple handles to a single named Mutex
**          - Ensuring that these handles work interchangeably
**          - Setting bInitialOwnerFlag to TRUE will cause the
**            initial call to a Wait function on the same Mutex
**            to actually wait.
**          - Waiting on a Mutex that a thread already owns does
**            not block.
**          - Create Named mutex with empty string ("")
**          - Create Named mutex with string of MAX_LONGPATH length
**          - Calling RelaseMutex with invalid Mutex handles and
**            valid but unowned Mutexes.
**
** Dependencies: CreateThread
**               ReleaseMutex
**               WaitForSingleObject
**               CloseHandle
**               Sleep
**               memset
** 

**
**=========================================================*/

#define UNICODE
#include <palsuite.h>

const char *szMutex = "MyMutex";
const char *szEmpty = "";

/* Function Prototypes */
BOOL TestNamedMutex(const char *szMutexName);
DWORD NamedMutexThread(LPVOID lpParam);
BOOL NegativeReleaseMutexTests();

struct ThreadData
{
    HANDLE hMutex;
    BOOL bReturnCode;
};
typedef struct ThreadData THREADDATA;


int __cdecl main (int argc, char **argv) 
{
    BOOL bFailures = FALSE;
    char *szMaxPath;

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return ( FAIL );
    }


    /*
     * Test named Mutexes with ordinary string
     */

    if (!TestNamedMutex(szMutex))
    {
        bFailures = TRUE;
    }


    /*
     * Test named Mutexes with empty ("") string
     */

    if (!TestNamedMutex(szEmpty))
    {
        bFailures = TRUE;
    }


    /*
     * Test named Mutexes with string of length MAX_LONGPATH
     */

    szMaxPath = (char *)malloc(MAX_LONGPATH+2);
    memset(szMaxPath, 'A', MAX_LONGPATH-60);
    szMaxPath[MAX_LONGPATH-60] = 0;

    if (!TestNamedMutex(szMaxPath))
    {
        bFailures = TRUE;
    }

    free(szMaxPath);


    /*
     * Run some negative tests on ReleaseMutex
     */

    if (!NegativeReleaseMutexTests())
    {
        bFailures = TRUE;
    }


    /*
     * If there were any failures, then abort with a call to Fail
     */

    if (bFailures == TRUE)
    {
        Fail("ERROR: There some failures in the Mutex tests.\n");
    }

    PAL_Terminate();
    return ( PASS );
}


/*
 * Testing Function
 *
 * Try to get multiple handles to a named Mutex and test 
 * to make sure they actually refer to same Mutex object.
 */
BOOL TestNamedMutex(const char *szMutexName)
{
    DWORD dwData;
    HANDLE hMutex1;
    HANDLE hMutex2;
    HANDLE hThread;
    WCHAR *swzMutexName;
    THREADDATA threadData;

    /* Convert the Mutex name to wide characters */
    swzMutexName = convert((char *)szMutexName);

    /* Create a mutex and take ownership immediately */
    hMutex1 = CreateMutexW (NULL, TRUE, swzMutexName);

    if (NULL == hMutex1)
    {
        Trace("ERROR: CreateMutex #1 failed. GetLastError returned %u\n",
              GetLastError());
        free(swzMutexName);
        return FALSE;
    }

    /* Try to wait on the Mutex we just created. We should not block. */
    if (WaitForSingleObject(hMutex1, 1000) == WAIT_TIMEOUT)
    {
        Trace("WaitForSingleObject blocked on a Mutex that we owned.\n");
        free(swzMutexName);
        return FALSE;
    }
    /* We have to call ReleaseMutex here because of the Wait */
    if (ReleaseMutex(hMutex1) == FALSE)
    {
        Trace("ReleaseMutex Failed.\n");    
        return FALSE;
    }
    
    /* Get a second handle to the same mutex */
    hMutex2 = CreateMutexW (NULL, FALSE, swzMutexName);

    if (NULL == hMutex2)
    {
        Trace("ERROR: CreateMutex #2 failed. GetLastError returned %u\n",
              GetLastError());
        free(swzMutexName);
        return FALSE;
    }

    /* Get rid of the wide character string */ 
    free(swzMutexName);

    /*
     * Create a thread that will Wait on the second handle.
     */
    threadData.hMutex = hMutex2;
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)NamedMutexThread,
                          (LPVOID)&threadData, 0, &dwData);

    if (NULL == hThread)
    {
        Trace("ERROR: CreateThread failed. GetLastError returned %u\n",
              GetLastError());
        return FALSE;
    }

    /* Give the thread a little time to execute & wait*/
    Sleep(500);

    /* Signal the the first handle */
    if (ReleaseMutex(hMutex1) == FALSE)
    {
        Trace("ReleaseMutex Failed.\n");    
        return FALSE;
    }

    /* Give the thread some time to finish */
    Sleep(2000);

    /* Clean Up */
    if (CloseHandle(hMutex1) == FALSE ||
        CloseHandle(hMutex2) == FALSE ||
        CloseHandle(hThread) == FALSE)
    {
        Trace("ERROR: CloseHandle failed.\n");
        return FALSE;
    }

    /* Check the return code to see if signalling the first */
    /* Mutex handle woke up the thread which was Waiting on */
    /* the second handle.                                   */
    if (threadData.bReturnCode != FALSE)
    {
        Trace("ERROR: The handles did not refer to the same Mutex object.\n");
        return FALSE;
    }

    return TRUE;
}


/*
 * Thread function used with above testing function.
 */
DWORD NamedMutexThread(LPVOID lpParam)
{
    BOOL bTimedOut =  FALSE;
    THREADDATA *lpThreadData = (THREADDATA *)lpParam;

    /* Wait on the Mutex that was passed to us */
    if (WaitForSingleObject(lpThreadData->hMutex, 10000) == WAIT_TIMEOUT)
    {
        /* The Mutex was not signaled in the allotted time */
        bTimedOut = TRUE;
    }
    if (ReleaseMutex(lpThreadData->hMutex) == FALSE)
    {
        Trace("ERROR: ReleaseMutex failed.\n");
        lpThreadData->bReturnCode = FALSE;
        return  0;
    }

    /* Indicate whether we timed out Waiting on the Mutex */
    lpThreadData->bReturnCode = bTimedOut;

    return 0;
}


/*
 * Testing Function
 *
 * Try some negative tests on ReleaseMutex
 */
BOOL NegativeReleaseMutexTests()
{
    HANDLE hMutex;
    BOOL bRet;
    BOOL bResults = TRUE;


    /*
     * Try calling ReleaseMutex on a null handle
     */
    hMutex = 0;
    bRet = ReleaseMutex(hMutex);

    if (bRet != 0)
    {
        Trace("Error: ReleaseMutex accepted null handle.\n");
        bResults =  FALSE;
    }


    /*
     * Try calling ReleaseMutex on an handle that we don't own
     */
    hMutex = CreateMutexW (NULL, TRUE, NULL);
    if (hMutex == 0)
    {
        Trace("Error: CreateMutex failed.\n");
        bResults =  FALSE;
    }

    bRet = ReleaseMutex(hMutex);
    bRet = ReleaseMutex(hMutex);

    if (bRet != FALSE)
    {
        Trace("Error: ReleaseMutex accepted unowned handle.\n");
        bResults =  FALSE;
    }

    if (CloseHandle(hMutex) == FALSE)
    {
        Trace("Error: CloseHandle failed.\n");
        bResults =  FALSE;
    }



    /*
     * Try calling ReleaseMutex on an handle that has been closed
     */
    hMutex = CreateMutexW (NULL, TRUE, NULL);
    if (hMutex == 0)
    {
        Trace("Error: CreateMutex failed.\n");
        bResults =  FALSE;
    }

    if (ReleaseMutex(hMutex) == FALSE)
    {
        Trace("Error: ReleaseMutex failed.\n");
        bResults =  FALSE;
    }
    if (CloseHandle(hMutex) == FALSE)
    {
        Trace("Error: CloseHandle failed.\n");
        bResults =  FALSE;
    }
    
    bRet = ReleaseMutex(hMutex);

    if (bRet != FALSE)
    {
        Trace("Error: ReleaseMutex accepted invalid handle.\n");
        bResults =  FALSE;
    }

    return bResults;
}