Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_fs2.js
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16
17
18 var fs = require('fs');
19 var assert = require('assert');
20
21
22 var srcFilePath = "../resources/test1.txt";
23 var dstFilePath = "../tmp/test_fs2.txt";
24
25 var data;
26
27 function onWrite(err, written, buffer) {
28   if (err) {
29     throw err;
30   } else {
31     var fd = fs.openSync(dstFilePath, 'r');
32     var buffer = new Buffer(128);
33     fs.readSync(fd, buffer, 0, buffer.length, 0);
34
35     var result = 'TEST File Read & Write\n';
36
37     assert.equal(buffer.toString(), result);
38   }
39 }
40
41 function onOpenForWrite(err, fd) {
42   if (err) {
43     throw err;
44   } else {
45     fs.write(fd, data, 0, data.length, onWrite);
46   }
47 }
48
49 function onRead(err, bytesRead, buffer) {
50   if (err) {
51     throw err;
52   } else {
53     data = new Buffer(buffer.toString());
54     fs.open(dstFilePath, 'w', onOpenForWrite);
55   }
56 }
57
58 function onOpenForRead(err, fd) {
59   if (err) {
60     throw err;
61   } else {
62     var buffer = new Buffer(128);
63     fs.read(fd, buffer, 0, buffer.length, 0, onRead);
64   }
65 }
66
67 fs.open(srcFilePath, 'r', onOpenForRead);