Beispiel #1
0
int __cdecl main(int argc, char *argv[])
{
    HANDLE TheFile = NULL;
    HANDLE WaitFile = NULL;
    char* WriteBuffer = "12345678901234567890123456"; 
   
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    /* Open up the file we'll be using for some crude IPC */ 
    WaitFile = CreateFile(WAITFILENAME,     
                          GENERIC_READ|GENERIC_WRITE, 
                          FILE_SHARE_READ|FILE_SHARE_WRITE,
                          NULL,                          
                          CREATE_ALWAYS,                 
                          FILE_ATTRIBUTE_NORMAL, 
                          NULL);
    
    if (WaitFile == INVALID_HANDLE_VALUE) 
    { 
        Fail("ERROR: Could not open file '%s' with CreateFile. "
             "GetLastError() returned %d.\n",WAITFILENAME,GetLastError()); 
    }
    
    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file from bytes 0-9.
    */
    TheFile = CreateAndLockFile(TheFile, FILENAME, WriteBuffer, 
                                0, 10);
    
    /* Run the test.  Better errors are displayed by Trace throughout. */
    if(RunTest(HELPER, TheFile, WaitFile))
    {
        Fail("ERROR: The test to check that the Unlock will not work on " 
             "on locks set by other proccesses failed.\n");
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file used for "
             "testing the locks.  GetLastError() returns %d.\n",
             GetLastError());
    }

    if(CloseHandle(WaitFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the wait file.  "
             "GetLastError() returns %d.\n",GetLastError());
    }
    
    PAL_Terminate();
    return PASS;
}
Beispiel #2
0
int __cdecl main(int argc, char *argv[])
{

    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    char* WriteBuffer = "12345678901234567890123456";

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

    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */

    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile,FILENAME, WriteBuffer,
                                FileStart+3, FileEnd-6);


    /* Launch another process, which will attempt to read and write from
       the locked file.

       If the helper program returns 1, then the test fails. More
       specific errors are given by the Helper file itself.
    */
    if(RunHelper(HELPER))
    {
        Fail("ERROR: The Helper program determined that the file was not "
             "locked properly by LockFile.");
    }

    if(UnlockFile(TheFile, FileStart+3, 0, FileEnd-6, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }

    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file. "
             "GetLastError() returned %d.",GetLastError());
    }

    PAL_Terminate();
    return PASS;
}
Beispiel #3
0
/* This test checks that you can't append to a file which is locked beyond 
   EOF.
*/
void Test2()
{
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    int result;
    char* WriteBuffer = "12345678901234567890123456"; 
    
    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */
    
    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile,FILENAME, WriteBuffer, 
                                FileStart, FileEnd+20);
    
    
    /*
      Launch another proccess which will attempt to append to the 
      end of the file.
    */

    result = RunHelper(HELPER);

    if(result == -1)
    {
        Fail("ERROR: The Helper program failed in setting up the "
             "test, so it could never be run.");
    }
    else if(result > 0)
    {
        Fail("ERROR: The Helper program successfully appended to the "
             "end of the file, even though it was locked beyond EOF.  This "
             "should have failed.");
    }

    if(UnlockFile(TheFile, FileStart, 0, FileEnd+20, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file. "
             "GetLastError() returned %d.",GetLastError());
    }

}
Beispiel #4
0
/* This test checks that you can append to a file which is locked from Start
   to EOF.
*/
void Test1()
{
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    int result;
    char* WriteBuffer = "12345678901234567890123456"; 
      
    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */
    
    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile,FILENAME, WriteBuffer, 
                                FileStart, FileEnd);
    
    
    /*
      Launch another proccess which will attempt to append to the 
      end of the file.  Note: This returns -1 if the setup failed in some way.
    */

    result = RunHelper(HELPER);

    if(result == -1)
    {
        Fail("ERROR: The Helper program failed in setting up the "
             "test, so it could never be run.");
    }
    else if(result == 0)
    {
        Fail("ERROR: Failed to append to the file which was Locked from "
             "start until EOF.  Should have been able to append to this "
             "file still.  GetLastError() is %d.",GetLastError());
    }

    if(UnlockFile(TheFile, FileStart, 0, FileEnd, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file. "
             "GetLastError() returned %d.",GetLastError());
    }

}
Beispiel #5
0
int __cdecl main(int argc, char *argv[])
{
    
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    DWORD BytesWritten = 0;
    DWORD BytesRead = 0;
    char WriteBuffer[] = "This is some test data.";
    char DataBuffer[128];

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

    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */

    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile, FILENAME, WriteBuffer, 
                                FileStart, FileEnd);
       
    /* Move the file pointer to the start of the file */
    if(SetFilePointer(TheFile, 0, NULL, FILE_BEGIN) != 0)
    {
        Fail("ERROR: SetFilePointer failed to move the file pointer back "
             "to the start of the file.");
    }
    
    /* Attempt to Read 5 bytes from this file.  Since the lock does not
       affect the calling process, this should succeed.
    */
    
    if(ReadFile(TheFile, DataBuffer, 5, &BytesRead, NULL) == 0)
    {
        Fail("ERROR: ReadFile has failed.  Attempted to read in 5 bytes from "
             "the file '%s' after it had LockFile called upon it, but within "
             "the same process.",FILENAME);
    }

    if(strncmp(DataBuffer, WriteBuffer, 5) != 0)
    {
        Fail("ERROR: The data read in from ReadFile is not what should have "
             "been written in the file. '%s' ",DataBuffer);
    }

    /* Attempt to Write 5 bytes to this file.  Since the lock does not affect
       the calling process, this should succeed.
    */

    memset(WriteBuffer, 'X', strlen(WriteBuffer));

    if(WriteFile(TheFile, WriteBuffer, 5,&BytesWritten, NULL) == 0)
    {
        Fail("ERROR: WriteFile has failed.  Attempted to write 5 bytes to "
             "the file '%s' after it had LockFile called upon it, but within "
             "the same process.",FILENAME);
    }

    if(UnlockFile(TheFile, FileStart, 0, FileEnd, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file.");
    }
  
    PAL_Terminate();
    return PASS;
}