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 "bigfile2.txt"
16 #ifdef WINCE
17 #define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile2.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
main(int argc,char ** argv)25 int main(int argc, char **argv)
26 {
27 PRFileDesc *fd;
28 PRInt64 offset, position;
29 PRInt32 nbytes;
30 char buf[MESSAGE_SIZE];
31 #ifdef _WIN32
32 HANDLE hFile;
33 LARGE_INTEGER li;
34 #endif /* _WIN32 */
35
36 LL_I2L(offset, 1);
37 LL_SHL(offset, offset, 32);
38
39 fd = PR_Open(TEST_FILE_NAME,
40 PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0666);
41 if (fd == NULL) {
42 fprintf(stderr, "PR_Open failed\n");
43 exit(1);
44 }
45 position = PR_Seek64(fd, offset, PR_SEEK_SET);
46 if (!LL_GE_ZERO(position)) {
47 fprintf(stderr, "PR_Seek64 failed\n");
48 exit(1);
49 }
50 PR_ASSERT(LL_EQ(position, offset));
51 strcpy(buf, MESSAGE);
52 nbytes = PR_Write(fd, buf, sizeof(buf));
53 if (nbytes != sizeof(buf)) {
54 fprintf(stderr, "PR_Write failed\n");
55 exit(1);
56 }
57 if (PR_Close(fd) == PR_FAILURE) {
58 fprintf(stderr, "PR_Close failed\n");
59 exit(1);
60 }
61
62 memset(buf, 0, sizeof(buf));
63
64 #ifdef _WIN32
65 hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_READ, 0, NULL,
66 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
67 if (hFile == INVALID_HANDLE_VALUE) {
68 fprintf(stderr, "CreateFile failed\n");
69 exit(1);
70 }
71 li.QuadPart = offset;
72 li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
73 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
74 fprintf(stderr, "SetFilePointer failed\n");
75 exit(1);
76 }
77 PR_ASSERT(li.QuadPart == offset);
78 if (ReadFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) {
79 fprintf(stderr, "ReadFile failed\n");
80 exit(1);
81 }
82 PR_ASSERT(nbytes == sizeof(buf));
83 if (strcmp(buf, MESSAGE)) {
84 fprintf(stderr, "corrupt data:$%s$\n", buf);
85 exit(1);
86 }
87 if (CloseHandle(hFile) == 0) {
88 fprintf(stderr, "CloseHandle failed\n");
89 exit(1);
90 }
91 #endif /* _WIN32 */
92
93 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
94 fprintf(stderr, "PR_Delete failed\n");
95 exit(1);
96 }
97
98 printf("PASS\n");
99 return 0;
100 }
101