summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/pal_specific/PAL_GetPALDirectoryW/test1/PAL_GetPALDirectoryW.cpp
blob: 856bfe86598186578de8909b369991a101a150b2 (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
// 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: pal_getpaldirectoryw.c
**
** Purpose: Positive test the PAL_GetPALDirectoryW API.
**          Call this API to retrieve a fully-qualified 
**          directory name where the PAL DLL is loaded from.
**
**
**============================================================*/
#define UNICODE
#include <palsuite.h>

int __cdecl main(int argc, char *argv[])
{
    int err;
    BOOL bValue;
    DWORD dwFileAttribute;
    WCHAR *wpDirectoryName = NULL;
    char *pDirectoryName = NULL;
  
    /*Initialize the PAL environment*/
    err = PAL_Initialize(argc, argv);
    if(0 != err)
    {
        return FAIL;
    }

    /*allocate momory to store the directory name*/
    wpDirectoryName = (WCHAR*)malloc(MAX_PATH*sizeof(WCHAR));
    if(NULL == wpDirectoryName)
    {
        Fail("\nFailed to allocate memory for storing directory name!\n");
    } 

    UINT size = MAX_PATH;
    /*retrieve the machine configuration directory*/
    bValue = PAL_GetPALDirectoryW(wpDirectoryName, &size);
    if(FALSE == bValue) 
    {
        free(wpDirectoryName);
        Fail("Failed to call PAL_GetPALDirectoryW API, "
                "error code =%u\n", GetLastError());
    }
    

    /*convert wide char string to a standard one*/
    pDirectoryName = convertC(wpDirectoryName);
    if(0 == strlen(pDirectoryName))
    {
        free(wpDirectoryName);
        free(pDirectoryName);
        Fail("The retrieved directory name string is empty!\n");
    }

    /*free the memory*/
    free(pDirectoryName);

    /*retrieve the attribute of a file or directory*/
    dwFileAttribute = GetFileAttributesW(wpDirectoryName);

    /*free the memory*/
    free(wpDirectoryName);

    /*check if the attribute indicates a directory*/
    if(FILE_ATTRIBUTE_DIRECTORY != 
            (dwFileAttribute & FILE_ATTRIBUTE_DIRECTORY))
    {
        Fail("The retrieved directory name is not a valid directory!\n");
    }

    PAL_Terminate();
    return PASS;
}