1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 var fs = require('fs');
19 var assert = require('assert');
22 var fileName = "../resources/greeting.txt";
25 // test sync open & close.
27 // test normal sequence.
29 var fd = fs.openSync(fileName, 'r');
36 // test trying to open not exist file - expecting exception.
38 var fd = fs.openSync('not_exist_file', 'r');
39 assert.fail('none', 'exception');
44 // test trying to close with bad fd - expecting exception.
47 assert.fail('none', 'exception');
52 // test async open & close.
54 // test normal sequence.
55 var fs_async_normal_ok = false;
56 fs.open(fileName, 'r', function(err, fd) {
60 fs.close(fd, function(err) {
64 fs_async_normal_ok = true;
71 // test not exist file - expecting exception.
72 var fs_async_open_not_exist_ok = false;
73 fs.open('not_exist_file', 'r', function(err, fd) {
75 fs_async_open_not_exist_ok = true;
77 assert.fail('none', 'exception');
82 // test trying to close with bad fd - expecting exception.
83 var fs_async_close_bad_fd_ok = false;
84 fs.close(-1, function(err) {
86 fs_async_close_bad_fd_ok = true;
88 assert.fail('none', 'exception');
92 var buffer = new Buffer(10);
93 // expect length out of bound
94 assert.throws(function () { fs.readSync(5, buffer, 0, 20); }, RangeError);
95 // expect offset out of bound
96 assert.throws(function () { fs.readSync(5, buffer, -1, 20); }, RangeError);
98 process.on('exit', function() {
99 assert.equal(fs_async_normal_ok, true);
100 assert.equal(fs_async_open_not_exist_ok, true);
101 assert.equal(fs_async_close_bad_fd_ok, true);