summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/CreateSemaphoreW_ReleaseSemaphore/test3/createsemaphore.cpp
blob: ea0a5e084662225d6839c04a1efae958e6004350 (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
// 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: createsemaphorew_releasesemaphore/test3/createsemaphore.c
**
** Purpose: Test attributes of CreateSemaphoreW and ReleaseSemaphore.  
** Insure for CreateSemaphore that lInitialCount and lMaximumCount
** constraints are respected.  Validate that CreateSemaphore rejects 
** conditions where initial count and / or maximum count are negative 
** and conditions where the initial count is greater than the maximum 
** count.  For ReleaseSemaphore validate that lpPreviousCount gets set
** to the previous semaphore count and lpPreviousCount can be NULL.
** Also establish ReleaseSemaphore fails when called in a semaphore 
** with count equal to lMaximumCount.
**
**
**==========================================================================*/

#include <palsuite.h>

struct testcase
{
    LPSECURITY_ATTRIBUTES lpSemaphoreAttributes;
    LONG lInitialCount;
    LONG lMaximumCount;
    LPCWSTR lpName;
    BOOL bNegativeTest;
};

struct testcase testCases[] =
{
    {NULL, -1, 1, NULL, TRUE},
    {NULL, 1, -1, NULL, TRUE},
    {NULL, -1, -1, NULL, TRUE},
    {NULL, 2, 1, NULL, TRUE},
    {NULL, 1, 2, NULL, FALSE},
    {NULL, 0, 10, NULL, FALSE},
    {NULL, INT_MAX - 1, INT_MAX, NULL, FALSE},
    {NULL, INT_MAX, INT_MAX, NULL, FALSE}
};

HANDLE hSemaphore[sizeof(testCases)/sizeof(struct testcase)];

BOOL cleanup(int index)
{
    int i;
    BOOL bRet = TRUE;
    for (i = 0; i < index; i++)
    {
        if (!CloseHandle(hSemaphore[i]))
        {
            bRet = FALSE;
            Trace("PALSUITE ERROR: CloseHandle(%p) call failed for index %d\n",
                  hSemaphore[i], i);
        }
    }
    return(bRet);
}

int __cdecl main (int argc, char **argv) 
{
    int i;
    int j;

    if(0 != (PAL_Initialize(argc, argv)))
    {
	return (FAIL);
    }
    /* create semaphores */
    for (i = 0; i < sizeof(testCases)/sizeof(struct testcase); i++)
    {
        hSemaphore[i] = CreateSemaphoreW (testCases[i].lpSemaphoreAttributes,
            testCases[i].lInitialCount,
            testCases[i].lMaximumCount,
            testCases[i].lpName);    
        
        if (NULL == hSemaphore[i])
        {
            if (!testCases[i].bNegativeTest)
            {
                Trace("PALSUITE ERROR: CreateSemaphoreW('%p' '%ld' '%ld' "
                      "'%p') returned NULL at index %d.\nGetLastError "
                      "returned %d.\n", testCases[i].lpSemaphoreAttributes,
                      testCases[i].lInitialCount, testCases[i].lMaximumCount,
                      testCases[i].lpName, i, GetLastError()); 
                if (i > 0)
                {
                    cleanup(i - 1);
                }
                Fail("");
            }
            else 
            {
                continue;
            }
        }

        /* increment semaphore count to lMaximumCount */
        for (j = testCases[i].lInitialCount; (ULONG)j <= (ULONG)testCases[i].lMaximumCount; 
             j++)    
        {
            if (testCases[i].lMaximumCount == j)
            {
                /* Call ReleaseSemaphore once more to ensure ReleaseSemaphore
                   fails */
                if(ReleaseSemaphore(hSemaphore[i], 1, NULL))
                {
                    Trace("PALSUITE ERROR: ReleaseSemaphore('%p' '%ld' '%p') "
                          "call returned %d\nwhen it should have returned "
                          "%d.\nThe semaphore's count was %d.\nGetLastError "
                          "returned %d.\n", hSemaphore[i], 1, NULL, TRUE, 
                          FALSE, j, GetLastError());
                    cleanup(i);
                    Fail("");
                }   
            }
            else 
            {
                int previous;
                BOOL bRet = ReleaseSemaphore(hSemaphore[i], 1, &previous);
                DWORD dwError = GetLastError();         

                if(!bRet)
                {
                    Trace("PALSUITE ERROR: ReleaseSemaphore('%p' '%ld' '%p') "
                          "call returned %d\nwhen it should have returned "
                          "%d.\nThe semaphore count was %d and it's "
                          "lMaxCount was %d.\nGetLastError returned %d.\n", 
                          hSemaphore[i], 1, &previous, bRet, TRUE, j, 
                          testCases[i].lMaximumCount, dwError);
                    cleanup(i);
                    Fail("");
                }
                if (previous != j)
                {
                    Trace("PALSUITE ERROR: ReleaseSemaphore('%p' '%ld' '%p') "
                          "call set %p to %d instead of %d.\n The semaphore "
                          "count was %d and GetLastError returned %d.\n", 
                          hSemaphore[i], 1, &previous, &previous, previous,
                          j, j, dwError);
                    cleanup(i);
                    Fail("");
                }
            }
        }

        // Skip exhaustive decrement tests for too large an initial count
        if(testCases[i].lInitialCount >= INT_MAX - 1)
        {
            continue;
        }

        /* decrement semaphore count to 0 */
        for (j = testCases[i].lMaximumCount; j >= 0; j--)    
        {
            DWORD dwRet = WaitForSingleObject(hSemaphore[i], 0);
            DWORD dwError = GetLastError();

            if (0 == j)
            {
                /* WaitForSingleObject should report that the
                   semaphore is nonsignaled */
                if (WAIT_TIMEOUT != dwRet)
                {
                    Trace("PALSUITE ERROR: WaitForSingleObject('%p' '%u') "
                          "call returned %d\nwhen it should have returned "
                          "%d.\nThe semaphore's count was %d.\nGetLastError "
                          "returned %d.\n", hSemaphore[i], 0, dwRet,
                          WAIT_TIMEOUT, j, dwError);
                    cleanup(i);
                    Fail("");
                }             
            }
            else 
            {
                /* WaitForSingleObject should report that the
                   semaphore is signaled */
                if (WAIT_OBJECT_0 != dwRet)
                {
                    Trace("PALSUITE ERROR: WaitForSingleObject('%p' '%u') "
                          "call returned %d\nwhen it should have returned "
                          "%d.\nThe semaphore's count was %d.\nGetLastError "
                          "returned %d.\n", hSemaphore[i], 0, dwRet,
                          WAIT_OBJECT_0, j, dwError);
                    cleanup(i);
                    Fail("");
                }
            }
        }
    }
    PAL_Terminate();
    return (PASS);
}