tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bigfile3.c (2513B)


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