summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/SearchPathA/test1/SearchPathA.c
blob: ab9eecdebcb3910b69d1322998243850dd598a74 (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
// 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:  SearchPathA.c
**
** Purpose: Tests the PAL implementation of the SearchFileA function.
**
**
** TODO: Write a test where complete path is passed (say c:\?)
**===================================================================*/
//SearchPath
//
//The SearchPath function searches for the specified file in the specified path.
//


#include <palsuite.h>
char* szDir                   =          ".";

char* szNoFileName            =          "333asdf";
char* szNoFileNameExt         =          ".x77t";

char* szFileNameExists        =          "searchfile";
char* szFileNameExtExists     =          ".txt";

char* szFileNameExistsWithExt =          "searchfile.txt";
char  fileloc[_MAX_PATH];

void removeFileHelper(LPSTR pfile, int location)
{    
    FILE *fp;
    fp = fopen( pfile, "r");

    if (fp != NULL)
    {
        if(fclose(fp))
        {
          Fail("ERROR: Failed to close the file [%s], Error Code [%d], location [%d]\n", pfile, GetLastError(), location);           
        }

        if(!DeleteFileA(pfile))
        {
            Fail("ERROR: Failed to delete file [%s], Error Code [%d], location [%d]\n", pfile, GetLastError(), location);           
        }
    }

}


void RemoveAll()
{
    removeFileHelper(fileloc, 1);
}

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

    char* lpPath        = NULL;
    char* lpFileName    = NULL;
    char* lpExtension   = NULL;
    DWORD  nBufferLength = 0;
    char  lpBuffer[_MAX_PATH];
    char** lpFilePart    = NULL;
    DWORD  error         = 0;
    DWORD  result        = 0;

    HANDLE hsearchfile;
    char fname[_MAX_FNAME];
    char ext[_MAX_EXT];
    char   fullPath[_MAX_DIR];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];


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

   
    /* Initalize the buffer.
     */
    memset(fullPath, 0, _MAX_DIR);

    /* Get the full path to the library (DLL).
     */
  
    if ( NULL != _fullpath( fullPath, argv[0], _MAX_DIR )) {
        _splitpath(fullPath,drive,dir,fname,ext);
        _makepath(fullPath,drive,dir,"","");
    } else {
        Fail("ERROR: conversion from relative path \" %s \" to absolute path failed. _fullpath returned NULL\n",argv[0]);
    }

    memset(fileloc, 0, _MAX_PATH);
    sprintf(fileloc, "%s%s", fullPath, szFileNameExistsWithExt);

    RemoveAll();

    hsearchfile = CreateFileA(fileloc, GENERIC_WRITE, 0, 0, CREATE_ALWAYS,                        
                            FILE_ATTRIBUTE_NORMAL, 0);

    if (hsearchfile == INVALID_HANDLE_VALUE)
    {
        Trace("ERROR[%ul]: couldn't create %s\n", GetLastError(), fileloc);
        return FAIL;    
    }

    CloseHandle(hsearchfile);

    //
    // find a file that doesn't exist
    //
    ZeroMemory( lpBuffer, sizeof(lpBuffer));
    lpPath        = fullPath;
    lpFileName    = szNoFileName;
    lpExtension   = NULL;

    if( SearchPathA( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart) != 0 ){
        error = GetLastError();
        Fail ("SearchPathA: ERROR1 -> Found invalid file[%s][%s][%s][%d]\n", lpPath, szNoFileName, szNoFileNameExt, error);
    }

    //
    // find a file that exists, when path is mentioned explicitly
    //
    ZeroMemory( lpBuffer, sizeof(lpBuffer));
    lpPath        = fullPath;
    lpFileName    = szFileNameExistsWithExt;
    lpExtension   = NULL;

    result  = SearchPathA( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart);

    if( result == 0 ){
        error = GetLastError();
        Fail ("SearchPathA: ERROR2 -> Did not Find valid file[%s][%s][%d]\n", lpPath, szFileNameExistsWithExt, error);
    }

    RemoveAll();
    PAL_Terminate();
    return PASS; 
}