summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/DuplicateHandle/test9/test9.c
blob: f15871c57d9fb60ad83cabf3a8173e7d4094bd61 (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
// 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:  test9.c (DuplicateHandle)
**
** Purpose: Tests the PAL implementation of the DuplicateHandle function,
**          with a handle from GetCurrentProcess. The test will create a 
**          process, duplicate it, then using ReadProcessMemory will
**          read from the memory location of the CreateProcess process
**          memory and the DuplicateHandle process memory. If the 
**          duplication is correct the memory will be the same for both.
**
**
**===================================================================*/

#include <palsuite.h>

int __cdecl main(int argc, char* argv[])
{
    HANDLE  hProcess;
    HANDLE  hDupProcess;
    char    lpBuffer[64];
    char    lpDupBuffer[64];
    SIZE_T  lpNumberOfBytesRead;
    SIZE_T  lpDupNumberOfBytesRead;
    char lpTestBuffer[] = "abcdefghijklmnopqrstuvwxyz";

    /* Initalize the PAL.
    */
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
    
    /* Initalize the buffers.
    */
    ZeroMemory( &lpBuffer, sizeof(lpBuffer) );
    ZeroMemory( &lpDupBuffer, sizeof(lpDupBuffer) );
    
    /* Get current proces, this will be duplicated.
    */
    hProcess = GetCurrentProcess();
    if(hProcess == NULL)
    {
        Fail("ERROR: Unable to get the current process\n");
    }

    /* Duplicate the current process handle.
    */
    if (!(DuplicateHandle(GetCurrentProcess(),       /* source handle process*/
                          hProcess,                  /* handle to duplicate*/
                          GetCurrentProcess(),       /* target process handle*/
                          &hDupProcess,              /* duplicate handle*/
                          (DWORD)0,                  /* requested access*/
                          FALSE,                     /* handle inheritance*/
                          DUPLICATE_SAME_ACCESS)))   /* optional actions*/
    {
        Trace("ERROR:%u: Failed to create the duplicate handle"
             " to hProcess=0x%lx",
             GetLastError(),
             hProcess);
        CloseHandle(hProcess);
        Fail("");
    }

    /* Get memory read of the current process.
    */
    if ((ReadProcessMemory(hDupProcess, &lpTestBuffer, 
         lpDupBuffer, sizeof(lpDupBuffer), &lpDupNumberOfBytesRead)) == 0)
    {
        Trace("ERROR:%u: Unable to read the process memory of "
             "hDupProcess=0x%lx.\n", 
             GetLastError(), 
             hDupProcess);
        CloseHandle(hProcess);
        CloseHandle(hDupProcess);
        Fail("");
    }
    
    /* Get read memory of the created process.
    */
    if ((ReadProcessMemory(hProcess, &lpTestBuffer,
         lpBuffer, sizeof(lpBuffer), &lpNumberOfBytesRead)) == 0)
    {
        Trace("ERROR:%u: Unable to read the process memory of "
             "hProcess=0x%lx.\n",
             GetLastError(), 
             hProcess);
        CloseHandle(hProcess);
        CloseHandle(hDupProcess);
        Fail("");
    }

    /* Compare the number of bytes that were read by each
     * ReadProcessMemory.*/
    if (lpDupNumberOfBytesRead != lpNumberOfBytesRead)
    {
        Trace("ERROR: ReadProcessMemory read different numbers of bytes "
            "from duplicate process handles.\n");
        CloseHandle(hProcess);
        CloseHandle(hDupProcess);
        Fail("");
    }

    /* Compare the two buffers to make sure they are equal.
    */
    if ((strcmp(lpBuffer, lpDupBuffer)) != 0)
    {
        Trace("ERROR: ReadProcessMemory read different numbers of bytes "
            "from duplicate process handles. hProcess read \"%s\" and "
            "hDupProcess read \"%s\"\n",
            lpBuffer,
            lpDupBuffer);
        CloseHandle(hProcess);
        CloseHandle(hDupProcess);
        Fail("");
    }
    
    /* Clean-up thread and Terminate the PAL.*/
    CloseHandle(hProcess);
    CloseHandle(hDupProcess);
    PAL_Terminate();
    return PASS;
}