📄 path.c
字号:
*curDrive = tmpstr[0];
else
trace( "Unable to discover current drive, some tests will not be conducted.\n");
/* Test GetTempPathA */
len=GetTempPathA(MAX_PATH,tmppath);
ok(len!=0 && len < MAX_PATH,"GetTempPathA failed\n");
ok(HAS_TRAIL_SLASH_A(tmppath),
"GetTempPathA returned a path that did not end in '\\'\n");
lstrcpyA(tmpstr,"aaaaaaaa");
len1=GetTempPathA(len,tmpstr);
ok(len1==len+1,
"GetTempPathA should return string length %ld instead of %ld\n",len+1,len1);
/* Test GetTmpFileNameA
The only test we do here is whether GetTempFileNameA passes or not.
We do not thoroughly test this function yet (specifically, whether
it behaves correctly when 'unique' is non zero)
*/
ok((id=GetTempFileNameA(tmppath,"path",0,newdir)),"GetTempFileNameA failed\n");
sprintf(tmpstr,"pat%.4x.tmp",id & 0xffff);
sprintf(tmpstr1,"pat%x.tmp",id & 0xffff);
ok(lstrcmpiA(newdir+lstrlenA(tmppath),tmpstr)==0 ||
lstrcmpiA(newdir+lstrlenA(tmppath),tmpstr1)==0,
"GetTempFileNameA returned '%s' which doesn't match '%s' or '%s'. id=%x\n",
newdir,tmpstr,tmpstr1,id);
ok((id=GetTempFileNameA(tmppath,NULL,0,newdir)),"GetTempFileNameA failed\n");
sprintf(tmpstr,"%.4x.tmp",id & 0xffff);
sprintf(tmpstr1,"%x.tmp",id & 0xffff);
ok(lstrcmpiA(newdir+lstrlenA(tmppath),tmpstr)==0 ||
lstrcmpiA(newdir+lstrlenA(tmppath),tmpstr1)==0,
"GetTempFileNameA returned '%s' which doesn't match '%s' or '%s'. id=%x\n",
newdir,tmpstr,tmpstr1,id);
/* Find first valid drive letter that is neither newdir[0] nor curDrive */
drives = GetLogicalDrives() & ~(1<<(newdir[0]-'A'));
if( *curDrive != NOT_A_VALID_DRIVE)
drives &= ~(1<<(*curDrive-'A'));
if( drives)
for( *otherDrive='A'; (drives & 1) == 0; drives>>=1, (*otherDrive)++);
else
trace( "Could not find alternative drive, some tests will not be conducted.\n");
/* Do some CreateDirectoryA tests */
/* It would be nice to do test the SECURITY_ATTRIBUTES, but I don't
really understand how they work.
More formal tests should be done along with CreateFile tests
*/
ok(CreateDirectoryA(newdir,NULL)==0,
"CreateDirectoryA succeeded even though a file of the same name exists\n");
ok(DeleteFileA(newdir),"Couldn't delete the temporary file we just created\n");
ok(CreateDirectoryA(newdir,NULL),"CreateDirectoryA failed\n");
/* Create some files to test other functions. Note, we will test CreateFileA
at some later point
*/
sprintf(tmpstr,"%s\\%s",newdir,SHORTDIR);
ok(CreateDirectoryA(tmpstr,NULL),"CreateDirectoryA failed\n");
sprintf(tmpstr,"%s\\%s",newdir,LONGDIR);
ok(CreateDirectoryA(tmpstr,NULL),"CreateDirectoryA failed\n");
bRes = CreateDirectoryA("c:",NULL);
ok(!bRes && (GetLastError() == ERROR_ACCESS_DENIED ||
GetLastError() == ERROR_ALREADY_EXISTS),
"CreateDirectoryA(\"c:\" should have failed (%ld)\n", GetLastError());
bRes = CreateDirectoryA("c:\\",NULL);
ok(!bRes && (GetLastError() == ERROR_ACCESS_DENIED ||
GetLastError() == ERROR_ALREADY_EXISTS),
"CreateDirectoryA(\"c:\\\" should have failed (%ld)\n", GetLastError());
sprintf(tmpstr,"%s\\%s\\%s",newdir,SHORTDIR,SHORTFILE);
hndl=CreateFileA(tmpstr,GENERIC_WRITE,0,NULL,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
ok(hndl!=INVALID_HANDLE_VALUE,"CreateFileA failed\n");
ok(CloseHandle(hndl),"CloseHandle failed\n");
sprintf(tmpstr,"%s\\%s\\%s",newdir,SHORTDIR,LONGFILE);
hndl=CreateFileA(tmpstr,GENERIC_WRITE,0,NULL,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
ok(hndl!=INVALID_HANDLE_VALUE,"CreateFileA failed\n");
ok(CloseHandle(hndl),"CloseHandle failed\n");
sprintf(tmpstr,"%s\\%s\\%s",newdir,LONGDIR,SHORTFILE);
hndl=CreateFileA(tmpstr,GENERIC_WRITE,0,NULL,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
ok(hndl!=INVALID_HANDLE_VALUE,"CreateFileA failed\n");
ok(CloseHandle(hndl),"CloseHandle failed\n");
sprintf(tmpstr,"%s\\%s\\%s",newdir,LONGDIR,LONGFILE);
hndl=CreateFileA(tmpstr,GENERIC_WRITE,0,NULL,
CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
ok(hndl!=INVALID_HANDLE_VALUE,"CreateFileA failed\n");
ok(CloseHandle(hndl),"CloseHandle failed\n");
}
/* Test GetCurrentDirectory & SetCurrentDirectory */
static void test_CurrentDirectoryA(CHAR *origdir, CHAR *newdir)
{
CHAR tmpstr[MAX_PATH],tmpstr1[MAX_PATH];
DWORD len,len1;
/* Save the original directory, so that we can return to it at the end
of the test
*/
len=GetCurrentDirectoryA(MAX_PATH,origdir);
ok(len!=0 && len < MAX_PATH,"GetCurrentDirectoryA failed\n");
/* Make sure that CetCurrentDirectoryA doesn't overwrite the buffer when the
buffer size is too small to hold the current directory
*/
lstrcpyA(tmpstr,"aaaaaaa");
len1=GetCurrentDirectoryA(len,tmpstr);
ok(len1==len+1, "GetCurrentDirectoryA returned %ld instead of %ld\n",len1,len+1);
ok(lstrcmpiA(tmpstr,"aaaaaaa")==0,
"GetCurrentDirectoryA should not have modified the buffer\n");
/* SetCurrentDirectoryA shouldn't care whether the string has a
trailing '\\' or not
*/
sprintf(tmpstr,"%s\\",newdir);
test_setdir(origdir,tmpstr,newdir,1,"check 1");
test_setdir(origdir,newdir,NULL,1,"check 2");
/* Set the directory to the working area. We just tested that this works,
so why check it again.
*/
SetCurrentDirectoryA(newdir);
/* Check that SetCurrentDirectory fails when a nonexistent dir is specified */
sprintf(tmpstr,"%s\\%s\\%s",newdir,SHORTDIR,NONDIR_SHORT);
test_setdir(newdir,tmpstr,NULL,0,"check 3");
/* Check that SetCurrentDirectory fails for a nonexistent lond directory */
sprintf(tmpstr,"%s\\%s\\%s",newdir,SHORTDIR,NONDIR_LONG);
test_setdir(newdir,tmpstr,NULL,0,"check 4");
/* Check that SetCurrentDirectory passes with a long directory */
sprintf(tmpstr,"%s\\%s",newdir,LONGDIR);
test_setdir(newdir,tmpstr,NULL,1,"check 5");
/* Check that SetCurrentDirectory passes with a short relative directory */
sprintf(tmpstr,"%s",SHORTDIR);
sprintf(tmpstr1,"%s\\%s",newdir,SHORTDIR);
test_setdir(newdir,tmpstr,tmpstr1,1,"check 6");
/* starting with a '.' */
sprintf(tmpstr,".\\%s",SHORTDIR);
test_setdir(newdir,tmpstr,tmpstr1,1,"check 7");
/* Check that SetCurrentDirectory passes with a short relative directory */
sprintf(tmpstr,"%s",LONGDIR);
sprintf(tmpstr1,"%s\\%s",newdir,LONGDIR);
test_setdir(newdir,tmpstr,tmpstr1,1,"check 8");
/* starting with a '.' */
sprintf(tmpstr,".\\%s",LONGDIR);
test_setdir(newdir,tmpstr,tmpstr1,1,"check 9");
}
/* Cleanup the mess we made while executing these tests */
static void test_CleanupPathA(CHAR *origdir, CHAR *curdir)
{
CHAR tmpstr[MAX_PATH];
sprintf(tmpstr,"%s\\%s\\%s",curdir,SHORTDIR,SHORTFILE);
ok(DeleteFileA(tmpstr),"DeleteFileA failed\n");
sprintf(tmpstr,"%s\\%s\\%s",curdir,SHORTDIR,LONGFILE);
ok(DeleteFileA(tmpstr),"DeleteFileA failed\n");
sprintf(tmpstr,"%s\\%s\\%s",curdir,LONGDIR,SHORTFILE);
ok(DeleteFileA(tmpstr),"DeleteFileA failed\n");
sprintf(tmpstr,"%s\\%s\\%s",curdir,LONGDIR,LONGFILE);
ok(DeleteFileA(tmpstr),"DeleteFileA failed\n");
sprintf(tmpstr,"%s\\%s",curdir,SHORTDIR);
ok(RemoveDirectoryA(tmpstr),"RemoveDirectoryA failed\n");
sprintf(tmpstr,"%s\\%s",curdir,LONGDIR);
ok(RemoveDirectoryA(tmpstr),"RemoveDirectoryA failed\n");
ok(SetCurrentDirectoryA(origdir),"SetCurrentDirectoryA failed\n");
ok(RemoveDirectoryA(curdir),"RemoveDirectoryA failed\n");
}
/* This routine will test Get(Full|Short|Long)PathNameA */
static void test_PathNameA(CHAR *curdir, CHAR curDrive, CHAR otherDrive)
{
CHAR curdir_short[MAX_PATH],
longdir_short[MAX_PATH];
CHAR tmpstr[MAX_PATH],tmpstr1[MAX_PATH],tmpstr2[MAX_PATH];
LPSTR strptr; /*ptr to the filename portion of the path */
DWORD len;
INT i;
CHAR dir[MAX_PATH],eight[MAX_PATH],three[MAX_PATH];
SLpassfail passfail;
/* Get the short form of the current directory */
ok((len=GetShortPathNameA(curdir,curdir_short,MAX_PATH)),
"GetShortPathNameA failed\n");
ok(!HAS_TRAIL_SLASH_A(curdir_short),
"GetShortPathNameA should not have a trailing \\\n");
/* Get the short form of the absolute-path to LONGDIR */
sprintf(tmpstr,"%s\\%s",curdir_short,LONGDIR);
ok((len=GetShortPathNameA(tmpstr,longdir_short,MAX_PATH)),
"GetShortPathNameA failed\n");
ok(lstrcmpiA(longdir_short+(len-1),"\\")!=0,
"GetShortPathNameA should not have a trailing \\\n");
if (pGetLongPathNameA) {
DWORD rc1,rc2;
sprintf(tmpstr,"%s\\%s\\%s",curdir,LONGDIR,LONGFILE);
rc1=(*pGetLongPathNameA)(tmpstr,NULL,0);
rc2=(*pGetLongPathNameA)(curdir,NULL,0);
ok((rc1-strlen(tmpstr))==(rc2-strlen(curdir)),
"GetLongPathNameA: wrong return code, %ld instead of %d\n",
rc1, strlen(tmpstr)+1);
sprintf(dir,"%c:",curDrive);
rc1=(*pGetLongPathNameA)(dir,tmpstr,sizeof(tmpstr));
ok(strcmp(dir,tmpstr)==0,
"GetLongPathNameA: returned '%s' instead of '%s' (rc=%ld)\n",
tmpstr,dir,rc1);
}
/* Check the cases where both file and directory exist first */
/* Start with a 8.3 directory, 8.3 filename */
test_ValidPathA(curdir,SHORTDIR,SHORTFILE,tmpstr,NULL,"test1");
sprintf(tmpstr1,"%s\\%s\\%s",curdir_short,SHORTDIR,SHORTFILE);
ok(lstrcmpiA(tmpstr,tmpstr1)==0,
"GetShortPathNameA returned '%s' instead of '%s'\n",tmpstr,tmpstr1);
/* Now try a 8.3 directory, long file name */
test_ValidPathA(curdir,SHORTDIR,LONGFILE,tmpstr,NULL,"test2");
sprintf(tmpstr1,"%s\\%s",curdir_short,SHORTDIR);
test_LongtoShortA(tmpstr,tmpstr1,"PAT","test2");
/* Next is a long directory, 8.3 file */
test_ValidPathA(curdir,LONGDIR,SHORTFILE,tmpstr,NULL,"test3");
sprintf(tmpstr1,"%s\\%s",longdir_short,SHORTFILE);
ok(lstrcmpiA(tmpstr,tmpstr1)==0,
"GetShortPathNameA returned '%s' instead of '%s'\n",tmpstr,tmpstr1);
/*Lastly a long directory, long file */
test_ValidPathA(curdir,LONGDIR,LONGFILE,tmpstr,NULL,"test4");
test_LongtoShortA(tmpstr,longdir_short,"PAT","test4");
/* Now check all of the invalid file w/ valid directory combinations */
/* Start with a 8.3 directory, 8.3 filename */
test_ValidPathA(curdir,SHORTDIR,NONFILE_SHORT,tmpstr,&passfail,"test5");
sprintf(tmpstr1,"%s\\%s\\%s",curdir_short,SHORTDIR,NONFILE_SHORT);
ok((passfail.shortlen==0 &&
(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND)) ||
(passfail.shortlen==strlen(tmpstr1) && lstrcmpiA(tmpstr,tmpstr1)==0),
"GetShortPathNameA error: len=%ld error=%ld tmpstr=[%s]\n",
passfail.shortlen,passfail.shorterror,tmpstr);
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetlongPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
}
/* Now try a 8.3 directory, long file name */
test_ValidPathA(curdir,SHORTDIR,NONFILE_LONG,tmpstr,&passfail,"test6");
ok(passfail.shortlen==0,"GetShortPathNameA passed when it shouldn't have\n");
ok(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND ||
!passfail.shorterror,
"GetShortPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetlongPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
}
/* Next is a long directory, 8.3 file */
test_ValidPathA(curdir,LONGDIR,NONFILE_SHORT,tmpstr,&passfail,"test7");
sprintf(tmpstr2,"%s\\%s",curdir_short,LONGDIR);
GetShortPathNameA(tmpstr2,tmpstr1,MAX_PATH);
strcat(tmpstr1,"\\" NONFILE_SHORT);
ok((passfail.shortlen==0 &&
(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND)) ||
(passfail.shortlen==strlen(tmpstr1) && lstrcmpiA(tmpstr,tmpstr1)==0),
"GetShortPathNameA error: len=%ld error=%ld tmpstr=[%s]\n",
passfail.shortlen,passfail.shorterror,tmpstr);
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetlongPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
}
/*Lastly a long directory, long file */
test_ValidPathA(curdir,LONGDIR,NONFILE_LONG,tmpstr,&passfail,"test8");
ok(passfail.shortlen==0,"GetShortPathNameA passed when it shouldn't have\n");
ok(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND ||
!passfail.shorterror,
"GetShortPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetlongPathA should have returned 'ERROR_FILE_NOT_FOUND'\n");
}
/* Now try again with directories that don't exist */
/* 8.3 directory, 8.3 filename */
test_ValidPathA(curdir,NONDIR_SHORT,SHORTFILE,tmpstr,&passfail,"test9");
sprintf(tmpstr1,"%s\\%s\\%s",curdir_short,NONDIR_SHORT,SHORTFILE);
ok((passfail.shortlen==0 &&
(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND)) ||
(passfail.shortlen==strlen(tmpstr1) && lstrcmpiA(tmpstr,tmpstr1)==0),
"GetShortPathNameA error: len=%ld error=%ld tmpstr=[%s]\n",
passfail.shortlen,passfail.shorterror,tmpstr);
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_PATH_NOT_FOUND ||
passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetLongPathA returned %ld and not 'ERROR_PATH_NOT_FOUND'\n",
passfail.longerror);
}
/* Now try a 8.3 directory, long file name */
test_ValidPathA(curdir,NONDIR_SHORT,LONGFILE,tmpstr,&passfail,"test10");
ok(passfail.shortlen==0,"GetShortPathNameA passed when it shouldn't have\n");
ok(passfail.shorterror==ERROR_PATH_NOT_FOUND ||
passfail.shorterror==ERROR_FILE_NOT_FOUND ||
!passfail.shorterror,
"GetShortPathA returned %ld and not 'ERROR_PATH_NOT_FOUND'\n",
passfail.shorterror);
if(pGetLongPathNameA) {
ok(passfail.longlen==0,"GetLongPathNameA passed when it shouldn't have\n");
ok(passfail.longerror==ERROR_PATH_NOT_FOUND ||
passfail.longerror==ERROR_FILE_NOT_FOUND,
"GetLongPathA returned %ld and not 'ERROR_PATH_NOT_FOUND'\n",
passfail.longerror);
}
/* Next is a long directory, 8.3 file */
test_ValidPathA(curdir,NONDIR_LONG,SHORTFILE,tmpstr,&passfail,"test11");
ok(passfail.shortlen==0,"GetShortPathNameA passed when it shouldn't have\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -