tstFileAppendWin-1.cpp revision d58a25651be819045c8e77b89a2f563f0b8982b0
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * IPRT Testcase - Exploration of File Appending on Windows.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * available from http://www.virtualbox.org. This file is free software;
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * you can redistribute it and/or modify it under the terms of the GNU
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * General Public License (GPL) as published by the Free Software
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * The contents of this file may alternatively be used under the terms
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * of the Common Development and Distribution License Version 1.0
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * CDDL are applicable instead of those of the GPL.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * You may elect to license modified versions of this file under the
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * terms and conditions of either the GPL or the CDDL or both.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * additional information or have any questions.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync/*******************************************************************************
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync* Header Files *
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync*******************************************************************************/
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync/*******************************************************************************
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync* Global Variables *
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync*******************************************************************************/
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsyncstatic int g_cErrors = 0;
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Open it write only and do some appending.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Checking that read fails and that the file position changes after the read.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync return MyFailure("1st CreateFile: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("1st SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!WriteFile(hFile, "0123456789", 10, &cb, NULL))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("2nd SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on write: %ld - expected 10\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld after first write\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("read didn't fail! cb=%#lx lasterr=%d\n", (long)cb, GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_BEGIN))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("3rd SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position after set file pointer: %ld - expected 5\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Open it write only and do some more appending.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Checking the initial position and that it changes after the write.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync return MyFailure("2nd CreateFile: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("4th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld on 2nd open\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!WriteFile(hFile, "abcdefghij", 10, &cb, NULL))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("5th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on 2nd write: %ld - expected 20\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld after 2nd write\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Check the initial position and read stuff. Then append some more and
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * check the new position and see that read returns 0/EOF. Finally,
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * do some seeking and read from a new position.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA) | GENERIC_READ,
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync return MyFailure("3rd CreateFile: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("6th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on open: %ld - expected 0\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld on 3rd open\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!ReadFile(hFile, szBuf, 10, &cb, NULL) || cb != 10)
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("1st ReadFile failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("read the wrong stuff: %.10s - expected 0123456789\n", szBuf);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("7th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on 1st read: %ld - expected 0\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld on 1st read\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!WriteFile(hFile, "klmnopqrst", 10, &cb, NULL))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("8th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on 3rd write: %ld - expected 30\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld after 3rd write\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (ReadFile(hFile, szBuf, 1, &cb, NULL) && cb != 0)
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("read after write didn't fail! cb=%#lx lasterr=%d\n", (long)cb, GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_BEGIN))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("9th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on 3rd write: %ld - expected 15\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!ReadFile(hFile, szBuf, 10, &cb, NULL) || cb != 10)
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("1st ReadFile failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync if (!SetFilePointerEx(hFile, off, &off, FILE_CURRENT))
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("10th SetFilePointerEx failed: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("unexpected position on 2nd read: %ld - expected 25\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync printf("tstFileAppendWin-1: off=%ld after 2nd read\n", (long)off.QuadPart);
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync * Open it read only + append and check that we cannot write to it.
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync return MyFailure("4th CreateFile: %d\n", GetLastError());
d58a25651be819045c8e77b89a2f563f0b8982b0vboxsync MyError("write didn't on read-only+append open: %d cb=%#lx\n", GetLastError(), (long)cb);