• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22// Flags: --expose_externalize_string
23'use strict';
24const common = require('../common');
25const assert = require('assert');
26const path = require('path');
27const fs = require('fs');
28const tmpdir = require('../common/tmpdir');
29
30tmpdir.refresh();
31
32const fn = path.join(tmpdir.path, 'write.txt');
33const fn2 = path.join(tmpdir.path, 'write2.txt');
34const fn3 = path.join(tmpdir.path, 'write3.txt');
35const expected = 'ümlaut.';
36const constants = fs.constants;
37
38/* eslint-disable no-undef */
39common.allowGlobals(externalizeString, isOneByteString, x);
40
41{
42  const expected = 'ümlaut eins';  // Must be a unique string.
43  externalizeString(expected);
44  assert.strictEqual(isOneByteString(expected), true);
45  const fd = fs.openSync(fn, 'w');
46  fs.writeSync(fd, expected, 0, 'latin1');
47  fs.closeSync(fd);
48  assert.strictEqual(fs.readFileSync(fn, 'latin1'), expected);
49}
50
51{
52  const expected = 'ümlaut zwei';  // Must be a unique string.
53  externalizeString(expected);
54  assert.strictEqual(isOneByteString(expected), true);
55  const fd = fs.openSync(fn, 'w');
56  fs.writeSync(fd, expected, 0, 'utf8');
57  fs.closeSync(fd);
58  assert.strictEqual(fs.readFileSync(fn, 'utf8'), expected);
59}
60
61{
62  const expected = '中文 1';  // Must be a unique string.
63  externalizeString(expected);
64  assert.strictEqual(isOneByteString(expected), false);
65  const fd = fs.openSync(fn, 'w');
66  fs.writeSync(fd, expected, 0, 'ucs2');
67  fs.closeSync(fd);
68  assert.strictEqual(fs.readFileSync(fn, 'ucs2'), expected);
69}
70
71{
72  const expected = '中文 2';  // Must be a unique string.
73  externalizeString(expected);
74  assert.strictEqual(isOneByteString(expected), false);
75  const fd = fs.openSync(fn, 'w');
76  fs.writeSync(fd, expected, 0, 'utf8');
77  fs.closeSync(fd);
78  assert.strictEqual(fs.readFileSync(fn, 'utf8'), expected);
79}
80/* eslint-enable no-undef */
81
82fs.open(fn, 'w', 0o644, common.mustCall((err, fd) => {
83  assert.ifError(err);
84
85  const done = common.mustCall((err, written) => {
86    assert.ifError(err);
87    assert.strictEqual(written, Buffer.byteLength(expected));
88    fs.closeSync(fd);
89    const found = fs.readFileSync(fn, 'utf8');
90    fs.unlinkSync(fn);
91    assert.strictEqual(found, expected);
92  });
93
94  const written = common.mustCall((err, written) => {
95    assert.ifError(err);
96    assert.strictEqual(written, 0);
97    fs.write(fd, expected, 0, 'utf8', done);
98  });
99
100  fs.write(fd, '', 0, 'utf8', written);
101}));
102
103const args = constants.O_CREAT | constants.O_WRONLY | constants.O_TRUNC;
104fs.open(fn2, args, 0o644, common.mustCall((err, fd) => {
105  assert.ifError(err);
106
107  const done = common.mustCall((err, written) => {
108    assert.ifError(err);
109    assert.strictEqual(written, Buffer.byteLength(expected));
110    fs.closeSync(fd);
111    const found = fs.readFileSync(fn2, 'utf8');
112    fs.unlinkSync(fn2);
113    assert.strictEqual(found, expected);
114  });
115
116  const written = common.mustCall((err, written) => {
117    assert.ifError(err);
118    assert.strictEqual(written, 0);
119    fs.write(fd, expected, 0, 'utf8', done);
120  });
121
122  fs.write(fd, '', 0, 'utf8', written);
123}));
124
125fs.open(fn3, 'w', 0o644, common.mustCall((err, fd) => {
126  assert.ifError(err);
127
128  const done = common.mustCall((err, written) => {
129    assert.ifError(err);
130    assert.strictEqual(written, Buffer.byteLength(expected));
131    fs.closeSync(fd);
132  });
133
134  fs.write(fd, expected, done);
135}));
136
137[false, 'test', {}, [], null, undefined].forEach((i) => {
138  assert.throws(
139    () => fs.write(i, common.mustNotCall()),
140    {
141      code: 'ERR_INVALID_ARG_TYPE',
142      name: 'TypeError'
143    }
144  );
145  assert.throws(
146    () => fs.writeSync(i),
147    {
148      code: 'ERR_INVALID_ARG_TYPE',
149      name: 'TypeError'
150    }
151  );
152});
153