nsprpub/pr/tests/bigfile2.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nspr.h"
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9 #include <stdlib.h>
michael@0 10 #include <string.h>
michael@0 11 #ifdef _WIN32
michael@0 12 #include <windows.h>
michael@0 13 #endif
michael@0 14
michael@0 15 #define TEST_FILE_NAME "bigfile2.txt"
michael@0 16 #ifdef WINCE
michael@0 17 #define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile2.txt"
michael@0 18 #else
michael@0 19 #define TEST_FILE_NAME_FOR_CREATEFILE TEST_FILE_NAME
michael@0 20 #endif
michael@0 21
michael@0 22 #define MESSAGE "Hello world!"
michael@0 23 #define MESSAGE_SIZE 13
michael@0 24
michael@0 25 int main(int argc, char **argv)
michael@0 26 {
michael@0 27 PRFileDesc *fd;
michael@0 28 PRInt64 offset, position;
michael@0 29 PRInt32 nbytes;
michael@0 30 char buf[MESSAGE_SIZE];
michael@0 31 #ifdef _WIN32
michael@0 32 HANDLE hFile;
michael@0 33 LARGE_INTEGER li;
michael@0 34 #endif /* _WIN32 */
michael@0 35
michael@0 36 LL_I2L(offset, 1);
michael@0 37 LL_SHL(offset, offset, 32);
michael@0 38
michael@0 39 fd = PR_Open(TEST_FILE_NAME,
michael@0 40 PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0666);
michael@0 41 if (fd == NULL) {
michael@0 42 fprintf(stderr, "PR_Open failed\n");
michael@0 43 exit(1);
michael@0 44 }
michael@0 45 position = PR_Seek64(fd, offset, PR_SEEK_SET);
michael@0 46 if (!LL_GE_ZERO(position)) {
michael@0 47 fprintf(stderr, "PR_Seek64 failed\n");
michael@0 48 exit(1);
michael@0 49 }
michael@0 50 PR_ASSERT(LL_EQ(position, offset));
michael@0 51 strcpy(buf, MESSAGE);
michael@0 52 nbytes = PR_Write(fd, buf, sizeof(buf));
michael@0 53 if (nbytes != sizeof(buf)) {
michael@0 54 fprintf(stderr, "PR_Write failed\n");
michael@0 55 exit(1);
michael@0 56 }
michael@0 57 if (PR_Close(fd) == PR_FAILURE) {
michael@0 58 fprintf(stderr, "PR_Close failed\n");
michael@0 59 exit(1);
michael@0 60 }
michael@0 61
michael@0 62 memset(buf, 0, sizeof(buf));
michael@0 63
michael@0 64 #ifdef _WIN32
michael@0 65 hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_READ, 0, NULL,
michael@0 66 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
michael@0 67 if (hFile == INVALID_HANDLE_VALUE) {
michael@0 68 fprintf(stderr, "CreateFile failed\n");
michael@0 69 exit(1);
michael@0 70 }
michael@0 71 li.QuadPart = offset;
michael@0 72 li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
michael@0 73 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
michael@0 74 fprintf(stderr, "SetFilePointer failed\n");
michael@0 75 exit(1);
michael@0 76 }
michael@0 77 PR_ASSERT(li.QuadPart == offset);
michael@0 78 if (ReadFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) {
michael@0 79 fprintf(stderr, "ReadFile failed\n");
michael@0 80 exit(1);
michael@0 81 }
michael@0 82 PR_ASSERT(nbytes == sizeof(buf));
michael@0 83 if (strcmp(buf, MESSAGE)) {
michael@0 84 fprintf(stderr, "corrupt data:$%s$\n", buf);
michael@0 85 exit(1);
michael@0 86 }
michael@0 87 if (CloseHandle(hFile) == 0) {
michael@0 88 fprintf(stderr, "CloseHandle failed\n");
michael@0 89 exit(1);
michael@0 90 }
michael@0 91 #endif /* _WIN32 */
michael@0 92
michael@0 93 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
michael@0 94 fprintf(stderr, "PR_Delete failed\n");
michael@0 95 exit(1);
michael@0 96 }
michael@0 97
michael@0 98 printf("PASS\n");
michael@0 99 return 0;
michael@0 100 }

mercurial