|
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 { |
|
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 #ifdef _WIN32 |
|
40 hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_WRITE, 0, NULL, |
|
41 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
|
42 if (hFile == INVALID_HANDLE_VALUE) { |
|
43 fprintf(stderr, "CreateFile failed\n"); |
|
44 exit(1); |
|
45 } |
|
46 li.QuadPart = offset; |
|
47 li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN); |
|
48 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) { |
|
49 fprintf(stderr, "SetFilePointer failed\n"); |
|
50 exit(1); |
|
51 } |
|
52 PR_ASSERT(li.QuadPart == offset); |
|
53 strcpy(buf, MESSAGE); |
|
54 if (WriteFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) { |
|
55 fprintf(stderr, "WriteFile failed\n"); |
|
56 exit(1); |
|
57 } |
|
58 PR_ASSERT(nbytes == sizeof(buf)); |
|
59 if (CloseHandle(hFile) == 0) { |
|
60 fprintf(stderr, "CloseHandle failed\n"); |
|
61 exit(1); |
|
62 } |
|
63 #endif /* _WIN32 */ |
|
64 |
|
65 memset(buf, 0, sizeof(buf)); |
|
66 |
|
67 fd = PR_Open(TEST_FILE_NAME, PR_RDONLY, 0666); |
|
68 if (fd == NULL) { |
|
69 fprintf(stderr, "PR_Open failed\n"); |
|
70 exit(1); |
|
71 } |
|
72 position = PR_Seek64(fd, offset, PR_SEEK_SET); |
|
73 if (!LL_GE_ZERO(position)) { |
|
74 fprintf(stderr, "PR_Seek64 failed\n"); |
|
75 exit(1); |
|
76 } |
|
77 PR_ASSERT(LL_EQ(position, offset)); |
|
78 nbytes = PR_Read(fd, buf, sizeof(buf)); |
|
79 if (nbytes != sizeof(buf)) { |
|
80 fprintf(stderr, "PR_Read failed\n"); |
|
81 exit(1); |
|
82 } |
|
83 if (strcmp(buf, MESSAGE)) { |
|
84 fprintf(stderr, "corrupt data:$%s$\n", buf); |
|
85 exit(1); |
|
86 } |
|
87 if (PR_Close(fd) == PR_FAILURE) { |
|
88 fprintf(stderr, "PR_Close failed\n"); |
|
89 exit(1); |
|
90 } |
|
91 |
|
92 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) { |
|
93 fprintf(stderr, "PR_Delete failed\n"); |
|
94 exit(1); |
|
95 } |
|
96 |
|
97 printf("PASS\n"); |
|
98 return 0; |
|
99 } |