summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/filemapping_memmgt/FreeLibrary/test1/FreeLibrary.cpp
blob: a06a2315868826f629662313e7882335131bf0db (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
// 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:
**
** Purpose: Positive test the FreeLibrary API.
**
**
**============================================================*/
#include <palsuite.h>

/*char    LibraryName[] = "dlltest";*/
/* SHLEXT is defined only for Unix variants */

#if defined(SHLEXT)
#define LibraryName    "dlltest"SHLEXT
#else
#define LibraryName   "dlltest"
#endif




BOOL  PALAPI TestDll(HMODULE, int);

int __cdecl main(int argc, char *argv[])
{
    HANDLE hLib;

    /* Initialize the PAL. */
    if ((PAL_Initialize(argc, argv)) != 0)
    {
        return (FAIL);
    }
    
    /*Load library (DLL). */
    hLib = LoadLibrary(LibraryName);

    if(hLib == NULL)
    {
        Fail("ERROR:%u:Unable to load library %s\n", 
             GetLastError(), 
             LibraryName);
    }

    /* Test access to DLL. */
    if(!TestDll(hLib, PASS))
        {
        Trace("ERROR: TestDll function returned FALSE "
             "expected TRUE\n.");
        FreeLibrary(hLib);
        Fail("");
    }

    /* Call the FreeLibrary API. */ 
    if (!FreeLibrary(hLib))
    {
        Fail("ERROR:%u: Unable to free library \"%s\"\n", 
             GetLastError(),
             LibraryName);
    }

    /* Test access to the free'd DLL. */
    if(!TestDll(hLib, FAIL))
    {
        Fail("ERROR: TestDll function returned FALSE "
            "expected TRUE\n.");
    }

    PAL_Terminate();
    return PASS;

}


BOOL PALAPI TestDll(HMODULE hLib, int testResult)
{
    int     RetVal;
#if WIN32
    char    FunctName[] = "_DllTest@0";
#else
    char    FunctName[] = "DllTest";
#endif
    FARPROC DllAddr;    

    /* Attempt to grab the proc address of the dll function.
     * This one should succeed.*/
    if(testResult == PASS)
    {
        DllAddr = GetProcAddress(hLib, FunctName);
        if(DllAddr == NULL)
        {
            Trace("ERROR: Unable to load function \"%s\" library \"%s\"\n", 
                    FunctName,
                    LibraryName);
            return (FALSE);
        }
        /* Run the function in the DLL, 
         * to ensure that the DLL was loaded properly.*/
        RetVal = DllAddr();
        if (RetVal != 1)
        {
            Trace("ERROR: Unable to receive correct information from DLL! "
                ":expected \"1\", returned \"%d\"\n",
                RetVal);
            return (FALSE);
        }
    }

    /* Attempt to grab the proc address of the dll function.
     * This one should fail.*/
    if(testResult == FAIL)
    {
        DllAddr = GetProcAddress(hLib, FunctName);
        if(DllAddr != NULL)
        {
            Trace("ERROR: Able to load function \"%s\" from free'd"
                " library \"%s\"\n", 
                FunctName, 
                LibraryName);
            return (FALSE);
        }
    }
    return (TRUE);
}