1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: SearchPathA.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the SearchFileA function. |
10 | ** |
11 | ** |
12 | ** TODO: Write a test where complete path is passed (say c:\?) |
13 | **===================================================================*/ |
14 | //SearchPath |
15 | // |
16 | //The SearchPath function searches for the specified file in the specified path. |
17 | // |
18 | |
19 | |
20 | #include <palsuite.h> |
21 | char* szDir = "." ; |
22 | |
23 | char* szNoFileName = "333asdf" ; |
24 | char* szNoFileNameExt = ".x77t" ; |
25 | |
26 | char* szFileNameExists = "searchfile" ; |
27 | char* szFileNameExtExists = ".txt" ; |
28 | |
29 | char* szFileNameExistsWithExt = "searchfile.txt" ; |
30 | char fileloc[_MAX_PATH]; |
31 | |
32 | void removeFileHelper(LPSTR pfile, int location) |
33 | { |
34 | FILE *fp; |
35 | fp = fopen( pfile, "r" ); |
36 | |
37 | if (fp != NULL) |
38 | { |
39 | if(fclose(fp)) |
40 | { |
41 | Fail("ERROR: Failed to close the file [%s], Error Code [%d], location [%d]\n" , pfile, GetLastError(), location); |
42 | } |
43 | |
44 | if(!DeleteFileA(pfile)) |
45 | { |
46 | Fail("ERROR: Failed to delete file [%s], Error Code [%d], location [%d]\n" , pfile, GetLastError(), location); |
47 | } |
48 | } |
49 | |
50 | } |
51 | |
52 | |
53 | void RemoveAll() |
54 | { |
55 | removeFileHelper(fileloc, 1); |
56 | } |
57 | |
58 | int __cdecl main(int argc, char *argv[]) { |
59 | |
60 | char* lpPath = NULL; |
61 | char* lpFileName = NULL; |
62 | char* lpExtension = NULL; |
63 | DWORD nBufferLength = 0; |
64 | char lpBuffer[_MAX_PATH]; |
65 | char** lpFilePart = NULL; |
66 | DWORD error = 0; |
67 | DWORD result = 0; |
68 | |
69 | HANDLE hsearchfile; |
70 | char fname[_MAX_FNAME]; |
71 | char ext[_MAX_EXT]; |
72 | char fullPath[_MAX_DIR]; |
73 | char drive[_MAX_DRIVE]; |
74 | char dir[_MAX_DIR]; |
75 | |
76 | |
77 | if(0 != (PAL_Initialize(argc, argv))) |
78 | { |
79 | return FAIL; |
80 | } |
81 | |
82 | |
83 | /* Initalize the buffer. |
84 | */ |
85 | memset(fullPath, 0, _MAX_DIR); |
86 | |
87 | if (GetTempPathA(_MAX_DIR, fullPath) == 0) |
88 | { |
89 | Fail("ERROR: GetTempPathA failed to get a path\n" ); |
90 | } |
91 | |
92 | memset(fileloc, 0, _MAX_PATH); |
93 | sprintf_s(fileloc, _countof(fileloc), "%s%s" , fullPath, szFileNameExistsWithExt); |
94 | |
95 | RemoveAll(); |
96 | |
97 | hsearchfile = CreateFileA(fileloc, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, |
98 | FILE_ATTRIBUTE_NORMAL, 0); |
99 | |
100 | if (hsearchfile == INVALID_HANDLE_VALUE) |
101 | { |
102 | Trace("ERROR[%ul]: couldn't create %s\n" , GetLastError(), fileloc); |
103 | return FAIL; |
104 | } |
105 | |
106 | CloseHandle(hsearchfile); |
107 | |
108 | // |
109 | // find a file that doesn't exist |
110 | // |
111 | ZeroMemory( lpBuffer, sizeof(lpBuffer)); |
112 | lpPath = fullPath; |
113 | lpFileName = szNoFileName; |
114 | lpExtension = NULL; |
115 | |
116 | if( SearchPathA( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart) != 0 ){ |
117 | error = GetLastError(); |
118 | Fail ("SearchPathA: ERROR1 -> Found invalid file[%s][%s][%s][%d]\n" , lpPath, szNoFileName, szNoFileNameExt, error); |
119 | } |
120 | |
121 | // |
122 | // find a file that exists, when path is mentioned explicitly |
123 | // |
124 | ZeroMemory( lpBuffer, sizeof(lpBuffer)); |
125 | lpPath = fullPath; |
126 | lpFileName = szFileNameExistsWithExt; |
127 | lpExtension = NULL; |
128 | |
129 | result = SearchPathA( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart); |
130 | |
131 | if( result == 0 ){ |
132 | error = GetLastError(); |
133 | Fail ("SearchPathA: ERROR2 -> Did not Find valid file[%s][%s][%d]\n" , lpPath, szFileNameExistsWithExt, error); |
134 | } |
135 | |
136 | RemoveAll(); |
137 | PAL_Terminate(); |
138 | return PASS; |
139 | } |
140 | |