summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/mochi/worker_test_osfile_win.js
blob: f41fdecfeac59b4b44112c04dfe57aba4b7313aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

importScripts('worker_test_osfile_shared.js');

self.onmessage = function(msg) {
  self.onmessage = function(msg) {
    log("ignored message "+JSON.stringify(msg.data));
  };

  test_init();
  test_GetCurrentDirectory();
  test_OpenClose();
  test_CreateFile();
  test_ReadWrite();
  test_passing_undefined();
  finish();
};

function test_init() {
  info("Starting test_init");
  importScripts("resource://gre/modules/osfile.jsm");
}

function test_OpenClose() {
  info("Starting test_OpenClose");
  is(typeof OS.Win.File.CreateFile, "function", "OS.Win.File.CreateFile is a function");
  is(OS.Win.File.CloseHandle(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "CloseHandle returns true given the invalid handle");
  is(OS.Win.File.FindClose(OS.Constants.Win.INVALID_HANDLE_VALUE), true, "FindClose returns true given the invalid handle");
  isnot(OS.Constants.Win.GENERIC_READ, undefined, "GENERIC_READ exists");
  isnot(OS.Constants.Win.FILE_SHARE_READ, undefined, "FILE_SHARE_READ exists");
  isnot(OS.Constants.Win.FILE_ATTRIBUTE_NORMAL, undefined, "FILE_ATTRIBUTE_NORMAL exists");
  let file = OS.Win.File.CreateFile(
    "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
    OS.Constants.Win.GENERIC_READ,
    0,
    null,
    OS.Constants.Win.OPEN_EXISTING,
    0,
    null);
  info("test_OpenClose: Passed open");
  isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: file opened");
  let result = OS.Win.File.CloseHandle(file);
  isnot(result, 0, "test_OpenClose: close succeeded");

  file = OS.Win.File.CreateFile(
    "\\I do not exist",
    OS.Constants.Win.GENERIC_READ,
    OS.Constants.Win.FILE_SHARE_READ,
    null,
    OS.Constants.Win.OPEN_EXISTING,
    OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
    null);
  is(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_OpenClose: cannot open non-existing file");
  is(ctypes.winLastError, OS.Constants.Win.ERROR_FILE_NOT_FOUND, "test_OpenClose: error is ERROR_FILE_NOT_FOUND");
}

function test_CreateFile()
{
  info("Starting test_CreateFile");
  let file = OS.Win.File.CreateFile(
    "test.tmp",
    OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
    OS.Constants.Win.FILE_SHARE_READ | OS.Constants.FILE_SHARE_WRITE,
    null,
    OS.Constants.Win.CREATE_ALWAYS,
    OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
    null);
  isnot(file, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_CreateFile: opening succeeded");
  let result = OS.Win.File.CloseHandle(file);
  isnot(result, 0, "test_CreateFile: close succeeded");
}

function test_GetCurrentDirectory()
{
  let array = new (ctypes.ArrayType(ctypes.char16_t, 4096))();
  let result = OS.Win.File.GetCurrentDirectory(4096, array);
  ok(result < array.length, "test_GetCurrentDirectory: length sufficient");
  ok(result > 0, "test_GetCurrentDirectory: length != 0");
}

function test_ReadWrite()
{
  info("Starting test_ReadWrite");
  let output_name = "osfile_copy.tmp";
  // Copy file
  let input = OS.Win.File.CreateFile(
    "chrome\\toolkit\\components\\osfile\\tests\\mochi\\worker_test_osfile_win.js",
     OS.Constants.Win.GENERIC_READ,
     0,
     null,
     OS.Constants.Win.OPEN_EXISTING,
     0,
     null);
  isnot(input, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: input file opened");
  let output = OS.Win.File.CreateFile(
     "osfile_copy.tmp",
     OS.Constants.Win.GENERIC_READ | OS.Constants.Win.GENERIC_WRITE,
     0,
     null,
     OS.Constants.Win.CREATE_ALWAYS,
     OS.Constants.Win.FILE_ATTRIBUTE_NORMAL,
     null);
  isnot(output, OS.Constants.Win.INVALID_HANDLE_VALUE, "test_ReadWrite: output file opened");
  let array = new (ctypes.ArrayType(ctypes.char, 4096))();
  let bytes_read = new ctypes.uint32_t(0);
  let bytes_read_ptr = bytes_read.address();
  log("We have a pointer for bytes read: "+bytes_read_ptr);
  let bytes_written = new ctypes.uint32_t(0);
  let bytes_written_ptr = bytes_written.address();
  log("We have a pointer for bytes written: "+bytes_written_ptr);
  log("test_ReadWrite: buffer and pointers ready");
  let result;
  while (true) {
    log("test_ReadWrite: reading");
    result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
    isnot (result, 0, "test_ReadWrite: read success");
    let write_from = 0;
    let bytes_left = bytes_read;
    log("test_ReadWrite: read chunk complete " + bytes_left.value);
    if (bytes_left.value == 0) {
      break;
    }
    while (bytes_left.value > 0) {
      log("test_ReadWrite: writing "+bytes_left.value);
      let ptr = array.addressOfElement(write_from);
      // Note: |WriteFile| launches an exception in case of error
      result = OS.Win.File.WriteFile(output, array, bytes_left, bytes_written_ptr, null);
      isnot (result, 0, "test_ReadWrite: write success");
      write_from += bytes_written;
      bytes_left -= bytes_written;
    }
  }
  info("test_ReadWrite: copy complete");

  // Compare files
  result = OS.Win.File.SetFilePointer(input, 0, null, OS.Constants.Win.FILE_BEGIN);
  isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: input reset");

  result = OS.Win.File.SetFilePointer(output, 0, null, OS.Constants.Win.FILE_BEGIN);
  isnot (result, OS.Constants.Win.INVALID_SET_FILE_POINTER, "test_ReadWrite: output reset");

  let array2 = new (ctypes.ArrayType(ctypes.char, 4096))();
  let bytes_read2 = new ctypes.uint32_t(0);
  let bytes_read2_ptr = bytes_read2.address();
  let pos = 0;
  while (true) {
    result = OS.Win.File.ReadFile(input, array, 4096, bytes_read_ptr, null);
    isnot(result, 0, "test_ReadWrite: input read succeeded");

    result = OS.Win.File.ReadFile(output, array2, 4096, bytes_read2_ptr, null);
    isnot(result, 0, "test_ReadWrite: output read succeeded");

    is(bytes_read.value > 0, bytes_read2.value > 0,
       "Both files contain data or neither does " + bytes_read.value + ", " + bytes_read2.value);
    if (bytes_read.value == 0) {
      break;
    }
    let bytes;
    if (bytes_read.value != bytes_read2.value) {
      // This would be surprising, but theoretically possible with a
      // remote file system, I believe.
      bytes = Math.min(bytes_read.value, bytes_read2.value);
      pos += bytes;
      result = OS.Win.File.SetFilePointer(input,  pos, null, OS.Constants.Win.FILE_BEGIN);
      isnot(result, 0, "test_ReadWrite: input seek succeeded");

      result = OS.Win.File.SetFilePointer(output, pos, null, OS.Constants.Win.FILE_BEGIN);
      isnot(result, 0, "test_ReadWrite: output seek succeeded");

    } else {
      bytes = bytes_read.value;
      pos += bytes;
    }
    for (let i = 0; i < bytes; ++i) {
      if (array[i] != array2[i]) {
        ok(false, "Files do not match at position " + i
           + " ("+array[i] + "/"+array2[i] + ")");
      }
    }
  }
  info("test_ReadWrite test complete");
  result = OS.Win.File.CloseHandle(input);
  isnot(result, 0, "test_ReadWrite: inpout close succeeded");
  result = OS.Win.File.CloseHandle(output);
  isnot(result, 0, "test_ReadWrite: outpout close succeeded");
  result = OS.Win.File.DeleteFile(output_name);
  isnot(result, 0, "test_ReadWrite: output remove succeeded");
  info("test_ReadWrite cleanup complete");
}

function test_passing_undefined()
{
  info("Testing that an exception gets thrown when an FFI function is passed undefined");
  let exceptionRaised = false;

  try {
    let file = OS.Win.File.CreateFile(
      undefined,
      OS.Constants.Win.GENERIC_READ,
      0,
      null,
      OS.Constants.Win.OPEN_EXISTING,
      0,
      null);
  } catch(e if e instanceof TypeError && e.message.indexOf("CreateFile") > -1) {
    exceptionRaised = true;
  }

  ok(exceptionRaised, "test_passing_undefined: exception gets thrown")
}